Skip to main content

Posts

Showing posts with the label loss function

Loss Function - Mean Absolute Error

 Mean Absolute Error -  it is straight forward import numpy as np import tensorflow as tf y_true = np.float32([ 1 , 2 ] ) y_pred = np.float32([ 2 , 2 ] ) #loss = mean(abs(y_true - y_pred)) #np.abs will return the absolute values , nps.abs[-1] will return 1 loss_mae = tf.keras.losses.mean_absolute_error(y_true, y_pred) print (loss_mae.numpy()) print (np.mean(np. abs (y_true - y_pred)))   print and verify  0.5 0.5 or u can run the code in colab using the link https://github.com/naveez-alagarsamy/matplotlib/blob/main/mean_absolute_error.ipynb   reference - https://www.tensorflow.org/api_docs/python/tf/keras/metrics/mean_absolute_error

Loss function - Mean Square Error

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