[英]TypeError: file must have 'read' and 'readline' attributes run in Python3
I have searched about this type of errors, tried to fix as much as I can but still, this error hasn't gone away我已经搜索过这种类型的错误,尝试尽可能多地修复,但是这个错误并没有消失
Traceback (most recent call last):
File "rbm.py", line 419, in <module>
learn_letters()
File "rbm.py", line 350, in learn_letters
import rbm
File "/Users/thienhua/Desktop/usingnow/coding/sachml/Ch17/rbm.py", line 423, in <module>
test_rbm()
File "/Users/thienhua/Desktop/usingnow/coding/sachml/Ch17/rbm.py", line 259, in test_rbm
train_set = pickle.load(file)
TypeError: file must have 'read' and 'readline' attributes
this is the line code where I got stuck:这是我卡住的行代码:
def test_rbm():
import rbm
import pickle
# Load the dataset
with open('mnist.pkl', 'rb') as f:
file = pickle._Unpickler(f)
file.encoding = 'latin1'
train_set = pickle.load(file)
valid_set = pickle.load(file)
test_set = pickle.load(file)
#train_set, valid_set, test_set = pickle.load(file)
file.close()
r = rbm.rbm(28*28,100,inputs = train_set[0][:100,:],momentum=0.4,nCDsteps=3,nepochs=5000)
r.contrastive_divergence(train_set[0][:100,:])
Im using python 3.8 at the moment.我目前使用 python 3.8。 Thank you so much for your help.
非常感谢你的帮助。
The issue is that pickle.load
accepts file-like objects, but pickle._Unpickler()
doesn't return that kind of object (it returns instance of class _Unpickler
, which doesn't have 'read' and 'readline' attrs).问题是
pickle.load
接受类似文件的对象,但pickle._Unpickler()
不返回那种 object (它返回class _Unpickler
的实例,它没有“读取”和“读取线”属性)。 If you have pickled python object into some file, you have to load it back through pickle.load('/path/to/pickle_file')
.如果您已将 python object 腌制到某个文件中,则必须通过
pickle.load('/path/to/pickle_file')
将其加载回来。 More info on https://docs.python.org/3/library/pickle.html有关https://docs.python.org/3/library/pickle.html的更多信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.