2.1 拼接与切分 cat & stack
cat 不会扩展维度 stack 会增加维度 2维 --> 3维
import torch
t = torch.eye(2,3)
t0 = torch.cat([t,t],dim=0) #在 第一轴上拼接 y 轴
t1 = torch.cat([t,t],dim=1) #在 第二轴上拼接 x 轴
print('output','\n', 't :', t)
print('\n', 't0 :', t0)
print('\n', 't1 :', t1)
output
t : tensor([[1., 0., 0.],
[0., 1., 0.]])
t0 : tensor([[1., 0., 0.],
[0., 1., 0.],
[1., 0., 0.],
[0., 1., 0.]])
t1 : tensor([[1., 0., 0., 1., 0., 0.],
[0., 1., 0., 0., 1., 0.]])
import torch
t = torch.eye(2,3)
t0 = torch.cat([t,t],dim=0) #在 第一轴上拼接 y 轴
ts0 = torch.stack([t,t],dim=0)
ts1 = torch.stack([t,t],dim=1)
#在 第二轴上拼接 x 轴
print('output','\n', 't :', t,t.shape)
print('\n', 't0 :', t0,t0.shape)
print('\n', 'ts0 :', ts0, ts0.shape)
print('\n', 'ts1 :', ts1, ts1.shape)
output
t : tensor([[1., 0., 0.],
[0., 1., 0.]]) torch.Size([2, 3])
t0 : tensor([[1., 0., 0.],
[0., 1., 0.],
[1., 0., 0.],
[0., 1., 0.]]) torch.Size([4, 3])
ts0 : tensor([[[1., 0., 0.],
[0., 1., 0.]],
[[1., 0., 0.],
[0., 1., 0.]]]) torch.Size([2, 2, 3])
ts1 : tensor([[[1., 0., 0.],
[1., 0., 0.]],
[[0., 1., 0.],
[0., 1., 0.]]]) torch.Size([2, 2, 3])
2.2 拆分 chuck & split
chunk 相当于 除几份,split 2 by dim =1 相当于 每两行 切一段
import torch
t = torch.eye(2,3)
t0 = torch.chunk(t,2,dim=0) #在 第一轴上切 y 轴
t1 = torch.chunk(t,3,dim=1) #在 第2轴上切
print('output','\n', 't :', t,t.shape)
print('\n', 't0 :', t0)
print('\n', 't1 :', t1)
output
t : tensor([[1., 0., 0.],
[0., 1., 0.]]) torch.Size([2, 3])
t0 : (tensor([[1., 0., 0.]]), tensor([[0., 1., 0.]]))
t1 : (tensor([[1.], [0.]]), tensor([[0.], [1.]]), tensor([[0.], [0.]]))
不能整除的case
import torch
t = torch.eye(3,5)
t0 = torch.chunk(t,2,dim=0) #在 第一轴上切 y 轴
t1 = torch.chunk(t,2,dim=1) #在 第2轴上切
print('output','\n', 't :', t,t.shape)
print('\n', 't0 :', t0)
print('\n', 't1 :', t1)
output
t : tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.],
[0., 0., 1., 0., 0.]]) torch.Size([3, 5])
t0 : (tensor([[1., 0., 0., 0., 0.],
[0., 1., 0., 0., 0.]]), tensor([[0., 0., 1., 0., 0.]]))
t1 : (tensor([[1., 0., 0.], [0., 1., 0.], [0., 0., 1.]]), tensor([[0., 0.], [0., 0.], [0., 0.]]))
是 3x3 +3x2 的张量
import torch
t = torch.ones(3,5)
l = torch.split(t,2)
print('output t :', t, '\n','list',l)
output t : tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
list (tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]]), tensor([[1., 1., 1., 1., 1.]]))
import torch
t = torch.ones(3,5)
l = torch.split(t,2,dim = 1)
print('output t :', t, '\n','list',l)
output t : tensor([[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.],
[1., 1., 1., 1., 1.]])
list (tensor([[1., 1.],
[1., 1.],
[1., 1.]]), tensor([[1., 1.],
[1., 1.],
[1., 1.]]), tensor([[1.],
[1.],
[1.]]))
2.3 张量索引 使用 index_select / Mask
import torch
out_t =torch.tensor([1])
print('output ', '\n out_t :', out_t, '\n',)
t0 = torch.randint(1,100,size=(3,4))
print('t0 :', t0, '\n')
idx = torch.tensor([0,2],dtype=torch.long) # float
print('index :', idx, '\n')
t1 = torch.index_select(t0,dim=0,index=idx,out=out_t)
print('t1 :', t1, '\n')
print('out_t :', out_t, '\n')
output
out_t : tensor([1])
初始化tensor
t0 : tensor([[23, 74, 97, 46],
[10, 29, 95, 38],
[23, 39, 55, 52]])
index : tensor([0, 2])
只取 0,2 row
t1 : tensor([[23, 74, 97, 46],
[23, 39, 55, 52]])
out_t 被重新赋值, 有 warning shape [1],-->[2, 4]
out_t : tensor([[23, 74, 97, 46],
[23, 39, 55, 52]])
UserWarning: An output with one or more elements was resized since it had shape [1], which does not match the required output shape [2, 4]. This behavior is deprecated, and in a future PyTorch release outputs will not be resized unless they have zero elements. You can explicitly reuse an out tensor t by resizing it, inplace, to zero elements with t.resize_(0).
修正代码如下,就不会报警了
import torch
out_t =torch.tensor([1])
print('output ', '\n out_t :', out_t, '\n',)
t0 = torch.randint(1,100,size=(3,4))
print('t0 :', t0, '\n')
idx = torch.tensor([0,2],dtype=torch.long) # float
print('index :', idx, '\n')
out_t.resize_(0) ## 《 resize_
t1 = torch.index_select(t0,dim=0,index=idx,out=out_t)
print('t1 :', t1, '\n')
print('out_t :', out_t, '\n')
masked_select(input, mask, *, out=None) -> Tensor
输出1 维的 数
Returns a new 1-D tensor which indexes the input
tensor according to the boolean mask mask
which is a BoolTensor
.
The shapes of the mask
tensor and the input
tensor don't need to match, but they must be broadcastable: broadcasting-semantics
.
🛈 Note: The returned tensor does not use the same storage as the original tensor
Args: input (Tensor): the input tensor. mask (BoolTensor): the tensor containing the binary mask to index with
Keyword args: out (Tensor, optional): the output tensor.
import torch
out_t =torch.tensor([1])
print('output ', '\n out_t :', out_t, '\n',)
t0 = torch.randint(1,10,size=(3,4))
print('t0 :', t0, '\n')
mask = t0.ge(5) # >= 5 的获得 true
print('mask :', mask, '\n')
out_t.resize_(0)
t1 = torch.masked_select(t0,mask,out=out_t)
print('t1 :', t1, '\n')
print('out_t :', out_t, '\n')
output
out_t : tensor([1])
t0 : tensor([[9, 7, 8, 7],
[4, 5, 5, 1],
[9, 1, 9, 4]])
mask : tensor([[ True, True, True, True],
[False, True, True, False],
[ True, False, True, False]])
t1 : tensor([9, 7, 8, 7, 5, 5, 9, 9])
out_t : tensor([9, 7, 8, 7, 5, 5, 9, 9])
2.4 张量变换
reshapre: 拉平了再铺
import torch
t0 = torch.randint(1,10,size=(3,4))
print('t0 :', t0, '\n')
t1 = torch.reshape(t0,(4,3))
print('t1 :', t1, '\n')
t0 : tensor([[6, 7, 5, 3],
[4, 3, 2, 5],
[5, 2, 3, 4]])
t1 : tensor([[6, 7, 5],
[3, 4, 3],
[2, 5, 5],
[2, 3, 4]])
transpose(): 行列交换,需要定义 dim
import torch
t0 = torch.randint(1,10,size=(3,4))
print('t0 :', t0, '\n')
t1 = torch.transpose(t0,dim0=1,dim1=0)
print('t1 :', t1, '\n')
t0 : tensor([[8, 8, 5, 5],
[2, 6, 3, 5],
[1, 6, 7, 5]])
t1 : tensor([[8, 2, 1],
[8, 6, 6],
[5, 3, 7],
[5, 5, 5]])
多维度情况
import torch
t0 = torch.randint(1,10,size=(2,3,4))
print('t0 :', t0, t0.shape,'\n')
t1 = torch.transpose(t0,dim0=1,dim1=0)
print('t1 :', t1, t1.shape ,'\n')
t0 : tensor([[[1, 1, 3, 4],
[4, 7, 4, 7],
[4, 9, 2, 8]],
[[2, 5, 1, 7],
[1, 5, 5, 7],
[5, 2, 7, 5]]]) torch.Size([2, 3, 4])
t1 : tensor([[[1, 1, 3, 4],
[2, 5, 1, 7]],
[[4, 7, 4, 7],
[1, 5, 5, 7]],
[[4, 9, 2, 8],
[5, 2, 7, 5]]]) torch.Size([3, 2, 4])
torch.t = transpose
import torch
t0 = torch.randint(1,10,size=(3,4))
print('t0 :', t0, '\n')
t1 = torch.t(t0)
print('t1 :', t1, '\n')
t0 : tensor([[9, 3, 6, 1],
[3, 3, 5, 3],
[2, 1, 2, 4]])
t1 : tensor([[9, 3, 2],
[3, 3, 1],
[6, 5, 2],
[1, 3, 4]])
squeeze 压缩
只压缩 长度 = 1 的张量,可指定维度压缩
压缩 dimension = 2
import torch
t0 = torch.randint(1,10,size=(1,3,1))
print('t0 :', t0, t0.shape,'\n')
t1 = torch.squeeze(t0,dim=2)
print('t1 :', t1, t1.shape ,'\n')
t0 : tensor([[[5],
[9],
[5]]]) torch.Size([1, 3, 1])
t1 : tensor([[5, 9, 5]]) torch.Size([1, 3])
dimension = 0 , output 如下
t0 : tensor([[[2], [6], [7]]]) torch.Size([1, 3, 1]) t1 : tensor([[2], [6], [7]]) torch.Size([3, 1])
import torch
t0 = torch.randint(1,10,size=(1,3,1))
print('t0 :', t0, t0.shape,'\n')
t1 = torch.squeeze(t0)
print('t1 :', t1, t1.shape ,'\n')
如果 dim 不指定, 压缩全部 len = 1 的维度
t0 : tensor([[[9],
[9],
[9]]]) torch.Size([1, 3, 1])
t1 : tensor([9, 9, 9]) torch.Size([3])
unsqueeze 扩展
import torch
t0 = torch.randint(1,10,size=(1,3,1))
print('t0 :', t0, t0.shape,'\n')
t1 = torch.squeeze(t0)
print('t1 :', t1, t1.shape ,'\n')
t2 = torch.unsqueeze(t1,dim=0)
print('t2 :', t2, t2.shape ,'\n')
t2 = torch.unsqueeze(t1,dim=1)
print('t2 :', t2, t2.shape ,'\n')
t2 = torch.unsqueeze(t2,dim=0)
print('t2 :', t2, t2.shape ,'\n')
t0 : tensor([[[2],
[9],
[3]]]) torch.Size([1, 3, 1])
t1 : tensor([2, 9, 3]) torch.Size([3])
t2 : tensor([[2, 9, 3]]) torch.Size([1, 3])
t2 : tensor([[2],
[9],
[3]]) torch.Size([3, 1])
t2 : tensor([[[2],
[9],
[3]]]) torch.Size([1, 3, 1])