torch.cat 和 torch.stack的区别
torch.cat
torch.cat(seq,dim=0,out=None)
# https://pytorch.org/docs/stable/generated/torch.cat.html#torch.cat
concatenate,英语单词,主要用作动词、形容词,作动词时译为“连接,连结,使连锁”,作形容词时译为“连接的,连结的,连锁的”。
>>> x = torch.randn(2, 3)
>>> x
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 0)
tensor([[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497],
[ 0.6580, -1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497]])
>>> torch.cat((x, x, x), 1)
tensor([[ 0.6580, -1.0969, -0.4614, 0.6580, -1.0969, -0.4614, 0.6580,
-1.0969, -0.4614],
[-0.1034, -0.5790, 0.1497, -0.1034, -0.5790, 0.1497, -0.1034,
-0.5790, 0.1497]])
torch.stack
torch.stack(seq,dim=0,out=None)
# https://pytorch.org/docs/stable/generated/torch.stack.html?highlight=torch%20stack#torch.stack
stack堆叠会增加维度n. 堆栈; (通常指码放整齐的)一叠,一摞,一堆; 大量; 许多; 一大堆; (尤指工厂的)大烟囱; 书库;v. (使)放成整齐的一叠(或一摞、一堆); 使成叠(或成摞、成堆)地放在…; 使码放在…; (令飞机)分层盘旋等待着陆;
self.layers = nn.ModuleList([nn.Linear(int(self.in_dim), int(self.out_dim)) for i in range(length)])
torch.stack([layer(x) for layer in self.layers], 0) # 会将矩阵size[the_first_dim_x,out_dim]的数据变为 size[n,the_first_dim_x,out_dim]
本文详细介绍了PyTorch中torch.cat和torch.stack两个函数的区别。torch.cat用于沿指定维度连接张量,增加的是元素数量;而torch.stack则是在张量的维度外增加新的轴,使得多个张量并列组成一个新的张量。通过实例展示了两种函数的不同使用方式及其效果。
1501

被折叠的 条评论
为什么被折叠?



