我目前正在从 tensorflow 系列学习机器学习。在这种情况下是关于文本分类的。正如您在代码中看到的,我已经训练了模型并将其保存为文件以进行测试。
加载保存的文件
modelFile = keras.models.load_model('model_text_classification.h5')
编码功能:
def review_encode(string):
'''look up the mapping of all the words and return to us an encoded list'''
encoded = [1] # start with 1 as a starting tag as the system with word_index['<START>'] = 1
for word in string:
if word in word_index:
encoded.append(word_index[word.lower()])
else:
encoded.append(2) # as the END tag
return encoded
预处理:
with open('lion_king.txt', encoding = 'utf-8') as f:
for line in f.readlines():
nline = line.replace(',', '').replace('.', '').replace('(', '').replace(')', '').replace('\"', '').replace(':', '')
nline = nline.split(' ')
# encode and trim the data down to 256 words
encode = review_encode(nline)
encode = keras.preprocessing.sequence.pad_sequences([encode], value = word_index['<PAD>'], padding = 'post', maxlen = 256) # [encode], because is expecting a list of lists
# using the model to make a prediction
predict = model.predict_classes(encode)
print(line)
print(encode)
print(predict(encode[0])) #HERE IS ERROR
预期输出:将预测打印为 96% 正。
示例: [0.9655667]
完整的追溯:
TypeError Traceback (most recent call last)
<ipython-input-58-790c338a89ce> in <module>()
13 print(line)
14 print(encode)
---> 15 print(predict(encode[0]))
TypeError: 'numpy.ndarray' object is not callable
您已经使用predict = model.predict_classes(encode)
which 覆盖了 predict 的任何函数定义并用数组替换。
所以 Predict 是一个不可调用的数组。如果您只是为了查看预测的类,encode[0]
可以使用:print(predict[0])
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句