I was reading about the minimum squared error(MSE) in the TensorwFlow (tf) user document.
https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_squared_error
When I hard-coded the MSE and print each loss calculated, I observe a different value than what is reported by tf.
import numpy as np
import random
import tensorflow as tf
from tensorflow import keras
m = 200
n = 5
my_input= np.random.random([m,n])
my_output = np.random.random([m,1])
def obj(y_true,y_pred):
loss = tf.keras.losses.mean_squared_error(y_true,y_pred)
tf.print("\n")
tf.print(tf.math.reduce_mean(loss),"\n")
return (loss)
my_model = tf.keras.Sequential([
tf.keras.layers.Flatten(input_shape=(my_input.shape[1],)),
tf.keras.layers.Dense(32, activation='softmax'),
tf.keras.layers.Dense(32, activation='relu'),
tf.keras.layers.Dense(1)
])
my_model.compile(loss=obj ,optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001))
my_model.fit(my_input, my_output, epochs=5, batch_size=20, verbose=1)
The loss values match in the first step, however, there is always a difference after that. Could someone explain me the mechanism behind the loss calculation?
Epoch 1/5
0.349255413
1/10 [==>...........................] - ETA: 3s - loss: 0.3493
0.449805915
0.453376621
0.500476539
0.294586331
0.269146353
0.358534873
7/10 [====================>.........] - ETA: 0s - loss: 0.3822
