之前有写过一篇关于张量的基础用法,有兴趣的可以看一看
https://blog.csdn.net/MR_kdcon/article/details/108900039
话不多说,直接上图
Tensor(*sizes) 称之为基础构造函数
创建不同类型的Tensor:内容随机
例1: y = torch.Tensor(2, 3); print(y.dtype) # float32 y1 = torch.FloatTensor(2, 3); print(y1.dtype) # float32 y2 = torch.DoubleTensor(2, 3); print(y2.dtype) # float64 y3 = torch.LongTensor(2, 3); print(y3.dtype) # int64 y4 = torch.IntTensor(2, 3); print(y4.dtype) # int32 y5 = torch.ShortTensor(2, 3); print(y5.dtype) # int16 y6 = torch.CharTensor(2, 3); print(y6.dtype) # int8 y7 = torch.ByteTensor(2, 3); print(y7.dtype) # uint8 例2: x = torch.FloatTensor(6) print(x) # tensor([9.0919e-39, 1.0102e-38, 8.9082e-39, 1.0653e-38, 8.7245e-39, 1.0194e-38]) print(type(x)) # <class 'torch.Tensor'>他们都是Tensor的子类:
除此之外,Tensor也可以像tensor一样创建特定的张量
y = torch.Tensor([10, 5, 96]) # tensor([10., 5., 96.]) yy = torch.tensor([10.0, 5, 96]) # tensor([10., 5., 96.])除了基础构造函数之外
还有
tensor(data),类似于np.array
ones(*sizes)
zeros(*size)
arange(first,last,step),从first到last,步长为step
linspace(first,last,steps),从first到last切成steps等分
rand/randn(*size),0-1均匀/标准正太分布
normal(mean,std,*size) 均值为mean,方差std的标准正太分布
note:这些函数都可以指定dtpye、device