在带有 Tensorflow 的 Jupyter notebook 中用什么代替 predict_classes()? (NLP 文本生成)

What to use in place of predict_classes() in a Jupyter notebook with Tensorflow? (NLP Text Generation)

我正在尝试按照 tensorflow NLP 教程来训练神经网络,以使用我自己编译的源代码生成 poetry/lyric-like 输出。我只会基本的python,所以这绝对是远远超出我的能力水平。教程似乎有点过时了,因为我收到了这个错误代码:

AttributeError: 'Sequential' object has no attribute 'predict_classes'

我了解属性 'predict_classes' 已弃用,在当前版本的 tensorflow 中不再使用。

这是答案中建议的一行代码,但我不明白如何将其包含到我的代码中:

= np.argmax(model.predict(x_test), axis=-1)

如有任何帮助,我们将不胜感激。这是给我带来麻烦的代码部分以及错误代码。 我也在完整的 Jupyter 笔记本中加入了 link。

seed_text = "Vernal sunlight"
next_words = 100
  
for _ in range(next_words):
    token_list = tokenizer.texts_to_sequences([seed_text])[0]
    token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
    predicted = model.predict_classes(token_list, verbose=0)
    output_word = ""
    for word, index in tokenizer.word_index.items():
        if index == predicted:
            output_word = word
            break
    seed_text += " " + output_word
print(seed_text)
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-16-0539a42e927b> in <module>()
      5         token_list = tokenizer.texts_to_sequences([seed_text])[0]
      6         token_list = pad_sequences([token_list], maxlen=max_sequence_len-1, padding='pre')
----> 7         predicted = model.predict_classes(token_list, verbose=0)
      8         output_word = ""
      9         for word, index in tokenizer.word_index.items():

AttributeError: 'Sequential' object has no attribute 'predict_classes'

Here is the link to the video I was following!

And here is the copy of the Jupyter notebook!

你可以通过使用argmax以预测的张量作为参数来找到预测的class。定义预测如下:

predicted = np.argmax(model.predict(x), axis=-1)