Skip to content
Advertisement

Error related to the number of input tensors in Keras

I am inputting series of float32 grayscale images as a list with 16*16 shape to python and try do a regression task with labels inputted from Pandas data frame.

Here is the shape of images and df:

print(np.shape(images))
(2000, 16, 16)

print(np.shape(df))
(2000, 1)

I used train_test_split from sklearn to split the data to train and test:

print (np.shape(trainY),np.shape(testY),np.shape(trainX),np.shape(testX))
(1700, 1) (300, 1) (1700, 16, 16) (300, 16, 16)

I am using the following model for doing the prediction, but model.fit returns error and does not run the training.

model = models.Sequential()
model.add(layers.Dense(512, activation='relu', input_shape=(16 * 16 * 1,)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['mae'])

history = model.fit(trainX, trainY, epochs=50, validation_split=.2, batch_size=128,verbose=1)

ValueError: Layer sequential_18 expects 1 input(s), but it received 1700 input tensors

I also tested trainX = np.expand_dims(trainX, -1) before model.fit but it still gives another error. Can anyone help me to solve this?

ValueError: Input 0 of layer sequential_18 is incompatible with the layer: expected axis -1 of input shape to have value 256 but received input with shape (None, 16, 16, 1)

Advertisement

Answer

Your next layers are simply Dense, so adding a Flatten layer on the top of your network does the job (no need to additional manipulate the input images)

trainX = np.random.uniform(0,1, (1700, 16, 16))
trainY = np.random.uniform(0,1, (1700, 1))

model = models.Sequential()
model.add(layers.Flatten(input_shape=(16,16)))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(512, activation='relu'))
model.add(layers.Dense(1, activation='linear'))

model.compile(optimizer='adam',
              loss='mean_squared_error',
              metrics=['mae'])

history = model.fit(trainX, trainY, epochs=50, 
                    validation_split=.2, batch_size=128, verbose=1)

Pay attention also to correctly manipulate your images…

Images are stores in a list of arrays. You have to transform the list into a single array of shapes (n_sample, 16, 16).

This can be done simply:

images = np.asarray(images)
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement