概述
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])
最后
以上就是怕孤独大船为你收集整理的Pytorch 维度变换Pytorch 维度变换的全部内容,希望文章能够帮你解决Pytorch 维度变换Pytorch 维度变换所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复