Mean Square Error is nothing but
loss = square(y_true - y_pred)
loss = (y_true - y_pred)**2
both are same
import numpy as np
import tensorflow as tf
y_true = np.float32([1,2] )
y_pred = np.float32([2,2] )
iam using numpy mean to calculate the average of values
loss = np.mean(np.square(y_true - y_pred) )
on the other hand u can verify with tensorflow MeanSquaredError
function to calculate mse because in future we will use these for our machine
learning samples
mse = tf.keras.losses.MeanSquaredError()
mse_loss_tf = mse(y_true, y_pred).numpy()
print the values to verify
print(loss)
print(mse_loss_tf)
0.5 0.5
u can use the git link and run it in colab
https://github.com/naveez-alagarsamy/matplotlib/blob/main/mean_square_error.ipynb
reference -
https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_squared_error
Comments
Post a Comment