Issue
I am very new to Keras and to machine learning in general, but here is what I want. I have a list of inputs (1 value for every input node) and a list of targets (1 value for every output node).
input_list = [1, 0, 1, 0, 1, 0] # maybe longer
wanted_output_list = [1, 0, 0, 0] # also maybe longer
And now I want to give these as input to train my neural network:
# create model
model = Sequential()
# get number of columns in training data
n_cols = 6
# add model layers
model.add(Dense(6, activation='relu', input_shape= (n_cols,)))
model.add(Dense(2, activation='relu'))
model.add(Dense(2, activation='relu'))
model.add(Dense(3))
# compile the model
model.compile(optimizer='adam', loss='mean_squared_error')
# train model
model.fit(input_list, wanted_output_list, validation_split=0.2, epochs=30)
However I get this error:
ValueError: Error when checking input: dense_1_input to have shape (6,) but got with shape (1,)
Can anyone please tell me why and how I can fix this?
Solution
When defining your model, you specified a model that accepts an input with 6 features, and output a vector with 3 component. You training data, however, is not shaped correctly (nor your labels, by the way). You should shape your data the way you have defined your model. In this case, that means that each sample of your training data is a vector with 6 components, and each label is a vector with 3 components.
Keras expects a list of numpy array
(or a 2D array) when training a model with multiple inputs, see the documentation.
x : Input data. It could be:
- A Numpy array (or array-like), or a list of arrays (in case the model has multiple inputs).
So per your model definition, you could shape your training data the following way :
import numpy as np
# your data, in this case 2 datapoints
X = np.array([[1, 0, 1, 0, 1, 0], [0, 1, 0, 1, 0, 1]])
# the corresponding labels
y = np.array([[1, 0, 0], [0, 1, 0]])
And then train your model by calling fit
.
model.fit(x, y, epochs=30)
Answered By - Lescurel
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.