{"id":228,"date":"2016-10-18T04:14:39","date_gmt":"2016-10-18T04:14:39","guid":{"rendered":"http:\/\/blog.cloudxlab.com\/?p=228"},"modified":"2017-09-15T06:20:06","modified_gmt":"2017-09-15T06:20:06","slug":"tensorflow-getting-started","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/","title":{"rendered":"Using TensorFlow on CloudxLab"},"content":{"rendered":"<p>We are glad to inform you that the <a href=\"https:\/\/www.tensorflow.org\/\">TensorFlow<\/a> is now available on CloudxLab. In this example, we will walk you through a basic tutorial on how to use TensorFlow.<\/p>\n<p><strong>What is TensorFlow?<\/strong><br \/>\nTensorFlow is an Open Source Software Library for Machine Intelligence. It is developed and supported by Google and is being adopted very fast.<\/p>\n<p><strong>What is CloudxLab?<\/strong><br \/>\n<a href=\"https:\/\/cloudxlab.com\/\">CloudxLab<\/a> provides a real cloud-based environment for practicing and learn various tools. You can start learning right away by just signing up online.<\/p>\n<p><!--more--><\/p>\n<p><span style=\"font-weight: 400;\">In this blog, we will go through a slight deviation of the <\/span><a href=\"https:\/\/www.tensorflow.org\/versions\/r0.9\/get_started\/index.html\"><span style=\"font-weight: 400;\">first example<\/span><\/a><span style=\"font-weight: 400;\"> of TensorFlow in the stepwise fashion as mentioned <\/span><a href=\"https:\/\/www.tensorflow.org\/versions\/r0.9\/get_started\/index.html\"><span style=\"font-weight: 400;\">here<\/span><\/a><span style=\"font-weight: 400;\">. The objective of the exercise is to figure out the straight line that fits the given data. A line is characterized by the slope and intercept. In other words, we need to figure out the line that best suits the following points.<\/span><\/p>\n<p><img class=\"alignnone wp-image-230\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0.png\" alt=\"Fitted Line\" width=\"1002\" height=\"589\" \/><\/p>\n<p><span style=\"font-weight: 400;\">The end result of this exercise is to figure out the blue line as shown in the graph below:<\/span><\/p>\n<p><img class=\"alignnone wp-image-231\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0-1.png\" alt=\"Fitted Line\" width=\"1041\" height=\"640\" \/><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 0:<\/strong> Login into cloudxLab<\/span><\/p>\n<p><span style=\"font-weight: 400;\">See <\/span><a href=\"https:\/\/www.youtube.com\/watch?time_continue=144&amp;v=fyOh7Afe2GM\"><span style=\"font-weight: 400;\">this video<\/span><\/a><span style=\"font-weight: 400;\"> to know how to login into the console.<\/span><\/p>\n<p><strong>Step 1:<\/strong> Start python interactive shell<\/p>\n<p><span style=\"font-weight: 400;\">Type &#8220;python&#8221; (without quotes) command on shell<\/span><\/p>\n<p><span style=\"font-weight: 400;\"><strong>Step 2:<\/strong> import libraries needed<\/span><\/p>\n<pre class=\"theme:github toolbar:2 plain:false plain-toggle:false popup:false lang:default decode:true\">import tensorflow as tf\r\nimport numpy as np<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 3:<\/strong> Generate Some random data<\/span><\/p>\n<pre class=\"theme:github plain:false plain-toggle:false popup:false lang:python decode:true \">x_data = np.random.rand(100).astype(np.float32)\r\ndef f(x):\r\n   return x*0.1 + 0.3 + (np.random.rand()-0.5)\/200\r\ny_data = map(f, x_data)<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 4:<\/strong> Define the variables that we need to figure out<\/span><\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true \">W = tf.Variable(tf.random_uniform([1], -1.0, 1.0))\r\nb = tf.Variable(tf.zeros([1]))<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 5:<\/strong> Define relation between them &#8211; For simplicity we are assuming relation to be linear<\/span><\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\">y = W * x_data + b\r\n<\/pre>\n<div id=\"cxl-affiliate\"><\/div>\n<p><script>\/\/ <![CDATA[ (function(a, b, c) { affiliate_code = \"CW7CF05WBUZ55SHRSH8R\"; lab = \"hadoop\"; s = b.createElement('script'); s.type = 'text\/javascript'; s.src = \"\/\/s3.amazonaws.com\/cloudxlab\/embed\/affl-without-analytics.js\"; s.async = 1; (b.head || b.body).appendChild(s); } (window, document)); \/\/ ]]><\/script><\/p>\n<p><strong>Step 6:<\/strong> Define Loss function<\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\"># Minimize the mean squared errors.\r\nloss = tf.reduce_mean(tf.square(y - y_data))<\/pre>\n<div id=\"cxl-affiliate\"><strong>\u00a0Step 7:<\/strong> Define the algorithm that we want to use.<\/div>\n<div><\/div>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\"># For simplicity we are going to use standard Gradient Descent Optimizer\r\noptimizer = tf.train.GradientDescentOptimizer(0.5)\r\ntrain = optimizer.minimize(loss)\r\n<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 8:<\/strong> Initialize<\/span><\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\"># Before starting, initialize the variables.  We will 'run' this first.\r\ninit = tf.initialize_all_variables()<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 9:<\/strong> Launch<\/span><\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\">sess = tf.Session()\r\nsess.run(init)<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 10:<\/strong> Try to fit the line 250 times<\/span><\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:python decode:true\">for step in range(251):\r\n    sess.run(train)\r\n    #Print once every 20 iterations\r\n    if step % 20 == 0:\r\n        print(step, sess.run(W), sess.run(b))<\/pre>\n<p><span style=\"font-weight: 400;\"><strong>Step 11:<\/strong> Understand the result<\/span><\/p>\n<p>The above command will produce the results somewhat like this:<\/p>\n<pre class=\"plain:false plain-toggle:false popup:false lang:default decode:true \">(0, array([-0.32815421], dtype=float32), array([ 0.71403891], dtype=float32))\r\n(20, array([-0.02378235], dtype=float32), array([ 0.36488634], dtype=float32))\r\n(40, array([ 0.0697251], dtype=float32), array([ 0.31596035], dtype=float32))\r\n(60, array([ 0.09259172], dtype=float32), array([ 0.30399585], dtype=float32))\r\n(80, array([ 0.09818359], dtype=float32), array([ 0.30107], dtype=float32))\r\n(100, array([ 0.09955103], dtype=float32), array([ 0.30035451], dtype=float32))\r\n(120, array([ 0.09988545], dtype=float32), array([ 0.30017954], dtype=float32))\r\n(140, array([ 0.09996723], dtype=float32), array([ 0.30013674], dtype=float32))\r\n(160, array([ 0.09998722], dtype=float32), array([ 0.30012628], dtype=float32))\r\n(180, array([ 0.09999212], dtype=float32), array([ 0.30012372], dtype=float32))\r\n(200, array([ 0.0999933], dtype=float32), array([ 0.3001231], dtype=float32))\r\n(220, array([ 0.09999361], dtype=float32), array([ 0.30012295], dtype=float32))\r\n(240, array([ 0.09999362], dtype=float32), array([ 0.30012295], dtype=float32))<\/pre>\n<p><span style=\"font-weight: 400;\">You can see that it has learned after 220 iterations that the best fit is W: [0.09999362] and b: [0.30012295]. So, the equation of the best line that fits the above points is <\/span><b>y = x*<\/b><b><i>0.09999362 + 0.30012295<\/i><\/b><\/p>\n<p><span style=\"font-weight: 400;\">If you plot this line along with the points<\/span><b><i>, <\/i><\/b><i><span style=\"font-weight: 400;\">you would get a plot like this:<\/span><\/i><\/p>\n<p><img class=\"alignnone wp-image-241\" src=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0-2.png\" alt=\"Fitted Line\" width=\"1076\" height=\"646\" \/><\/p>\n<p>Practice TenserFlow now on <a href=\"https:\/\/cloudxlab.com\/\">CloudxLab<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>We are glad to inform you that the TensorFlow is now available on CloudxLab. In this example, we will walk you through a basic tutorial on how to use TensorFlow. What is TensorFlow? TensorFlow is an Open Source Software Library for Machine Intelligence. It is developed and supported by Google and is being adopted very &hellip; <a href=\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using TensorFlow on CloudxLab&#8221;<\/span><\/a><\/p>\n","protected":false},"author":14,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[14],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Using TensorFlow on CloudxLab | CloudxLab Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Using TensorFlow on CloudxLab | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"We are glad to inform you that the TensorFlow is now available on CloudxLab. In this example, we will walk you through a basic tutorial on how to use TensorFlow. What is TensorFlow? TensorFlow is an Open Source Software Library for Machine Intelligence. It is developed and supported by Google and is being adopted very &hellip; Continue reading &quot;Using TensorFlow on CloudxLab&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2016-10-18T04:14:39+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2017-09-15T06:20:06+00:00\" \/>\n<meta property=\"og:image\" content=\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0.png\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"3 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0.png\",\"contentUrl\":\"http:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2016\/10\/pasted-image-0.png\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/\",\"name\":\"Using TensorFlow on CloudxLab | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#primaryimage\"},\"datePublished\":\"2016-10-18T04:14:39+00:00\",\"dateModified\":\"2017-09-15T06:20:06+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/4835f1b3d5000626cb15e9311d748e09\"},\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/tensorflow-getting-started\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/4835f1b3d5000626cb15e9311d748e09\",\"name\":\"Sandeep Giri\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/1393214840cf7455bb4cba055cb30468?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/1393214840cf7455bb4cba055cb30468?s=96&d=mm&r=g\",\"caption\":\"Sandeep Giri\"},\"sameAs\":[\"https:\/\/cloudxlab.com\"]}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/228"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=228"}],"version-history":[{"count":38,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions"}],"predecessor-version":[{"id":753,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/228\/revisions\/753"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=228"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=228"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=228"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}