pytorch 基础

数据结构

FloatTensor

import torch

a = torch.FloatTensor(2, 3)
b = torch.FloatTensor([2, 3, 4, 5])

print(a)
print(b)

以上代码的输出如下:

tensor([[7.0072e+22, 1.6242e+22, 2.0621e-19],
        [4.5157e+27, 1.3017e+34, 6.6643e-33]])
tensor([2., 3., 4., 5.])

以上代码中的a = torch.FloatTensor(2, 3) 表示的是生成2*3的矩阵

b = torch.FloatTensor([2, 3, 4, 5]) 是生成数组

输入参数的不同,决定输出的数据结构的不同

IntTensor

import torch

a = torch.IntTensor(2, 3)
b = torch.IntTensor([2, 3, 4, 5])

print(a)
print(b)

print("done")

OutPut:

tensor([[6029427, 7274612, 6488178],
        [6029416, 6357090, 7012451]], dtype=torch.int32)
tensor([2, 3, 4, 5], dtype=torch.int32)

类似FloatTensor 只不过这次每个元素的值都是整型

torch.randn