简体   繁体   中英

Problem with the input layer with Keras: expected dense_1_input to have shape (11,) but got array with shape (15,)

It seems there is a number of similar questions but I haven't been able to solve my issue.

There is a dataset with 15 features (columns) and one dependent binary feature that I want to predict.

I do all the prep work:

features = df.iloc[:,:-1]
result = df.iloc[:,-1]

# Splitting the dataset into the Training set and Test set
from sklearn.model_selection import train_test_split
X_train, X_test, y_train, y_test = train_test_split(features, result, test_size = 0.2, random_state = 0)

# Feature Scaling
from sklearn.preprocessing import StandardScaler
sc = StandardScaler()
X_train = sc.fit_transform(X_train)
X_test = sc.transform(X_test)

Checking the sizes of X_train and y_train:

X_train.shape

returns (3392, 15) And

Y_train.shape

returns (3392,)

Y_train is an array: array([0, 1, 0, ..., 0, 0, 0])

Then I build a network:

from keras.models import Sequential
from keras.layers import Dense

classifier = Sequential()

classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu', input_dim= X_train[0].shape)) # 8 units because (15 features + 1 to forecast) / 2 
# Adding the second hidden layer
classifier.add(Dense(units = 8, kernel_initializer = 'uniform', activation = 'relu'))
# Adding the output layer
classifier.add(Dense(units = 1, kernel_initializer = 'uniform', activation = 'sigmoid'))
# Compiling the ANN
classifier.compile(optimizer = 'adam', loss = 'binary_crossentropy', metrics = ['accuracy'])
# Fitting the ANN to the Training set
classifier.fit(X_train, y_train, batch_size = 10, epochs = 100)

And got an error message: ValueError: Error when checking input: expected dense_1_input to have shape (11,) but got array with shape (15,)

I can't understand why it is expecting a shape of 11 (this number isn't used anywhere in my code). In similar questions posted here problems are usually coming from incorrectly specifying a training set's size (like zero instead of whatever the number of features is). But I'm clearly passing the right number to the first layer by writing

input_dim= X_train[0].shape

which I can also write as

input_dim= 15

with the same result.

What am I doing wrong?

PS I also thought the problem was with y_train and did this:

y_train = y_train.reshape(3392,1)

but with no effect.

Change this

X_train[0].shape

to this

X_train.shape[1]

to get the number of columns in your matrix.

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