EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials Keras Tutorial Keras Layers
Secondary Sidebar
Keras Tutorial
  • Basic
    • What is Keras?
    • Keras Install
    • Keras Applications
    • Keras Sequential
    • Keras Model Predict
    • Keras Save Model
    • Keras conv2D
    • Keras ImageDataGenerator
    • Keras input
    • Keras Datasets
    • Keras Early Stopping
    • Keras input
    • Keras Model Save
    • Keras LSTM Example
    • Keras Flatten
    • Keras Optimizers
    • Keras Layers
    • Keras Dense
    • Keras fit
    • Keras Model
    • Keras Metrics
    • Keras Batch Normalization
    • Keras CNN
    • Keras predict
    • Keras Dropout
    • Keras Embedding
    • Keras LSTM
    • Keras GPU
    • Keras Tuner
    • Keras VGG16
    • Keras Generator
    • Keras Pre-trained Models
    • Keras Custom Loss Function
    • keras.utils.to_categorical
    • Keras Neural Network
    • Keras Preprocessing
    • Keras Regularization
    • Keras Softmax
    • Keras Regression
    • Keras MaxPooling2D
    • Keras U-Net
    • Keras Initializers
    • Keras Transformer
    • Keras Data Augmentation
    • Keras ResNet50
    • Keras Verbose
    • Keras Plot Model
    • Keras OCR
    • Keras Utils Sequence
    • Keras Binary Classification
    • Keras Padding
    • UpSampling2d
    • Keras EfficientNet
    • Keras pad_sequences

Keras Layers

Keras Layers

Introduction to Keras Layers

Keras layers form the base and the primary blocks on which the building of Keras models is constructed. They act as the basic building block for models of Keras. Every layer inside the Keras models is responsible for accepting some of the input values, performing some manipulations and computations, and then generating the appropriate output containing the required transformed information. Also, it is a repetitive structure where the computed information that is the output of the particular layer acts as the values of input for other models.

In this article, we will learn about the basics of Keras layers and try to understand the Keras layers and the Keras layer’s basic concept.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

What are Keras layers?

To gain insights into the input structure, the layers of Keras need some input_shape, which is the input shape. Further, we need an initializer to assign the weights to every input. Lastly, we will need an activator to convert the achieved output into a non-linear format.

The specification of the value of the range between which all the input data must have their weights for the generation and the restrictions to be put on them are specified by the constraints. There is one more component named regularizer that proves to be helpful for the process of optimization. It tries to make the model and layer as optimized as possible by dynamically putting some penalties on the model’s weights in the optimization process.

In short, the Keras layers need to be provided with the following minimum information for the completion of layer –

• Constraints
• Regularizers
• Input shape
• Activators
• Initializers
• Count the units or neurons that are present inside the layer

Keras layers Basic Concept.

Now, we will try to understand every concept inside the Keras layer and look at how Keras supports that concept.

Input shape

Whenever you create any machine learning program, the model takes the input into videos, text, images, or others and converts them into an array containing numbers that are then supplied to the algorithm as input. The value of input numbers can be a one-dimensional, two-dimensional, or multi-dimensional array that is a matrix. The shape of input is used for the specification of its dimensions which is just a tuple or pair of integers. Let us take one example; if we say input shape id (4,2), the input matrix will contain two columns and four rows.

Let us consider some examples to understand input shape –

Example #1

import numpy as np
sampleInputShape= (4, 2)
educbaInput = np.zeros(shape)
print(educbaInput)

Output if the above code snippet after execution is –

12

Example #2

import numpy as np
sampleInputShape= (3, 4, 2)
educbaInput = np.zeros(shape)
print(educbaInput)

Output if the above code snippet after execution is –

Keras Layers

To create the first input layer, we need to specify the input shape of the model’s input data.

Initializers

We can assign the weights to all the inputs while creating a machine learning model. Initializers make the provision of various functions available for the initial weight assignation. Let us discuss some of the available functions provided by initializers –

  • Zeros – For giving the value 0 for all the input data during generation. We had a look at its implementation above two examples.
  • Ones – It helps assign one value for all the input data that will eb generated. Let us consider one example,

from Keras.sampleEducbaModels import Sequential
from Keras.layers import Activation, Dense
from Keras import initializers
assignOneAsInitialValue = initializers.Ones()
sampleEducbaModel.add(Dense(512, activation = 'relu', input_shape = (3, 4, 2),
kernel_initializer = assignOneAsInitialValue))

which results in the following output –

14

Constant

We can assign a particular constant value specified by the user to all the input values of the data. Let us take the same kind of example code snippet but with the specification of 5 as the constant number to initialize the input data –

from Keras.models import Sequential
from Keras.layers import Activation, Dense
from Keras import initializers
sampleEducbaConstantInitializer = initializers.Constant(value = 5) model.add(
Dense(512, activation = 'relu', input_shape = (3, 4, 2), kernel_initializer = sampleEducbaConstantInitializer)
)

Whose output is –

l

The other functions we have are RandomNormal, RandomUniform, TruncatedNormal, VarianceScaling, lecun_normal, lecun_uniform, glorot_normal, glorot_uniform, he_normal, he_uniform, orthogonal, and identity that is used for assigning initial values to inputs and are functions provided by the initializer.

Constraints

We can set the constraints on the weights or parameters in the machine learning models when in the optimization phase. The constraints module provides various functions for setting the Keras model’s constraints. Let’s have a look at some of them in the below tabular structure –

Function Purpose/ Description
NonNeg It helps apply constraints on the weights so that they should have non-negative values.
UnitNorm It helps apply the constraints on the weights to the unit norm value.
MaxNorm Helps for the specification that the weights should have a value equal to or less than the specified value.
MinMaxNorm Applies the constraints on the weight that the norm value on the weight should be between the specified minimum and maximum values of the range.

 

Regularizers

They are used in the optimization phase for applying certain penalties on the layer’s parameters during optimization. Note that the process of regularization is applied on an individual layer only. The functions provided by the regularizer of Keras for penalty application on layers are as mentioned below in table –

Function Description
L1 regularizer Used for the application of L1-based regularization.
L2 regularizer Application of the L2-based regularization can be made by using this function.
L1 and L2 regularizer  We can apply both the regularizations of L1 and L2 by using this method.

Activations – This is the component to determine whether a specific neuron inside the machine learning network or model is activated or deactivated.

Besides this, there are various Keras layers: Dense layer, Dropout layer, Flatten layer, reshape layer, permute layer, repeat vector layer, lambda layer, convolution layer, pooling locally connected layer, merge layer, an embedding layer.

Conclusion

Each Keras layer takes certain input, performs computation, and generates the output. Basic concepts of the Keras layers include input shape, initializers, regularizers, constraints, and activations.

Recommended Articles

This is a guide to Keras Layers. Here we discuss the introduction What is Keras layers, the Basic Concept, and examples with code implementation. You may also have a look at the following articles to learn more –

  1. TensorFlow Keras Model
  2. PyTorch vs. Keras
  3. Keras vs. TensorFlow vs. PyTorch
  4. PyTorch optimizer
Popular Course in this category
Keras Training (2 Courses, 8 Projects)
  2 Online Courses |  8 Hands-on Project |  24+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more