Skip to main content

Input Keras Layer Explanation With Code Samples

Simple answers to common questions related to the Keras layer arguments, including input shape, weight, units and dim. With examples.
Created on August 12|Last edited on March 20
There's a common question around understanding the difference between input_shape, units, dim, etc. for any Keras layer (layer class).
For example, the doc says units specify the output shape of a Keras layer but in the image of the neural net below, hidden layer1 has four units. Does this directly translate to the units attribute of the Layer object? Or does units in Keras equal the shape of every weight in the hidden layer times the number of units?
How does one understand/visualize the attributes of the model - particularly the layers - with the image below?
Try it on Colab Notebook



To properly answer this we'll look at the following ...

What We'll Be Covering



What Are Units In A Keras Layer?

In a Keras layer, units are the number of neurons in each layer of your neural network architecture. For example, for:
some_layer = tf.keras.layers.Dense(10, activation=None)
The number of units is 10. Thus there are 10 neurons.
In the image above, the hidden layer 1 has 4 units, the hidden layer 2 has 4 units, and the output layer has 1 units.

What Are Shapes In A Keras Layer?

In a Keras layer, shapes are tuples representing how many elements an array or tensor has in each dimension.
For Example: A tensor with shape (3, 4, 4) is 3 dimensional with the first dimension having 3 elements. Each of these 3 elements has 4 elements, and each of these 4 elements has 4 elements. Thus a total of 3*4*4 = 48 elements.

What Is The Input Shape In A Keras Layer?

In a Keras layer, the input shape is generally the shape of the input data provided to the Keras model while training. The model cannot know the shape of the training data. The shape of other tensors(layers) is computed automatically.
Each type of Keras layer requires the input with a certain number of dimensions:
  • Dense layers require inputs as (batch_size, input_size)
  • 2D convolutional layers need inputs as:
    • if using channels_last: (batch_size, imageside1, imageside2, channels)
    • if using channels_first: (batch_size, channels, imageside1, imageside2)
  • 1D convolutions and recurrent layers use(batch_size, sequence_length, features)
  • The shape of other tensors is computed based on the number of units provided along with other particularities like kernel_size in the Conv2D layer.

What Are Weights In A Keras Layer?

In a Keras layer, weights are the matrix capable of transforming the input shape into the output shape by some learned mathematical operation.
Weights will be entirely automatically calculated based on the input and output shapes. Again, each type of layer works in a certain way.

What Is Dim In A Keras Layer?

Commonly dim refers to how many dimensions a tensor has in a Keras layer. For instance, a tensor with shape (32, 1024) has 2 dimensions.
We usually define a one dim tensor like (3,). Thus this one-dimensional input has 3 elements.

The Input Layer Image in the Problem Section in Keras

Once more, let's look at the image from the problem section above, and define the image in Keras.

Try it on Colab Notebook


Using Sequential API

from keras.models import Sequential
from keras.layers import *

model = Sequential()

model.add(Input(shape=(3,))) # Input tensor
model.add(Dense(units=4)) # hidden layer 1
model.add(Dense(units=4)) #hidden layer 2
model.add(Dense(units=1)) #output layer

Using Functional API

from keras.models import Model
from keras.layers import *


inputs = Input(shape=(3,)) # input tensor
hidden1 = Dense(units=4)(inputs) # hidden layer 1
hidden2 = Dense(units=4)(hidden1) # hidden layer 2
outputs = Dense(units=1)(hidden2) # hidden layer 1

#define the model's start and end points
model = Model(inputs, outputs)
Let us look at the model summary and the output shape for each layer.

Fig 1: Model summary
The answer was inspired by this Stack Overflow thread.

Joseph Jung
Joseph Jung •  
Thank you for this article. It cleared up a lot of question marks for me. However, could you please verify something for me? In the "What Are Units In a Keras Layer?" section, it states that the output layer has 2 units. I see 1. What am I missing?
1 reply
Iterate on AI agents and models faster. Try Weights & Biases today.