title: TensorFlow Linear Regression
body: <p>I am trying to use Tensorflow to calculate the linear regression of some data.
  I do not understand why cannot predict a decent line.
  Below the result I am getting:</p>
  <p><a href="https://i.stack.imgur.com/JLwat.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/JLwat.png" alt="enter image description here" /></a></p>
  <p>This is my code, I have tried to change different parameters but nothing to do.</p>
  <p>Any suggestion is welcome.</p>
  <pre><code># Prepare the data
  x = df[&quot;Attainment8_float&quot;]
  y = df[&quot;Progress8_float&quot;]
  
  # Check the data
  plt.scatter(x, y)
  plt.show()
  
  # TensorFlow Model
  
  # Config
  num_epochs = 1000
  learning_rate = 0.0001
  # /Config
  
  # Creating the graph
  ops.reset_default_graph()
  
  tf.disable_v2_behavior()
  X = tf.placeholder(tf.float32, name='X')
  Y = tf.placeholder(tf.float32, name='Y')
  
  a = tf.get_variable('a', initializer=0.)
  b = tf.get_variable('b', initializer=0.)
  
  h = a * X + b
  
  cost = tf.reduce_mean( (h - Y)**2 )
  
  optimizer = tf.train.GradientDescentOptimizer(
      learning_rate=learning_rate
  ).minimize(cost)
  
  init = tf.global_variables_initializer()
  
  # Running the Model
  found_a = 0
  found_b = 0
  
  with tf.Session() as sess:
      sess.run(init)
      for epoch in range(num_epochs):
          _, costValue = sess.run(
              [optimizer, cost],
              feed_dict={
                  X: x,
                  Y: y,
              }
          )
          found_a = a.eval()
          found_b = b.eval()
          if epoch % (num_epochs/10) == 0: # Every 10 percent
              print(&quot;... epoch: &quot; + str(epoch))
              print(f&quot;cost[{str(costValue)}] / a[{str(a.eval())}] / b[{str(b.eval())}]&quot;)
  
  # Seing the obtained values in a plot
  xrange = np.linspace(x.min(), x.max(), 2)
  
  # Plot points
  plt.plot(x, y, 'ro')
  
  # Plot resulting function
  plt.plot(xrange, xrange * found_a + found_b, 'b')
  
  plt.show()
  </code></pre>