动手学深度学习pytorch版-填坑-TypeError: normal() received an invalid combination of arguments

在8章的序列模型这里:
动手学深度学习pytorch版-填坑-TypeError: normal() received an invalid combination of arguments_第1张图片

%matplotlib inline
import torch
from torch import nn
from d2l import torch as d2l
T = 1000 # 总共产⽣1000个点
time = torch.arange(1, T + 1, dtype=torch.float32)
x = torch.sin(0.01 * time) + torch.normal(0, 0.2, (T,))
d2l.plot(time, [x], 'time', 'x', xlim=[1, 1000], figsize=(6, 3))

报错:
TypeError: normal() received an invalid combination of arguments - got (int, float, tuple), but expected one of:

  • (Tensor mean, Tensor std, torch.Generator generator, Tensor out)
  • (Tensor mean, float std, torch.Generator generator, Tensor out)
  • (float mean, Tensor std, torch.Generator generator, Tensor out)

``
第8章的代码会报错,跑不了,`

在windows anaconda3环境代码的修改-亲测可行

  1. 修改torch.normal函数用np.random.normal替代(借鉴他人思想)
  2. TypeError: len() of a 0-d tensor-因为d2l包依旧是调用的matplotlib,所以利用np.array修改了原来的写法。画图成功。
    总结:失败应该是由于d2l的底层源码的一些实现问题,或者说还没更新。
import torch
from torch import nn
from d2l import torch as d2l
import numpy as np
from matplotlib import pyplot as plt
T=1000 #总共产生1000个点
time=torch.arange(1,T+1,dtype=torch.float32)
x=torch.sin(0.01*time)+torch.tensor(np.random.normal(0,0.2,size=(T,)),dtype=torch.float32)
d2l.plot(np.array(time),np.array(x),'time','x',xlim=[1,1000],figsize=(6,3))
plt.show()


动手学深度学习pytorch版-填坑-TypeError: normal() received an invalid combination of arguments_第2张图片

你可能感兴趣的:(python,深度学习,人工智能)