python joblib.load:迭代生成器导致无限循环
python joblib.load: Iterating over generator results in infinite loop
下面是我用于生成器的代码。它是从 Saving and loading multiple objects in pickle file? 修改而来的。该文件是使用 joblib.dump 创建的,并且(到目前为止)由单个机器学习模型组成。
import pickle
from sklearn.externals import joblib
filename = 'ML_models.pkl'
def load(filename):
with open(filename, "rb") as f:
while True:
try:
yield joblib.load(f)
except EOFError:
break
接下来我测试生成器如下:
models = load(filename)
for model in models:
print model
不幸的是,这会重复打印模型,直到我中断内核。
发出 EOF 信号的正确方法是什么?
根据documentation,joblib.load
returns完整对象。
def load(filename):
with open(filename, "rb") as f:
return joblib.load(f)
下面是我用于生成器的代码。它是从 Saving and loading multiple objects in pickle file? 修改而来的。该文件是使用 joblib.dump 创建的,并且(到目前为止)由单个机器学习模型组成。
import pickle
from sklearn.externals import joblib
filename = 'ML_models.pkl'
def load(filename):
with open(filename, "rb") as f:
while True:
try:
yield joblib.load(f)
except EOFError:
break
接下来我测试生成器如下:
models = load(filename)
for model in models:
print model
不幸的是,这会重复打印模型,直到我中断内核。 发出 EOF 信号的正确方法是什么?
根据documentation,joblib.load
returns完整对象。
def load(filename):
with open(filename, "rb") as f:
return joblib.load(f)