当前位置:网站首页>解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
解决:RuntimeError: Expected object of scalar type Int but got scalar type Double
2022-07-26 04:11:00 【淮雵的Blog】
问题描述:
最近在写使用神经网络预测的代码,在更换导入的数据后,原本的loss函数(如下所示)报错RuntimeError: Expected object of scalar type Int but got scalar type Double。后来根据CSDN上的许多博文尝试使用众多方法,例如:loss函数前面加X=torch.tensor(np.int(X))或者y=torch.IntTensor(y),但是之后又出现“ValueError: only one element tensors can be converted to Python scalar等报错,于是根据其它博文更改为X= torch.tensor([item.cpu().detach().numpy() for item in np.int(X)]).cuda(),于是又回到上述的报错,让人头疼不已。
for X,y in data_iter(batch_size,train_feature,train_label):#内循环训练一个batch
l=loss(X,y).sum() #计算模型输出与真实数据之间的差距
解决方案:
其实解决该问题十分简单,由于导入的表格后缀名为xlsx(即:正常的Excel文件),只要将该文件另存为csv格式即可(需要注意的是,读取个数必须为utf-8,否则在读取时会由于csv中的编码格式是‘ANSI’,而Python 3默认读取格式为‘utf-8’,导致decode异常。因此会导致产生报错’utf-8’ codec can’t decode bytes in position 16: invalid continuation byte)。
在将后缀名由xlsx改为csv后,顺利读取文件并进行训练,中途没有再产生任何其它报错(ヾ(◍°∇°◍)ノ゙开森森~开森森~)
修改理由:
1、CSV是纯文本文件,excel不是纯文本,excel包含很多格式信息在里面。即:CSV是纯文本文件,CSV文件在windows平台默认的打开方式是excel,但是它的本质是一个文本文件。excel不是纯文本,Excel是一个二进制文件,它保存有关工作簿中所有工作表的信息。因此,csv文件更加地纯粹。
其它理由:
1、CSV文件的体积会更小,创建分发读取更加方便,适合存放结构化信息,比如记录的导出,流量统计等等。
2、由于数值和文本之间没有明确的区别或区分,Excel可以使用自动格式化功能搞乱数值。
CSV是比较安全的,可以清楚地区分数值和文本。CSV不处理数据并按原样存储。
3、Excel导入数据时消耗更多的内存。导入CSV文件可以更快,而且消耗更少的内存。
4、Excel文件只能用Microsoft Excel文档打开。CSV可以用Windows中的任何文本编辑器打开,如记事本,MS Excel,Microsoft Works 9等。
参考文献:
https://www.cnblogs.com/Dongmy/p/16434120.html
https://blog.csdn.net/qq_38703529/article/details/120216078
https://blog.csdn.net/qq_18649781/article/details/90771682
https://blog.csdn.net/qq_38703529/article/details/120216078
https://blog.csdn.net/yzf0011/article/details/93321576
边栏推荐
- Small ball and box model, arrangement and combination
- 【云原生】谈谈老牌消息中间件ActiveMQ的理解
- Verilog implementation of key dithering elimination
- 【SVN】一直出现 Please execute the ‘Cleanup‘ command,cleanup以后没有反应的解决办法
- 【二叉树】二叉树中的最长交错路径
- Uniapp pit filling Tour
- Lua and go mixed call debugging records support cross platform (implemented through C and luajit)
- Firewall command simple operation
- operator new、operator delete补充讲义
- 【云原生之kubernetes】kubernetes集群下ConfigMap使用方法
猜你喜欢

Helloworld案例分析

Retail chain store cashier system source code management commodity classification function logic sharing

按键消抖的Verilog实现

如何构建面向海量数据、高实时要求的企业级OLAP数据引擎?

《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器

Constructing verb sources for relation extraction

Verilog implementation of key dithering elimination

Go Plus Security:一款Build Web3不可或缺的安全生态基础设施

How to build an enterprise level OLAP data engine for massive data and high real-time requirements?

Analysis on the infectious problem of open source license
随机推荐
[in depth study of 4g/5g/6g topic-42]: urllc-13 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -7-low delay technology-1-subcarrier spacing expansio
Brief tutorial for soft exam system architecture designer | case analysis and problem solving skills
[question 019: what is the understanding of spherecastcommand in unity?]
PHP implements the algorithm of adding from 1 to 100
Laravel8 implements interface authentication encapsulation using JWT
工程师如何对待开源 --- 一个老工程师的肺腑之言
I.MX6U-ALPHA开发板(主频和时钟配置实验)
operator new、operator delete补充讲义
Working ideas of stability and high availability guarantee
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
Connect external MySQL databases in istio Service Grid
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
1311_硬件设计_ICT概念、应用以及优缺点学习小结
[project chapter - how to write and map the business model? (3000 word graphic summary suggestions)] project plan of innovation and entrepreneurship competition and application form of national Entrep
零售连锁门店收银系统源码管理商品分类的功能逻辑分享
电商运营小白,如何快速入门学习数据分析?
STM32状态机编程实例——全自动洗衣机(下)
当你尝试删除程序中所有烂代码时 | 每日趣闻
PHP < => spacecraft operator (combined comparator)
What model is good for the analysis of influencing factors?