当前位置:网站首页>Solution: runtimeerror: expected object of scalar type int but got scalar type double
Solution: runtimeerror: expected object of scalar type int but got scalar type double
2022-07-26 04:15:00 【Huai's blog】
Problem description :
Recently, I am writing code using neural network prediction , After replacing the imported data , The original loss function ( As shown below ) Report errors RuntimeError: Expected object of scalar type Int but got scalar type Double. Later according to CSDN Many blog posts on try to use many methods , for example :loss Function is preceded by X=torch.tensor(np.int(X)) perhaps y=torch.IntTensor(y), But then again “ValueError: only one element tensors can be converted to Python scalar Waiting for a wrong report , So according to other blog posts, it is changed to X= torch.tensor([item.cpu().detach().numpy() for item in np.int(X)]).cuda(), So I went back to the above error report , It's a headache .
for X,y in data_iter(batch_size,train_feature,train_label):# Internal circulation training one batch
l=loss(X,y).sum() # Calculate the gap between the output of the model and the real data
Solution :
In fact, solving this problem is very simple , Because the suffix of the imported table is xlsx( namely : natural Excel file ), Just save the file as csv The format is just ( It should be noted that , The number of reads must be utf-8, Otherwise, when reading, it will be due to csv The encoding format in is ‘ANSI’, and Python 3 The default read format is ‘utf-8’, Lead to decode abnormal . Therefore, an error will be reported ’utf-8’ codec can’t decode bytes in position 16: invalid continuation byte).
After adding the suffix from xlsx Change it to csv after , Read the file smoothly and train , There is no other error report in the middle (ヾ(◍°∇°◍)ノ゙ Kaisensen ~ Kaisensen ~)
Reason for modification :
1、CSV It's a plain text file ,excel It's not plain text ,excel Contains a lot of format information in it . namely :CSV It's a plain text file ,CSV The file in windows The default opening mode of the platform is excel, But its essence is a text file .excel It's not plain text ,Excel It's a binary file , It saves information about all worksheets in the workbook . therefore ,csv The document is more pure .
Other reasons :
1、CSV The file size will be smaller , Creating a distribution is easier to read , Suitable for storing structured information , For example, export of records , Traffic statistics, etc .
2、 Because there is no clear distinction or distinction between numerical value and text ,Excel You can use the automatic formatting function to confuse the values .
CSV It's safer , Value and text can be clearly distinguished .CSV Do not process data and store as is .
3、Excel More memory is consumed when importing data . Import CSV Files can be faster , And consume less memory .
4、Excel Files can only be used Microsoft Excel documents opening .CSV It can be used Windows Open any text editor in , Like Notepad ,MS Excel,Microsoft Works 9 etc. .
reference :
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
边栏推荐
- 支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布
- 匿名函数的作用
- 荐书|《DBT技巧训练手册》:宝贝,你就是你活着的原因
- Operator new, operator delete supplementary handouts
- Acwing第 61 场周赛【完结】
- The PHP Eval () function can run a string as PHP code
- 智装时代已来,智哪儿邀您一同羊城论剑,8月4日,光亚展恭候
- [digital ic/fpga] Hot unique code detection
- 荐书丨《教育心理学》:送给明日教师的一本书~
- Huawei issued another global convening order of "genius youth", and some people once gave up their annual salary of 3.6 million to join
猜你喜欢

软模拟光栅化渲染器

吴恩达机器学习课后习题——线性回归

Seat / safety configuration upgrade is the administrative experience of the new Volvo S90 in place

Which websites can I visit to check the latest medical literature?

支持代理直连Oracle数据库,JumpServer堡垒机v2.24.0发布

The era of smart clothes has come. Zhinar invites you to discuss swords in Yangcheng. Guangya Exhibition is waiting for you on August 4

2021 CIKM |GF-VAE: A Flow-based Variational Autoencoder for Molecule Generation
![[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](/img/e8/b115b85e2e0547545e85b2058a9bb0.png)
[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

This article takes you to graph transformers

p-范数(2-范数 即 欧几里得范数)
随机推荐
Pits encountered by sdl2 OpenGL
Luoda Development -- the context of sidetone configuration
Method of test case design: introduction, trial recruit, preliminary exploration of equivalence boundary
I.MX6U-ALPHA开发板(GPIO中断实验)
构建关系抽取的动词源
Educational Codeforces Round 132 (Rated for Div. 2) E. XOR Tree
Acwing刷题
This article takes you to graph transformers
[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
dijango学习
Wechat applet to realize music player (4) (use pubsubjs to realize inter page communication)
生活相关——减少期待,更快乐
Huawei issued another global convening order of "genius youth", and some people once gave up their annual salary of 3.6 million to join
What are the duplicate check rules for English papers?
Web测试方法大全
LeetCode. 6115 count the number of ideal arrays
How to download the supplementary literature?
Which websites can I visit to check the latest medical literature?
Leetcode:1184. Distance between bus stops -- simple
Constructing verb sources for relation extraction