šŸŽ Checkout my Learn React by Making a Game course and get 1 month Free on Skillshare!

Understanding the model.summary() method in TensorflowJs

The summary() method of a TensorflowJs model prints a text summary of that given model.

This is quite useful when you are working with models that you have not created or you want to check that your model has the correct architecture.

Let's implement a model with the following architecture:
model.summary() method in TensorflowJs

This is the code that will implement the above model:

// create the inputs
const input = tf.input({shape: [3], name:'Blue'})
const hidden = tf.layers.dense({units: 2, name:'Gray'}).apply(input)
const output = tf.layers.dense({units: 3, name:'Orange'}).apply(hidden)

// build the model
const model = tf.model({inputs: input, outputs: output})

// list the summary of the model
model.summary()

We have used the more flexible model() function, but you can also use a sequential model.

The name parameter for each layer is purely optional. Just added it to make the output a bit more clear.

And speaking of output, this is what we will get from the model.summary():
output for model.summary() method in TensorflowJs

By default, this is logged into the console, but you can change this using the printFn parameter of the summary.

For each layer of the model, we will see the Input Shape and Output shape. This means from how many neurons data flows into that layer, and to how many neurons the data flows out.

The null values from notations such as [null,3] means that the layer can process any batch size.

The Param value is a bit more tricky. It represents how many synapses are entering that layer. A synapse is a link between 2 neurons.

Given that we are using dense layers, each neuron from one layer will have a synapse with all the neurons in the next layer.

But why if in the input layer we have 3 neurons and in the hidden layer we have 2 neurons, we get a total of 8 parameters? Should we not have 3 x 2 = 6 parameters?

Well, the reason for this are the bias neurons. Each layer (except for the input one) comes with an implicit bias neuron, and this neuron adds its own synapses.

Below is the real full architecture of the network. The bias neurons are marked with green.
TensorflowJs model arhitecture with bias neurons

The Trainable params refers to the total number of weights that our model will calculate so that it can accomplish the given task.

Parameters for the model.summary()

The summary() method comes with a set of parameters to customize the look of the outputted summary:

  • lineLength - specifies the characters number for a line
  • positions (number[]) - an array of numbers with custom widths for each of the columns
  • printFn - custom print function. Defaults to console.log()

All of these parameters are optional.

You can see here the codepen for this example. Be sure to have your console open to see the output.

And speaking of finding out the internals of a TensorflowJs model you can use the getWeights() method to take a look at the current values that are set as the weights of the that model.

šŸ“– 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 *