

{"id":78460,"date":"2020-06-10T15:36:05","date_gmt":"2020-06-10T10:06:05","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=78460"},"modified":"2021-08-25T13:54:53","modified_gmt":"2021-08-25T08:24:53","slug":"compile-evaluate-predict-model-in-keras","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/","title":{"rendered":"Compile, Evaluate and Predict Model in Keras"},"content":{"rendered":"<p>Welcome to <strong>DataFlair Keras Tutorial series.<\/strong> This chapter explains how to compile, evaluate and make predictions from <strong>Model in Keras<\/strong>.<\/p>\n<p><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-78463\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg\" alt=\"Model in keras\" width=\"802\" height=\"420\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg 802w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction-520x272.jpg 520w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><\/p>\n<h2>Keras Compile Models<\/h2>\n<p>After defining our model and stacking the layers, we have to configure our model. We do this configuration process in the compilation phase.<br \/>\nBefore training the model we need to compile it and define the loss function, optimizers, and metrics for prediction.<\/p>\n<p>We compile the model using <strong>.compile()<\/strong> method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">model.compile ( optimizer, loss, metrics, loss_weights, sample_weight_mode, weighted_metrics, target_tensors)<\/pre>\n<p>Optimizer, loss, and metrics are the necessary arguments.<\/p>\n<p>Keras provides various loss functions, optimizers, and metrics for the compilation phase.<\/p>\n<h3>Loss Function in Keras<\/h3>\n<p>These are available in the losses module and is one of the two arguments required for compiling a Keras model.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras import losses<\/pre>\n<p>Below are the various available loss functions.<\/p>\n<ul>\n<li>mean_squared_error<\/li>\n<li>mean_absolute _error<\/li>\n<li>hinge<\/li>\n<li>mean_absolute_percentage _error<\/li>\n<li>mean_squared_logarithmic_error<\/li>\n<li>Poisson<\/li>\n<li>binary_crossentropy<\/li>\n<li>categorical_crossentropy<\/li>\n<li>and there are also some more.<\/li>\n<\/ul>\n<h2>Keras Model Optimization<\/h2>\n<p>These are very important since we use optimizers to adjust input weights. We optimize input weights by comparing <strong>prediction<\/strong> and the loss function. These are available in the optimizer module.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras import optimizers<\/pre>\n<p>Below are the various available optimizers:<\/p>\n<ul>\n<li>SGD ( Stochastic Gradient Descent )<\/li>\n<li>RMSprop<\/li>\n<li>Adagrad<\/li>\n<li>Adam<\/li>\n<li>Adamax<\/li>\n<li>Nadam<\/li>\n<\/ul>\n<p>We need to specify the learning rate for the following optimizers.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">keras.optimizers.Adam(learning_rate=0.001)<\/pre>\n<h3>Keras Metrics<\/h3>\n<p>This specifies the evaluation criteria for the model. These are present in the <a href=\"https:\/\/keras.io\/api\/metrics\/\">Keras metrics<\/a> module. We import it as below:<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">from keras import metrics<\/pre>\n<p>Below are the various available metrics in Keras.<\/p>\n<ul>\n<li>accuracy<\/li>\n<li>binary_accuracy<\/li>\n<li>categorical_accuracy<\/li>\n<li>cosine_proximity<\/li>\n<li>clone_metric<\/li>\n<\/ul>\n<h2>Keras Model Evaluation<\/h2>\n<p>In this phase, we model, whether it is the best to fit for the unseen data or not. For this, Keras provides <strong>.evaluate()<\/strong> method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">model.evaluate(X_test,Y_test, verbose)<\/pre>\n<p>As you can observe, it takes three arguments, <b>Test data, Train data and verbose {true or false}<\/b><\/p>\n<p>.evaluate() method returns a score which is used to measure the performance of our model.<\/p>\n<h2>Keras Model Prediction<\/h2>\n<p>When we get satisfying results from the evaluation phase, then we are ready to make predictions from our model. This is the final phase of the model generation. For this Keras provides <strong>.predict()<\/strong> method.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">model.predict( X_test, batch_size, verbose, steps, callbacks, max_queue_size, workers, use_multiprocessing)<\/pre>\n<p>Where X_test is the necessary parameter.<\/p>\n<h2>Summary<\/h2>\n<p>This article explains the compilation, evaluation and prediction phase of model in Keras. After adding all the layers to our model, we need to define the loss function, optimizers and metrics to train our model. We define these in the compilation phase. After compilation we evaluate our model on unseen data to test the performance. Finally we take output from the model. This article also explains about the various arguments and their uses in these three phases.<\/p>\n<p><strong>Do share your feedback in the comment section if you liked the article.<\/strong><span hidden class=\"__iawmlf-post-loop-links\" data-iawmlf-links=\"[{&quot;id&quot;:1079,&quot;href&quot;:&quot;https:\\\/\\\/keras.io\\\/api\\\/metrics&quot;,&quot;archived_href&quot;:&quot;http:\\\/\\\/web-wp.archive.org\\\/web\\\/20250718074602\\\/https:\\\/\\\/keras.io\\\/api\\\/metrics\\\/&quot;,&quot;redirect_href&quot;:&quot;&quot;,&quot;checks&quot;:[{&quot;date&quot;:&quot;2025-12-09 00:41:57&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-15 12:14:53&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2025-12-18 13:54:11&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-06 14:25:50&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-12 03:31:23&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-01-18 07:57:21&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-03 16:00:48&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-07 13:21:09&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-02-26 03:35:40&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-04 02:43:27&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-03-16 01:40:55&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-05-27 05:53:43&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-18 15:43:22&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-06-28 07:50:17&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-01 18:29:20&quot;,&quot;http_code&quot;:206},{&quot;date&quot;:&quot;2026-07-12 05:03:22&quot;,&quot;http_code&quot;:206}],&quot;broken&quot;:false,&quot;last_checked&quot;:{&quot;date&quot;:&quot;2026-07-12 05:03:22&quot;,&quot;http_code&quot;:206},&quot;process&quot;:&quot;done&quot;}]\"><\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Welcome to DataFlair Keras Tutorial series. This chapter explains how to compile, evaluate and make predictions from Model in Keras. Keras Compile Models After defining our model and stacking the layers, we have to&#46;&#46;&#46;<\/p>\n","protected":false},"author":10,"featured_media":78463,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22185],"tags":[22406,22408,22407,22405,22404],"class_list":["post-78460","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-keras","tag-keras-compile","tag-keras-evaluate","tag-keras-predict","tag-model-in-keras","tag-models-in-keras"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Compile, Evaluate and Predict Model in Keras - DataFlair<\/title>\n<meta name=\"description\" content=\"Learn How to compile, evaluate and predict Model in Keras, various methods and their arguments, keras loss functions, optimizers and metrics.\" \/>\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\/compile-evaluate-predict-model-in-keras\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Compile, Evaluate and Predict Model in Keras - DataFlair\" \/>\n<meta property=\"og:description\" content=\"Learn How to compile, evaluate and predict Model in Keras, various methods and their arguments, keras loss functions, optimizers and metrics.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/\" \/>\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-10T10:06:05+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-08-25T08:24:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.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=\"2 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Compile, Evaluate and Predict Model in Keras - DataFlair","description":"Learn How to compile, evaluate and predict Model in Keras, various methods and their arguments, keras loss functions, optimizers and metrics.","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\/compile-evaluate-predict-model-in-keras\/","og_locale":"en_US","og_type":"article","og_title":"Compile, Evaluate and Predict Model in Keras - DataFlair","og_description":"Learn How to compile, evaluate and predict Model in Keras, various methods and their arguments, keras loss functions, optimizers and metrics.","og_url":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2020-06-10T10:06:05+00:00","article_modified_time":"2021-08-25T08:24:53+00:00","og_image":[{"width":802,"height":420,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/a90b082e16aa38d207212d22b0581f33"},"headline":"Compile, Evaluate and Predict Model in Keras","datePublished":"2020-06-10T10:06:05+00:00","dateModified":"2021-08-25T08:24:53+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/"},"wordCount":449,"commentCount":3,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg","keywords":["keras compile","keras evaluate","keras predict","model in keras","Models in Keras"],"articleSection":["Keras Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/","url":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/","name":"Compile, Evaluate and Predict Model in Keras - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg","datePublished":"2020-06-10T10:06:05+00:00","dateModified":"2021-08-25T08:24:53+00:00","description":"Learn How to compile, evaluate and predict Model in Keras, various methods and their arguments, keras loss functions, optimizers and metrics.","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2020\/06\/Keras-Model-Compilation-evaluation-prediction.jpg","width":802,"height":420,"caption":"Model in keras"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/compile-evaluate-predict-model-in-keras\/#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":"Compile, Evaluate and Predict Model in Keras"}]},{"@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\/78460","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=78460"}],"version-history":[{"count":2,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78460\/revisions"}],"predecessor-version":[{"id":78464,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/78460\/revisions\/78464"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/78463"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=78460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=78460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=78460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}