torch.cat((A,B),dim=1)解析

官方说明torch.cat

引用自:Pytorch中的torch.cat()函数

python 复制代码
torch.cat(tensors, dim=0, *, out=None) → Tensor
# 连接给定维数的给定序列的序列张量。所有张量要么具有相同的形状(除了连接维度),要么为空。

示例

输入:

python 复制代码
import torch
a = torch.Tensor(2,3)   #  (2行,3列)
b = torch.Tensor(2,3)
print (a)
print (b)

输出:

python 复制代码
tensor([[8.9082e-39, 1.0194e-38, 9.1837e-39],
        [8.4490e-39, 9.6429e-39, 8.4490e-39]])
tensor([[-2.0541e-05,  5.0727e-43, -2.0541e-05],
        [ 5.0727e-43, -2.1039e-05,  5.0727e-43]])

输入:

python 复制代码
print(torch.cat([a,b], dim= 0))  
# 1. torch.cat((x,y),dim=0) :张量 X,Y按照列堆起来

输出:

python 复制代码
tensor([[ 8.9082e-39,  1.0194e-38,  9.1837e-39],
        [ 8.4490e-39,  9.6429e-39,  8.4490e-39],
        [-2.0541e-05,  5.0727e-43, -2.0541e-05],
        [ 5.0727e-43, -2.1039e-05,  5.0727e-43]])

输入:

python 复制代码
print(torch.cat([a,b], dim=-1))
# 2. torch.cat((x,y),dim=1) :张量 X,Y按照行并排起来

输出:

python 复制代码
tensor([[ 8.9082e-39,  1.0194e-38,  9.1837e-39, -2.0541e-05,  5.0727e-43,
         -2.0541e-05],
        [ 8.4490e-39,  9.6429e-39,  8.4490e-39,  5.0727e-43, -2.1039e-05,
          5.0727e-43]])

总结:

torch.cat((x,y),dim=0) :张量 X,Y按照列堆起来
torch.cat((x,y),dim=1) :张量 X,Y按照行并排起来

相关推荐
默_笙3 天前
🍙 给每个请求过安检:FastAPI 是怎么把校验写进类型注解的
python
LaughingZhu3 天前
Product Hunt 每日热榜 | 2026-09-19
人工智能·深度学习·神经网络·搜索引擎·百度
qq_426003963 天前
启动playwright录制codegen生成自动化测试脚本
python·自动化
虎头金猫3 天前
4K 视频总卡在公网带宽?用 N1 + OpenList 把网盘播放链路重新理顺
运维·服务器·网络·python·容器·beautifulsoup·pandas
长沙三为智能科技3 天前
家政小程序开发从0到上线:五阶段交付流程与验收清单
python
风合星语3 天前
2026 具身智能技术实战(一):VLA 到底怎么控制机器人?——用 LeRobot 跑通 SmolVLA 推理
pytorch·机器人·具身智能·vla·lerobot·smolvla
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
只睡四小时3 天前
Canvas 弹道联机实战:700 行 + 固定时间步长
python·websocket·html5·游戏开发·canvas
高洁013 天前
未来三年,AI 落地的五个确定性判断一
人工智能·深度学习·机器学习·transformer·知识图谱