Keras - Layers
Keras - Layers
As learned earlier, Keras layers are the primary building block of Keras models. Each layer receives input
information, do some computation and finally output the transformed information. The output of one
layer will flow into the next layer as its input. Let us learn complete details about layers in this chapter.
Introduction
A Keras layer requires shape of the input (input_shape) to understand the structure of the input data,
initializer to set the weight for each input and finally activators to transform the output to make it non-
linear. In between, constraints restricts and specify the range in which the weight of input data to be
generated and regularizer will try to optimize the layer (and the model) by dynamically applying the
penalties on the weights during optimization process.
To summarise, Keras layer requires below minimum details to create a complete layer.
Initializers
Regularizers
Constraints
Activations
Let us understand the basic concept in the next chapter. Before understanding the basic concept, let us
create a simple Keras layer using Sequential model API to get the idea of how Keras model and layer
works.
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers
from keras import regularizers
from keras import constraints
model = Sequential()
where,
Line 9 creates a new Dense layer and add it into the model. Dense is an entry level layer
provided by Keras, which accepts the number of neurons or units (32) as its required
parameter. If the layer is first layer, then we need to provide Input Shape, (16,) as well.
Otherwise, the output of the previous layer will be used as input of the next layer. All other
parameters are optional.
Line 10 creates second Dense layer with 16 units and set relu as the activation function.
Here we are.
Input shape
In machine learning, all type of input data like text, images or videos will be first converted into array of
numbers and then feed into the algorithm. Input numbers may be single dimensional array, two
dimensional array (matrix) or multi-dimensional array. We can specify the dimensional information
using shape, a tuple of integers. For example, (4,2) represent matrix with four rows and two columns.
Similarly, (3,4,2) three dimensional matrix having three collections of 4x2 matrix (two rows and four
columns).
To create the first layer of the model (or input layer of the model), shape of the input data should be
specified.
Explore our latest online courses and learn new skills at your own pace. Enroll and become a
certified expert to boost your career.
Find your
American way.
Initializers
In Machine Learning, weight will be assigned to all input data. Initializers module provides different
functions to set these initial weight. Some of the Keras Initializer function are as follows −
Zeros
my_init = initializers.Zeros()
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_initializer = my_init))
Ones
my_init = initializers.Ones()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_initializer = my_init))
Constant
Generates a constant value (say, 5) specified by the user for all input data.
from keras.models import Sequential
from keras.layers import Activation, Dense
from keras import initializers
RandomNormal
my_init = initializers.RandomNormal(mean=0.0,
stddev = 0.05, seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_initializer = my_init))
where,
RandomUniform
where,
minval represent the lower bound of the random values to generate
TruncatedNormal
VarianceScaling
Generates value based on the input shape and output shape of the layer along with the specified scale.
my_init = initializers.VarianceScaling(
scale = 1.0, mode = 'fan_in', distribution = 'normal', seed = None)
model.add(Dense(512, activation = 'relu', input_shape = (784,),
skernel_initializer = my_init))
where,
VarianceScaling
It finds the stddev value for normal distribution using below formula and then find the weights using
normal distribution,
stddev = sqrt(scale / n)
where n represent,
Similarly, it finds the limit for uniform distribution using below formula and then find the weights using
uniform distribution,
lecun_normal
It finds the stddev using the below formula and then apply normal distribution
lecun_uniform
It finds the limit using the below formula and then apply uniform distribution
where,
glorot_normal
It finds the stddev using the below formula and then apply normal distribution
where,
glorot_uniform
It finds the limit using the below formula and then apply uniform distribution
where,
he_normal
It finds the stddev using the below formula and then apply normal distribution.
he_uniform
It finds the limit using the below formula and then apply uniform distribution.
Orthogonal
Identity
NonNeg
UnitNorm
my_constrain = constraints.UnitNorm(axis = 0)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_constraint = my_constrain))
MaxNorm
where,
axis represent the dimension in which the constraint to be applied. e.g. in Shape (2,3,4) axis 0
denotes first dimension, 1 denotes second dimension and 2 denotes third dimension
MinMaxNorm
where, rate represent the rate at which the weight constrain is applied.
Regularizers
In machine learning, regularizers are used in the optimization phase. It applies some penalties on the
layer parameter during optimization. Keras regularization module provides below functions to set
penalties on the layer. Regularization applies per-layer basis only.
L1 Regularizer
where, kernel_regularizer represent the rate at which the weight constrain is applied.
L2 Regularizer
my_regularizer = regularizers.l2(0.)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_regularizer = my_regularizer))
L1 and L2 Regularizer
my_regularizer = regularizers.l2(0.)
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,),
kernel_regularizer = my_regularizer))
Activations
In machine learning, activation function is a special function used to find whether a specific neuron is
activated or not. Basically, the activation function does a nonlinear transformation of the input data and
thus enable the neurons to learn better. Output of a neuron depends on the activation function.
As you recall the concept of single perception, the output of a perceptron (neuron) is simply the result of
the activation function, which accepts the summation of all input multiplied with its corresponding
weight plus overall bias, if any available.
So, activation function plays an important role in the successful learning of the model. Keras provides a
lot of activation function in the activations module. Let us learn all the activations available in the
module.
linear
model = Sequential()
model.add(Dense(512, activation = 'linear', input_shape = (784,)))
Where, activation refers the activation function of the layer. It can be specified simply by the name of
the function and the layer will use corresponding activators.
elu
model = Sequential()
model.add(Dense(512, activation = 'elu', input_shape = (784,)))
selu
model = Sequential()
model.add(Dense(512, activation = 'selu', input_shape = (784,)))
relu
model = Sequential()
model.add(Dense(512, activation = 'relu', input_shape = (784,)))
softmax
model = Sequential()
model.add(Dense(512, activation = 'softmax', input_shape = (784,)))
softplus
model = Sequential()
model.add(Dense(512, activation = 'softplus', input_shape = (784,)))
softsign
model = Sequential()
model.add(Dense(512, activation = 'softsign', input_shape = (784,)))
tanh
sigmoid
model = Sequential()
model.add(Dense(512, activation = 'sigmoid', input_shape = (784,)))
hard_sigmoid
model = Sequential()
model.add(Dense(512, activation = 'hard_sigmoid', input_shape = (784,)))
exponential
model = Sequential()
model.add(Dense(512, activation = 'exponential', input_shape = (784,)))
Dropout Layers
2
Dropout is one of the important concept in the machine learning.
Flatten Layers
3
Flatten is used to flatten the input.
Reshape Layers
4
Reshape is used to change the shape of the input.
Permute Layers
5
Permute is also used to change the shape of the input using pattern.
RepeatVector Layers
6
RepeatVector is used to repeat the input for set number, n of times.
Lambda Layers
7
Lambda is used to transform the input data using an expression or function.
Convolution Layers
8 Keras contains a lot of layers for creating Convolution based ANN, popularly called as
Convolution Neural Network (CNN).
Pooling Layer
9
It is used to perform max pooling operations on temporal data.
Merge Layer
11
It is used to merge a list of inputs.
Embedding Layer
12
It performs embedding operations in input layer.