繁体   English   中英

如何在 Tensorflow-keras 中对 nlp 使用预测?

[英]How to use predict for nlp in Tensorflow-keras?

我在预测命名实体识别集时遇到了一些问题。 经过我的培训和测试,一切顺利。 现在我想测试像字符串这样的原始数据。

我试着用

model.predict(['Elon musk is good guy , he owns spacex, tesla.'])

但它抛出错误,

UnimplementedError:  Cast string to float is not supported
     [[node functional_29/Cast (defined at <ipython-input-210-e13dae4a124d>:1) ]] [Op:__inference_predict_function_223088]

Function call stack:
predict_function

我有 token2index 和

tag2index ,从训练集构建的字典。 我试图转换它并使用这些但预测显示全部为 0,

word = ['Elon musk is good guy , he owns spacex, tesla.']
word_index = [[token2idx[word] for word in word]]
X = pad_sequences(sequences=word_index, maxlen=7, padding='post')
predicted = np.argmax(model.predict(X), axis=-1) 
print(predicted)

给出 array([[0, 0, 0, 0, 0, 0, 0]]) 这是不正确的。 甚至尝试了 x_train[0] 的片段句子,但它会抛出这样的错误。 谢谢你的帮助。

我猜你想预测单词,对吧?

那么你应该分开你的话:

sentence = 'Elon musk is good guy , he owns spacex, tesla.'
word_index = [[token2idx[word] for word in sentence.split(' ')]]
X = pad_sequences(sequences=word_index, maxlen=7, padding='post')
predicted = np.argmax(model.predict(X), axis=-1) 
print(predicted)

更新

正如讨论所示,问题在于模型在学习过程中具有很高的准确性,但输出始终为零。

由于您的 y 类大小甚至没有分布,模型会学习到,改进一个类的预测将非常快速地提高准确性。 所以你的 y 数据是这样的: [0,0,0,0,0,0,0,1,0,0,0,0,0,3,0] 对于三个类: 0,1,3 ,模型可以快速学习以很好地预测零,因为这可以最大程度地提高准确性。

该模型学习预测 0,这已经给了它很高的准确性。 EG 当一个序列包含 20 个单词时,因此 20 个 y 值和 19 个为 0,模型将通过始终预测 0 达到95%的准确度。 因此,在这种情况下,高精度并不能衡量模型的质量,因为为了提高所有类别的模型性能,从 95% 跃升至 98% 确实比从 50% 跃升至 98% 对模型的提升更大95%。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM