用于输入kerastensor(type_spec = tensorsPec(shape =(none,50),

发布于 2025-01-23 01:56:54 字数 1331 浏览 0 评论 0 原文

我已经接受了某种培训,最后的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,).

我在这里缺少什么?

I have followed some kind of training and the final Jupyter notebook was this:

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

I understand the entire code, and how the model was trained.

However, at the end I am predicting emotions for tweets in the test dataset like this:

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)

This works perfectly fine.

However I want to test the model prediction with random sentences, like:

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)

But I got this error:

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,).

What am I missing here?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

谎言月老 2025-01-30 01:56:54

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

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

Your model needs an integer sequence, not a raw string. Try converting the sentence to its corresponding integer sequence first:

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
明月松间行 2025-01-30 01:56:54

只是添加一点:

shape

  • np.expand_dims(句子).shape is (1,),而不是(无,50 )
  • 对于 batch 大小,应该将其扩展一个维度。

序列

  • 输入您的模型是一个填充的数字序列,由令牌器转换。
  • 它应该长50。

Just to add a little:

Shape

  • np.expand_dims(sentence).shape is (1,), not (None, 50).
  • it should be expanded one more dimension for batch size.

Sequences

  • Input of your model is a padded sequence of numbers, transformed by a tokenizer.
  • it should be 50 in length.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文