我一直试图弄清楚为什么这几个小时没有工作但是我无处可去。真的很感激一些帮助。
它基本上是在tensorflow网站上找到的教程的副本,其中包含一些使用本地数据集的调整。但我只有10%的准确率,这与猜测相同!
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
import tensorflow as tf
df = pd.read_csv('train.csv')
yi = df['label']
df = df.drop('label',1)
labels=[]
for i in range(len(yi)):
#convert to one hot
label = [0,0,0,0,0,0,0,0,0,0]
label[yi[i]]= 1
labels.append(label)
labels = np.array(labels)
df = df.as_matrix()
df_train, df_test, y_train, y_test = train_test_split(df,labels)
x = tf.placeholder('float', [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder('float', [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
def next_batch(num, data, labels):
#get batches for training
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[ i] for i in idx]
labels_shuffle = [labels[ i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
for _ in range(1000):
df_train0, y_train0 = next_batch(100, df_train, y_train)
sess.run(train_step, feed_dict={ x: df_train0, y_: y_train0})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
print(sess.run(accuracy, feed_dict={x:df_test, y_:y_test}))
答案 0 :(得分:1)
你的问题是你用0初始化W,因此没有要修改的梯度,所有的logits都是0
W = tf.Variable(tf.zeros([784, 10]))
您应该随机初始化它以打破对称性。
W = tf.Variable(tf.random_normal([784, 10]))
编辑:没有必要随机化,因为目标logit会破坏对称性。然而,如果有一个隐藏层,那将是必要的。真正的问题似乎在于输入的规模。除以255应该可以解决问题。
答案 1 :(得分:1)
我不知道为什么这有助于提高准确性,所以如果有人能给出更好的答案,请做!
我改变了:
y = tf.nn.softmax(tf.matmul(x, W) + b)
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
是:
y = tf.matmul(x, W) + b
cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
完整代码示例:
import numpy as np
import pandas as pd
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import MultiLabelBinarizer
import tensorflow as tf
from scipy.stats import entropy
def next_batch(num, data, labels):
'''get batches for training'''
idx = np.arange(0 , len(data))
np.random.shuffle(idx)
idx = idx[:num]
data_shuffle = [data[ i] for i in idx]
labels_shuffle = [labels[ i] for i in idx]
return np.asarray(data_shuffle), np.asarray(labels_shuffle)
df = pd.read_csv('train.csv')
df_X = df.iloc[:, 1:]
df_y = df['label']
y_one_hot = MultiLabelBinarizer().fit_transform(df_y.values.reshape(-1, 1))
df_train, df_test, y_train, y_test = train_test_split(df_X.values, y_one_hot)
x = tf.placeholder('float', [None, 784])
W = tf.Variable(tf.zeros([784, 10]))
b = tf.Variable(tf.zeros([10]))
y = tf.nn.softmax(tf.matmul(x, W) + b)
y_ = tf.placeholder('float', [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(y), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
for _ in range(1000):
df_train0, y_train0 = next_batch(100, df_train, y_train)
sess.run(train_step, feed_dict={ x: df_train0, y_: y_train0})
correct_prediction = tf.equal(tf.argmax(y,1), tf.argmax(y_,1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, 'float'))
print(sess.run(accuracy, feed_dict={x:df_test, y_:y_test}))
结果准确度:大约0.88