{"id":79954,"date":"2020-10-05T09:00:33","date_gmt":"2020-10-05T03:30:33","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79954"},"modified":"2020-10-05T09:00:33","modified_gmt":"2020-10-05T03:30:33","slug":"keras-customized-layer","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/","title":{"rendered":"Keras Customized Layer"},"content":{"rendered":"<p>Keras is the most popular Python Library.\u00a0Keras provides you an environment to work with a model. The models contain various layers. The neural network is a combination of a number of layers. Each of these layers has an arrangement in order.<\/p>\n<p>These layers receive input, process it accordingly, and produce the desired output. This output is fed as the input to the next layer. Let us learn about Keras Customized Layer.<\/p>\n<h3>Keras Customized Layer<\/h3>\n<p>Keras also offers you an opportunity to design your own layer. This layer is known as Customized Layer. This is the most useful opportunity that Keras offers. Sometimes, the layer that Keras provides you do not satisfy your requirements.<\/p>\n<p>So, you have to build your own layer. Here, it allows you to apply the necessary algorithms for the input data.<\/p>\n<h3>Adding a Custom Layer in Keras<\/h3>\n<p>There are two ways to include the Custom Layer in the Keras. The methods are:<\/p>\n<ul>\n<li>Custom Class Layer<\/li>\n<li>Lambda Layer<\/li>\n<\/ul>\n<h4>Custom Class Layer<\/h4>\n<p>This method allows you to create a Custom Layer. To build the custom layer, this method provides you four methods:<\/p>\n<ul>\n<li><strong>_init_:<\/strong> This method helps you to initialize the class variables. It contains arguments that allow you to give the dimension of the output.<\/li>\n<li><strong>Build(input_shape):<\/strong> This function allows you to define the raining weights.<\/li>\n<li><strong>Call(x):<\/strong> The Call Method performs the exact working of the layer. It is done during the training process.<\/li>\n<li><strong>Compute_output_shape(input_shape):<\/strong> It helps you to compute the shape of the output.<\/li>\n<\/ul>\n<h3>Steps to create Custom Layers using Custom Class Layer Method<\/h3>\n<p>It is very easy to create a custom layer in Keras.<\/p>\n<h4>Step 1: Importing the useful modules<\/h4>\n<p>The very first is to import the necessary modules. You need to import the backend module and layer module. Here, both modules have a unique and specific task. The Backend Module helps you to access the dot function.<\/p>\n<p>The Layer Module enables you to create the layer. It is a base class. It allows you to sub-class the layers to design your own layer.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">To import these modules, refer the code below:\nfrom keras import backend as k\nfrom keras.layers import Layers\n<\/pre>\n<h4>Step 2: Definition of a layer<\/h4>\n<p>The layer is a class that is further sub-classed. Here, you need to create the layer and name it. For instance, take the name of the layer as TechVidvan.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">class TechVidvan (Layer):\n<\/pre>\n<p>Here, with the code above, you are creating a new layer TechVidvan by sub-classing the Layer Class.<\/p>\n<h4>Step 3: Initializing the Layer Class<\/h4>\n<p>Here, the output_dim enables you to set the dimension of the output. Further, in the next line, it calls the _init_ function of the base class.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def _init_(self, output_dim, **kwargs):\nself.output_dim = output_dim\nsuper(TechVidvan, self)._init_(**kwargs)\n<\/pre>\n<h4>Step 4: Implement a build method<\/h4>\n<p>Here, you have to use the Build Method. It is the main method that enables you to build the layer properly. It enables you to change the inner working of the layer. You can customize the functionalities. You can now call the base class Build function.<\/p>\n<p>Here, the function is defined with one argument &#8211; input_shape. It specifies the shape of the input. Further, you need to create the weight according to the input shape.<\/p>\n<p>And it is set in the kernel. Now, it creates it in normal initializer mode. Further, the base class Build Method is called.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def build(self, input shape):\nself.kernel = self.add_weight(name = \u2018kernel\u2019, shape = (input_shape[1], self.output_dim), initializer = \u2018normal\u2019, trainable = True)\nsuper(TechVidvan, self).build(input_shape)\n<\/pre>\n<h4>Step 5: Implementation of the class method<\/h4>\n<p>It is during the training process. The Call Method performs the exact working of the layer. To create a custom class method, follow the code below:<\/p>\n<p>Here, you need to define the call function passing one argument \u2013 input_data. This will be the input data for your custom layer. Further, you need to return the dot product of the input data.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def call(self, input_data):\nreturn K.dot(input_data, self.kernel)\n<\/pre>\n<h4>Step 6: Implementation of custom_output_shape method<\/h4>\n<p>Here, you need to define the custom_output_shape method while passing one argument input_shape to it. Now, you have to compute the shape of the output. This is using the shape of input data and the dimensions of the output.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">def compute _output_shape( self, input_shape): return (input_shape[0], self.output_dim)\n<\/pre>\n<p>Once you implement the <strong>Build Method, Call Method,<\/strong> and <strong>comput_output_shape Method,<\/strong> it completes the creation of a custom layer.<\/p>\n<h3>Summary<\/h3>\n<p>Keras is a proficient library that provides you a user-friendly environment. It focuses on the idea of models. It is the stack of layers. Sometimes, the layers that Keras provides do not satisfy all your requirements.<\/p>\n<p>So, you need to create a layer of your choice. Keras allows you to build layers according to your requirements. This concept is the Keras Customized Layer, where you can design the functionality of the layer on your own.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keras is the most popular Python Library.\u00a0Keras provides you an environment to work with a model. The models contain various layers. The neural network is a combination of a number of layers. Each of&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79995,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3263],"tags":[3287,3288],"class_list":["post-79954","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras-tutorials","tag-keras-custom-layer","tag-keras-customized-layer"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras Customized Layer - TechVidvan<\/title>\n<meta name=\"description\" content=\"Keras Customized Layer - 1. Importing the useful modules 2. Definition of a layer 3. Initializing the Layer Class 4. Implement a build method\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras Customized Layer - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Keras Customized Layer - 1. Importing the useful modules 2. Definition of a layer 3. Initializing the Layer Class 4. Implement a build method\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/\" \/>\n<meta property=\"og:site_name\" content=\"TechVidvan\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/TechVidvan\/\" \/>\n<meta property=\"article:published_time\" content=\"2020-10-05T03:30:33+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.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=\"TechVidvan Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:site\" content=\"@vidvantech\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"TechVidvan 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 Customized Layer - TechVidvan","description":"Keras Customized Layer - 1. Importing the useful modules 2. Definition of a layer 3. Initializing the Layer Class 4. Implement a build method","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:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/","og_locale":"en_US","og_type":"article","og_title":"Keras Customized Layer - TechVidvan","og_description":"Keras Customized Layer - 1. Importing the useful modules 2. Definition of a layer 3. Initializing the Layer Class 4. Implement a build method","og_url":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-10-05T03:30:33+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.jpg","type":"image\/jpeg"}],"author":"TechVidvan Team","twitter_card":"summary_large_image","twitter_creator":"@vidvantech","twitter_site":"@vidvantech","twitter_misc":{"Written by":"TechVidvan Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Keras Customized Layer","datePublished":"2020-10-05T03:30:33+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/"},"wordCount":746,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.jpg","keywords":["keras custom layer","keras customized layer"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/","url":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/","name":"Keras Customized Layer - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.jpg","datePublished":"2020-10-05T03:30:33+00:00","description":"Keras Customized Layer - 1. Importing the useful modules 2. Definition of a layer 3. Initializing the Layer Class 4. Implement a build method","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Customized-Layers-of-Keras.jpg","width":802,"height":420,"caption":"Customized Layers of Keras"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/keras-customized-layer\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Keras Customized Layer"}]},{"@type":"WebSite","@id":"https:\/\/techvidvan.com\/tutorials\/#website","url":"https:\/\/techvidvan.com\/tutorials\/","name":"TechVidvan Blogs","description":"","publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/techvidvan.com\/tutorials\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/techvidvan.com\/tutorials\/#organization","name":"TechVidvan","url":"https:\/\/techvidvan.com\/tutorials\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2024\/03\/techvidvan-logo-200x50-1.webp","width":200,"height":50,"caption":"TechVidvan"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/TechVidvan\/","https:\/\/x.com\/vidvantech"]},{"@type":"Person","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22","name":"TechVidvan Team","description":"The TechVidvan Team delivers practical, beginner-friendly tutorials on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our experts are here to help you upskill and excel in today\u2019s tech industry."}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79954","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/comments?post=79954"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79954\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79995"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79954"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79954"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79954"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}