For any Keras layer (layer class), can someone explain how to understand the difference between
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?
10is the number of units.
some_layer = tf.keras.layers.Dense(10, activation=None)
hidden layer 1has 4, the
hidden layer 2has 4, and the
outputlayer has 2 units each.
(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 = 48elements.
This is generally the shape of the input data provided to the 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 layer requires the input with a certain number of dimensions:
(batch_size, imageside1, imageside2, channels)
(batch_size, channels, imageside1, imageside2)
(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
(32, 1024)has 2 dimensions.
(3,). Thus this one-dimensional input has 3 elements.
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
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.