为什么这段代码不管用?我尝试了所有方法,但都没有有效
import tensorflow as tf
from keras.preprocessing.sequence import Tokenizer
from keras.utils import pad_sequences
# Set parameters
vocab_size = 5000
embedding_dim = 64
max_length = 100
trunc_type='post'
padding_type='post'
oov_tok = "<OOV>"
training_size = len(processed_data)
# Create tokenizer
tokenizer = Tokenizer(num_words=vocab_size, oov_token=oov_tok)
tokenizer.fit_on_sequences(processed_data)
word_index = tokenizer.word_index
# Create sequences
sequences = tokenizer.texts_to_sequences(processed_data)
padded_sequences = pad_sequences(sequences, maxlen=max_length, padding=padding_type, truncating=trunc_type)
# Create training data
training_data = padded_sequences[:training_size]
training_labels = padded_sequences[:training_size]
# Build model
model = tf.keras.Sequential([
tf.keras.layers.Embedding(vocab_size, embedding_dim, input_length=max_length),
tf.keras.layers.Dropout(0.2),
tf.keras.layers.Conv1D(64, 5, activation='relu'),
tf.keras.layers.MaxPooling1D(pool_size=4),
tf.keras.layers.LSTM(64),
tf.keras.layers.Dense(64, activation='relu'),
tf.keras.layers.Dense(vocab_size, activation='softmax')
])
# Compile model
model.compile(loss='sparse_categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train model
num_epochs = 50
history = model.fit(training_data, training_labels, epochs=num_epochs, verbose=2)
def predict_answer(model, tokenizer, question):
# Preprocess question
question = preprocess(question)
# Convert question to sequence
sequence = tokenizer.texts_to_sequences([question])
# Pad sequence
padded_sequence = pad_sequences(sequence, maxlen=max_length, padding=padding_type, truncating=trunc_type)
# Predict answer
pred = model.predict(padded_sequence)[0]
# Get index of highest probability
idx = np.argmax(pred)
# Get answer
answer = tokenizer.index_word[idx]
return answer
# Start chatbot
while True:
question = input('You: ')
answer = predict_answer(model, tokenizer, question)
print('Chatbot:', answer)
在终端里有一个可以发送聊天信息的地方。当我输入我的消息时,它却出现了一个错误:
Traceback (most recent call last):
File "<stdin>", line 3, in <module>
NameError: name 'model' is not defined
0 个回答
暂无回答