用于输入kerastensor(type_spec = tensorsPec(shape =(none,50),
我已经接受了某种培训,最后的jupyter笔记本是:
我理解整个代码,以及如何培训模型。
但是,最后我预测了这样的测试数据集中推文的情绪:
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,).
我在这里缺少什么?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的模型需要整数序列,而不是原始字符串。尝试首先将句子转换为相应的整数序列:
Your model needs an integer sequence, not a raw string. Try converting the sentence to its corresponding integer sequence first:
只是添加一点:
shape
np.expand_dims(句子).shape
is(1,)
,而不是(无,50 )
。batch
大小,应该将其扩展一个维度。序列
输入
您的模型是一个填充的数字序列,由令牌器转换。Just to add a little:
Shape
np.expand_dims(sentence).shape
is(1,)
, not(None, 50)
.batch
size.Sequences
Input
of your model is a padded sequence of numbers, transformed by a tokenizer.