{"id":400,"date":"2024-01-23T16:35:43","date_gmt":"2024-01-23T15:35:43","guid":{"rendered":"https:\/\/bitwise.exposed\/?p=400"},"modified":"2026-03-16T09:38:47","modified_gmt":"2026-03-16T08:38:47","slug":"regression-basic","status":"publish","type":"post","link":"https:\/\/bitwise.exposed\/index.php\/2024\/01\/23\/regression-basic\/","title":{"rendered":"Regression &#8211; TensorFlow"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">Cookbook for basic steps to create a regression model with TensorFlow.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Create some sample data<\/li>\n\n\n\n<li>setup a model<\/li>\n\n\n\n<li>measure performance<\/li>\n\n\n\n<li>visualize train, test and predicted data<\/li>\n\n\n\n<li>evaluate model<\/li>\n<\/ul>\n\n\n\n<h5 class=\"wp-block-heading\">Data<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Import<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"import tensorflow as tf\nimport matplotlib.pyplot as plt\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #AF00DB\">import<\/span><span style=\"color: #000000\"> tensorflow <\/span><span style=\"color: #AF00DB\">as<\/span><span style=\"color: #000000\"> tf<\/span><\/span>\n<span class=\"line\"><span style=\"color: #AF00DB\">import<\/span><span style=\"color: #000000\"> matplotlib.pyplot <\/span><span style=\"color: #AF00DB\">as<\/span><span style=\"color: #000000\"> plt<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Create some sample data.<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# create some data \nX = tf.range (-100, 100, 4)\nY = X + 10\n\n# Calculate the splitting index for 80% training and 20% testing\nsplit_index = int(len(X)*0.8)\n\n# Splitting the data into training and testing sets\ntrain_data = X[:split_index]\ntrain_label = Y[:split_index]\ntest_data = X[split_index:]\ntest_label = Y[split_index:]\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># create some data <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">X = tf.range (-<\/span><span style=\"color: #098658\">100<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">100<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">4<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">Y = X + <\/span><span style=\"color: #098658\">10<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Calculate the splitting index for 80% training and 20% testing<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">split_index = <\/span><span style=\"color: #267F99\">int<\/span><span style=\"color: #000000\">(<\/span><span style=\"color: #795E26\">len<\/span><span style=\"color: #000000\">(X)*<\/span><span style=\"color: #098658\">0.8<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Splitting the data into training and testing sets<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">train_data = X[:split_index]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">train_label = Y[:split_index]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">test_data = X[split_index:]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">test_label = Y[split_index:]<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Model<\/h5>\n\n\n\n<p class=\"wp-block-paragraph\">Setup\/configure your model<\/p>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# set seed\ntf.random.set_seed(42)\n\n# create\nmodel_1 = tf.keras.models.Sequential([\n    tf.keras.layers.Dense(1, name=&quot;output_layer&quot;)\n    ], name=&quot;model_1&quot;)\n\n# compile\n\nmodel_1.compile(\n    loss        = tf.keras.losses.mae,\n    optimizer   = tf.keras.optimizers.SGD(),\n    metrics     = ['mae']\n)\n\n# fit\ntrain_data_expand = tf.expand_dims(train_data, axis=-1)\n\nmodel_1_history = model_1.fit(train_data_expand, train_label, epochs=100)\n\n# predict\ntest_data_expand = tf.expand_dims(test_data, axis = -1)\n\npredict_label_1 = model_1.predict(test_data_expand)\n\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># set seed<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">tf.random.set_seed(<\/span><span style=\"color: #098658\">42<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># create<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">model_1 = tf.keras.models.Sequential([<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    tf.keras.layers.Dense(<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;output_layer&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    ], <\/span><span style=\"color: #001080\">name<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;model_1&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># compile<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">model_1.compile(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">loss<\/span><span style=\"color: #000000\">        = tf.keras.losses.mae,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">optimizer<\/span><span style=\"color: #000000\">   = tf.keras.optimizers.SGD(),<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><span style=\"color: #001080\">metrics<\/span><span style=\"color: #000000\">     = [<\/span><span style=\"color: #A31515\">&#39;mae&#39;<\/span><span style=\"color: #000000\">]<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># fit<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">train_data_expand = tf.expand_dims(train_data, <\/span><span style=\"color: #001080\">axis<\/span><span style=\"color: #000000\">=-<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">model_1_history = model_1.fit(train_data_expand, train_label, <\/span><span style=\"color: #001080\">epochs<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #098658\">100<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># predict<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">test_data_expand = tf.expand_dims(test_data, <\/span><span style=\"color: #001080\">axis<\/span><span style=\"color: #000000\"> = -<\/span><span style=\"color: #098658\">1<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #000000\">predict_label_1 = model_1.predict(test_data_expand)<\/span><\/span>\n<span class=\"line\"><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Some notes about hyper parameters<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>Hyper parameter<\/strong><\/td><td><strong>Typical value<\/strong><\/td><\/tr><tr><td>Input layer shape<\/td><td>Same shape as number of features (e.g. 3 for # bedrooms, # bathrooms, # car spaces in housing price prediction) -&gt; shape()<\/td><\/tr><tr><td>Hidden layer(s)<\/td><td>Problem specific, minimum = 1, maximum = unlimited<\/td><\/tr><tr><td>Neurons per hidden layer<\/td><td>Problem specific, generally 10 to 100 -&gt; Dense()<\/td><\/tr><tr><td>Output layer shape<\/td><td>Same shape as desired prediction shape (e.g. 1 for house price)<\/td><\/tr><tr><td>Hidden layer activation<\/td><td>Usually ReLU(rectified linear unit)<\/td><\/tr><tr><td>Output activation<\/td><td>None,ReLU, logistic\/tanh<\/td><\/tr><tr><td>Loss function<\/td><td>MSE (mean spare error) or MAE (mean absolute error) \/ Huber (combination of MAE\/MSE) if outliers<\/td><\/tr><tr><td>Optimizer<\/td><td>SGD (stochastic gradient descent), Adam<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Visualization &#8211; prediction<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(2 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# Initialize the figure \nplt.figure(figsize=(7, 10))\n    \n# Plot training data in blue\nplt.scatter(train_data, train_label, color=&quot;b&quot;, \n            label=&quot;training data&quot;)\n    \n# Plot testing data in green\nplt.scatter(test_data, test_label, color=&quot;g&quot;, label=&quot;test data&quot;)\n\n# Plot model's predictions in red\nplt.scatter(test_data, predict_label_1, color=&quot;r&quot;,\n            label=&quot;predictions&quot;)\n\n# Display the legend\nplt.legend()\n\n# Show the plot\nplt.show()\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># Initialize the figure <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.figure(<\/span><span style=\"color: #001080\">figsize<\/span><span style=\"color: #000000\">=(<\/span><span style=\"color: #098658\">7<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #098658\">10<\/span><span style=\"color: #000000\">))<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Plot training data in blue<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.scatter(train_data, train_label, <\/span><span style=\"color: #001080\">color<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;b&quot;<\/span><span style=\"color: #000000\">, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            <\/span><span style=\"color: #001080\">label<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;training data&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">    <\/span><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Plot testing data in green<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.scatter(test_data, test_label, <\/span><span style=\"color: #001080\">color<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;g&quot;<\/span><span style=\"color: #000000\">, <\/span><span style=\"color: #001080\">label<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;test data&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Plot model&#39;s predictions in red<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.scatter(test_data, predict_label_1, <\/span><span style=\"color: #001080\">color<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;r&quot;<\/span><span style=\"color: #000000\">,<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">            <\/span><span style=\"color: #001080\">label<\/span><span style=\"color: #000000\">=<\/span><span style=\"color: #A31515\">&quot;predictions&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Display the legend<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.legend()<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># Show the plot<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.show()<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure data-wp-context=\"{&quot;imageId&quot;:&quot;6aa4e4f9ebac3&quot;}\" data-wp-interactive=\"core\/image\" data-wp-key=\"6aa4e4f9ebac3\" class=\"wp-block-image size-full is-resized wp-lightbox-container\"><img loading=\"lazy\" decoding=\"async\" width=\"1033\" height=\"1333\" data-wp-class--hide=\"state.isContentHidden\" data-wp-class--show=\"state.isContentVisible\" data-wp-init=\"callbacks.setButtonStyles\" data-wp-on--click=\"actions.showLightbox\" data-wp-on--load=\"callbacks.setButtonStyles\" data-wp-on--pointerdown=\"actions.preloadImage\" data-wp-on--pointerenter=\"actions.preloadImageWithDelay\" data-wp-on--pointerleave=\"actions.cancelPreload\" data-wp-on-window--resize=\"callbacks.setButtonStyles\" src=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/01\/plot_fct_output.png\" alt=\"\" class=\"wp-image-416\" style=\"width:219px;height:auto\" srcset=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/01\/plot_fct_output.png 1033w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/01\/plot_fct_output-232x300.png 232w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/01\/plot_fct_output-794x1024.png 794w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/01\/plot_fct_output-768x991.png 768w\" sizes=\"auto, (max-width: 1033px) 100vw, 1033px\" \/><button\n\t\t\tclass=\"lightbox-trigger\"\n\t\t\ttype=\"button\"\n\t\t\taria-haspopup=\"dialog\"\n\t\t\tdata-wp-bind--aria-label=\"state.thisImage.triggerButtonAriaLabel\"\n\t\t\tdata-wp-init=\"callbacks.initTriggerButton\"\n\t\t\tdata-wp-on--click=\"actions.showLightbox\"\n\t\t\tdata-wp-style--right=\"state.thisImage.buttonRight\"\n\t\t\tdata-wp-style--top=\"state.thisImage.buttonTop\"\n\t\t>\n\t\t\t<svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"12\" height=\"12\" fill=\"none\" viewBox=\"0 0 12 12\">\n\t\t\t\t<path fill=\"#fff\" d=\"M2 0a2 2 0 0 0-2 2v2h1.5V2a.5.5 0 0 1 .5-.5h2V0H2Zm2 10.5H2a.5.5 0 0 1-.5-.5V8H0v2a2 2 0 0 0 2 2h2v-1.5ZM8 12v-1.5h2a.5.5 0 0 0 .5-.5V8H12v2a2 2 0 0 1-2 2H8Zm2-12a2 2 0 0 1 2 2v2h-1.5V2a.5.5 0 0 0-.5-.5H8V0h2Z\" \/>\n\t\t\t<\/svg>\n\t\t<\/button><\/figure>\n\n\n\n<h5 class=\"wp-block-heading\">Measure performance<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"# mean_absolute_error\ntf.keras.metrics.mean_absolute_error(\n                      test_label, \n                      tf.squeeze(predict_label_1))\n\n# mean_squared_error\ntf.keras.metrics.mean_squared_error(\n                      test_label, \n                      tf.squeeze(predict_label_1))\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #008000\"># mean_absolute_error<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">tf.keras.metrics.mean_absolute_error(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                      test_label, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                      tf.squeeze(predict_label_1))<\/span><\/span>\n<span class=\"line\"><\/span>\n<span class=\"line\"><span style=\"color: #008000\"># mean_squared_error<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">tf.keras.metrics.mean_squared_error(<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                      test_label, <\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">                      tf.squeeze(predict_label_1))<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Evaluate<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"model_1.evaluate(test_data, test_label)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #000000\">model_1.evaluate(test_data, test_label)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\">Evaluate is a method available in TensorFlow&#8217;s Kreas API.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">model.evaluate(x, y)<\/mark>: This method <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">computes the loss<\/mark> based on the input you pass it, <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">along with<\/mark> any other <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">metrics <\/mark>that are <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">requested <\/mark>in the metrics parameter <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">when <\/mark>the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">model will be compiled<\/mark>, such as <strong>mae <\/strong>in this example.<br>\n<ul class=\"wp-block-list\">\n<li>x is the input data (the testing data set).<\/li>\n\n\n\n<li>y is the true labels for x.<br><\/li>\n<\/ul>\n<\/li>\n\n\n\n<li>The <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">method returns<\/mark> the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">loss <\/mark>value and <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">metric values<\/mark> for the model in test mode.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Loss value<\/mark>: This is a measure of the <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">model&#8217;s error on the test data<\/mark>. A low loss signifies that the model is performing well.<br><br><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-surface-brand-primary-color\">Metrics Values<\/mark>: These are the evaluation metrics that were <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">specified when the model was compiled<\/mark>. <mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-text-accent-color\">These could be metrics like accuracy, precision, recall, mae etc. <\/mark>The actual metrics returned would depend on what metrics were passed to the model.compile() function during the model&#8217;s compilation. If <strong>mae <\/strong>was specified as a metric, for example, the function will return the mae of the model on the test data.<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Visualize &#8211; training loss per epoch<\/h5>\n\n\n\n<div class=\"wp-block-kevinbatdorf-code-block-pro cbp-has-line-numbers\" data-code-block-pro-font-family=\"Code-Pro-JetBrains-Mono\" style=\"font-size:1rem;font-family:Code-Pro-JetBrains-Mono,ui-monospace,SFMono-Regular,Menlo,Monaco,Consolas,monospace;--cbp-line-number-color:#000000;--cbp-line-number-width:calc(1 * 0.6 * 1rem);line-height:1.5rem;--cbp-tab-width:2;tab-size:var(--cbp-tab-width, 2)\"><span style=\"display:flex;align-items:center;padding:10px 0px 10px 16px;margin-bottom:-2px;width:100%;text-align:left;background-color:#f2f2f2;color:#0d0d0d\">Python<\/span><span role=\"button\" tabindex=\"0\" data-code=\"pd.DataFrame(model_1_history.history).plot()\nplt.ylabel(&quot;loss&quot;)\nplt.xlabel(&quot;epoch&quot;)\" style=\"color:#000000;display:none\" aria-label=\"Copy\" class=\"code-block-pro-copy-button\"><svg xmlns=\"http:\/\/www.w3.org\/2000\/svg\" style=\"width:24px;height:24px\" fill=\"none\" viewBox=\"0 0 24 24\" stroke=\"currentColor\" stroke-width=\"2\"><path class=\"with-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M4.5 12.75l6 6 9-13.5\"><\/path><path class=\"without-check\" stroke-linecap=\"round\" stroke-linejoin=\"round\" d=\"M16.5 8.25V6a2.25 2.25 0 00-2.25-2.25H6A2.25 2.25 0 003.75 6v8.25A2.25 2.25 0 006 16.5h2.25m8.25-8.25H18a2.25 2.25 0 012.25 2.25V18A2.25 2.25 0 0118 20.25h-7.5A2.25 2.25 0 018.25 18v-1.5m8.25-8.25h-6a2.25 2.25 0 00-2.25 2.25v6\"><\/path><\/svg><\/span><pre class=\"shiki light-plus\" style=\"background-color: #FFFFFF\" tabindex=\"0\"><code><span class=\"line\"><span style=\"color: #000000\">pd.DataFrame(model_1_history.history).plot()<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.ylabel(<\/span><span style=\"color: #A31515\">&quot;loss&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span>\n<span class=\"line\"><span style=\"color: #000000\">plt.xlabel(<\/span><span style=\"color: #A31515\">&quot;epoch&quot;<\/span><span style=\"color: #000000\">)<\/span><\/span><\/code><\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"876\" height=\"655\" src=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/02\/image.png\" alt=\"\" class=\"wp-image-516\" srcset=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/02\/image.png 876w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/02\/image-300x224.png 300w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/02\/image-768x574.png 768w\" sizes=\"auto, (max-width: 876px) 100vw, 876px\" \/><\/figure>\n\n\n\n<div style=\"height:100px\" aria-hidden=\"true\" class=\"wp-block-spacer\"><\/div>\n\n\n\n<h5 class=\"wp-block-heading\">Links<\/h5>\n\n\n\n<div class=\"wp-block-media-text is-stacked-on-mobile\" style=\"grid-template-columns:15% auto\"><figure class=\"wp-block-media-text__media\"><img loading=\"lazy\" decoding=\"async\" width=\"975\" height=\"570\" src=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/readmydocs_icon.png\" alt=\"\" class=\"wp-image-626 size-full\" srcset=\"https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/readmydocs_icon.png 975w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/readmydocs_icon-300x175.png 300w, https:\/\/bitwise.exposed\/wp-content\/uploads\/2024\/04\/readmydocs_icon-768x449.png 768w\" sizes=\"auto, (max-width: 975px) 100vw, 975px\" \/><\/figure><div class=\"wp-block-media-text__content\">\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/mydocs-bitwiseexposed.readthedocs.io\/en\/latest\/tensorflow\/index.html\">Installing TensorFlow with GPU support<\/a><\/p>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Basic architecture of a regression model in TensorFlow.<\/p>\n","protected":false},"author":1,"featured_media":401,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23,13,25],"tags":[],"class_list":["post-400","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-ai","category-python","category-tensorflow"],"_links":{"self":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/400","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/comments?post=400"}],"version-history":[{"count":30,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/400\/revisions"}],"predecessor-version":[{"id":629,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/posts\/400\/revisions\/629"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media\/401"}],"wp:attachment":[{"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/media?parent=400"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/categories?post=400"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/bitwise.exposed\/index.php\/wp-json\/wp\/v2\/tags?post=400"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}