import torch
import numpy as np
矩阵torch的构建
1.empty()初始化一个5*3的矩阵
x = torch.empty(5,3)
x
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
2. rand()随机初始化一个5*3矩阵
x = torch.rand(5,3)
x
tensor([[0.9752, 0.2380, 0.2949],
[0.4990, 0.8772, 0.1745],
[0.9362, 0.4198, 0.1290],
[0.8173, 0.5672, 0.5823],
[0.7380, 0.2954, 0.5019]])
3. zeros()初始化一个全0的5*3矩阵
x = torch.zeros(5,3)
x
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
x.dtype
torch.float32
4. 修改矩阵的数据类型 : dtype=torch.XXX
x = torch.zeros(5,3,dtype=torch.long)
x.dtype
torch.int64
tensor的构建的其他方式
1.直接将一个数据构建成tensor
x = torch