问题描述:

最近在写使用神经网络预测的代码,在更换导入的数据后,原本的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):#内循环训练一个batchl=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后,顺利读取文件并进行训练,中途没有再产生任何其它报错(ヾ(◍°∇°◍)ノ゙开森森~开森森~)
  • 第二种方案:可能是由于数据的问题,只要在Excel后面加上几行没用的数据,在使用时Python不去读取它们,然后就可以顺利使用了(这个办法亲测有效,但是博主目前还不明白为什么,有知道的大佬欢迎在评论区里交流)。

修改理由:

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

解决:RuntimeError: Expected object of scalar type Int but got scalar type Double相关推荐

  1. 使用PyTorch前向运算时出现“RuntimeError: Expected object of scalar type Long but got scalar type Float for ……”

    1 问题描述 今天在使用PyTorch搭建模型时,出现了一个问题: RuntimeError: Expected object of scalar type Long but got scalar t ...

  2. RuntimeError: Expected object of type torch.cuda.LongTensor but found type torch.cuda.IntTensor

    问题: anchor_label = np.array(self.id_dict[int(anchor_id)])identity_loss = criterion_identity(predicte ...

  3. RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAType

    RuntimeError: Expected tensor for argument #1 'indices' to have scalar type Long; but got torch.cuda ...

  4. RuntimeError: Expected object of device type cuda but got device type cpu for argument pytorch数据位置

    RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...

  5. RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target'

    RuntimeError: Expected object of device type cuda but got device type cpu for argument #2 'target' i ...

  6. RuntimeError: Expected object of backend CUDA but got backend CPU for argument

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

  7. RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1'

    RuntimeError: Expected object of backend CUDA but got backend CPU for argument #4 'mat1' 原因:变量没有加cud ...

  8. 解决RuntimeError: Expected all tensors to be on the same device, but found at least two devices, cp

    今天在把.pt文件转ONNX文件时,遇到此错误. 报错 RuntimeError: Expected all tensors to be on the same device, but found a ...

  9. RuntimeError: Expected tensor for argument #1 ‘indices‘ to have scalar type Long; but got CUDAFloatT

    使用torchsummary 中的 summary函数时,模型中有Embedding层的时候,会报错.原因是Embedding 需要整型的Tensor. pip install 的torchsumma ...

最新文章

  1. LeetCode简单题之两个列表的最小索引总和
  2. 精选 Github 近期13款开源工具包!(附数据集、链接)
  3. acdream 1157Segments cdq分治
  4. UML大战需求分析阅读笔记——03
  5. 如何成为一名合格的数据科学家?这5个要点一定要掌握
  6. 大数据技术周报第 003 期
  7. Docker学习总结(47)——温故Docker常用命令行
  8. “飞天、无影、小蛮驴…”带你走进2020云栖大会
  9. 鹏华基金:科技赋能 打造公募基金的头部机构
  10. MSDN下载出现链接格式有误,如何解决
  11. 沪漂五年:我是如何从职场失意,走向皮实的人生?
  12. 谈谈认证中的黄埔军---信息安全认证(CISP与CISSP)
  13. 第73课内幕资料详细版 Spark SQL Thrift Server 实战 每天晚上20:00YY频道现场授课频道68917580
  14. 解决eclipse 错误: 找不到或无法加载主类
  15. Python学习记录—— 绘制百分比堆积柱状图
  16. [转]ubuntu常用软件
  17. 一步一步的手写Promise
  18. 免费获取全球生物量密度1km网格tif数据(GEDI L4B Gridded Aboveground Biomass Density, Version 2)
  19. 配置nginx.conf证书,实现http跳转htpps(80-->443)
  20. 计算机视觉顶会影响因子,最新SCI期刊影响因子出炉!

热门文章

  1. matplotlib 绘图 中文乱码 0.5
  2. nginx根据url中的参数进行转发
  3. 免费的B站短链生成器,将链接转成b23.tv
  4. Qt Mtd调用方式
  5. 新鲜的蔬菜和水果中富含抗氧化物,可以有效延缓衰老和疾病
  6. python 制作网站教程_Python爬取网站博客教程并制作成PDF
  7. Linux中使用交叉编译、二进制分析工具链
  8. Windows10+YOLOv5训练自己的数据集
  9. 英国G5、亚洲top1…未明学员斩获15枚世界top100学校offer!
  10. 基于互质阵的DOA估计