Reputation: 35
I have a training dataset like this (the number of items in the main list is 211 and the number of numbers in every array is 185):
[np.array([2, 3, 4, ... 5, 4, 6]) ... np.array([3, 4, 5, ... 3, 4, 5])]
and I use this code to train the model:
def create_model():
model = keras.Sequential([
keras.layers.Flatten(input_shape=(211, 185), name="Input"),
keras.layers.Dense(211, activation='relu', name="Hidden_Layer_1"),
keras.layers.Dense(185, activation='relu', name="Hidden_Layer_2"),
keras.layers.Dense(1, activation='softmax', name="Output"),
])
model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
return model
but whenever I fit it like this:
model.fit(x=training_data, y=training_labels, epochs=10, validation_data = [training_data,training_labels])
it returns this error:
ValueError: Layer sequential expects 1 inputs, but it received 211 input tensors.
What could possibly be the problem?
Upvotes: 2
Views: 3622
Reputation: 51
For me it was a silly mistake , i was taking the input in list rather than numpy.ndarray
1.check the type of data format your X_train is in:
type(X_train)
2.If you get output as list or any other format just convert it into numpy.ndarray
X_train = numpy.array(X_train)
Hope this helps Thank you
Upvotes: 2
Reputation: 36604
You don't need to flatten your input. If you have 211 samples of shape (185,)
, this already represents flattened input.
But your initial error is that you can't pass a list of NumPy arrays as input. It needs to be lists of lists or a NumPy array. Try this:
x = np.stack([i.tolist() for i in x])
Then, you made other mistakes. You can't have an output of 1 neuron with a SoftMax activation. It will just output 1, so use "sigmoid"
. This is also the wrong loss function. If you have two categories, you should use "binary_crossentropy"
.
Here is a working example of fixing your mistakes, starting from your invalid input:
import tensorflow as tf
import numpy as np
x = [np.random.randint(0, 10, 185) for i in range(211)]
x = np.stack([i.tolist() for i in x])
y = np.random.randint(0, 2, 211)
model = tf.keras.Sequential([
tf.keras.layers.Dense(21, activation='relu', name="Hidden_Layer_1"),
tf.keras.layers.Dense(18, activation='relu', name="Hidden_Layer_2"),
tf.keras.layers.Dense(1, activation='sigmoid', name="Output"),
])
model.compile(optimizer='adam',
loss='binary_crossentropy',
metrics=['accuracy'])
history = model.fit(x=x, y=y, epochs=10)
Upvotes: 5
Reputation: 6367
You have two errors:
You can not feed a list of arrays. Convert you input to array:
input = np.asarray(input)
You declared input shape of (211, 185). Keras automatically adds batch dimension. So change the shape to (185,):
keras.layers.Flatten(input_shape=(185,), name="Input"),
Upvotes: 1