1
$\begingroup$

I'm trying to implement the One Hidden Layer Model presented in this article using Keras.

This is my code:

from keras.models import Sequential
from keras.layers import Dense, Dropout, Flatten, Activation
from keras import optimizers

model = Sequential()
model.add(Dense(100, input_dim=9216))
model.add(Activation('relu'))
model.add(Dense(30))
model.compile(loss='mean_squared_error', optimizer=sgd, metrics=['accuracy'])

sgd = optimizers.SGD(lr=0.01, momentum=0.9, nesterov=True)
hist = model.fit(X_train, y_train, epochs=10, verbose=0, validation_split=0.2)

y_pred = model.predict_classes(X_valid)

X_train shape is (2140, 9216)

y_train shape is (2140, 30)

X_valid shape is (1783, 9216)

But y_valid shape is (1783,). I'm trying to understand why I'm not getting a (1783, 30) output. Am I missing something?

$\endgroup$
3
$\begingroup$

The problem is using predict_classes in:

model.predict_classes(X_valid)

this is designed to select the argmax (index of the maximum output) and choose it as the predicted class, for a classifier.

You have a regression problem, and just want the raw output from the network. So instead you should call:

y_pred = model.predict(X_valid)
| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.