pytorch中tensor的view用法小记

用法类似于numpy的resize,主要是用来改变tensor的大小

若参数不带-1

1
2
3
4
5
temp1 = torch.tensor([-0.3623, -0.6115,  0.7283,  0.4699,  2.3261,  0.1599])
result1 = temp1.view(3,2)

result1

1
2
3
4
5
Output: >>

tensor([[-0.3623, -0.6115],
[ 0.7283, 0.4699],
[ 2.3261, 0.1599]])

有的时候会出现torch.view(-1)或者torch.view(参数a,-1)这种情况

1
2
3
4
5
6
7
temp2=torch.tensor([[-0.3623, -0.6115],
... [ 0.7283, 0.4699],
... [ 2.3261, 0.1599]])

result2 = temp2.view(-1)
result2

1
2
3
Output: >>

tensor([-0.3623, -0.6115, 0.7283, 0.4699, 2.3261, 0.1599])
1
2
3
4
5
6
temp3=torch.tensor([[-0.3623, -0.6115],
... [ 0.7283, 0.4699],
... [ 2.3261, 0.1599]])
result3=temp3.view(2,-1)
result3

1
2
3
4
Output: >>

tensor([[-0.3623, -0.6115, 0.7283],
[ 0.4699, 2.3261, 0.1599]])
作者

Jhuoer Yen

发布于

2024-05-06

更新于

2024-05-06

许可协议

评论

Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×