title: Tensorflow- How to display accuracy rate for a linear regression model
body: <p>I have a linear regression model that seems to work. I first load the <code>data</code> into <code>X</code> and the target column into <code>Y</code>, after that I implement the following...</p>
  
  <pre><code>X_train, X_test, Y_train, Y_test = train_test_split(
      X_data, 
      Y_data, 
      test_size=0.2
  )
  
  rng = np.random
  
  n_rows = X_train.shape[0]
  
  X = tf.placeholder("float")
  Y = tf.placeholder("float")
  
  
  W = tf.Variable(rng.randn(), name="weight")
  b = tf.Variable(rng.randn(), name="bias")
  
  pred = tf.add(tf.multiply(X, W), b)
  
  cost = tf.reduce_sum(tf.pow(pred-Y, 2)/(2*n_rows))
  
  optimizer = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(cost)
  
  
  
  init = tf.global_variables_initializer()
  init_local = tf.local_variables_initializer()
  
  with tf.Session() as sess:
  
      sess.run([init, init_local])
  
      for epoch in range(FLAGS.training_epochs):
  
          avg_cost = 0
  
          for (x, y) in zip(X_train, Y_train):
  
              sess.run(optimizer, feed_dict={X:x, Y:y})
  
          # display logs per epoch step
          if (epoch + 1) % FLAGS.display_step == 0:
  
              c = sess.run(
                  cost, 
                  feed_dict={X:X_train, Y:Y_train}
              )
  
              print("Epoch:", '%04d' % (epoch + 1), "cost=", "{:.9f}".format(c))
  
      print("Optimization Finished!")
  
      accuracy, accuracy_op = tf.metrics.accuracy(labels=tf.argmax(Y_test, 0), predictions=tf.argmax(pred, 0))
  
      print(sess.run(accuracy))
  </code></pre>
  
  <p>I cannot figure out how to print out the model's accuracy. For example, in <code>sklearn</code>, it is simple, if you have a model you just print <code>model.score(X_test, Y_test)</code>. But I do not know how to do this in <code>tensorflow</code> or if it is even possible.</p>
  
  <p>I think I'd be able to calculate the <code>Mean Squared Error</code>. Does this help in any way? </p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried implementing <code>tf.metrics.accuracy</code> as suggested in the comments but I'm having an issue implementing it. The documentation says it takes 2 arguments, <code>labels</code> and <code>predictions</code>, so I tried the following...</p>
  
  <pre><code>accuracy, accuracy_op = tf.metrics.accuracy(labels=tf.argmax(Y_test, 0), predictions=tf.argmax(pred, 0))
  
  print(sess.run(accuracy))
  </code></pre>
  
  <p>But this gives me an error...</p>
  
  <blockquote>
    <p>FailedPreconditionError (see above for traceback): Attempting to use uninitialized value accuracy/count
         [[Node: accuracy/count/read = IdentityT=DT_FLOAT, _class=["loc:@accuracy/count"], _device="/job:localhost/replica:0/task:0/device:CPU:0"]]</p>
  </blockquote>
  
  <p>How exactly does one implement this?</p>