目录)
PyTorch 入门笔记总结
1、Tensor张量
创建张量
Torch构造张量有如下几种形式:
import torch
#直接构造类似于numpy中ndarray的随机张量
x = troch.rand(5,3)
print(x)
#利用已有数据构造张量
x = torch.tensor([[[5,3,1],[6,4,2]])
print(x)
#根据已经存在的tensor创建
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype=torch.float32)
print(x)
#输出
tensor([[0.4041, 0.4289, 0.2413],
[0.8269, 0.2149, 0.6988],
[0.3254, 0.6165, 0.4514],
[0.9963, 0.1263, 0.7608],
[0.6543, 0.1745, 0.6938]])
tensor([[5, 3, 1],
[6, 4, 2]])
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[-0.4634, 0.2839, -0.3666],
[-0.5293, 0.2354, 0.6911],
[-1.6419, 0.5335, 0.7720],
[ 0.5266, -0.9511, 0.0539],
[-2.4723, 0.4116, -0.3734]]