**
pytorch:https://pytorch.org/docs/stable/index.html
**
PyTorch的Permuted的使用:
PyTorch的Transpose的使用:transpose只能操作2D矩阵的转置。
>>> a = torch.randn(1, 2, 3, 4)
>>> a.size()
torch.Size([1, 2, 3, 4])
>>> b = a.transpose(1, 2) # Swaps 2nd and 3rd dimension
>>> b.size()
torch.Size([1, 3, 2, 4])
>>> c = a.view(1, 3, 2, 4) # Does not change tensor layout in memory
>>> c.size()
torch.Size([1, 3, 2, 4])
PyTorch的squeeze、unsqueeze的使用:
torch.squeeze(input, dim=None, out=None) → Tensor
eg: A1BC1D → ABCD
当dim给定的时候,压缩运算仅仅运算在给定的维度,
>>> x = torch.zeros(2, 1, 2, 1, 2)
>>> x.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x)
>>> y.size()
torch.Size([2, 2, 2])
>>> y = torch.squeeze(x, 0)
>>> y.size()
torch.Size([2, 1, 2, 1, 2])
>>> y = torch.squeeze(x, 1)
>>> y.size()
torch.Size([2, 2, 1, 2])
torch.unsqueeze(input, dim, out=None) → Tensor
PyTorch的reshape的使用:只改变输入数据的维度,内容不变。(变形而已)
PyTorch的view的使用:
>>> x = torch.randn(4, 4)
>>> x.size()
torch.Size([4, 4])
>>> y = x.view(16)
>>> y.size()
torch.Size([16])
>>> z = x.view(-1, 8) # the size -1 is inferred from other dimensions
>>> z.size()
torch.Size([2, 8])
**
**
reshape:numpy中的reshape类似pytorch的reshape
resize:numpy中resize类似pytorch的view
>>> a=np.array([[0,1],[2,3]])
>>> np.resize(a,(2,3))
array([[0, 1, 2],
[3, 0, 1]])
>>> np.resize(a,(1,4))
array([[0, 1, 2, 3]])
>>> np.resize(a,(2,4))
array([[0, 1, 2, 3],
[0, 1, 2, 3]])