1 张量(Tensor)
1.1 Tensor的定义与创建
torch.empty() 声明一个未初始化的矩阵
创建一个5*3的矩阵
x = torch.empty(5, 3)
print(x)
>>>out
>>>tensor([[1.9370e+31, 4.5764e-41, 1.9370e+31],
[4.5764e-41, 1.3476e+37, 1.5637e-01],
[3.1529e-43, 0.0000e+00, 1.9370e+31],
[4.5764e-41, 1.9370e+31, 4.5764e-41],
[1.4585e-19, 9.3233e-09, 1.1703e-19]])
torch.rand() :随机初始化一个矩阵,数值分布在[0, 1]之间,也可以称为"01"初始化
rand_x = torch.rand(5, 3)
print(rand_x)
>>>out
tensor([[0.8683, 0.6435, 0.3607],
[0.9137, 0.8846, 0.6968],
[0.8968, 0.9220, 0.0576],
[0.0260, 0.1657, 0.6797],
[0.4731, 0.3189, 0.0485]])
# 随机生成一个3维的tensor
a = torch.rand(1,2,3)
>>>
tensor([[[0.8720, 0.8969, 0.8131],
[0.8776, 0.5745, 0.7943]]])
a.shape # 返回tensor a的尺寸
>>> torch.Size([1, 2, 3])
a.size()
>>> torch.Size([1, 2, 3])
a.dim() # 返回tensor 的维度
>>> 3
a.numel # 返回tensor 元素个数 shape[0]*shape[1]*shape[2]
>>> 6
torch.randn() 输入初始化tensor的shape.#N(0, 1)生成一个均值为0,方差为1的一个tensor
k = torch.randn(3, 2)
k
>>> tensor([[-0.6163, -0.2755],
[ 0.8480, -0.0991],
[-2.0894, 0.5705]])
如果是自定义一个指定均值以及方差的tensor(N(u, std)),可以使用torch.normal()函数,但是此函数使用起来有点变扭,传入的参数的格式如下:
m = torch.normal(mean=torch.full([10], 0), std=torch.arange(1, 0, -0.1))
m
>>>
tensor([-0.4171, 0.5065, 1.0309, -0.2772, -0.3444, -0.2812, 0.0121, 0.7589,
-0.1971, -0.0674])
m.shape
>>>torch.Size([10])
# 再进行reshape,才能形成一个二维的tensor
# 其中torch.full对应tensor进行填充同一数值
a = torch.full([3,2], 1.0)
>>>tensor([[1., 1.],
[1., 1.],
[1., 1.]])
# 其中torch.arange(min, max, step)生成一个[min,max)公差为step的等差数组的list
l = torch.arange(0, 10, 2)
>>>
tensor([0, 2, 4, 6, 8])
torch.tensor():直接传递tensor数值来创建,直接传入数值
tensor_1 = torch.tensor([5, 3, 2])
print(tensor_1)
>>>out
tensor([5, 3, 2])
# 在pytorch中新建一个标量
torch.tensor(1.)
>>>
tensor(1.)
t = torch.tensor([[2, 1, 3], [1, 3, 5]])
t.shape
>>> torch.Size([2, 3])
torch.Tensor():可以直接传入参数(但必须是使用list),也可以传入shape,建议构建的时候尽量传入shape,传值的方式可以使用torch.tensor(),这样可以避免混肴。
t1 = torch.Tensor([2, 3])
t1.shape()
>>> torch.Size([2])
t2 = torch.Tensor(2, 3)
t2.shape
>>> torch.Size([2, 3])
torch.zeros():创建数值皆为 0 的矩阵,数据类型为long
zero_x = torch.zeros(5, 3, dtype=torch.long)
print(zero_x)
>>>out
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
torch.newones():新输入tensor尺寸的大小
tensor2 = tensor1.newones(5, 3, dtype=torch.double) # new* 方法需要输入 tensor 大小
torch.randn_like(old_tensor): 与之前tensor保持相同的大小
tensor1 = torch.rand(5, 3)
tensor2 = torch.randn_like(tensor1, dtpye=torch.float)
print(tensor1)
print(tensor2)
>>>tensor1
tensor([[0.0818, 0.6360, 0.1734],
[0.4239, 0.2940, 0.1296],
[0.5019, 0.6955, 0.4951],
[0.6301, 0.6848, 0.4905],
[0.1273, 0.2412, 0.5523]])
>>> tensor2
tensor([[ 2.7484, 0.1545, 0.7206],
[ 1.4911, -1.4498, -1.1515],
[ 0.8653, -0.1471, -0.7242],
[ 0.4210, 2.2713, -0.4736],
[-1.2145, 0.9841, -0.7411]])
torch.randint(1, 10, [3, 3]) 参数分别表示[min,max)之间随机生成一个(3, 3)的tensor
torch.randint(1, 10, [3, 3])
>>>
tensor([[4, 3, 2],
[5, 7, 5],
[6, 4, 7]])
torch.linspace(min, max, steps), 将区间[min,max]等分成steps份
n = torch.linspace(0, 10, 11)
>>>
tensor([ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10.])
n = torch.linspace(0, 10, 11)
>>>
tensor([ 0.0000, 1.1111, 2.2222, 3.3333, 4.4444, 5.5556, 6.6667, 7.7778,
8.8889, 10.0000])
torch.logspace(min, max, steps)
1.1.2 tensor type check
t = torch.randn(2, 3)
print(f"t.type:{t.type()}")
print(f"type(t):{type(t)}")
>>>
t.type:torch.FloatTensor
type(t):<class 'torch.Tensor'>
第二种方式
isinstance(t, torch.FloatTensor)
>>>
True
1.1.3 set default type
torch.set_default_tensor_type(torch.DoubleTensor)