文章目录
单个tensor
torch的数据类型转换
tensor.int()
tensor.long()
tensor.float()
tensor.double()
tensor.byte() ## 转换为无符号整型
## 查看类型
tensor_a.type()
torch.arange(), torch.range()
torch.arange() 内部元素默认为整数
torch.range()内部元素默认为浮点数
t = torch.arange(7) # t = tensor([0, 1, 2, 3, 4, 5, 6])
t = torch.range(0, 7) # t = tensor([0., 1., 2., 3., 4., 5., 6., 7.])
正常的range(0,7)生成长度为7的list
torch.linespace(start,end,steps=100,out=None)
返回一个tensor 包含step个元素,每个元素之间间隔(end-start)/step
torch.linspace(0,1,10, dtype=torch.float32)
tensor([ 0.0000, 0.1111, 0.2222, 0.3333, 0.4444, 0.5556, 0.6667,
0.7778, 0.8889, 1.0000])
torch.flatten(input,start_dim=0,end_dim=-1)
>>c = torch.randn(3,3)
>>tensor([[ 0.3476, -1.4443, -0.7253],
[ 1.0562, -0.8764, -1.0724],
[-0.3447, -0.7049, -0.0549]])
>>d = torch.flatten(c)
>>tensor([ 0.3476, -1.4443, -0.7253, 1.0562, -0.8764, -1.0724, -0.3447, -0.7049,-0.0549])
torch.repeat(input,*size)
对张量进行重复扩充
- 当参数只有两个时:(行的重复倍数,列的重复倍数),1表示不重复。
- 当参数有三个时:(通道数的重复倍数,行的重复倍数,列的重复倍数),1表示不重复。
x = torch.tensor([1,2,3])
x1 = x.repeat(4)
x2 = x.repeat(4,1)
x3 = x.repeat(4,2)
x4 = x.repeat(4,2,1)
x1:
tensor([1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3])
x2:
tensor([[1, 2, 3],
[1, 2, 3],
[1, 2, 3],
[1, 2, 3]])
x3:
tensor([[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3],
[1, 2, 3, 1, 2, 3]])
x4:
tensor([[[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3]],
[[1, 2, 3],
[1, 2, 3]]])
torch.split()
向量分割,生成的每个向量的长度为传入的参数值
a = [1,2,3,4,5,6,7,0,2]
a = torch.tensor(a)
a = a.unsqueeze(dim=1)
print(a.size()) # torch.Size([9, 1])
t = a.split(2)
print(t)
# (tensor([[1],[2]]), tensor([[3],[4]]), tensor([[5],[6]]), tensor([[7], [0]]), tensor([[2]]))
t = a.split([3,6])
print(t)
# (tensor([[1],[2],[3]]), tensor([[4],[5],[6], [7], [0], [2]]))
两个tensor间操作
首先生成两个4*3的数据
#start到end均匀分为num个,返回值是tensor
>>> a=t.linspace(1,23,12).reshape(4,3)
>>> a
tensor([[ 1., 3., 5.],
[ 7., 9., 11.],
[13., 15., 17.],
[19., 21., 23.]])
>>> b=t.linspace(2,24,12).reshape(4,3)
>>> b
tensor([[ 2., 4., 6.],
[ 8., 10., 12.],
[14., 16., 18.],
[20., 22., 24.]])
cat
沿着某一维进行拼接,cat后数据的总维度不变
>>> c=t.cat((a,b),0) # 0表示对行拼接,1表示对列拼接,这个值不能超过a,b的最大维数
>>> c
tensor([[ 1., 3., 5.],
[ 7., 9., 11.],
[13., 15., 17.],
[19., 21., 23.],
[ 2., 4., 6.],
[ 8., 10., 12.],
[14., 16., 18.],
[20., 22., 24.]])
>>> c.shape
torch.Size([8, 3])
>>> c=t.cat((a,b),1)
>>> c
tensor([[ 1., 3., 5., 2., 4., 6.],
[ 7., 9., 11., 8., 10., 12.],
[13., 15., 17., 14., 16., 18.],
[19., 21., 23., 20., 22., 24.]])
>>> c.shape
torch.Size([4, 6])
Stack
增加新的维度进行堆叠,如对tensor a,b在第0个维度上stack,则会变为2×4×3的tensor;在第1个维度上stack,则会变为4×2×3的tensor。
# 测试第0个维度
>>> d=t.stack((a,b),0)
>>> d
tensor([[[ 1., 3., 5.],
[ 7., 9., 11.],
[13., 15., 17.],
[19., 21., 23.]],
[[ 2., 4., 6.],
[ 8., 10., 12.],
[14., 16., 18.],
[20., 22., 24.]]])
>>> d.shape
torch.Size([2, 4, 3])
# 测试第1个维度
>>> e=t.stack((a,b),1)
>>> e
tensor([[[ 1., 3., 5.],
[ 2., 4., 6.]],
[[ 7., 9., 11.],
[ 8., 10., 12.]],
[[13., 15., 17.],
[14., 16., 18.]],
[[19., 21., 23.],
[20., 22., 24.]]])
>>> e.shape
torch.Size([4, 2, 3])
# 测试第2个维度
>>> f=t.stack((a,b),2)
>>> f
tensor([[[ 1., 2.],
[ 3., 4.],
[ 5., 6.]],
[[ 7., 8.],
[ 9., 10.],
[11., 12.]],
[[13., 14.],
[15., 16.],
[17., 18.]],
[[19., 20.],
[21., 22.],
[23., 24.]]])
>>> f.shape
torch.Size([4, 3, 2])
Tranpose
torch.transpose 只能交换2维的tensor
numpy.transpose 可以交换任意维的数组
#transpose 只能交换2维的tensor,输入三个参数会报错
## 这时可以尝试使用permute
Permute(交换多维的数组)
squeeze和unsqueeze
>>> a = a.reshape(1,-1) #reshape成1x12的
>>> a_ = a.squeeze() # squeeze 不写参数,默认把所有等于1的维度都给压缩了
>>>_a = a__.unsqueeze(0) # 指定在第0维上扩展