{"id":40346,"date":"2016-04-13T18:46:52","date_gmt":"2016-04-13T15:46:52","guid":{"rendered":"https:\/\/www.altoros.com\/blog\/?p=40346"},"modified":"2019-06-07T17:40:07","modified_gmt":"2019-06-07T14:40:07","slug":"using-linear-regression-in-tensorflow","status":"publish","type":"post","link":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/","title":{"rendered":"Implementing Linear Regression with TensorFlow"},"content":{"rendered":"<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 counter-hierarchy ez-toc-counter ez-toc-transparent ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Introduction\" >Introduction<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Prerequisites\" >Prerequisites<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#A_data_set\" >A data set<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Data_set_pre-processing\" >Data set pre-processing<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Applying_the_cost_function_and_gradient_descent\" >Applying the cost function and gradient descent<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Selecting_a_learning_rate\" >Selecting a learning rate<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Running_example_source_code\" >Running example source code<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-3'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#Further_reading\" >Further reading<\/a><\/li><\/ul><\/nav><\/div>\n<h3><span class=\"ez-toc-section\" id=\"Introduction\"><\/span>Introduction<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p><a href=\"https:\/\/en.wikipedia.org\/wiki\/Linear_regression\" rel=\"noopener noreferrer\" target=\"_blank\">Linear regression<\/a> helps to predict scores on the variable <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">Y<\/code> from the scores on the variable <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">X<\/code>. The variable <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">Y<\/code> that we are predicting is usually called the <em>criterion variable<\/em>, and the variable <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">X<\/code> that we are basing our predictions on is called the <em>predictor variable<\/em>. If there is only one predictor variable, the prediction method is called <em>simple regression<\/em>.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Prerequisites\"><\/span>Prerequisites<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>In addition to TensorFlow, install <a href=\"https:\/\/matplotlib.org\/\" rel=\"noopener noreferrer\" target=\"_blank\">Matplotlib<\/a> for using plots: <code style=\"color: black; background-color: #e6e6e6;\">pip install matplotlib<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"A_data_set\"><\/span>A data set<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<\/p>\n<p>As a training set for the tutorial, we use house prices in Portland, Oregon, where <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">X<\/code> (the predictor variable) is the house size and <code style=\"color: #222222; background-color: #e6e6e6; padding: 1px 2px;\">Y<\/code> (the criterion variable) is the house price. The data set contains 47 examples.<\/p>\n<p><center><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/using-linear-regression-in-temsorflow-samle-data.png\"><img decoding=\"async\" class=\"aligncenter size-full wp-image-40348\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/using-linear-regression-in-temsorflow-samle-data.png\" alt=\"using-linear-regression-in-temsorflow-samle-data\" width=\"640\" \/><\/a><\/center><\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Data_set_pre-processing\"><\/span>Data set pre-processing<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Normalizing your data helps to improve the performance of gradient descent, especially in the case of <em>multivariate linear regression<\/em>.<\/p>\n<p>We can do this with the following formula:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40349\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/linear-regression-in-tensorflow-formula-one.png\" alt=\"linear-regression-in-tensorflow-formula-one\" width=\"54\" height=\"41\" \/><\/center><\/p>\n<p>&#8230;where <code style=\"color: black; background-color: #e6e6e6;\">m<\/code> is the mean value of the variable and <code style=\"color: black; background-color: #e6e6e6;\">q<\/code> is the standard deviation.<\/p>\n<p>Here&#8217;s the implementation of the formula in the source code.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">def normalize(array): \r\n    return (array - array.mean()) \/ array.std()\r\n\r\nsize_data_n = normalize(size_data)\r\nprice_data_n = normalize(price_data)<\/pre>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Applying_the_cost_function_and_gradient_descent\"><\/span>Applying the cost function and gradient descent<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>The next step is to implement the cost function and to apply the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Gradient_descent\" rel=\"noopener noreferrer\" target=\"_blank\">gradient descent<\/a> method to it for minimizing squared errors.<\/p>\n<p>The cost function formula:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40350\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/cost-function-formula.png\" alt=\"cost-function-formula\" width=\"260\" height=\"55\" \/><\/center><\/p>\n<p>This is its implementation in the source code.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">cost_function = tf.reduce_sum(tf.pow(model - Y, 2))\/(2 * samples_number)\r\ntf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function)<\/pre>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Selecting_a_learning_rate\"><\/span>Selecting a learning rate<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Typically, the learning rate is selected in the following range:<\/p>\n<p><center><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-40351\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/selecting-a-learning-rate.png\" alt=\"selecting-a-learning-rate\" width=\"123\" height=\"31\" \/><\/center><\/p>\n<p>After the initial selection, running gradient descent, and observing the cost function value, you can adjust the learning rate accordingly. For this task, we choose the learning rate equal to <code style=\"color: black; background-color: #e6e6e6;\">0.1<\/code>.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Running_example_source_code\"><\/span>Running example source code<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<p>Now, try to run the example source code.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport tensorflow as tf\r\nimport numpy\r\nimport matplotlib.pyplot as plt\r\n\r\n# Train a data set\r\n\r\nsize_data = numpy.asarray(&#x5B; 2104,  1600,  2400,  1416,  3000,  1985,  1534,  1427,\r\n  1380,  1494,  1940,  2000,  1890,  4478,  1268,  2300,\r\n  1320,  1236,  2609,  3031,  1767,  1888,  1604,  1962,\r\n  3890,  1100,  1458,  2526,  2200,  2637,  1839,  1000,\r\n  2040,  3137,  1811,  1437,  1239,  2132,  4215,  2162,\r\n  1664,  2238,  2567,  1200,   852,  1852,  1203 ])\r\nprice_data = numpy.asarray(&#x5B; 399900,  329900,  369000,  232000,  539900,  299900,  314900,  198999,\r\n  212000,  242500,  239999,  347000,  329999,  699900,  259900,  449900,\r\n  299900,  199900,  499998,  599000,  252900,  255000,  242900,  259900,\r\n  573900,  249900,  464500,  469000,  475000,  299900,  349900,  169900,\r\n  314900,  579900,  285900,  249900,  229900,  345000,  549000,  287000,\r\n  368500,  329900,  314000,  299000,  179900,  299900,  239500 ])\r\n\r\n# Test a data set\r\n\r\nsize_data_test = numpy.asarray(&#x5B; 1600, 1494, 1236, 1100, 3137, 2238 ])\r\nprice_data_test = numpy.asarray(&#x5B; 329900, 242500, 199900, 249900, 579900, 329900 ])\r\n\r\ndef normalize(array): \r\n    return (array - array.mean()) \/ array.std()\r\n\r\n# Normalize a data set\r\n\r\nsize_data_n = normalize(size_data)\r\nprice_data_n = normalize(price_data)\r\n\r\nsize_data_test_n = normalize(size_data_test)\r\nprice_data_test_n = normalize(price_data_test)\r\n\r\n# Display a plot\r\nplt.plot(size_data, price_data, 'ro', label='Samples data')\r\nplt.legend()\r\nplt.draw()\r\n\r\nsamples_number = price_data_n.size\r\n\r\n# TF graph input\r\nX = tf.placeholder(&quot;float&quot;)\r\nY = tf.placeholder(&quot;float&quot;)\r\n\r\n# Create a model\r\n\r\n# Set model weights\r\nW = tf.Variable(numpy.random.randn(), name=&quot;weight&quot;)\r\nb = tf.Variable(numpy.random.randn(), name=&quot;bias&quot;)\r\n\r\n# Set parameters\r\nlearning_rate = 0.1\r\ntraining_iteration = 200\r\n\r\n# Construct a linear model\r\nmodel = tf.add(tf.mul(X, W), b)\r\n\r\n# Minimize squared errors\r\ncost_function = tf.reduce_sum(tf.pow(model - Y, 2))\/(2 * samples_number) #L2 loss\r\noptimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost_function) #Gradient descent\r\n\r\n# Initialize variables\r\ninit = tf.initialize_all_variables()\r\n\r\n# Launch a graph\r\nwith tf.Session() as sess:\r\n    sess.run(init)\r\n\r\n    display_step = 20\r\n    # Fit all the training data\r\n    for iteration in range(training_iteration):\r\n        for (x, y) in zip(size_data_n, price_data_n):\r\n            sess.run(optimizer, feed_dict={X: x, Y: y})\r\n\r\n        # Display logs per iteration step\r\n        if iteration % display_step == 0:\r\n            print &quot;Iteration:&quot;, '%04d' % (iteration + 1), &quot;cost=&quot;, &quot;{:.9f}&quot;.format(sess.run(cost_function, feed_dict={X:size_data_n, Y:price_data_n})),\\\r\n            &quot;W=&quot;, sess.run(W), &quot;b=&quot;, sess.run(b)\r\n            \r\n    tuning_cost = sess.run(cost_function, feed_dict={X: normalize(size_data_n), Y: normalize(price_data_n)})\r\n            \r\n    print &quot;Tuning completed:&quot;, &quot;cost=&quot;, &quot;{:.9f}&quot;.format(tuning_cost), &quot;W=&quot;, sess.run(W), &quot;b=&quot;, sess.run(b)\r\n    \r\n    # Validate a tuning model\r\n    \r\n    testing_cost = sess.run(cost_function, feed_dict={X: size_data_test_n, Y: price_data_test_n})\r\n    \r\n    print &quot;Testing data cost:&quot; , testing_cost\r\n    \r\n    # Display a plot\r\n    plt.figure()\r\n    plt.plot(size_data_n, price_data_n, 'ro', label='Normalized samples')\r\n    plt.plot(size_data_test_n, price_data_test_n, 'go', label='Normalized testing samples')\r\n    plt.plot(size_data_n, sess.run(W) * size_data_n + sess.run(b), label='Fitted line')\r\n    plt.legend()\r\n    \r\n    plt.show()<\/pre>\n<p>The plot below illustrates the normalized data set along with the trained model.<\/p>\n<p><center><a href=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/normalized-data-set-with-trained-model-tensorflow.png\"><img decoding=\"async\" src=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2018\/12\/normalized-data-set-with-trained-model-tensorflow.png\" alt=\"\" width=\"640\" class=\"aligncenter size-full wp-image-40347\" \/><\/a><\/center><\/p>\n<p>The blue line displays predicted values, using linear regression.<\/p>\n<p>&nbsp;<\/p>\n<h3><span class=\"ez-toc-section\" id=\"Further_reading\"><\/span>Further reading<span class=\"ez-toc-section-end\"><\/span><\/h3>\n<ul>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/using-logistic-and-softmax-regression-with-tensorflow\/\">Implementing Logistic and Softmax Regression with TensorFlow<\/a><\/li>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/basic-concepts-and-manipulations-with-tensorflow\/\">Basic Concepts and Manipulations with TensorFlow<\/a><\/li>\n<li><a href=\"https:\/\/www.altoros.com\/blog\/visualizing-tensorflow-graphs-with-tensorboard\/\">Visualizing TensorFlow Graphs with TensorBoard<\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<hr\/>\n<p><center><small>The tutorial was written by Sergey Kovalev; edited and published by Sophia Turol and <a href=\"https:\/\/www.altoros.com\/blog\/author\/alex\/\">Alex Khizhniak<\/a>.<\/small><\/center><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Introduction<\/p>\n<p>Linear regression helps to predict scores on the variable Y from the scores on the variable X. The variable Y that we are predicting is usually called the criterion variable, and the variable X that we are basing our predictions on is called the predictor variable. If there is only [&#8230;]<\/p>\n","protected":false},"author":122,"featured_media":40358,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"content-type":"","footnotes":"","_links_to":"","_links_to_target":""},"categories":[214],"tags":[748,749],"class_list":["post-40346","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-tutorials","tag-machine-learning","tag-tensorflow"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Implementing Linear Regression with TensorFlow | Altoros<\/title>\n<meta name=\"description\" content=\"In this tutorial, we demonstrate how to create a linear regression model with TensorFlow and optimize it using gradient descent.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Implementing Linear Regression with TensorFlow | Altoros\" \/>\n<meta property=\"og:description\" content=\"Introduction Linear regression helps to predict scores on the variable Y from the scores on the variable X. The variable Y that we are predicting is usually called the criterion variable, and the variable X that we are basing our predictions on is called the predictor variable. If there is only [...]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/\" \/>\n<meta property=\"og:site_name\" content=\"Altoros\" \/>\n<meta property=\"article:published_time\" content=\"2016-04-13T15:46:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-06-07T14:40:07+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif\" \/>\n\t<meta property=\"og:image:width\" content=\"640\" \/>\n\t<meta property=\"og:image:height\" content=\"480\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/gif\" \/>\n<meta name=\"author\" content=\"Sergey Kovalev\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Sergey Kovalev\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/\"},\"author\":{\"name\":\"Sergey Kovalev\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/7bb3c35a84eeaa32af69b53773e7fe1a\"},\"headline\":\"Implementing Linear Regression with TensorFlow\",\"datePublished\":\"2016-04-13T15:46:52+00:00\",\"dateModified\":\"2019-06-07T14:40:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/\"},\"wordCount\":796,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/04\\\/using-linear-regression-in-tensorflow-machine-learning.gif\",\"keywords\":[\"Machine Learning\",\"TensorFlow\"],\"articleSection\":[\"Tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/\",\"name\":\"Implementing Linear Regression with TensorFlow | Altoros\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/04\\\/using-linear-regression-in-tensorflow-machine-learning.gif\",\"datePublished\":\"2016-04-13T15:46:52+00:00\",\"dateModified\":\"2019-06-07T14:40:07+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/7bb3c35a84eeaa32af69b53773e7fe1a\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/04\\\/using-linear-regression-in-tensorflow-machine-learning.gif\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2016\\\/04\\\/using-linear-regression-in-tensorflow-machine-learning.gif\",\"width\":640,\"height\":480},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/using-linear-regression-in-tensorflow\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Implementing Linear Regression with TensorFlow\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/\",\"name\":\"Altoros\",\"description\":\"Insight\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/#\\\/schema\\\/person\\\/7bb3c35a84eeaa32af69b53773e7fe1a\",\"name\":\"Sergey Kovalev\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/01\\\/3faf706-150x150.jpg\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/01\\\/3faf706-150x150.jpg\",\"contentUrl\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/wp-content\\\/uploads\\\/2017\\\/01\\\/3faf706-150x150.jpg\",\"caption\":\"Sergey Kovalev\"},\"description\":\"Sergey Kovalev is a senior software engineer with extensive experience in high-load application development, big data and NoSQL solutions, cloud computing, data warehousing, and machine learning. He has strong expertise in back-end engineering, applying the best approaches for development, architecture design, and scaling. He has solid background in software development practices, such as the Agile methodology, prototyping, patterns, refactoring, and code review. Now, Sergey\u2019s main interest lies in big data distributed computing and machine learning.\",\"url\":\"https:\\\/\\\/www.altoros.com\\\/blog\\\/author\\\/s-kovalev\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Implementing Linear Regression with TensorFlow | Altoros","description":"In this tutorial, we demonstrate how to create a linear regression model with TensorFlow and optimize it using gradient descent.","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:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/","og_locale":"en_US","og_type":"article","og_title":"Implementing Linear Regression with TensorFlow | Altoros","og_description":"Introduction Linear regression helps to predict scores on the variable Y from the scores on the variable X. The variable Y that we are predicting is usually called the criterion variable, and the variable X that we are basing our predictions on is called the predictor variable. If there is only [...]","og_url":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/","og_site_name":"Altoros","article_published_time":"2016-04-13T15:46:52+00:00","article_modified_time":"2019-06-07T14:40:07+00:00","og_image":[{"width":640,"height":480,"url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif","type":"image\/gif"}],"author":"Sergey Kovalev","twitter_misc":{"Written by":"Sergey Kovalev","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#article","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/"},"author":{"name":"Sergey Kovalev","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/7bb3c35a84eeaa32af69b53773e7fe1a"},"headline":"Implementing Linear Regression with TensorFlow","datePublished":"2016-04-13T15:46:52+00:00","dateModified":"2019-06-07T14:40:07+00:00","mainEntityOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/"},"wordCount":796,"commentCount":0,"image":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif","keywords":["Machine Learning","TensorFlow"],"articleSection":["Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/","url":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/","name":"Implementing Linear Regression with TensorFlow | Altoros","isPartOf":{"@id":"https:\/\/www.altoros.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#primaryimage"},"image":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#primaryimage"},"thumbnailUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif","datePublished":"2016-04-13T15:46:52+00:00","dateModified":"2019-06-07T14:40:07+00:00","author":{"@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/7bb3c35a84eeaa32af69b53773e7fe1a"},"breadcrumb":{"@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#primaryimage","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2016\/04\/using-linear-regression-in-tensorflow-machine-learning.gif","width":640,"height":480},{"@type":"BreadcrumbList","@id":"https:\/\/www.altoros.com\/blog\/using-linear-regression-in-tensorflow\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.altoros.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Implementing Linear Regression with TensorFlow"}]},{"@type":"WebSite","@id":"https:\/\/www.altoros.com\/blog\/#website","url":"https:\/\/www.altoros.com\/blog\/","name":"Altoros","description":"Insight","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.altoros.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.altoros.com\/blog\/#\/schema\/person\/7bb3c35a84eeaa32af69b53773e7fe1a","name":"Sergey Kovalev","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/01\/3faf706-150x150.jpg","url":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/01\/3faf706-150x150.jpg","contentUrl":"https:\/\/www.altoros.com\/blog\/wp-content\/uploads\/2017\/01\/3faf706-150x150.jpg","caption":"Sergey Kovalev"},"description":"Sergey Kovalev is a senior software engineer with extensive experience in high-load application development, big data and NoSQL solutions, cloud computing, data warehousing, and machine learning. He has strong expertise in back-end engineering, applying the best approaches for development, architecture design, and scaling. He has solid background in software development practices, such as the Agile methodology, prototyping, patterns, refactoring, and code review. Now, Sergey\u2019s main interest lies in big data distributed computing and machine learning.","url":"https:\/\/www.altoros.com\/blog\/author\/s-kovalev\/"}]}},"_links":{"self":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/40346","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/users\/122"}],"replies":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/comments?post=40346"}],"version-history":[{"count":17,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/40346\/revisions"}],"predecessor-version":[{"id":43980,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/posts\/40346\/revisions\/43980"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media\/40358"}],"wp:attachment":[{"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/media?parent=40346"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/categories?post=40346"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.altoros.com\/blog\/wp-json\/wp\/v2\/tags?post=40346"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}