【发布时间】:2017-12-03 06:26:03
【问题描述】:
我有以下代码,我在 MNIST 数据集上训练神经网络。然后,通过训练有素的网络,我试图预测 test_inputs 中的值。
import pandas as pd
import numpy as np
import tensorflow as tf
from math import trunc
from subprocess import check_output
def make_one_hot(m):
result = pd.DataFrame((np.asarray(m)[:,None] == np.arange(10)).astype(int))
return result
train_data = pd.read_csv("../input/train.csv", delimiter=',')
train_labels = make_one_hot(train_data.ix[:, 0])
train_inputs = train_data.ix[:, 1:]
test_inputs = pd.read_csv("../input/test.csv", delimiter=',')
x = tf.placeholder(tf.float32, [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(tf.float32, [None, 10])
cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_ * tf.log(tf.clip_by_value(y,1e-10,1.0)), reduction_indices=[1]))
train_step = tf.train.GradientDescentOptimizer(0.05).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
for _ in range(1000):
batch_xs = train_inputs.sample(n=100)
batch_ys = train_labels.sample(n=100)
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
y = tf.nn.softmax(tf.matmul(x,W) + b)
result = sess.run(y, feed_dict={x: test_inputs})
f = open("results.csv","w+")
f.write("ImageId,Label\n")
for i in range(0, len(result)):
x = 0
for j in range(0, 10):
if(result.item(i, j) == 1):
x = j
f.write("{},{}\n".format(i+1, x))
但是,无论输入如何,网络总是将所有示例的数字预测为相同的数字。
数字本身会发生变化,有时是1,有时是6 或7,但所有示例在同一次运行中得到相同的数字。
知道它可能有什么问题吗?
编辑:
我修复了最后一行的缩进(这是错误的),但所有测试示例的结果仍然具有相同的标签。
【问题讨论】:
-
您是否尝试使用步骤打印训练/开发损失?另外,你为什么不使用
softmax_cross_entropy_with_logits? -
“您是否尝试使用步骤打印训练/开发损失?”不,我没有。我怎样才能做到这一点? "另外,你为什么不使用 softmax_cross_entropy_with_logits" 在哪里使用这个?
-
另外,我认为采用
batch_xs = train_inputs.sample()和batch_ys = train_labels.sample()可能会导致不相关的输入和标签,这不会导致良好的学习...试试batch_ys = train_labels.loc[batch_xs.index]。 -
但我认为主要问题是@jack6e 指出的
-
@PietroTortella 谢谢这实际上是主要问题
标签: python pandas numpy tensorflow