torch.cat 与 torch.concat函数与torch.stack函数

cat和concat的区别

  先说结论:没有区别在功能、用法以及作用上,concat函数就是cat函数的别名(官方就是这样说的)。下面截图为证:在这里插入图片描述
  因此接下来就主要是介绍 torch.cat 函数的功能和用法。

torch.cat介绍

参考🔗:link

torch.cat(tensors, dim=0, *, out=None) → Tensor

作用

  将给定序列的张量在给定维度上连接起来。所有张量必须具有相同的形状(除了连接维度之外),或者是一个尺寸为(0,)的一维空张量。
Concatenates the given sequence of seq tensors in the given dimension. All tensors must either have the same shape (except in the concatenating dimension) or be a 1-D empty tensor with size (0,)

参数

  • 第一个参数 tensors :除了要连接的维度外,其他维度的形状都要相同的张量。tensors: Tuple[Tensor, …] | List[Tensor]。写法可以是(x, x, x)or [x, x, x]
  • 第二个参数 dim:(int, optiona)指定的连接的维度,可选,默认就是 dim=0,表示水平方向上拼接,即行拼接。这个参数可以是整数,负数,0,以及没有。
  • 其他参数不用管。

使用实例

import torch
x = torch.randn(2, 3)
x
# 输出
tensor([[ 1.3524,  0.7867, -0.1423],
        [ 1.1235,  0.0221, -0.5478]])

dim=0 表示水平方向的拼接,也就说从shape(2, 3) -> shape(6, 3):

<
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值