2

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?

2
  • 2
    What are you trying to predict? Dense layer with a one neuron should not have softmax as activation. Also your loss is not set correctly. Commented Jan 13, 2021 at 16:18
  • the actual code has data from a temp. sensor from breathing for an hour and I'm trying to predict if he/she have health problems. I'm a beginner to tensorflow. Commented Jan 13, 2021 at 16:39

3 Answers 3

5

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)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! It now runs the code without any problems!
2

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

Comments

1

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"), 

3 Comments

I'm sorry but it didn't seem to have solved the issue.
@Ahmadfromjameedium see the edited answer
It runs for a little bit and then returns this error: ValueError: Layer sequential_38 expects 1 inputs, but it received 2 input tensors.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.