unsqueeze,squeeze
生活随笔
收集整理的這篇文章主要介紹了
unsqueeze,squeeze
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
import torch
import matplotlib.pyplot as plta = torch.randn(2, 3) # 標準正態(tài)分布生成隨機數(shù)
print("a:", a)
print("a.shape:", a.shape) # torch.Size([2, 3])# unsqueeze:擴充數(shù)據(jù)維度,在0起的指定位置N加上維數(shù)為一的維度
b = torch.unsqueeze(a, 1) # [2, 3]中在位置1,就是=3的位置增加維度1,3向后串
print("b.shape:", b.shape) # torch.Size([2, 1, 3])
print("b:", b)
c = torch.unsqueeze(a, 0) # [2, 3]中在位置0,就是=1的位置增加維度1,2向后串
print("c.shape:", c.shape) # torch.Size([1, 2, 3])
# --------------------------------------------------------------#
f = torch.randn(3)
print("f:", f)
print("f.shape:", f.shape) # torch.Size([3])
g = f.unsqueeze(0) # [3]中在位置0,就是=3的位置增加維度1,3向后串
print("g.shape:", g.shape) # torch.Size([1, 3])
print("g:", g)
# --------------------------------------------------------------#
# squeeze:維度壓縮,在0起的指定位置,去掉維數(shù)為1的的維度
print("c:", c)
print("c.shape:", c.shape)
d = torch.squeeze(c) # d=c.squeeze(0)
print("d:", d)
print(d.shape) # torch.Size([2, 3])
總結
以上是生活随笔為你收集整理的unsqueeze,squeeze的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Matlab gui用户图形设计界面学习
- 下一篇: 质量保障之路:达达测试团队成长记