-
Notifications
You must be signed in to change notification settings - Fork 19.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
model.evaluate() not giving the same loss value as model.predict() #20841
Comments
Thanks for posting the issue @goncrosa!
When using Relevant Documentation:
I've also attached a gist demonstrating the fix. |
what i don't understand is that predict() is different from evaluate() and in your example, it gives the same value. Moreover I now changed "modeldo.evaluate(X_val, y_val)" to "eval_mse = model.evaluate(X_val, y_val, verbose=0) |
I can't seem to understand why is model.predict giving me the same value as the last iteration of my training but model.evaluate is giving me a different value
My code looks like this
random_seed = random.randint(0, 1000) # Random seed between 0 and 1000
print("Random Seed:", random_seed)
tf.random.set_seed(random_seed)
initializer = tf.keras.initializers.GlorotUniform(seed=42)
def build_mlpdo(hidden_layer1, hidden_layer2, dropout_rate):
model = Sequential([
Input(shape=(5,)),
Dense(hidden_layer1, activation='sigmoid', kernel_initializer=initializer),
Dropout(dropout_rate),
Dense(hidden_layer2, activation='sigmoid', kernel_initializer=initializer),
Dropout(dropout_rate),
Dense(1, activation='linear', kernel_initializer=initializer) # Output layer
])
modeldo = build_mlpdo(hidden_layer1=2, hidden_layer2=9, dropout_rate=0.5)
early_stop = keras.callbacks.EarlyStopping(monitor='val_loss', patience=pa, restore_best_weights=True)
historydo = modeldo.fit(X_train, y_train, validation_data=(X_val, y_val), epochs=ep, batch_size=b_s, callbacks=[early_stop])
modeldo.evaluate(X_val, y_val)
test_predictions = modeldo.predict(X_val)
test_predictions = test_predictions.ravel()
mse = np.mean((test_predictions - y_val)**2)
print("Val MSE:", mse)
giving me the outputs
Epoch 10/10
25/25 ━━━━━━━━━━━━━━━━━━━━ 0s 3ms/step - loss: 0.4850 - val_loss: 0.0973
7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 2ms/step - loss: 0.1068
7/7 ━━━━━━━━━━━━━━━━━━━━ 0s 7ms/step
Val MSE: 0.09726350368324282
The text was updated successfully, but these errors were encountered: