steve
steve

Reputation: 153

Check if the way of evaluating keras model via unseen data is correct

I studied Keras and created my first neural network model as the following:

from keras.layers import Dense
import keras
from keras import Sequential
from sklearn.metrics import accuracy_score

tr_X, tr_y = getTrainingData()

# NN Architecture
model = Sequential()
model.add(Dense(16, input_dim=tr_X.shape[1]))
model.add(keras.layers.advanced_activations.PReLU())

model.add(Dense(16))
model.add(keras.layers.advanced_activations.PReLU())

model.add(Dense(1, activation='sigmoid'))

# Compile the Model
model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy'])

# Fit the Model
model.fit(tr_X, tr_y, epochs=1000, batch_size=200, validation_split=0.2)

# ----- Evaluate the Model (Using UNSEEN data) ------
ts_X, ts_y = getTestingData()
yhat_classes = model.predict_classes(ts_X, verbose=0)[:, 0]
accuracy = accuracy_score(ts_y, yhat_classes)
print(accuracy)

I am not sure about the last portion of my code, i.e., model evaluation using model.predict_classes() where new data are loaded via a custom method getTestingData(). See my goal is to test the final model using new UNSEEN data to evaluate its prediction. My question is about this part: Am I evaluating the model correctly?

Thank you,

Upvotes: 0

Views: 547

Answers (1)

Manoj Mohan
Manoj Mohan

Reputation: 6044

Yes, that is correct. You can use predict or predict_classes to get the predictions on test data. If you need the loss & metrics directly, you can use the evaluate method by feeding ts_X and ts_y.

y_pred = model.predict(ts_X)

loss, accuracy = model.evaluate(ts_X, ts_y)

https://keras.io/models/model/#predict

https://keras.io/models/model/#evaluate

Difference between predict & predict_classes: What is the difference between "predict" and "predict_class" functions in keras?

Upvotes: 1

Related Questions