

{"id":77676,"date":"2020-04-29T10:00:17","date_gmt":"2020-04-29T04:30:17","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=77676"},"modified":"2021-08-25T13:56:33","modified_gmt":"2021-08-25T08:26:33","slug":"keras-layers","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/keras-layers\/","title":{"rendered":"Keras layers &#8211; Parameters and Properties"},"content":{"rendered":"<p>Layers are the primary unit to create neural networks. We compose a deep learning architecture by adding successive layers. Each successive layer performs some computation on the input it receives. Then after it propagates the output information to the next layer. At last, we get the desired results from the output of the last layer. In this Keras article, we will walk through different types of<strong> Keras layers, its properties and its parameters.<\/strong><\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77677\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg\" alt=\"Keras Layers\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Keras Layers<\/h2>\n<p>To define or create a Keras layer, we need the following information:<\/p>\n<ul>\n<li><strong>The shape of Input:<\/strong> To understand the structure of input information.<\/li>\n<li><strong>Units:<\/strong> To determine the number of nodes\/ neurons in the layer.<\/li>\n<li><strong>Initializer:<\/strong> To determine the weights for each input to perform computation.<\/li>\n<li><strong>Activators:<\/strong> To transform the input in a nonlinear format, such that each neuron can learn better.<\/li>\n<li><strong>Constraints:<\/strong> To put restrictions on weights at the time of optimization.<\/li>\n<li><strong>Regularizers:<\/strong> To apply a penalty on the parameters during optimization.<\/li>\n<\/ul>\n<h2>Different Layers in Keras<\/h2>\n<h3>1. Core Keras Layers<\/h3>\n<ul>\n<li><strong>Dense<\/strong><br \/>\nIt computes the output in the following way:<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">output=activation(dot(input,kernel)+bias)<\/pre>\n<p>Here,<br \/>\n\u201cactivation\u201d is the activator, \u201ckernel\u201d is a weighted matrix which we apply on input tensors, and \u201cbias\u201d is a constant which helps to fit the model in a best way.<\/p>\n<p>Dense layer receives input from all the nodes in the previous layer. It has the following arguments and its default values:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Dense(units, activation=NULL, use_bias=TRUE,kernel_initializer=\u2019glorot_uniform\u2019,bias_regularizer=NULL,activity_regularizer=NULL,kernel_constraint=NULL,bias_constrain=NULL)<\/pre>\n<ul>\n<li><strong>Activation<\/strong><\/li>\n<\/ul>\n<p>It is used to apply the activation function to output. It is the same as passing activation in the Dense layer and\u00a0has the following arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Activation(activation_function)<\/pre>\n<p>If you do not give activation_function, it will perform linear activation.<\/p>\n<ul>\n<li><strong>Dropout<\/strong><\/li>\n<\/ul>\n<p>We use Dropout in our neural network to save it from overfitting. To prevent overfitting, it randomly chooses a fraction of units and set to 0 at each update.<\/p>\n<p>It has the following arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Dropout(rate, noise_shape=NULL, seed=NULL)<\/pre>\n<ul>\n<li><strong>Flatten<\/strong><\/li>\n<\/ul>\n<p>We use Flatten to convert the input to a lower dimension.<\/p>\n<p>For example: an input layer of shape(batch_size, 3,2) is flatten to output of shape(batch_size, 6). It has the following arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Flatten(data_format=None)<\/pre>\n<ul>\n<li><strong>Input<\/strong><\/li>\n<\/ul>\n<p>We use this layer to create Keras model using only the input and output of the model. This layer is the entry point into the model graph.<\/p>\n<p>It has following arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Input(shape, batch_shape, name, dtype, sparse=FALSE,tensor=NULL)<\/pre>\n<ul>\n<li><strong>Reshape<\/strong><\/li>\n<\/ul>\n<p>Reshape output to a particular dimension.<\/p>\n<p>Argument:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Reshape(target_shape)<\/pre>\n<p>Gives output of shape:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">(batch_size,)+ target_shape<\/pre>\n<ul>\n<li><strong>Permute<\/strong><\/li>\n<\/ul>\n<p>Permute input according to the given pattern. We may also use the Permute layer to change input shapes using specified patterns.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Permute(dims)<\/pre>\n<ul>\n<li><strong>Lambda<\/strong><\/li>\n<\/ul>\n<p>We use Lambda layers to build extra layer features, which are not provided in Keras.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Lambda(lambda_fun,output_shape=None, mask=None, arguments= None)<\/pre>\n<ul>\n<li><strong>Masking<\/strong><\/li>\n<\/ul>\n<p>To skip the timestep if all the features are equal to mask_value.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Masking(mask_value=0.0)<\/pre>\n<h3>2. Convolution Layers of Keras<\/h3>\n<ul>\n<li><strong>Conv1D and Conv2D<\/strong><\/li>\n<\/ul>\n<p>Here we define a weighted kernel, we convolve this kernel all over the input and produce output tensors.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">Conv1D(filters,kernel_size,strides=1, padding=\u2019valid\u2019, data_format=\u2019channels_last\u2019, dilation_rate=1, activation=None, use_bias=True, kernel_initializer=\u2019glorot_uniform\u2019, bias_initializer=\u2019zeros\u2019, kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=None)\r\n\r\nConv2D(filters,kernel_size,strides=(1,1) , padding=\u2019valid\u2019, data_format=\u2019channels_last\u2019, dilation_rate=(1,1) , activation=None, use_bias=True, kernel_initializer=\u2019glorot_uniform\u2019, bias_initializer=\u2019zeros\u2019, kernel_regularizer=None, bias_regularizer=None, activity_regularizer=None, kernel_constraint=None, bias_constraint=Non<\/pre>\n<h3><strong>3. Pooling Layers<\/strong><\/h3>\n<p>We use pooling to reduce the size of the input and extract important information.<\/p>\n<ul>\n<li><strong>MaxPooling1D and MaxPooling2D<\/strong><\/li>\n<\/ul>\n<p>Extract maximum in the pooling window.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">MaxPooling1D(pool_size=2, strides=None, padding=\u2019valid\u2019, data_format=\u2019channels_last\u2019)\r\n\r\nMaxPooling2D(pool_size=(2,2), strides=None, padding=\u2019valid\u2019, data_format=\u2019channels_last\u2019)<\/pre>\n<ul>\n<li><strong>AveragePooling1D and AveragePooling2D<\/strong><\/li>\n<\/ul>\n<p>Get average from the pooling window.<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">AveragePooling1D(pool_size=2, strides=None, padding=\u2019valid\u2019, data_format=\u2019channels_last\u2019)\r\n\r\nAveragePooling1D(pool_size=(2,2), strides=None, padding=\u2019valid\u2019, data_format=None)<\/pre>\n<h3>4. Recurrent layer<\/h3>\n<p>We use this layer to compute sequence data, i.e time series or natural language.<\/p>\n<ul>\n<li><strong>SimpleRNN<\/strong><\/li>\n<\/ul>\n<p>This is fully connected RNN, where output of layer is fed back to the input<\/p>\n<p>Arguments:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">SimpleRNN(units, activation, use_bias, kernel_initializer, recurrent_initializer, bias_initializer, kernel_regularizer, recurrent_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, recurrent_constraint, bias_constraint, dropout, recurrent_dropout, return_sequences, return_state)<\/pre>\n<ul>\n<li><strong>LSTM<\/strong><\/li>\n<\/ul>\n<p>It is a big form of RNN, it has some storage to keep the information.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">LSTM(units, activation , recurrent_activation, use_bias, kernel_initializer, recurrent_initializer, bias_initializer, unit_forget_bias, kernel_regularizer, recurrent_regularizer, bias_regularizer, activity_regularizer, kernel_constraint, recurrent_constraint, bias_constraint, dropout, recurrent_dropout, implementation, return_sequences, return_state)<\/pre>\n<p>Keras provides many other layers, but in general, we work upon the layers described above.<\/p>\n<h2>Summary<\/h2>\n<p>This article explains the concept of layers in building Keras models. We learn about the basic attributes required to build a layer.<br \/>\nThen we discussed the different types of <strong>Keras layers i.e Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, its properties, and parameters.<\/strong><\/p>\n<p><strong>Any suggestion or changes are most welcomed in the comment section.<\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Layers are the primary unit to create neural networks. We compose a deep learning architecture by adding successive layers. Each successive layer performs some computation on the input it receives. Then after it propagates&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":77677,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22185],"tags":[22208,22209],"class_list":["post-77676","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras","tag-keras-layers","tag-layers-of-keras-tenorflow-python"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras layers - Parameters and Properties - DataFlair<\/title>\n<meta name=\"description\" content=\"Keras layers - Learn about various layers of Keras - Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, their properties, and parameters.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/keras-layers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras layers - Parameters and Properties - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Keras layers - Learn about various layers of Keras - Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, their properties, and parameters.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/keras-layers\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-29T04:30:17+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:26:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"802\" \/>\n\t<meta property=\"og:image:height\" content=\"420\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Keras layers - Parameters and Properties - DataFlair","description":"Keras layers - Learn about various layers of Keras - Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, their properties, and parameters.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/keras-layers\/","og_locale":"en_US","og_type":"article","og_title":"Keras layers - Parameters and Properties - DataFlair","og_description":"Keras layers - Learn about various layers of Keras - Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, their properties, and parameters.","og_url":"https:\/\/data-flair.training\/blogs\/keras-layers\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-04-29T04:30:17+00:00","article_modified_time":"2021-08-25T08:26:33+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Keras layers &#8211; Parameters and Properties","datePublished":"2020-04-29T04:30:17+00:00","dateModified":"2021-08-25T08:26:33+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/"},"wordCount":627,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg","keywords":["Keras layers","layers of keras Tenorflow python"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/keras-layers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/","url":"https:\/\/data-flair.training\/blogs\/keras-layers\/","name":"Keras layers - Parameters and Properties - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg","datePublished":"2020-04-29T04:30:17+00:00","dateModified":"2021-08-25T08:26:33+00:00","description":"Keras layers - Learn about various layers of Keras - Core Layers, Convolution Layers, Pooling Layers, Recurrent Layers, their properties, and parameters.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/keras-layers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/keras-layers.jpg","width":802,"height":420,"caption":"Keras Layers"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/keras-layers\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Keras Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/keras\/"},{"@type":"ListItem","position":3,"name":"Keras layers &#8211; Parameters and Properties"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/dd6de0d647a0185cd6faf264e4ba860b0d85d08d7070766f9cd41bea5bb0b227?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"The DataFlair Team is passionate about delivering top-notch tutorials and resources on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. With expertise in the tech industry, we simplify complex topics to help learners excel. Stay updated with our latest insights.","url":"https:\/\/data-flair.training\/blogs\/author\/dfadteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77676","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=77676"}],"version-history":[{"count":1,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77676\/revisions"}],"predecessor-version":[{"id":77678,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77676\/revisions\/77678"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/77677"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=77676"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=77676"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=77676"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}