简体   繁体   English

Keras 中嵌入层的输入形状出现问题

[英]Trouble with input shape of Embedding layer in Keras

I am new at modeling in Keras Python. I wanted to create a model that will generate text answer to my messages like chatbot.我是 Keras Python 中的建模新手。我想创建一个 model,它将像聊天机器人一样生成对我的消息的文本答复。 I read that I need to use tokenizer and to_categorical method.我读到我需要使用 tokenizer 和 to_categorical 方法。 There is my code:有我的代码:

import numpy as np
from keras.layers import Dense, LSTM, Input, Dropout, Embedding
from keras.models import Sequential
from keras.optimizers import Adam
from keras.preprocessing.text import Tokenizer, text_to_word_sequence
from keras.utils import pad_sequences, to_categorical


X = ["Hello!", "Greetings!"]

tokenizer = Tokenizer(1000)

X_seq = pad_sequences(tokenizer.texts_to_sequences(X), 20)

model = Sequential()

model.add(Embedding(1000, 100, input_length=20))
model.add(LSTM(100))
model.add(Dense(1000, "softmax"))

model.summary()

model.compile(loss='categorical_crossentropy', metrics=['accuracy'])

history = model.fit(X_seq, to_categorical(X_seq), epochs=10, batch_size=20)

print(model.predict(pad_sequences(tokenizer.texts_to_sequences(["Greetings!"]), 20)))

value 1000 is max count of unique words in tokenizer vocabulary X is my input list of messages.1000是分词器词汇表中唯一单词的最大数量X是我的消息输入列表。 20 is max length of text. 20是文本的最大长度。 I wanted to create self-learning model but I don't really inderstand how to do it.我想创建自学 model 但我真的不知道该怎么做。 In inte.net I found that I need to pass same input and output values but my model should return something like that [0.1,0.2,0.3....] - the max value represents the word which model predicted.在 inte.net 中,我发现我需要传递相同的输入和 output 值,但我的 model 应该返回类似 [0.1,0.2,0.3....] 的内容 - 最大值表示 model 预测的单词。 But when I try to fit that it raises exception:但是当我尝试适应它时会引发异常:

ValueError: Shapes (None, 20) and (None, 1000) are incompatible

I guess that it is a trouble with shapes of input list and embedding layer.我猜这是输入列表和嵌入层形状的问题。 Guys please help me deal with that.伙计们请帮我处理一下。 Thank you in advance!先感谢您!

In this particular example using to_categorical(X_seq) gives you targets having [num_of_samples, 20] shape.在此特定示例中,使用to_categorical(X_seq)为您提供具有[num_of_samples, 20]形状的目标。 It means that if you use this target variable to fit model it will expect that number of classes will equal 20 (model output shape = (None, 20) ).这意味着,如果您使用此目标变量来拟合 model,则预计类数将等于 20(模型 output 形状 = (None, 20) )。 So to fix your example I would suggest you change model.add(Dense(1000, "softmax")) to model.add(Dense(20, "softmax")) .因此,为了修复您的示例,我建议您将model.add(Dense(1000, "softmax"))更改为model.add(Dense(20, "softmax"))

Additionaly before using tokenizer.texts_to_sequences(X) I would suggest you to fit tokenizer using tokenizer.fit_on_texts(X) .另外,在使用tokenizer.texts_to_sequences(X)之前,我建议您使用tokenizer.fit_on_texts(X)来安装 tokenizer。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM