DL Practical 3 Loss Function
DL Practical 3 Loss Function
DL Practical 3 Loss Function
To train — the process by which the model maps the relationship between the
training data and the outputs — the neural network updates its hyperparameters,
the weights, wT, and biases, b, to satisfy the equation above. Each training input
is loaded into the neural network in a process called forward propagation. Once
the model has produced an output, this predicted output is compared against the
given target output in a process called backpropagation — the hyperparameters of
the model are then adjusted so that it now outputs a result closer to the target
output. This is where loss functions come in.
Four Important Loss Functions
Mean absolute error (MAE) also called L1 Loss is a loss function used for
regression problems. It represents the difference between the original and
predicted values extracted by averaging the absolute difference over the
data set.
Mean absolute error is used when you are doing regression and don’t want
outliers to play a big role. It can also be useful if you know that your distribution
is multimodal, and it’s desirable to have predictions at one of the modes, rather
than at the mean of them.
Mean Squared Error (MSE) also called L2 Loss is also a loss function used
for regression. It represents the difference between the original and
predicted values extracted by squared the average difference over the data
set.
MSE is used when doing regression, believing that your target, conditioned on
the input, is normally distributed, and want large errors to be significantly
(quadratically) more penalized than small ones.
The Root Mean Squared Error (RMSE) is a mostly used error function.
It is the differences between values predicted by a model and the actual
desired values. The RMSE can be calculated by taking the square root of
above-mentioned Mean Squared Errors (MSE) / L2 Loss.
The root mean squared error (RMSE) is a useful loss function when large
errors are more costly, and it is important to minimize them.
4. Cross-Entropy Loss
a. Binary cross entropy: which is for binary classification tasks with two
classes 0 and 1
b. Categorical cross-entropy and sparse categorical cross- entropy: Both
categorical cross entropy and sparse categorical cross-entropy have the
same loss function. The only difference between the two is on how truth
labels are defined that is categorical cross entropy is used when true
labels are one-hot encoded and sparse categorical cross-entropy is used
when truth labels are integer encoded .
Code:
Implementing Loss Functions in Python
#Python implementation for MAE is as follows :
import numpy as np
def MAE(act, pred):
print("Actual :: ", act)
print("Predict:: ",pred)
diff = act - pred
print("Difference is ::", diff)
abs_diff = np.absolute(diff)
print("Absolute Difference is ::", abs_diff)
mean_diff = abs_diff.mean()
return mean_diff
# use of python mean absolute error function
act = np.array([100,200,300])
pred = np.array([115,180,331])
MAE (act,pred)
Output:
Actual :: [100 200 300]
Predict:: [115 180 331]
Difference is :: [-15 20 -31]
Absolute Difference is :: [15 20 31]
22.0
• You can also use mean_absolute_error from sklearn to calculate MAE:
from sklearn.metrics import mean_absolute_error
mean_absolute_error(act, pred)
Output: 22.0
• You can also use mean_squared_error from sklearn to calculate MSE. Here’s how the
function works:
from sklearn.metrics import mean_squared_error
act = np.array([1.1,2,1.7])
pred = np.array([1,1.7,1.5])
mean_squared_error(act, pred)
Output: 0.04666666666666667
• You can use mean_squared_error from sklearn to calculate RMSE as well. Let’s see
how to implement the RMSE using the same function: