Loss function by mean squared error

Next, we need to define the loss function as the objective function to be optimized. The loss function that's commonly used for regression problems is mean squared error (MSE). This can be calculated as follows:

It is just an average of the difference between the target value and the predicted value from the model. The gradient of the loss function is calculated like this so that we can apply the updating logic:

We can define the MSE loss function in TensorFlow.js as follows:

const loss = (pred, label) => ...

Get Hands-On Machine Learning with TensorFlow.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.