模型是用形状 (None, 50) 构造的,用于输入 KerasTensor(type_spec=TensorSpec(shape=(None, 50),

Model was constructed with shape (None, 50) for input KerasTensor(type_spec=TensorSpec(shape=(None, 50),

我接受了某种培训,最终的 Jupyter notebook 是这样的:

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