title: Logistic regression 100% test accuracy: What am I doing wrong?
body: <p>I am using tensorflow on mnist handwritten numbers. Here, I keep getting 100% accuracy on a test set when using logistic regression (I am expecting something much less accuracy). </p>
  
  <p>The following is the code I used. Can someone please point out what I am doing wrong? </p>
  
  <p>I must be somehow overfitting the data, but I cannot figure out why the accuracy is this high. Also, when I tested the model on random  handwritten numbers, it doesn't get the prediction right always, so most likely the way I am calculating the accuracy must be incorrect. I believe I am using the correct formulas here. I am stumped here. Any help will be much appreciated. Thanks.</p>
  
  <pre><code>import tensorflow as tf
  gpu_options = tf.GPUOptions(allow_growth=True, per_process_gpu_memory_fraction=0.5)
  s = tf.InteractiveSession(config=tf.ConfigProto(gpu_options=gpu_options))
  
  import numpy as np
  import matplotlib.pyplot as plt
  %matplotlib inline 
  
  from sklearn.datasets import load_digits
  cl = 2                       # number of classes = 2
  mnist = load_digits(cl) 
  X,y = mnist.data, mnist.target
  
  # inputs and shareds
  weights = tf.Variable(tf.zeros([64, cl])) # 8x8 image, 2 output classes
  b = tf.Variable(tf.zeros([cl]))
  input_X = tf.placeholder('float32', [None,64])
  input_y = tf.placeholder('float32', [None,cl])
  
  accuracy_train = tf.Variable(tf.zeros([1]))
  accuracy_test = tf.Variable(tf.zeros([1]))
  
  
  # create model to predict y
  predicted_y = tf.nn.sigmoid(tf.matmul(input_X, weights) + b) 
  
  loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=predicted_y, labels = input_y))
  
  optimizer = tf.train.GradientDescentOptimizer(0.001).minimize(loss)
  
  def train_function(X, y):
       weights, c = s.run([optimizer, loss], {input_X:X, input_y:y})
       return weights, c
  
  def predict_function(X):
      predict = s.run(probs, {input_X:X})
      return predict
  
  from sklearn.model_selection import train_test_split
  X_train, X_test, y_train, y_test = train_test_split(X, y)
  y_train.shape = (y_train.shape[0],1)
  y_test.shape = (y_test.shape[0],1)
  
  # one-hot encoding
  from sklearn.preprocessing import OneHotEncoder
  enc = OneHotEncoder(sparse=False)
  y_train = enc.fit_transform(y_train).astype('int')
  y_test = enc.fit_transform(y_test).astype('int')
  
  from sklearn.metrics import roc_auc_score
  batch_size =16
  s.run(tf.global_variables_initializer())
  
  for epoch in range(15):
      avg_loss = 0
  
      num_batches = int(X_train.shape[0]/batch_size)
      for i in range(num_batches):
          batch_x, batch_y = X_train[i:i+num_batches], y_train[i:i+num_batches]
          _, c = train_function(batch_x, batch_y)
  
  
          avg_loss += c/num_batches
  
          print("\nloss at iter %i:%.4f" % (i, np.sum(c)))
          print("train auc:",roc_auc_score(y_train, s.run(predicted_y, {input_X:X_train})))
          print("test auc:",roc_auc_score(y_test, s.run(predicted_y, {input_X:X_test})))
          print("Average loss = ", avg_loss)
  
  plt.scatter(epoch,avg_loss, color='b', marker='x')
  plt.xlabel('epoch')
  plt.ylabel('Avg loss')
  
  # Prediction
  probs = tf.equal(tf.argmax(predicted_y,1), tf.argmax(input_y,1))
  
  # Accuracy
  accuracy = tf.reduce_mean(tf.cast(probs, tf.float32))
  
  
  print("Training accuracy: ",s.run(accuracy, {input_X:X_train, input_y:y_train}))
  print("Test accuracy: ", s.run(accuracy, {input_X:X_test, input_y:y_test}))
  
  
  # Testing on a random image
  fn = ["0.png", "1.jpg", "1_1.jpg", "One.png", "1_2.jpeg", "1-3.png"]
  filename = np.random.choice(fn)
  
  from PIL import Image
  def resize_image(image):
      img = Image.open(image)
      img = img.resize((8,8))
      arr = np.array(img)
  
      #convert to gray scale
      if len(arr.shape) &gt; 2:
          arr = np.mean(arr, 2)
  
      #flatten 
      arr = arr.flatten()
  
      return arr          
  
  if any("0" in s for s in filename):
      test_label = 0
  elif any("1" in s for s in filename):
      test_label = 1
  elif any("2" in s for s in filename):
      test_label = 2
  elif any("3" in s for s in filename):
      test_label = 3
  elif any("4" in s for s in filename):
      test_label = 4
  elif any("5" in s for s in filename):
      test_label = 5
  elif any("6" in s for s in filename):
      test_label = 6
  elif any("7" in s for s in filename):
      test_label = 7
  elif any("8" in s for s in filename):
      test_label = 8
  elif any("9" in s for s in filename):
      test_label = 9
  test_label
  
  test_image = resize_image(filename)
  
  new_predict = predict_function(test_image.reshape((1, 64)))
  print("predicted label: ", s.run(tf.argmax(new_predict, 1))[0])
  </code></pre>