用d2l.plot画不出来东西

用d2l.plot画不出来东西

环境

  • Ubuntu20
  • python3.10

前言

d2l里面的plot估计跟matplotlib里面的plot一样。我的代码是这样子的:

import random
import torch
from d2l import torch as d2l
import time

def synthetic_data(w, b, num_examples):  # @save
    X = torch.normal(0, 1, (num_examples, len(w)))
    y = torch.matmul(X, w) + b
    y += torch.normal(0, 0.01, y.shape)
    return X, y.reshape((-1, 1))


true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = synthetic_data(true_w, true_b, 1000)


def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))
    # 这些样本是随机读取的,没有特定的顺序
    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        batch_indices = torch.tensor(indices[i : min(i + batch_size, num_examples)])
        yield features[batch_indices], labels[batch_indices]

d2l.set_figsize()

# 绘制散点图
d2l.plt.scatter(features[:, 1].detach().numpy(), labels.detach().numpy(), 1)

# 设置标题和标签
d2l.plt.title('Scatter Plot of Features vs. Labels')
d2l.plt.xlabel('Feature 1')
d2l.plt.ylabel('Label')

# 显示图形
d2l.plt.show()

结果运行后没有画图就结束了,python也没有报错。说明是运行成功了

解决方法

下载pyQt5

pip install PyQt5

可能遇到的问题:

pip install PyQt5
Collecting PyQt5
  WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out. (read timeout=15)")': /packages/2f/e6/a1f9853e4933c312c6de9c79d126c7d92ef69ae0e53895fb1ceb0ecc77a6/PyQt5-5.15.10-cp37-abi3-manylinux_2_17_x86_64.whl.metadata
  WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.HTTPSConnection object at 0x7fa1741d1780>: Failed to establish a new connection: [Errno 101] 网络不可达')': /packages/2f/e6/a1f9853e4933c312c6de9c79d126c7d92ef69ae0e53895fb1ceb0ecc77a6/PyQt5-5.15.10-cp37-abi3-manylinux_2_17_x86_64.whl.metadata

然后就下载不了说超时,这个一般来讲就是访问网络问题,理论上来说换个软件源就好了:

pip config set global.index-url https://mirrors.aliyun.com/pypi/simple/
pip install --upgrade pip

换完后更新以下pip再下载。

有个问题是换源不就是因为国内访问外面的软件下载很慢才做的操作么?我已经用了代理,github啥的都能正常访问,理论上不用换源也能正常下载,但是事实却不是这样的

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值