python-AttributeError:“令牌生成器”对象在Keras中没有属性“ oov_token”


AttributeError: 'Tokenizer' object has no attribute 'oov_token'


from keras.preprocessing.text import Tokenizer
from keras.preprocessing import sequence
from keras.models import Model, Input, Sequential, load_model
import pickle
import h5py

maxlen = 100
tok = open('tokenizer.pickle', 'rb')
tokenizer = pickle.load(tok)
model = load_model('weights.h5')

def predict():
    new_text = sequence.pad_sequences((tokenizer.texts_to_sequences(['heyyyy'])), maxlen=maxlen)
    prediction = model.predict(new_text,batch_size=1,verbose=2)

该问题发生在行tokenizer.texts_to_sequences([‘heyyyy’])上,我不确定为什么.泡菜有问题吗? tokenizer.texts_to_sequences与’hey’,’heyy’和’heyyy’一起使用.


这很可能是this issue

You can manually set tokenizer.oov_token = None to fix this.

Pickle is not a reliable way to serialize objects since it assumes
that the underlying Python code/modules you’re importing have not
changed. In general, DO NOT use pickled objects with a different
version of the library than what was used at pickling time. That’s not
a Keras issue, it’s a generic Python/Pickle issue. In this case
there’s a simple fix (set the attribute) but in many cases there will
not be.

转载注明原文:python-AttributeError:“令牌生成器”对象在Keras中没有属性“ oov_token” - 代码日志