当前位置:网站首页>PyTorch框架的 torch.cat()函数
PyTorch框架的 torch.cat()函数
2022-08-09 18:52:00 【哈哈哈哈哈嗝哈哈哈】
前言
搭建深度神经网络模型时,难免会遇到 torch.cat()
函数,来进行tensor
的拼接。
错误信息
代码案例如下:
import torch
import numpy as np
array1 = np.zeros((4, 1, 28, 28))
array2 = np.zeros((4, 1, 28, 28))
print("array1.shape:", array1.shape) # (4, 1, 28, 28)
tensor1 = torch.tensor(array1)
tensor2 = torch.tensor(array2)
print("tensor1.shape", tensor1.shape) # torch.Size([4, 1, 28, 28])
c = torch.cat(tensor1, tensor2, dim=0) # 报错
这样执行会报错,错误信息如下:
TypeError: cat() received an invalid combination of arguments - got (Tensor, Tensor, dim=int), but expected one of:
* (tuple of Tensors tensors, name dim, Tensor out)
didn't match because some of the keywords were incorrect: dim
* (tuple of Tensors tensors, int dim, Tensor out)
didn't match because some of the keywords were incorrect: dim
解决方法
torch.cat()
函数进行tensor
的拼接,将要拼接的tensor
组合成元组,即可解决该报错1。
代码修改如下:
import torch
import numpy as np
array1 = np.zeros((4, 1, 28, 28))
array2 = np.zeros((4, 1, 28, 28))
print("array1.shape:", array1.shape) # (4, 1, 28, 28)
tensor1 = torch.tensor(array1)
tensor2 = torch.tensor(array2)
print("tensor1.shape", tensor1.shape) # torch.Size([4, 1, 28, 28])
# c = torch.cat(tensor1, tensor2, dim=0) # 报错
c = torch.cat((tensor1, tensor2), dim=0)
print("c.shape", c.shape)
c1 = torch.cat((tensor1, tensor2), dim=1)
print("c1.shape", c1.shape)
此时输出为:
array1.shape: (4, 1, 28, 28)
tensor1.shape torch.Size([4, 1, 28, 28])
c.shape torch.Size([8, 1, 28, 28])
c1.shape torch.Size([4, 2, 28, 28])
从该案例中,不仅可以学到dubug信息,还可以详细了解到 torch.cat()
函数的具体用法,比如参数dim的含义等等。
如果我的这篇文章帮助到了你,那我也会感到很高兴,一个人能走多远,在于与谁同行。
参考
边栏推荐
- Codesys结构变量编程应用(STRUCT类型)
- [免费专栏] Android安全之Android Studion 动态调试APK的两种方法
- php删除字符串的空格
- [Free Column] Android Fragment Injection for Android Security
- Openharmony轻量系统实验--GPIO点灯
- ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
- Laravel之队列「建议收藏」
- Abbkine TraKine Pro 活细胞微管染色试剂盒重要特色
- Toronto Research Chemicals单羟基舒更葡糖钠说明书
- Flume (六) --------- Flume 数据流监控
猜你喜欢
基于CC2530 E18-MS1-PCB Zigbee DIY作品(三)
ebook下载 | 《 企业高管IT战略指南——企业为何要落地DevOps》
为什么数字钱包需要引入小程序生态
[免费专栏] Android安全之APK动态方式逆向应用【三种Smali注入方法】
小满nestjs(第五章 nestjs cli)
重磅!上海985教授当选!全球仅4人!
嵌入式开发:使用FILL提高代码完整性
【kali-权限提升】(4.2.7)社会工程学工具包:权限维持创建后门、清除痕迹
[Free Column] Android Fragment Injection for Android Security
Leetcode 739.每日温度 单调栈
随机推荐
这年头还不来尝试线稿图视频??
[免费专栏] Android安全之Android应用的汉化功能(修改so中的字符串内容)
[Free Column] Android Fragment Injection for Android Security
漏洞复现-redis未授权getshell
时序攻击
ebook download | "Business executives' IT strategy guide - why enterprises should implement DevOps"
鲜花线上销售管理系统的设计与实现
Toronto Research Chemicals盐酸乙环胺应用说明
毕昇编译器优化:Lazy Code Motion
mysql duplicate data group multiple latest records
An overview of Office 365 Groups and how to create them
为什么数字钱包需要引入小程序生态
[免费专栏] Android安全之ZIP文件目录遍历漏洞
纸业供应链协同管理系统:重构纸业智慧供应网络,支撑企业数字化转型升级
Transformer如何用于3D视觉?阿联酋MBZUAI最新《3D视觉Transformers处理》综述,涵盖100+种方法
2021 RoboCom 世界机器人开发者大赛-本科组(决赛)
NetCore路由的Endpoint模式
Flume (六) --------- Flume 数据流监控
基于CC2530 E18-MS1-PCB Zigbee DIY作品
laravel 时区问题timezone