在学习pytorch时,按照官方文档进行练习,刚输入了一行代码,就出现了问题:
import torch
# construct a 5 * 3 matrix, uninitialized
x = torch.empty(5, 3)
print(x)
报了个错,说torch模块没有empty属性。搜索引擎上搜两个网页
https://discuss.pytorch.org/t/torch-empty-doesnt-work/17147
看了一个叫Richard的大神回答,顺利定位出问题原因。
原来这本身没有什么问题,是print()函数的一个bug导致的。
This happens because torch.empty initializes your tensor with “un-initialized” data. Some of this data happened to have very, very large float values. The tensor printing code attempts to convert this number to an int (to see if it would be better printed as an integer), causing an overflow because there is a limit on how large a python int/long can be.
啥意思呢:
torch.empty初始化了一个张量的值,这些值是非常大的浮点数,print()函数就尝试把这些浮点数转换为int类型的值,转换时由于python的int/long类型长度限制,产生溢出报错。
在头上加上了这条代码,果然没有问题了:
from __future__ import print_function
👏👏👏再看看我们以前的文章😃😃😃
🌺 Excel中数据分析工具库-相关系数篇
🌺 干货,手把手教会你做相关性分析
🌺 5年数据分析路,小结。
🌺 用户细分及画像分析
🌺 K-近邻算法及实践
欢迎关注,微信公众号“数据分析师之家”
扫描二维码 关注我们
💁提供职业规划、简历指导、面试辅导服务哦
QQ交流群:254674155
数据分析之家联合JEE RAY品牌为粉丝派发福利
添加粉丝福利派发官,领取粉丝福利哦




3732

被折叠的 条评论
为什么被折叠?



