【问题标题】:Tensorflow predicts always the same resultTensorFlow 预测总是相同的结果
【发布时间】:2016-03-21 09:38:48
【问题描述】:

我试图让TensorFlow example 使用我自己的数据运行,但不知何故,分类器总是为每个测试示例选择相同的类。输入数据总是先洗牌。我有大约 4000 张图像作为训练集,500 张图像作为测试集。

我得到的结果如下:

Result: [[ 1.  0.]] Actually: [ 1.  0.] 
Result: [[ 1.  0.]] Actually: [ 0.  1.] 
Result: [[ 1.  0.]] Actually: [ 1.  0.] 
Result: [[ 1.  0.]] Actually: [ 1.  0.] 
Result: [[ 1.  0.]] Actually: [ 0.  1.] 
Result: [[ 1.  0.]] Actually: [ 0.  1.]
...

所有 500 张图片的右侧仍然是 [1. 0.]。分类是二元的,所以我只有两个标签。

这是我的源代码:

import tensorflow as tf
import input_data as id

test_images, test_labels = id.read_images_from_csv(
    "/home/johnny/Desktop/tensorflow-examples/46-model.csv")

train_images = test_images[:4000]
train_labels = test_labels[:4000]
test_images = test_images[4000:]
test_labels = test_labels[4000:]

print len(train_images)
print len(test_images)

pixels = 200 * 200
labels = 2

sess = tf.InteractiveSession()

# Create the model
x = tf.placeholder(tf.float32, [None, pixels])
W = tf.Variable(tf.zeros([pixels, labels]))
b = tf.Variable(tf.zeros([labels]))
y_prime = tf.matmul(x, W) + b
y = tf.nn.softmax(y_prime)

# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, labels])
cross_entropy = tf.nn.softmax_cross_entropy_with_logits(y_prime, y_)
train_step = tf.train.GradientDescentOptimizer(0.01).minimize(cross_entropy)

# Train
tf.initialize_all_variables().run()
for i in range(10):
    res = train_step.run({x: train_images, y_: train_labels})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(accuracy.eval({x: test_images, y_: test_labels}))

for i in range(0, len(test_images)):
    res = sess.run(y, {x: [test_images[i]]})
    print("Result: " + str(res) + " Actually: " + str(test_labels[i]))

我错过了一点吗?

【问题讨论】:

  • 遇到同样的问题,请问您是如何解决上述问题的?
  • @XiaotaoLuo 看答案...
  • 1.根据question,我正在使用默认权重初始化程序glorot_uniform_initializer。 2.cross_entropy是对的。 3. 也减少到更小的批量大小,但我仍然得到所有输入的相同预测。

标签: python numpy tensorflow


【解决方案1】:

您的代码中存在三个潜在问题:

  1. 权重W 被初始化为零。 This question from stats.stackexchange.com 很好地讨论了为什么这会导致训练结果不佳(例如陷入局部最小值)。相反,您应该随机初始化它们,例如如下:

    W = tf.Variable(tf.truncated_normal([pixels, labels],
                                        stddev=1./math.sqrt(pixels)))
    
  2. cross_entropy 应在最小化之前聚合为单个标量值,例如使用 tf.reduce_mean()

    cross_entropy = tf.reduce_mean(
        tf.nn.softmax_cross_entropy_with_logits(y_prime, y_))
    
  3. 如果您在小批量(甚至单个示例)上进行训练而不是一次在整个数据集上进行训练,您可能会获得更快的收敛:

    for i in range(10):
            for j in range(4000):
                res = train_step.run({x: train_images[j:j+1],
                                      y_: train_labels[j:j+1]})
    

【讨论】:

  • 感谢您的提示,帮了我很大的忙
【解决方案2】:

您可能遇到的另一个问题是类别不平衡。如果您有一个类大大超过另一个类,您的功能可能会收敛到该值。尝试平衡训练样本中的类以及使用较小的批次。例如,如果您的标签是二进制的,那么请确保您的训练样本中有相同数量的零和一个标签。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    相关资源
    最近更新 更多