跟李沐学深度学习使用Dataspell图像不显示问题

源代码:

# %% 生成训练样本
def synthetic_data(w, b, num_examples):
    """ 生成 y = Xw + b + 噪声"""
    X = torch.normal(0, 1, (num_examples, len(w)))  # 均值为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))  # 指定列为1,-1为自动调整行数


true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = synthetic_data(true_w, true_b, 1000)
print('features', features[0], '\nlabels', labels[0])
d2l.set_figsize()
d2l.plt.scatter(features[:, 1].detach().numpy(), labels.detach().numpy(), 1)
d2l.plt.show()  # 添加这一行来显示图像

输出

features tensor([-1.8246,  1.7506]) 
labels tensor([-5.3920])
<Figure size 350x250 with 1 Axes>

可见并没有如期生成图像

解决办法
可能是由于d2l.plt的绘图问题,将源代码的绘图部分修改为

plt.figure()
plt.scatter(features[:, 1].detach().numpy(), labels.detach().numpy(), 1)
plt.show()  # 添加这一行来显示图像

完美解决

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值