gpt4 book ai didi

python - 类型错误 : can't pickle generator objects

转载 作者:太空狗 更新时间:2023-10-29 18:05:57 26 4
gpt4 key购买 nike

我正在尝试将一些结果写入 pickle 文件,如下所示:

raw_X = (self.token_ques(text) for text in training_data)
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(raw_X, handle)

错误:

    raise TypeError, "can't pickle %s objects" % base.__name__
TypeError: can't pickle generator objects

任何帮助将不胜感激。

最佳答案

当您想要 pickle 数据时,不要使用生成器表达式。请改用列表理解,或在生成器上调用 list() 以捕获所有生成的元素以进行 pickle 。

例如,以下工作正常:

raw_X = [self.token_ques(text) for text in training_data]
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(raw_X, handle)

同样:

raw_X = (self.token_ques(text) for text in training_data)
with open('/root/Desktop/classifier_result.pkl', 'wb') as handle:
pickle.dump(list(raw_X), handle)

关于python - 类型错误 : can't pickle generator objects,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28963354/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com