{"id":79955,"date":"2020-10-06T09:00:28","date_gmt":"2020-10-06T03:30:28","guid":{"rendered":"https:\/\/techvidvan.com\/tutorials\/?p=79955"},"modified":"2020-10-06T09:00:28","modified_gmt":"2020-10-06T03:30:28","slug":"keras-models","status":"publish","type":"post","link":"https:\/\/techvidvan.com\/tutorials\/keras-models\/","title":{"rendered":"Keras Models &#8211; Sequential and Functional Model of Keras"},"content":{"rendered":"<p>Keras is one of the most popular Python libraries for Deep Learning. It is a library that provides you various tools to deal with neural network models. These tools enable you to visualize and understand your model.<\/p>\n<p>Keras is a simple and user-friendly library that focuses on the idea of Models. Let us learn more about Keras Models.<\/p>\n<h3>Keras Models<\/h3>\n<p>These represent the actual neural network model. These models group layers into objects. There are two types of Models available in Keras: The Sequential model and the Functional model.<\/p>\n<h3>Keras Sequential Model<\/h3>\n<p>It is simple and easy to use model. It is a linear stack of methods that groups a linear stack of layers into a tf.keras.Model. According to its name, its main task is to arrange the layers of the Keras in sequential order.<\/p>\n<p>In this model, the data flow from one layer to another layer. The flow of data is continued until the data reaches the final layer. Most of the ANN\u2019s use the Sequential API Model.<\/p>\n<h4>Create a Sequential Model<\/h4>\n<p>You can create a Sequential model by calling the Sequential() method. You have to pass a list of instances to the constructor.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from keras.models import Sequential\nmodel = Sequential()\n<\/pre>\n<h4>Adding a Layer in the Sequential Model<\/h4>\n<p>Adding a layer in the Sequential Model is very easy. You can add any layer easily by the add() method. To use add() method, you initially need to create a layer using API then pass the layer through the add() method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">model = Sequential()\nmodel.add(Dense(32, input_dim=784))\nmodel.add(Activation(\u2019relu\u2019))\n<\/pre>\n<h4>Specifying the Input Shape<\/h4>\n<p>It is necessary to specify the shape of the input to the layer. You need to specify the shape of the first layer only. Because the output of the first layer becomes the input of the sun-sequent layers automatically.<\/p>\n<p>1. To specify the input shape to the layer, there is a number of methods available. You can use input_shape argument. You can pass this argument to the very first layer.<\/p>\n<p>2. For 2D layers like Dense, there is input_dim argument. 3D layers have input_dim and input_lenght to specify the input shape of the model.<\/p>\n<h4>Accessing and Serializing the Keras Model<\/h4>\n<p>To access the model, Keras provides a ton of methods. These methods help you to get the model information.<\/p>\n<p><strong>1. model.layers():<\/strong><br \/>\nThis method returns all the layers of the model as a list. It informs you about the layer that comprises a model.<\/p>\n<p><strong>2. model.inputs():<\/strong><br \/>\nIt is the list of the input tensors of the model. It provides you the list of all the input tensors in a model.<\/p>\n<p><strong>3. model.outputs():<\/strong><br \/>\nThis method is useful in returning the list of all the output tensors of a model.<\/p>\n<p><strong>4. model.summary():<\/strong><br \/>\nIt helps you to create a summary of a model. It provides you a summary of full information about the model and the layer.<\/p>\n<p><strong>5. model.to_yaml():<\/strong><br \/>\nIt returns the model as a YAML string.<\/p>\n<p><strong>6. model.get_weights():<\/strong><br \/>\nIt provides you a list of all the weights tensor as a NumPy array.<\/p>\n<p><strong>7. model.get_config():<\/strong><br \/>\nIt provides the configuration of the model in the form of the dictionary.<\/p>\n<p><strong>8. model.to_json():<\/strong><br \/>\nIt returns the representation of the model. This representation is in the form of a JSON string.<\/p>\n<p><strong>9. model.from_json():<\/strong><br \/>\nIt accepts the representation of the model in the form of a JSON string. It further helps you to create a new model.<\/p>\n<p><strong>10. model.from_config():<\/strong><br \/>\nIt accepts the configuration of the model. It further helps you to create a new model according to the information it accepts.<\/p>\n<h4>Compiling the Keras Model<\/h4>\n<p>The compilation is the process that is to be done before training the model. It configures the learning process of the model. You need to configure the model. To compile a model, you can use the compile() method.<\/p>\n<p>To use the compile method, you must know about its arguments:<\/p>\n<p><strong>1. Loss:<\/strong><br \/>\nIt is an objective function. It helps you to optimize the score of a function.<\/p>\n<p><strong>2. Optimizer:<\/strong><br \/>\nIt contains built-in optimizer classes.<\/p>\n<p><strong>3. Metrics:<\/strong><br \/>\nIt provides you a list of built-in metrics functions.<\/p>\n<h4>Training and Predicting the Keras Model<\/h4>\n<p>Training of models is done after the compilation of the model and is done on NumPy array. The array consists of input data and labels.<\/p>\n<ul>\n<li>To train your model, the <strong>fit()<\/strong> method is useful. This enables you to train the model using the training data.<\/li>\n<li>To predict the model,<strong> predict()<\/strong> method suits them best. It helps you to predict the results for the new input.<\/li>\n<\/ul>\n<h3>Keras Functional Model<\/h3>\n<p>It is a more flexible API and is more advanced in nature. It is an alternative model against the Sequential Model that enables you to create models in a more complex manner. This model helps you to define multiple numbers of input and output.<\/p>\n<p>These models are sharing layers. This model helps you define directed cyclic graphs, multi-layer output, sharing of the layer, etc.<\/p>\n<p>To perform tasks using this model, you initially need to create an instance of the model. Further, connect the layers with this model to access the input and the output of the model.<\/p>\n<p>Functional Model API enables you to reuse the trained model. Calling the model on the tensor, you can use the model as the layer. Functional Model API enables you to work with a large number of branched networks.<\/p>\n<ul>\n<li>To create a new model in the Functional Model API, you can use the input() method.<\/li>\n<li>After creating a new model, you need to create an input layer. This will help you to specify the input dimension shapes.<\/li>\n<li>Now, import the dense model.<\/li>\n<li>After importing the dense model, create a dense layer for the input. Now, define the model using the import module.<\/li>\n<li>Now, specify the input and the output using the Functional API.<\/li>\n<\/ul>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">from keras.layers import Input\ndata = input(shape(2,3))\nfrom keras.layers import Dense\nlayer = Dense(2)(data)\nprint(layer)\nfrom keras.models import Model\nmodel = Model(inputs=data, outputs=layer)\n<\/pre>\n<h3>Conclusion<\/h3>\n<p>Keras is the best platform to deal with a neural network. It works on the top of TensorFlow and Theano. It focuses on the idea of Model. Keras provides two types of models: The Sequential Model and The Functional Model.<\/p>\n<p>The Sequential Model is simple. It deals with non-complex models and works with a single layer. It is an easy to use model. The Functional Model is a complex model. It enables you to work with multiple layers. It works with the sharing of the layers.<\/p>\n<p>These Keras Models are very useful. These enable you to create a model of your own. These are very simple and easy to work with.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Keras is one of the most popular Python libraries for Deep Learning. It is a library that provides you various tools to deal with neural network models. These tools enable you to visualize and&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":79997,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3263],"tags":[3289,3290,3291],"class_list":["post-79955","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras-tutorials","tag-keras-functional-model","tag-keras-models","tag-keras-sequential-model"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Keras Models - Sequential and Functional Model of Keras - TechVidvan<\/title>\n<meta name=\"description\" content=\"Keras Models and its types - Sequential model and Functional Model. Leran more about Keras models, Accessing, Serializing and compiling them.\" \/>\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-models\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Keras Models - Sequential and Functional Model of Keras - TechVidvan\" \/>\n<meta property=\"og:description\" content=\"Keras Models and its types - Sequential model and Functional Model. Leran more about Keras models, Accessing, Serializing and compiling them.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/techvidvan.com\/tutorials\/keras-models\/\" \/>\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-06T03:30:28+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.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=\"5 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Keras Models - Sequential and Functional Model of Keras - TechVidvan","description":"Keras Models and its types - Sequential model and Functional Model. Leran more about Keras models, Accessing, Serializing and compiling them.","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-models\/","og_locale":"en_US","og_type":"article","og_title":"Keras Models - Sequential and Functional Model of Keras - TechVidvan","og_description":"Keras Models and its types - Sequential model and Functional Model. Leran more about Keras models, Accessing, Serializing and compiling them.","og_url":"https:\/\/techvidvan.com\/tutorials\/keras-models\/","og_site_name":"TechVidvan","article_publisher":"https:\/\/www.facebook.com\/TechVidvan\/","article_published_time":"2020-10-06T03:30:28+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.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":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#article","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/"},"author":{"name":"TechVidvan Team","@id":"https:\/\/techvidvan.com\/tutorials\/#\/schema\/person\/e9c26e74dd3d87421f7ada9433b8cd22"},"headline":"Keras Models &#8211; Sequential and Functional Model of Keras","datePublished":"2020-10-06T03:30:28+00:00","mainEntityOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/"},"wordCount":1069,"commentCount":0,"publisher":{"@id":"https:\/\/techvidvan.com\/tutorials\/#organization"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.jpg","keywords":["keras functional model","keras models","keras sequential model"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/techvidvan.com\/tutorials\/keras-models\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/","url":"https:\/\/techvidvan.com\/tutorials\/keras-models\/","name":"Keras Models - Sequential and Functional Model of Keras - TechVidvan","isPartOf":{"@id":"https:\/\/techvidvan.com\/tutorials\/#website"},"primaryImageOfPage":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#primaryimage"},"image":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#primaryimage"},"thumbnailUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.jpg","datePublished":"2020-10-06T03:30:28+00:00","description":"Keras Models and its types - Sequential model and Functional Model. Leran more about Keras models, Accessing, Serializing and compiling them.","breadcrumb":{"@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/techvidvan.com\/tutorials\/keras-models\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#primaryimage","url":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.jpg","contentUrl":"https:\/\/techvidvan.com\/tutorials\/wp-content\/uploads\/2020\/09\/Important-Keras-Models.jpg","width":802,"height":420,"caption":"Important Keras Models"},{"@type":"BreadcrumbList","@id":"https:\/\/techvidvan.com\/tutorials\/keras-models\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/techvidvan.com\/tutorials\/"},{"@type":"ListItem","position":2,"name":"Keras Models &#8211; Sequential and Functional Model of Keras"}]},{"@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\/79955","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=79955"}],"version-history":[{"count":0,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/posts\/79955\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media\/79997"}],"wp:attachment":[{"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/media?parent=79955"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/categories?post=79955"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/techvidvan.com\/tutorials\/wp-json\/wp\/v2\/tags?post=79955"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}