Logistic回归100%测试准确度:我做错了什么?

时间:2018-08-23 01:47:06

标签: python-3.x tensorflow machine-learning logistic-regression mnist

我在mnist手写数字上使用tensorflow。在这里,使用逻辑回归时,我在测试集上的准确率一直保持100%(我希望准确度要低得多)。

以下是我使用的代码。有人可以指出我做错了吗?

我必须以某种方式过度拟合数据,但是我无法弄清楚为什么精度如此之高。另外,当我在随机手写数字上测试模型时,它并不能始终正确地进行预测,因此很可能我计算精度的方法必须不正确。我相信我在这里使用正确的公式。我被困在这里。任何帮助都感激不尽。谢谢。

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) > 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])

0 个答案:

没有答案