简体   繁体   中英

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083)

I was trying to build a CNN model using Keras. Here is my code:

import keras
import tensorflow as tf
from keras.layers import Dense
from keras.models import Sequential
from keras.models import load_model
from keras.callbacks import EarlyStopping  
from sklearn.model_selection import train_test_split
from __future__ import print_function
import pandas as pd
import numpy as np
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D
from google.colab import drive
drive.mount('/content/drive')
                  ...

# Define x_train...data
x_data=df.iloc[:,1:10084].values-25 
# x_data = np.array(x_data).tolist()  
y_data=df[['type1','type2']].values
X_train, X_test, y_train, y_test = train_test_split(x_data, y_data,test_size=0.2)

model.fit(X_train, y_train,
          batch_size=100,
          epochs=100,
          verbose=1,
          validation_data=(X_test, y_test),
          callbacks=[history])

It returned the error

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (595, 10083)

After referring to other questions I tried to reshape the dimension of the data array using

X_train = X_train[np.newaxis, :, :, :]

which changed it to 3-dimentional and returned the error:

ValueError: Error when checking input: expected conv2d_1_input to have 4 dimensions, but got array with shape (1, 595, 10083)

How should I increase the dimension of the data to 4?

The first layer of the model, which a 2D convolution, is expecting (I believe) 2D image data with multiple channels. Something like:

(img_height, img_width, num_channels).

In addition, you need to have another dimension for the "batch" or "sample." Therefore the final shape will be:

(num_samples, img_height, img_width, num_channels).

So, you'll need to reshape your input data to be in the above pattern, with the various dimensions having the interpretation I've indicated, based on what I infer from your code.

You might look at functions like np.reshape and np.expand_dims, depending on how your input data is formatted in the first place.

You can create some test data like this:

x_train = np.random.random((4,32,32,3))

# then try out the model prediction

model.predict(x_train)  

which is four images of 32x32 size, and 3 channels, to verify your processing.

You'll also need to create something appropriate for the y_train when you do the actual fitting.

I hope this helps.

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