The Mean Squared Error function in TensorflowJs

During the training phase, we are evaluating the performance of an AI model by measuring its error level.

The error represents how far from the truth the prediction is vs the outcome in the real world. The smaller the error, the better.

Based on this error we will tweak the weights of our Javascript neuronal networks up and down so that we will get a more precise model.

The formula for a squared error is:

const squaredError = (predictionValue - realValue) ** 2

By the way, the double asterisk operator ** in Javascript means to raise the first operand to the power of the second operand. So, ** 2 means square the result.

Coming back to the squared error, let's say we are trying to build an AI model that will predict the price of APPLE stocks. At the time of this writing, the price for one share of AAPL is 155$. However, our model predicted that the price will be 158$.

A Squared Error for the above stock pricing case is:

const se = (158 - 155) ** 2 // 9$ 

Okay, but why the squaring? For two reasons:

  1. the error is always non-negative. For example, our model is equally far from the truth if it predicts the price of a share to be 158$ or 152$
  2. it increases the error when we have larger deviations from the true values. And this is a good thing because we want to change our model more aggressively when it makes huge errors.

Mean Squared Error in Javascript

So far we have talked about the Squared Error. The Mean Squared Error refers to getting the average squared error from a set of multiple values.

Below is the general formal for the mean squared error:
The Mean Squared Error function in TensorflowJs

In Javascript a very trivial implementation will look like this:

const real = [7, 9, 3, 10]
const predictions = [7, 8, 2, 12]

const len = real.length
let sum = 0

for(let i = 0; i < len; i++) {
  sum+= (predictions[i] - real[i]) ** 2
}

const meanSquaredError = sum / len // 1.5

In general, we will work with a lot of inputs and outputs when training the model, and we need to have a general overview of how well the model performed on average. Therefore the need for a Mean Squared Error.

Using the Mean Squared Error in TensorflowJs

TensorflowJs comes with a full set of error functions included.

We have a direct implementation of the mean squared error in TensorflowJs:

tf.losses.meanSquaredError(real,predictions)

Given that tensors are just a general represetantion of arrays we can create 1d tensors with the values in the above example and use the built mean squared error:

const real = tf.tensor1d([7, 9, 3, 10]);
const predictions = tf.tensor1d([7, 8, 2, 12]);

const mse = tf.losses.meanSquaredError(real,predictions)

mse.print() // 1.5

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

📖 50 Javascript, React and NextJs Projects

Hi friend! Before you go, just wanted to let you know about the 50 Javascript, React and NextJs Projects FREE ebook.

One of the best ways to learn is by doing the work. Choose from 8 project categories and get started right away:

  • Business & Real-World
  • Games & Puzzles
  • Fun & Interesting
  • Tools & Libraries
  • Personal & Portfolio
  • Project Add-Ons
  • Productivity
  • Clones

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects complete with project briefs and wireframes!

Keep building and level up! Get all projects as an ebook right to your inbox!

X