当前位置:网站首页>解决: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
边栏推荐
- Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
- The second article, which is still unfinished, will be introduced again, and continue to explain oracledb_ Exporter monitors Oracle, a very low intrusive monitoring scheme.
- Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
- sorting and searching 二分查找法
- PHP implements the algorithm of adding from 1 to 100
- Verilog implementation of key dithering elimination
- [深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
- 苹果在其产品中拿掉了最后一颗Intel芯片
- How engineers treat open source -- the heartfelt words of an old engineer
- JS get some attributes of the object
猜你喜欢

Basic line chart: the most intuitive presentation of data trends and changes

Constructing verb sources for relation extraction

Mantium 如何在 Amazon SageMaker 上使用 DeepSpeed 实现低延迟 GPT-J 推理

redux

荐书丨《教育心理学》:送给明日教师的一本书~

Opencv learning notes -- Hough transform

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

荐书 |《学者的术与道》:写论文是门手艺

Dracoo master

工程师如何对待开源 --- 一个老工程师的肺腑之言
随机推荐
Matrix and Gauss elimination [matrix multiplication, Gauss elimination, solving linear equations, solving determinants] the most detailed in the whole network, with examples and sister chapters of 130
PHP implements the algorithm of adding from 1 to 100
《opencv学习笔记》-- 边缘检测和canny算子、sobel算子、LapIacian 算子、scharr滤波器
How to write abstract in English thesis?
Functions of anonymous functions
How engineers treat open source -- the heartfelt words of an old engineer
Constructing verb sources for relation extraction
Inventory the concept, classification and characteristics of cloud computing
Go plus security: an indispensable security ecological infrastructure for build Web3
【第019问 Unity中对SpherecastCommand的理解?】
Share | 2022 big data white paper of digital security industry (PDF attached)
CPU and GPU are out of date, and the era of NPU and APU begins
Can't the container run? The Internet doesn't have to carry the blame
In PHP, you can use the abs() function to turn negative numbers into positive numbers
[digital ic/fpga] Hot unique code detection
Write a paper for help, how to write the discussion part?
当你尝试删除程序中所有烂代码时 | 每日趣闻
[深入研究4G/5G/6G专题-42]: URLLC-13-《3GPP URLLC相关协议、规范、技术原理深度解读》-7-低延时技术-1-子载波间隔扩展
测试用例设计方法之:入门试招,等价边界初探
【数字IC/FPGA】热独码检测