英文:
ValueError: Shapes (None, 20, 9) and (None, 9) are incompatible
问题
这是我的代码:
import tensorflow as tf
from tensorflow import keras
from keras import layers
# 定义问题和答案
questions = [
"你好嗎?",
"今天天氣如何?",
"你有什麼興趣?",
"你喜歡什麼食物?"
]
answers = [
"我很好,謝謝你。",
"今天天氣非常好。",
"我喜歡看書和打電子遊戲。",
"我喜歡吃中式食物,特別是炒飯。"
]
# 将问题和回答转换成数字
tokenizer = keras.preprocessing.text.Tokenizer()
tokenizer.fit_on_texts(questions + answers)
question_seqs = tokenizer.texts_to_sequences(questions)
answer_seqs = tokenizer.texts_to_sequences(answers)
# 将问题和回答填充到相同的长度
max_len = 20
question_seqs_padded = keras.preprocessing.sequence.pad_sequences(question_seqs, maxlen=max_len)
answer_seqs_padded = keras.preprocessing.sequence.pad_sequences(answer_seqs, maxlen=max_len)
# 定义模型
model = keras.Sequential()
model.add(layers.Embedding(len(tokenizer.word_index) + 1, 50, input_length=max_len))
model.add(layers.LSTM(64))
model.add(layers.Dense(len(tokenizer.word_index) + 1, activation='softmax'))
# 编译模型
model.compile(loss='categorical_crossentropy', optimizer='adam')
# 训练模型
model.fit(question_seqs_padded, keras.utils.to_categorical(answer_seqs_padded, num_classes=len(tokenizer.word_index)+1), epochs=100, batch_size=32)
这是报错信息:
ValueError: Shapes (None, 20, 9) and (None, 9) are incompatible
我尝试修复这个问题:
model.fit(question_seqs_padded, keras.utils.to_categorical(answer_seqs_padded, num_classes=len(tokenizer.word_index)+1), epochs=100, batch_size=32)
我尝试删除 answer_seqs_padded
以解决不兼容的问题,但仍然不起作用。
英文:
This is my code
import tensorflow as tf
from tensorflow import keras
from keras import layers
# definition question and answer
questions = [
"你好嗎?",
"今天天氣如何?",
"你有什麼興趣?",
"你喜歡什麼食物?"
]
answers = [
"我很好,謝謝你。",
"今天天氣非常好。",
"我喜歡看書和打電子遊戲。",
"我喜歡吃中式食物,特別是炒飯。"
]
# 將問題和回答轉換成數字
tokenizer = keras.preprocessing.text.Tokenizer()
tokenizer.fit_on_texts(questions + answers)
question_seqs = tokenizer.texts_to_sequences(questions)
answer_seqs = tokenizer.texts_to_sequences(answers)
# 將問題和回答填充到相同的長度
max_len = 20
question_seqs_padded = keras.preprocessing.sequence.pad_sequences(question_seqs, maxlen=max_len)
answer_seqs_padded = keras.preprocessing.sequence.pad_sequences(answer_seqs, maxlen=max_len)
# 定義模型
model = keras.Sequential()
model.add(layers.Embedding(len(tokenizer.word_index) + 1, 50, input_length=max_len))
model.add(layers.LSTM(64))
model.add(layers.Dense(len(tokenizer.word_index) + 1, activation='softmax'))
# 編譯模型
model.compile(loss='categorical_crossentropy', optimizer='adam')
# 訓練模型
model.fit(question_seqs_padded, keras.utils.to_categorical(answer_seqs_padded, num_classes=len(tokenizer.word_index)+1), epochs=100, batch_size=32)
and this is it ran out of error
ValueError: Shapes (None, 20, 9) and (None, 9) are incompatible
I tryed to fix the Shapes (None, 20, 9) and (None, 9) are incompatible
model.fit(question_seqs_padded, keras.utils.to_categorical(answer_seqs_padded, num_classes=len(tokenizer.word_index)+1), epochs=100, batch_size=32)
I try to delete the answer_seqs_padded to incompatible (None, 9),but it is still not work.
答案1
得分: 1
在这种情况下,您需要在LSTM中返回整个序列,所以只需使用:
layers.LSTM(64, return_sequences=True)。如果不使用return_sequences=True,它将只返回最后的输出。
英文:
In this case, you need to return the whole sequence in lstm, so just use:
layers.LSTM(64, return_sequences=True) instead. If you don't use return_sequences=True, it will just return the last output.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论