创建Tensor
- 从numpy引入 torch.from_numpy()
a = np.array([2,3])
torch.from_numpy(a)
- 使用list导入,不用numpy作为载体
torch.tensor([2,3.3]) #具体的数据作为参数
torch.Tensor(2,3) #size作为参数
-
torch.tensor()与torch.Tensor()
torch.tensor()生成的Tensor会根据括号里边的参数类型而自动修改类型, 而FloatTensor,IntTensor,DoubleTensor则一开始就定义好了参数类型。 -
随机初始化生成Tensor rand/rand_like/randint/randn
a = torch.rand(2,3) #rand生成0-1之间的数
b = torch.rand_like(a) #生成一个与a形状相同的Tensor
c = torch.randint(1,10,[3,3]) #生成一个1到10(不包括10)之间的[3,3]的Tensor
d = torch.randn(3,3) #生成0到1之间的[3,3]的Tensor
- 赋值生成Tensor
torch.full([2,3]