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]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值