为输入KerasTensor(type_spec=TensorSpec(shape=(None, 50)),构造形状(N



我遵循了某种训练,最终的Jupyter笔记本是这样的:

https://colab.research.google.com/drive/1Lmh1b5Ge9NodxIrukCTJC3cpYQDn9VuM?usp=sharing

我理解整个代码,以及模型是如何训练的。

然而,最后我在测试数据集中预测推文的情绪,像这样:

i = random.randint(0, len(test_labels)-1)
print('Sentence:', test_tweets[i])
print('Emotion:', index_to_class[test_labels[i]])
p = model.predict(np.expand_dims(test_seq[i], axis=0))[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)

这个工作很好。

然而,我想用随机句子来测试模型预测,比如:

sentence = 'I love you more than ever'
print('Sentence:',  sentence)
#print('Emotion:', index_to_class[test_labels[i]])
p = model.predict(np.expand_dims(sentence, axis=0))[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)

但是我得到了这个错误:

Sentence: I love you more than ever
WARNING:tensorflow:Model was constructed with shape (None, 50) for input KerasTensor(type_spec=TensorSpec(shape=(None, 50), dtype=tf.float32, name='embedding_input'), name='embedding_input', description="created by layer 'embedding_input'"), but it was called on an input with incompatible shape (None,).

我在这里错过了什么?

您的模型需要一个整数序列,而不是一个原始字符串。尝试先将句子转换为相应的整数序列:

sentence = 'I love you more than ever'
print('Sentence:',  sentence)
#print('Emotion:', index_to_class[test_labels[i]])
sentence = get_sequences(tokenizer, np.expand_dims(sentence, axis=0))
p = model.predict(sentence)[0]
pred_class = index_to_class[np.argmax(p).astype('uint8')]
print('Predicted Emotion:', pred_class)
Sentence: I love you more than ever
Predicted Emotion: joy

再添加一点:

  • np.expand_dims(sentence).shape(1,),不是(None, 50)
  • 应该为batch尺寸扩展一个维度。

序列

  • Input是一个填充的数字序列,由标记器转换。
  • 长度应该是50。

相关内容

  • 没有找到相关文章

最新更新