AttributeError: 'Tokenizer' object has no attribute 'oov_token' in Keras
AttributeError: 'Tokenizer' object has no attribute 'oov_token' in Keras
我正在尝试使用加载的分词器对我的文本进行编码,但出现以下错误
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)
tok.close()
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.
我正在尝试使用加载的分词器对我的文本进行编码,但出现以下错误
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)
tok.close()
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.