x.squeeze()、x.unsqueeze()用法
生活随笔
收集整理的這篇文章主要介紹了
x.squeeze()、x.unsqueeze()用法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
x.squeeze(dim)
用途:進行維度壓縮,去掉tensor中維數為1的維度
參數設置:如果設置dim=a,就是去掉指定維度中維數為1的
示例:
import torch x = torch.tensor([[[1],[2]],[[3],[4]]]) print('x:',x) x1 = x.squeeze() print('x1:',x1) x2 = x.squeeze(2) print('x2:',x2)?輸出:
x: tensor([[[1],[2]],[[3],[4]]]) x1: tensor([[1, 2],[3, 4]]) x2: tensor([[1, 2],[3, 4]])Process finished with exit code 0x.unsqueeze(dim=a)
用途:進行維度擴充,在指定位置加上維數為1的維度
參數設置:如果設置dim=a,就是在維度為a的位置進行擴充
示例:
import torch x = torch.tensor([1,2,3,4]) print(x) x1 = x.unsqueeze(dim=0) print(x1) x2 = x.unsqueeze(dim=1) print(x2)y = torch.tensor([[1,2,3,4],[9,8,7,6]]) print(y) y1 = y.unsqueeze(dim=0) print(y1) y2 = y.unsqueeze(dim=1) print(y2)輸出:
x: tensor([1, 2, 3, 4]) x1: tensor([[1, 2, 3, 4]]) x2: tensor([[1],[2],[3],[4]]) y: tensor([[1, 2, 3, 4],[9, 8, 7, 6]]) y1: tensor([[[1, 2, 3, 4],[9, 8, 7, 6]]]) y2: tensor([[[1, 2, 3, 4]],[[9, 8, 7, 6]]])Process finished with exit code 0總結
以上是生活随笔為你收集整理的x.squeeze()、x.unsqueeze()用法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 我说CMMI2.0之:II点睛
- 下一篇: 看完95后阿里p7的工资,觉得自己这两年