我遵循了tensorflow教程,我的代码产生了90%的预测率。
但是,我在下面的链接中使用Kaggle的mnist数据集尝试了相同的代码, https://www.kaggle.com/c/digit-recognizer 但是预测率完全被搞砸了。
我仔细检查了训练数据和标签格式,但无法解决差异的来源。请帮我解决这个问题。
import tensorflow as tf
import pandas as pd
import math
import pylab as plt
import numpy as np
df = pd.read_csv("data/train.csv")
data1 = df.iloc[:,1:].values.astype(np.float32).copy()
label_ = (df.iloc[:,0].values.astype(np.int32).copy())
label1 = np.zeros([label_.shape[0],10]).astype(np.float32)
label1[np.arange(label_.shape[0]),label_] = 1
print(data1.shape)
print(label1[:3,:])
print(label_[:3])
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("MNIST_data/", one_hot=True)
graph = tf.Graph()
optimizer, loss, prediction = None, None, None
x,y = None, None
with graph.as_default():
x = tf.placeholder(tf.float32,[None, 784])
y = tf.placeholder(tf.float32,[None, 10])
hidden = 100
W1 = tf.Variable(tf.truncated_normal([784,hidden]))
b1 = tf.Variable(tf.zeros([hidden]))
W2 = tf.Variable(tf.truncated_normal([hidden,10]))
b2 = tf.Variable(tf.zeros([10]))
hidden = tf.nn.relu(tf.matmul(x,W1) + b1)
logits = tf.matmul(hidden,W2)+b2
loss = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits,y))
optimizer = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
prediction = tf.nn.softmax(logits)
test_prediction = tf.nn.softmax(logits)
def accuracy(predict, label):
return 100*np.sum(np.argmax(predict,1)==np.argmax(label,1))/float(label.shape[0])
with tf.Session(graph=graph) as sess:
tf.initialize_all_variables().run()
dd = mnist.train.images
ll = mnist.train.labels
#dd = data1
#ll = label1
for step in range(10000):
s = step*100%(dd.shape[0])
e = (step+1)*100%(dd.shape[0])
if s>e:
s = (step+1)*100%(dd.shape[0])
e = (step+2)*100%(dd.shape[0])
data = dd[s:e,:]
label = ll[s:e,:]
_,l,predict = sess.run([optimizer, loss, prediction], feed_dict={x:data,y:label})
if step %100 ==0:
print("step at %i: loss at %f and accuracy of %.2f%%" % (step,l,accuracy(predict,label)))