송준석
송준석

Reputation: 1031

ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (1,)

I am trying to predict using the learned .h5 file. The learning model is as follows.

model =Sequential()
model.add(Dense(12, input_dim=3, activation='relu'))
model.add(Dense(8, activation='relu'))
model.add(Dense(4, activation='relu'))
model.add(Dense(1, activation='sigmoid'))
model.compile(loss='binary_crossentropy', optimizer = 'adam', metrics = ['accuracy'])

And I wrote the form of the input as follows.

x = np.array([[band1_input[input_cols_loop][input_rows_loop]],[band2_input[input_cols_loop][input_rows_loop]],[band3_input[input_cols_loop][input_rows_loop]]])

prediction_prob = model.predict(x)

I thought the shape was correct, but the following error occurred.

ValueError: Error when checking : expected dense_1_input to have shape (3,) but got array with shape (1,)

The shape of x is obviously (3,1), but the above error doesn't disappear (the data is from a csv file in the form of (value 1, value 2, value 3, class)).

How can I solve this problem?

Upvotes: 2

Views: 6712

Answers (1)

Maxim
Maxim

Reputation: 53758

The shape of x is obviously (3,1), but the above error continues.

You are right, but that's not what keras expects. It expects (1, 3) shape: by convention, axis 0 denotes the batch size and axis 1 denotes the features. The first Dense layer accepts 3 features, that's why it complains when it sees just one.

The solution is simply to transpose x.

Upvotes: 5

Related Questions