Reputation: 634
I try to create character-level machine learning translation using neural networks. I did pre-processing of the text. input_one_hot_encoded_list
contains input one-hot encoded sentences and output_one_hot_encoded_list
contains one-hot encoded sentences in another language which I want to achieve. In this example my dictionary has 55 characters and I have 100 sentences so both arrays consist of 100 lists which contains 50 lists (the longest sentence has 50 characters) which contains 55 integers (one hot encoding, each list contains fifty four 0 and one 1). Do you have any ideas why it is not working? Error is shown at the bottom.
print('shape of input_one_hot_encoded_list: ' + str(array(input_one_hot_encoded_list).shape))
print('shape of output_one_hot_encoded_list: ' + str(array(output_one_hot_encoded_list).shape))
shape = array(input_one_hot_encoded_list).shape
model = Sequential()
model.add(LSTM(len(dict), return_sequences=True, stateful=True,
batch_input_shape=shape))
model.add(LSTM(len(dict), return_sequences=True, stateful=True))
model.add(LSTM(len(dict), return_sequences=True, stateful=True))
model.add(Dense(len(dict), activation='softmax'))
model.compile(loss='categorical_crossentropy',
optimizer='rmsprop',
metrics=['accuracy'])
print(model.summary())
model.fit(input_one_hot_encoded_list, output_one_hot_encoded_list, epochs=20)
The output of running the code above:
shape of input_one_hot_encoded_list: (100, 50, 55)
shape of output_one_hot_encoded_list: (100, 50, 55)
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
lstm_1 (LSTM) (100, 50, 55) 24420
_________________________________________________________________
lstm_2 (LSTM) (100, 50, 55) 24420
_________________________________________________________________
lstm_3 (LSTM) (100, 50, 55) 24420
_________________________________________________________________
dense_1 (Dense) (100, 50, 55) 3080
=================================================================
Total params: 76,340
Trainable params: 76,340
Non-trainable params: 0
_________________________________________________________________
None
Traceback (most recent call last):
File "data_preparation.py", line 175, in <module>
model.fit(input_one_hot_encoded_list, output_one_hot_encoded_list, epochs=20)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 952, in fit
batch_size=batch_size)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training.py", line 751, in _standardize_user_data
exception_prefix='input')
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/training_utils.py", line 102, in standardize_input_data
str(len(data)) + ' arrays: ' + str(data)[:200] + '...')
ValueError: Error when checking model input: the list of Numpy arrays that you are passing to your model is not the size the model expected. Expected to see 1 array(s), but instead got the following list of 100 arrays: [array([[1, 0, 0, ..., 0, 0, 0],
[0, 1, 0, ..., 0, 0, 0],
[0, 0, 1, ..., 0, 0, 0],
...,
[1, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0, 0],
[1, 0, 0, ..., 0, 0,...
Upvotes: 0
Views: 1008
Reputation: 33460
Instead of passing the input and output as a list of numpy arrays, which makes Keras think that you have multilple input and output layers, pass them as one single numpy array:
import numpy as np
input_one_hot_encoded = np.array(input_one_hot_encoded_list)
output_one_hot_encoded = np.array(output_one_hot_encoded_list)
model.fit(input_one_hot_encoded, output_one_hot_encoded, epochs=20)
Upvotes: 1