在torch中,我们会使用张量对模型输入和输出进行编码,包括模型的参数也是一样,使用张量作为模型的参数。张量是一个数据容器。它包含的数据几乎总是数值数据。矩阵就是一个2维的张量,张量时矩阵向任意维度的推广(张量的维度通常叫做轴)
参考:什么是张量
在pytorch中常用的对张量进行操作的api
张量的创建
torch.tensor
torch.tensor可以将列表转换为张量。
import torch
import numpy as np
a = [1,2,3.]
b = torch.tensor(a)
print('type(a):',type(a))
print('type(a):',type(b))
print('b:',b)
输出:
type(a): <class 'list'>
type(a): <class 'torch.Tensor'>
b: tensor([1., 2., 3.])
也可以从一个numpy数组中创建一个张量
import torch
import numpy as np
a = np.random.normal((2,3))
b = torch.tensor(a)
print('b:',b)
输出:
b: tensor([1.1853, 2.4746], dtype=torch.float64)
我们还可以从一个已存在的张量初始化为另一个张量
ones_like:生成一个size相同但是全为1的张量。
import torch
import numpy as np
a = np.random.normal((2,3))
b = torch.tensor(a)
c = torch.ones_like(b)
print('c:',c)
输出:
c: tensor([1., 1.], dtype=torch.float64)
同样用法还有zeros_like,rand_like.
import torch
import numpy as np
a = np.random.normal((2,3))
b = torch.tensor(a)
c = torch.zeros_like(b)
d = torch.rand_like(b)
print('c:',c)
print('d:',d)
输出:
c: tensor([0., 0.], dtype=torch.float64)
d: tensor([0.9689, 0.5169], dtype=torch.float64)
torch.is_nonzero
torch.is_nonzero可以判断张量是否为0
num01 = torch.tensor([2.])
num02 = torch.tensor([0.])
a = torch.is_nonzero(num01)
b = torch.is_nonzero(num02)
print(a)
print(b)
输出:
True
False
torch.arange
torch.arange会生成一个张量,默认start=0,step=1,size为
a = torch.arange(5)
b = torch.arange(0,5,2)
print('a:',a)
print('-'*20)
print('b:',b)
输出:
a: tensor([0, 1, 2, 3, 4])
--------------------
b: tensor([0, 2, 4])
torch.range
torch.range会生成一个size为+1的张量
b = torch.range(start=0,end=5)
print('b:',b)
输出:
b: tensor([0., 1., 2., 3., 4., 5.])
torch.eye
torch.eye会生成一个对角线上全为1的方阵
b = torch.eye(3)
print('b:',b)
输出:
b: tensor([[1., 0., 0.],
[0., 1., 0.],
[0., 0., 1.]])
torch.full
torch.full(size,fill_value) 例如创建一个2x2全为5的张量
b = torch.full([2,2],5)
print('b:',b)
输出
b: tensor([[5, 5],
[5, 5]])
torch.cat
torch.cat可以把两个张量连接起来,但是前提是这两个张量必须又一个维度相同,例如将【2,2】的a和【2,3】的b以第一个维度连接起来和【2,2】的c和【3,2】的d按第0维拼接起来。
a = torch.rand([2,2])
b = torch.rand([2,3])
c = torch.rand([2,2])
d = torch.rand([3,2])
print('a:',a)
print('b:',b)
output01 = torch.cat([a,b],dim=1)
print('-'*20)
print('output01:',output01)
print('-'*20)
print('c:',c)
print('d:',d)
output02 = torch.cat([c,d],dim=0)
print('-'*20)
print('output02:',output02)
输出:
a: tensor([[0.0892, 0.8746],
[0.5646, 0.9864]])
b: tensor([[0.7732, 0.1734, 0.2263],
[0.3184, 0.6276, 0.0686]])
--------------------
output01: tensor([[0.0892, 0.8746, 0.7732, 0.1734, 0.2263],
[0.5646, 0.9864, 0.3184, 0.6276, 0.0686]])
--------------------
c: tensor([[0.4564, 0.2129],
[0.3277, 0.1094]])
d: tensor([[0.3777, 0.4254],
[0.7303, 0.7682],
[0.4415, 0.2494]])
--------------------
output02: tensor([[0.4564, 0.2129],
[0.3277, 0.1094],
[0.3777, 0.4254],
[0.7303, 0.7682],
[0.4415, 0.2494]])
torch.chunk
torch.chunk是将张量分割,默认dim=0是按照行分割,当dim=1时按列进行分割
如果input不能够整除chunks,那么最后一个张量会变小。
b = torch.rand(3,2)
print('b:',b)
print('-'*20)
c = torch.chunk(b,chunks=2)
print('c:',c)
print('-'*20)
d,f = c
print('d:',d)
print('-'*20)
print('f:',f)
输出:
b: tensor([[0.5903, 0.9238],
[0.7511, 0.3397],
[0.3646, 0.8335]])
--------------------
c: (tensor([[0.5903, 0.9238],
[0.7511, 0.3397]]), tensor([[0.3646, 0.8335]]))
--------------------
d: tensor([[0.5903, 0.9238],
[0.7511, 0.3397]])
--------------------
f: tensor([[0.3646, 0.8335]])