

{"id":78461,"date":"2020-06-11T09:00:38","date_gmt":"2020-06-11T03:30:38","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=78461"},"modified":"2021-08-25T13:54:52","modified_gmt":"2021-08-25T08:24:52","slug":"keras-models","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/keras-models\/","title":{"rendered":"Keras Models &#8211; Types and Examples"},"content":{"rendered":"<p>A model is the basic data structure of Keras. Keras models define how to organize layers. In this article, we will discuss Keras Models and its two types with examples. We will also learn about Model subclassing through which we can create our own fully-customizable models.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78468\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg\" alt=\"Keras Models\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df-1024x536.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df-520x272.jpg 520w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><\/p>\n<h2>Types of Keras Models<\/h2>\n<p>Models in keras are available in two types:<\/p>\n<ul>\n<li>Keras Sequential Model<\/li>\n<li>Keras Functional API<\/li>\n<\/ul>\n<h3>1. Sequential Model in Keras<\/h3>\n<p>It allows us to create models layer by layer in sequential order. But it does not allow us to create models that have multiple inputs or outputs. It is best for simple stack of layers which have 1 input tensor and 1 output tensor.<\/p>\n<p>This model is not suited when any of the layer in the stack has multiple inputs or outputs. Even if we want non-linear topology, it is not suited.<\/p>\n<p>Here is an example for Sequential model:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras.models import Sequential\r\nfrom keras.layers import Dense\r\n\r\nmodel=Sequential()\r\nmodel.add(Dense(64,input_shape=8,))\r\nmode.add(Dense(32))<\/pre>\n<h3>2. Functional API in Keras<\/h3>\n<p>It provides more flexibility to define a model and add <a href=\"https:\/\/data-flair.training\/blogs\/keras-layers\/\">layers in keras<\/a>. Functional API allows us to create models that have multiple input or output. It also allows us to share these layers. In other words. we can make graphs of layers using Keras functional API.<\/p>\n<p>As functional API is a data structure, it is easy to save it as a single file that helps in recreating the exact model without having the original code. Also its easy to model the graph here and access its nodes as well.<\/p>\n<p>Below is the Example for Functional API:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras.models import Model\r\nfrom keras.layers import Input, Dense\r\n\r\ninput=Input(shape=(32,))\r\nlayer=Dense(32)(input)\r\nmodel=Model(inputs=input,outputs=layer)\r\n\r\n\/\/To create model with multiple inputs and outputs:\r\n\r\nmodel=Model(inputs=[input1,input2],outputs=[layer1,layer2,layer3])<\/pre>\n<h2>Model Subclassing in Keras<\/h2>\n<p>Sequential model does not allow you much flexibility to create your models. Functional API also only has a little of customization available for you. But you may create your own fully-customizable models in Keras. This is done by subclassing the Model class and implementing a call method.<\/p>\n<p>For example:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras.layers import Dense,Dropout,BatchNormalization\r\n\r\nClass Subclass(keras.Model):\r\n\r\n    def __init__(self, use_bn=False, use_dp=False, num_classes=10):\r\n        super(Subclass, self).__init__(name='mlp')\r\n        self.use_bn = use_bn\r\n        self.use_dp = use_dp\r\n        self.num_classes = num_classes\r\n\r\n        self.dense_1 = Dense(32, activation='relu')\r\n        self.dense_2 = Dense(num_classes, activation='softmax')\r\n        if self.use_dp:\r\n            self.dp = Dropout(0.5)\r\n        if self.use_bn:\r\n            self.bn = BatchNormalization(axis=-1)\r\n\r\n    def call(self, inputs):\r\n        x = self.dense_1(inputs)\r\n        if self.use_dp:\r\n            x = self.dp(x)\r\n        if self.use_bn:\r\n            x = self.bn(x)\r\n        return self.dense2(x)\r\n\r\nmodel =Subclass()<\/pre>\n<h2>Summary<\/h2>\n<p>In conclusion, this article explains about Keras Models and how to define your own model in Keras. We saw its two types that are Sequential and Functional API. We also discussed the flexibility above Models provide. After this, we saw how to create our own fully-customizable Model using Model Subclassing.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A model is the basic data structure of Keras. Keras models define how to organize layers. In this article, we will discuss Keras Models and its two types with examples. We will also learn&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":78468,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22185],"tags":[22411,22409,22412,22404,22410],"class_list":["post-78461","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras","tag-functional-api-in-keras","tag-keras-models","tag-model-subclassing-in-keras","tag-models-in-keras","tag-sequential-model-in-keras"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras Models - Types and Examples - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn what are Keras Models - Its definition, types and examples. Types of models in keras - Sequential and Functional API and Model Subclassing in keras.\" \/>\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-models\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras Models - Types and Examples - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn what are Keras Models - Its definition, types and examples. Types of models in keras - Sequential and Functional API and Model Subclassing in keras.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/keras-models\/\" \/>\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-06-11T03:30:38+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:24:52+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\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 Models - Types and Examples - DataFlair","description":"Learn what are Keras Models - Its definition, types and examples. Types of models in keras - Sequential and Functional API and Model Subclassing in keras.","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-models\/","og_locale":"en_US","og_type":"article","og_title":"Keras Models - Types and Examples - DataFlair","og_description":"Learn what are Keras Models - Its definition, types and examples. Types of models in keras - Sequential and Functional API and Model Subclassing in keras.","og_url":"https:\/\/data-flair.training\/blogs\/keras-models\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-06-11T03:30:38+00:00","article_modified_time":"2021-08-25T08:24:52+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.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-models\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Keras Models &#8211; Types and Examples","datePublished":"2020-06-11T03:30:38+00:00","dateModified":"2021-08-25T08:24:52+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/"},"wordCount":356,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg","keywords":["functional api in keras","Keras Models","model subclassing in keras","Models in Keras","sequential model in keras"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/keras-models\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/keras-models\/","url":"https:\/\/data-flair.training\/blogs\/keras-models\/","name":"Keras Models - Types and Examples - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg","datePublished":"2020-06-11T03:30:38+00:00","dateModified":"2021-08-25T08:24:52+00:00","description":"Learn what are Keras Models - Its definition, types and examples. Types of models in keras - Sequential and Functional API and Model Subclassing in keras.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/keras-models\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Models-df.jpg","width":1200,"height":628,"caption":"Keras Models"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/keras-models\/#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 Models &#8211; Types and Examples"}]},{"@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\/78461","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=78461"}],"version-history":[{"count":4,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78461\/revisions"}],"predecessor-version":[{"id":86526,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78461\/revisions\/86526"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/78468"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=78461"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=78461"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=78461"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}