

{"id":77679,"date":"2020-04-30T10:00:04","date_gmt":"2020-04-30T04:30:04","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=77679"},"modified":"2021-08-25T13:56:29","modified_gmt":"2021-08-25T08:26:29","slug":"keras-custom-layers","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/","title":{"rendered":"Keras Custom Layers &#8211; Lambda Layer and Custom Class Layer"},"content":{"rendered":"<p>Here we are back with another interesting Keras tutorial which will teach you about <strong>Keras Custom Layers.<\/strong><\/p>\n<p>A Neural Network is a stack of layers. Each layer receives some input, makes computation on this input and propagates the output to the next layer. Though there are many in-built layers in Keras for different use cases, Keras Layers like Conv2D, MaxPooling2D, Dense, Flatten have different applications and we use them according to our requirements. But sometimes we may want to perform computations other than what these Keras Layers do.<\/p>\n<p>Therefore we have to build our own layer and define our own algorithm for computation on input data. Keras provides this feature to write our own Custom Layers. In this article we will study the concept of Custom Layers and we will see some examples to build our own custom layer.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-77680\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg\" alt=\"Keras Custom Layers\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Keras Custom Layers<\/h2>\n<p>We add custom layers in Keras in the following two ways:<\/p>\n<ul>\n<li>Lambda Layer<\/li>\n<li>Custom class layer<\/li>\n<\/ul>\n<p>Let us discuss each of these now.<\/p>\n<h3>1. Lambda layer in Keras<\/h3>\n<p>We use Keras lambda layers when we do not want to add trainable weights to the previous layer. Here we customize a layer for simple operations. Its implementation is similar to that of lambda functions.<\/p>\n<p>First we define a function which takes the previous layer as input, apply computations to it and then return update tensors. Then we pass this function to our custom lambda layer.<\/p>\n<p>Most common application of the lambda layer is to <strong>define our own activation function.<\/strong><\/p>\n<p>Let&#8217;s say we want to define our own <strong><a href=\"https:\/\/en.wikipedia.org\/wiki\/Rectifier_(neural_networks)\">RELU activation function<\/a> using a lambda layer.<\/strong><\/p>\n<p>Then,<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras.layer import Lambda\r\nfrom keras import backend as K\r\n\r\ndef custom_function(input):\r\n  return K.maximum(0.,input)\r\n\r\nlambda_output= Lambda(custom_function)(input)<\/pre>\n<p>and that&#8217;s it, we have built our lambda layer.<\/p>\n<p>After that, we will add this layer to our model the same way we add other layers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">model.add(lambda_output)<\/pre>\n<h3>2. Custom Class Layer in keras<\/h3>\n<p>Here we create our own layer with trainable weights. To build the layer we need to implement the following four methods:<\/p>\n<ul>\n<li><strong>__init__<\/strong><br \/>\nHere we initialize class variable and super class variable.<\/li>\n<li><strong>build(input_shape)<\/strong><br \/>\nHere we define our trainable weights.<\/li>\n<li><strong>call(x)<\/strong><br \/>\nHere we define the algorithm for computation.It takes tensor as input argument.<\/li>\n<li><strong>compute_output_shape(input_shape)<\/strong><br \/>\nHere we define the output shape of our custom layer.<\/li>\n<\/ul>\n<p>Let&#8217;s see the implementation now:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras import backend as K\r\nfrom keras.layers import Layer\r\n\r\nclass custom_layer(Layer):\r\n  def  __init__(self,output_dim,**kwargs):\r\n    self.output_dim=output_dim\r\n    super(custom_layer,self).__init__(**kwargs)\r\n  def build(self,input_shape):\r\n    self.W=self.add_weight(name=\u2019kernel\u2019,\r\n                           shape=(input_shape[1],\r\n                           self.output_dim),\r\n                           initializer=\u2019uniform\u2019,\r\n                           trainable=True)\r\n    self.built = True\r\n\r\n  # this self.built is necessary .\r\n\r\n  def call(self,x):\r\n    return K.dot(x,self.W)\r\n\r\n  def compute_output_shape(self,input_shape):\r\n    return (input_shape[0], self.output_dim)<\/pre>\n<p>Here we are using only a single tensor, but we can pass multiple input tensor as a list.<\/p>\n<h2>Summary<\/h2>\n<p>This article explains the concept of writing our own Keras custom layers and why we need them. There are two ways to write custom layers, <strong>Lambda Layers and Custom Class Layers.<\/strong><\/p>\n<p>We use Lambda Layers for simple customization and we use Custom Class Layers when we want to apply trainable weights on the input.<\/p>\n<p><strong>Keep checking our further articles to get exciting Keras projects.<\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1146,&quot;href&quot;:&quot;https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Rectifier_(neural_networks)&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250918103324\\\/https:\\\/\\\/en.wikipedia.org\\\/wiki\\\/Rectifier_(neural_networks)&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 01:55:39&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2025-12-28 22:57:28&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-01-02 16:50:33&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-02-12 06:30:27&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-06-09 20:24:35&quot;,&quot;http_code&quot;:404},{&quot;date&quot;:&quot;2026-06-21 21:43:44&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-01 13:53:14&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-05 19:48:17&quot;,&quot;http_code&quot;:200},{&quot;date&quot;:&quot;2026-07-13 02:06:22&quot;,&quot;http_code&quot;:200}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-07-13 02:06:22&quot;,&quot;http_code&quot;:200},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here we are back with another interesting Keras tutorial which will teach you about Keras Custom Layers. A Neural Network is a stack of layers. Each layer receives some input, makes computation on this&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":77680,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22185],"tags":[22215,22210,22211,22213,22212,22214],"class_list":["post-77679","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras","tag-keras-custom-layer","tag-keras-custom-layers","tag-keras-lambda-layer","tag-keras-layers-lambda","tag-lambda-layer-keras","tag-lambda-layer-keras-example"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras Custom Layers - Lambda Layer and Custom Class Layer - DataFlair<\/title>\n<meta name=\"description\" content=\"Leran how to customize layers in keras - Keras Custom layers using two methods - Lambda layers and Custom class layer. learn their implementation &amp; example\" \/>\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-custom-layers\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras Custom Layers - Lambda Layer and Custom Class Layer - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Leran how to customize layers in keras - Keras Custom layers using two methods - Lambda layers and Custom class layer. learn their implementation &amp; example\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/keras-custom-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-30T04:30:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:26:29+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Keras Custom Layers - Lambda Layer and Custom Class Layer - DataFlair","description":"Leran how to customize layers in keras - Keras Custom layers using two methods - Lambda layers and Custom class layer. learn their implementation & example","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-custom-layers\/","og_locale":"en_US","og_type":"article","og_title":"Keras Custom Layers - Lambda Layer and Custom Class Layer - DataFlair","og_description":"Leran how to customize layers in keras - Keras Custom layers using two methods - Lambda layers and Custom class layer. learn their implementation & example","og_url":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-04-30T04:30:04+00:00","article_modified_time":"2021-08-25T08:26:29+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Keras Custom Layers &#8211; Lambda Layer and Custom Class Layer","datePublished":"2020-04-30T04:30:04+00:00","dateModified":"2021-08-25T08:26:29+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/"},"wordCount":469,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg","keywords":["keras custom layer","Keras custom layers","keras lambda layer","keras layers lambda","lambda layer keras","lambda layer keras example"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/","url":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/","name":"Keras Custom Layers - Lambda Layer and Custom Class Layer - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg","datePublished":"2020-04-30T04:30:04+00:00","dateModified":"2021-08-25T08:26:29+00:00","description":"Leran how to customize layers in keras - Keras Custom layers using two methods - Lambda layers and Custom class layer. learn their implementation & example","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/keras-custom-layers\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/keras-custom-layers\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/04\/Keras-Customized-Layers.jpg","width":802,"height":420,"caption":"Keras Custom Layers"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/keras-custom-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 Custom Layers &#8211; Lambda Layer and Custom Class Layer"}]},{"@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\/77679","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=77679"}],"version-history":[{"count":3,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77679\/revisions"}],"predecessor-version":[{"id":77703,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/77679\/revisions\/77703"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/77680"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=77679"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=77679"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=77679"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}