代码是用jupyter notebook写的
import torch
a = torch.arange(6).reshape(2,3)
a
'''输出
tensor([[0, 1, 2],
[3, 4, 5]])
'''
a = a.T
a
'''输出
tensor([[0, 3],
[1, 4],
[2, 5]])
'''
a.reshape(-1)
'''输出
tensor([0, 3, 1, 4, 2, 5])
'''
a.view(-1)
'''输出
RuntimeError: view size is not compatible with input tensor's size and stride (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
'''
转置应该只改变读取顺序,不改变内存顺序(不确定是不是)
view需要保证读取顺序是内存连续的
总结:用reshape
import torch
a = torch.arange(6).reshape(6,1)
b = torch.arange(3).reshape(1,3)
a,b,a+b
'''输出
(tensor([[0],
[1],
[2],
[3],
[4],
[5]]),
tensor([[0, 1, 2]]),
tensor([[0, 1, 2],
[1, 2, 3],
[2, 3, 4],
[3, 4, 5],
[4, 5, 6],
[5, 6, 7]]))
'''
m×1的矩阵 + 1×n的矩阵 = m×n的矩阵
可以理解为坐标轴,练习题里的三维张量相加可以看作三个坐标轴
实际上张量相加时,值为1的维度都会尝试进行广播,比如
# 有一个维度值为1
a2 = torch.arange(24).reshape(2,3,4)
b2 = torch.ones(1,3,4)
a2,b2,a2+b2
'''输出
(tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]),
tensor([[[1., 1., 1., 1.],
[1., 1., 1., 1.],
[1., 1., 1., 1.]]]),
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.]]]))
'''
# 有两个维度值为1
a3 = torch.arange(24).reshape(2,3,4)
b3 = torch.ones(1,1,4)
a3,b3,a3+b3
'''输出
(tensor([[[ 0, 1, 2, 3],
[ 4, 5, 6, 7],
[ 8, 9, 10, 11]],
[[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23]]]),
tensor([[[1., 1., 1., 1.]]]),
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.]]]))
'''