Pytorch 维度变换

Pytorch 维度变换

view reshape
import torch
a = torch.rand(4, 1, 28, 28)
print("a.shape:\t", a.shape)
# prod(a.size) = prod(a'.size)
b = a.view(4, 28*28)
print("b:\t", b)

c = a.view(4, 28*28).shape
print("c:\t", c)

d = a.view(4, 784).shape
print("d:\t", d)

e = a.view(4*28, 28).shape
print("e:\t", e)

f = a.view(4*1, 28, 28).shape
print("f:\t", f)

g = a.view(4, 784)
print("g:\t", g)
a.shape:	 torch.Size([4, 1, 28, 28])
b:	 tensor([[0.7603, 0.8994, 0.2818,  ..., 0.1005, 0.2483, 0.6218],
        [0.3760, 0.4059, 0.1720,  ..., 0.8974, 0.9278, 0.8993],
        [0.9729, 0.7864, 0.0886,  ..., 0.1318, 0.5178, 0.1670],
        [0.7797, 0.0013, 0.5324,  ..., 0.2778, 0.4463, 0.6496]])
c:	 torch.Size([4, 784])
d:	 torch.Size([4, 784])
e:	 torch.Size([112, 28])
f:	 torch.Size([4, 28, 28])
g:	 tensor([[0.7603, 0.8994, 0.2818,  ..., 0.1005, 0.2483, 0.6218],
        [0.3760, 0.4059, 0.1720,  ..., 0.8974, 0.9278, 0.8993],
        [0.9729, 0.7864, 0.0886,  ..., 0.1318, 0.5178, 0.1670],
        [0.7797, 0.0013, 0.5324,  ..., 0.2778, 0.4463, 0.6496]])
unsqueeze 新插入一个维度
import torch
a = torch.rand(4, 1, 28, 28)
b = torch.Size([4, 1, 28, 28])
# 在最前面增加了一个维度
c = a.unsqueeze(0).shape
print("c:\t", c)

d = a.unsqueeze(-1).shape
print("d:\t", d)

e = a.unsqueeze(4).shape
print("e:\t", e)

f = a.unsqueeze(-4).shape
print("f:\t", f)

g = a.unsqueeze(-5).shape
print("g:\t", g)

h = torch.tensor([1.2, 2.3])
I = h.unsqueeze(-1)
print("I:\t", I)

J = h.unsqueeze(0)
print("J:\t", J)
c:	 torch.Size([1, 4, 1, 28, 28])
d:	 torch.Size([4, 1, 28, 28, 1])
e:	 torch.Size([4, 1, 28, 28, 1])
f:	 torch.Size([4, 1, 1, 28, 28])
g:	 torch.Size([1, 4, 1, 28, 28])
I:	 tensor([[1.2000], [2.3000]])
J:	 tensor([[1.2000, 2.3000]])

import torch
a = torch.rand(32)
print("a:\t", a)

b = torch.rand(4, 32, 14, 14)
c = a.unsqueeze(1).unsqueeze(2).unsqueeze(0)

print("c.shape:\t", c.shape)
a:	 tensor([0.3070, 0.2438, 0.3220, 0.2777, 0.6202, 0.7803, 0.3970, 0.5021, 0.1788,
        0.2335, 0.3152, 0.0943, 0.8410, 0.9082, 0.3120, 0.9525, 0.4011, 0.1827,
        0.4022, 0.5850, 0.7507, 0.6316, 0.1653, 0.7424, 0.0729, 0.1857, 0.4692,
        0.1159, 0.9131, 0.3927, 0.0413, 0.8741])
c.shape:	 torch.Size([1, 32, 1, 1])
Squeeze:
import torch
a = torch.rand(32)
b = a.unsqueeze(1).unsqueeze(2).unsqueeze(0)
print("b.shape:\t", b.shape)
# squeeze
c = b.squeeze().shape
print("c:\t", c)

d = b.squeeze(0).shape
print("d:\t", d)

e = b.squeeze(-1).shape
print("e:\t", e)
# shape不为1, 因此32不会变
f = b.squeeze(1).shape
print("f:\t", f)

g = b.squeeze(-4).shape
print("g:\t", g)
b.shape:	 torch.Size([1, 32, 1, 1])
c:	 torch.Size([32])
d:	 torch.Size([32, 1, 1])
e:	 torch.Size([1, 32, 1])
f:	 torch.Size([1, 32, 1, 1])
g:	 torch.Size([32, 1, 1])
Expand (扩张) :
import torch
a = torch.rand(32)
b = a.unsqueeze(1).unsqueeze(2).unsqueeze(0)
print("b.shape:\t", b.shape)

c = b.expand(4, 32, 14, 14).shape
print("c:\t", c)
# -1表示不变 
d = b.expand(-1, 32, -1, -1).shape
print("d:\t", d)

e = b.expand(-1, 32, -1, -4).shape
print("e:\t", e)
b.shape:	 torch.Size([1, 32, 1, 1])
c:	 torch.Size([4, 32, 14, 14])
d:	 torch.Size([1, 32, 1, 1])
e:	 torch.Size([1, 32, 1, -4])
repeat(扩张/重复):
import torch
a = torch.rand(32)
b = a.unsqueeze(1).unsqueeze(2).unsqueeze(0)
print("b.shape:\t", b.shape)
# 每一个维度要重复的次数
c = b.repeat(4, 32, 1, 1).shape
print("c:\t", c)

d = b.repeat(4, 1, 1, 1).shape
print("d:\t", d)

e = b.repeat(4, 1, 32, 32).shape
print("e:\t", e)
b.shape:	 torch.Size([1, 32, 1, 1])
c:	 torch.Size([4, 1024, 1, 1])
d:	 torch.Size([4, 32, 1, 1]) 
e:	 torch.Size([4, 32, 32, 32])
Transpose:
permute:
import torch
a = torch.rand(4, 3, 28, 28)
b = a.transpose(1, 3).shape
print("b:\t", b)

c = torch.rand(4, 3, 28, 32)
d = c.transpose(1, 3).shape
print("d:\t", d)

e = c.transpose(1, 3).transpose(1, 2).shape
print("e:\t", e)

f = c.permute(0, 2, 3, 1).shape
print("f:\t", f)
b:	 torch.Size([4, 28, 28, 3])
d:	 torch.Size([4, 32, 28, 3])
e:	 torch.Size([4, 28, 32, 3])
f:	 torch.Size([4, 28, 32, 3])
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值