Keras Input Explanation: input_shape, units, batch_size, dim, etc

A simple explanation to Keras layers' arguments. Made by Ayush Thakur using Weights & Biases
Ayush Thakur

Question

For any Keras layer (layer class), can someone explain how to understand the difference between input_shape, units, dim, etc.?

For example, the doc says units specify the output shape of a layer.

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?

iHW2o.jpg

Answer

Units

Shape

Input Shape

Weights

Dim

Let us define the image in the Problem section in Keras

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.

outputshape.png -> Fig 1: Model summary <-

The answer was inspired by this Stack Overflow thread.