简体   繁体   中英

Why did I receive input error in a simple Keras Functional API?

I hope that together we can solve my problem, I will try to summarise it and paste in some codes. At our research we are using TensorFlow/Keras for our DNN, to classify images (convolutional network). It was a quite simple Sequential model, but now we are trying to add more inputs, so I started to change the network to the Functional API (honestly, this is the first time that I used it). I recreated the original convolutional network and everything was working fine. So I generated the necessary additional data into simple text files (one for each image) and these will be the second input in our model. This is were something went wrong, because I've got an error and now I am stuck with the solution.

To recreate the error, I made a new simple python file without the convolution part, just to try out the model's text file part. The input is 5000 text files, containing only a float number. After the preprocessing, we will have 4000 for train and additional 1000 for testing, both are stored in a numpy array. The train and test arrays were split through sklearn.model_selection.train_test_split .

In the original multi-channel network, I tried to concatenate the convolutional part with the text file and after it some dense layers are coming. But no concatenate here, just the train for the data stored in the text files.

Here are the shapes of the input and label arrays:
X_train.shape
(4000,)
y_train.shape
(4000, 5)

The - very - simple network:

inputA = Input(shape=(X_train.shape[0],))
x = Flatten()(inputA)
x = Dense(256, activation='relu')(x)
x = Dense(256, activation='relu')(x)
x = Dense(64, activation='relu')(x)
x = Dense(5, activation='softmax')(x)
x = Model(inputs=inputA, outputs = x)

Compile:

x.compile(loss='categorical_crossentropy', optimizer='sgd', metrics=['accuracy'])

Model fit:

x.fit(X_train, y_train, 
          validation_data=(X_test, y_test),
          batch_size=batch, 
          verbose=1, 
          epochs=epoch_number)

And the received error message:

ValueError: Error when checking input: expected input_8 to have shape (4000,) but got array with shape (1,)

The question is, what did I wrong? The previous codes are working just fine in the Sequential model, but here no. Could somebody help me solve this mystery?

Best wishes, Tamas

It's because of this line:

inputA = Input(shape=(X_train.shape[0],))

Keras expects the number of features, not the number of samples. In your case, the input_shape=(1,) .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM