我提前道歉,但我剛剛開始探索 NLP 文本生成器的世界。在對文本進行神經網路訓練后,我嘗試根據該模型和初始句子生成新文本。無論seed text我從哪個起始句 ( ) 開始,下一個自動生成的單詞都是and. 我不明白為什么以及如何解決這個問題。同樣,我對此很陌生,所以我將不勝感激任何幫助。
import tensorflow as tf
from tensorflow.keras.preprocessing.sequence import pad_sequences
from tensorflow.keras.layers import Embedding, LSTM, Dense, Bidirectional
from tensorflow.keras.preprocessing.text import Tokenizer
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import Adam
import numpy as np
import matplotlib.pyplot as plt
data=[' Hello, Chicago.If there is anyone out there who still doubts that America is a place where all things are possible,' ,
'who still wonders if the dream of our founders is alive in our time, who still questions the power of our democracy, tonight is your answer.',
'It’s the answer told by lines that stretched around schools and churches in numbers this nation has never seen,',
'by people who waited three hours and four hours, many for the first time in their lives, ',
'because they believed that this time must be different, that their voices could be that difference.',
'It’s the answer spoken by young and old, rich and poor, Democrat and Republican, black, white,',
'Hispanic, Asian, Native American, gay, straight, disabled and not disabled.',
'Americans who sent a message to the world that we have never been just a collection of individuals',
'or a collection of red states and blue states.',
'We are, and always will be, the United States of America.']
tokenizer = Tokenizer()
tokenizer.fit_on_texts(data)
total_words = len(tokenizer.word_index) 1
input_sequences = []
for line in data:
token_list = tokenizer.texts_to_sequences([line])[0]
for i in range(1, len(token_list)):
n_gram_sequence = token_list[:i 1]
input_sequences.append(n_gram_sequence)
# pad sequences
max_sequence_len = max([len(x) for x in input_sequences])
input_sequences = np.array(pad_sequences(input_sequences, maxlen=max_sequence_len, padding='pre'))
xs, labels = input_sequences[:,:-1],input_sequences[:,-1]
ys=(tf.keras.utils.to_categorical(labels, num_classes=total_words))
nr_epochs=50
model = Sequential()
model.add(Embedding(total_words, nr_epochs, input_length=max_sequence_len-1))
model.add(Bidirectional(LSTM(150)))
model.add(Dense(total_words, activation='softmax'))
adam = Adam(lr=0.01)
model.compile(loss='categorical_crossentropy', optimizer=adam, metrics=['accuracy'])
history = model.fit(xs, ys, epochs=nr_epochs, verbose=1)
seed_text = "If there is anyone out there who still doubts"
next_words = 100
for _ in range(next_words):
token_list1 = tokenizer.texts_to_sequences([seed_text])[0]
token_list = pad_sequences([token_list1], maxlen=max_sequence_len-1, padding='pre')
predicted = model.predict(token_list, verbose=0)
output_word = ""
for word, index in tokenizer.word_index.items():
if index == predicted.all():
output_word = word
break
seed_text = " " output_word
print(seed_text) # it prints the starting sentence plus 'and' 100 times
uj5u.com熱心網友回復:
我運行您的代碼并檢查了該程序。predicted.all()函式回傳 1 并且由于tokenizer.word_index.items()回傳包含 item 的 ditc ('and':1),因此您的代碼總是選擇單詞“and”,因為它的值是 1。
您可以嘗試將回傳陣列中最大值的索引更改predicted.all()為。np.argmax(predicted)從而回傳預測變數中得分最高的詞索引,意思是最可能的詞。
它仍然有 100 個單詞的重復,但這與模型的性能有關。只有 10 個單詞的預測看起來還不錯。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/479255.html
