torch.cat 和 torch.stack的区别

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,算法)