I am a beginner in machine learning and I am trying to train a model with nltk and tensorflow. But I get the following error when I run my program. I understand the problem. it seems that the shape of my list does not pass but I do not know why and I do not find any relief. I specify that I use a list of lists with different sizes. Need help please I need to understand, solve and move forward code and error:
I am trying to train a model with nltk and tensorflow. But it seems that the shape of my list does not pass but I do not know why and I do not find any relief. I specify that I use a list of lists with different sizes.
github code: https://github.com/maeltoukap/whatsapp-chat-bot
First of all, you are missing two reshape steps. You need to add the lines
train_x = np.expand_dims(train_x, axis=1)
train_y = np.expand_dims(train_y, axis=1)
after you define train_x
and train_y
(so after line 67 in your picture). Your input shape is then the shape of your first training example, so change input_shape: train_x[0]
to input_shape: train_x[0].shape
. Also change the number of neurons in your last dense layer. Currently you have in your last layer Dense(len(train_y[0])...
. You need to change that to Dense(30, ...)
. Then you should be good.
The complete code would look like this:
random.shuffle(training)
training = np.array(training, dtype=object)
train_x = list(training[:, 0])
train_y = list(training[:, 1])
train_x = np.expand_dims(train_x, axis=1)
train_y = np.expand_dims(train_y, axis=1)
print(len(train_x))
model = Sequential()
# model.add(Dense(128, input_shape=113, activation='relu'))
model.add(Dense(128, input_shape=train_x[0].shape, activation='relu'))
# model.add(Dense(128, input_shape=(len(train_x[0])), activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(54, activation='relu'))
model.add(Dropout(0.5))
# model.add(Dense(113, activation='softmax'))
model.add(Dense(30, activation='softmax'))
sgd = SGD(learning_rate=0.01, momentum=0.9, nesterov=True)
model.compile(optimizer=sgd, loss='categorical_crossentropy', metrics=['accuracy'])
hist = model.fit(train_x, train_y, epochs=200, batch_size=5, verbose=1)