Tensorflow多GPU MNIST分类器:精度低

时间:2017-12-15 01:05:34

标签: machine-learning tensorflow neural-network classification conv-neural-network

我在Tensorflow中遇到了多个GPU MNIST分类器。代码运行没有错误,但准确性很差(30%)。我是Tensorflow的新手,所以我不知道问题出在哪里? GPU:2x GTX 1080 Ti。

我找到了多个GPU的教程,但代码很难遵循。出于这个原因,我试图从头开发MNIST CNN分类器。

{{1}}

3 个答案:

答案 0 :(得分:1)

我注意到的问题:

  • 您的交叉熵丢失是错误的(有关详细信息,请参阅this question,简而言之,您计算二进制交叉熵)。
  • 我放弃了手动渐变计算,转而使用tf.train.AdamOptimizer
  • 我放弃了x输入的分割(它不是在tensorflow中进行分布式计算的正确方法)。

即使在一个GPU上,结果模型也很容易达到99%的准确度。

from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
import datetime

x = tf.placeholder(tf.float32, [None, 784], name='x')
x_img = tf.reshape(x, [-1, 28, 28, 1])
y = tf.placeholder(tf.float32, [None, 10], name='y')
keep_prob = tf.placeholder(tf.float32)

stddev = 0.1
w0 = tf.get_variable('w0', initializer=tf.truncated_normal([5, 5, 1, 32], stddev=stddev))
b0 = tf.get_variable('b0', initializer=tf.zeros([32]))

w1 = tf.get_variable('w1', initializer=tf.truncated_normal([5, 5, 32, 64], stddev=stddev))
b1 = tf.get_variable('b1', initializer=tf.zeros([64]))

w2 = tf.get_variable('w2', initializer=tf.truncated_normal([7 * 7 * 64, 1024], stddev=stddev))
b2 = tf.get_variable('b2', initializer=tf.zeros([1024]))

w3 = tf.get_variable('w3', initializer=tf.truncated_normal([1024, 10], stddev=stddev))
b3 = tf.get_variable('b3', initializer=tf.zeros([10]))

def conv2d(xx, W):
  return tf.nn.conv2d(xx, W, strides=[1, 1, 1, 1], padding='SAME')

def max_pool_2x2(xx):
  return tf.nn.max_pool(xx, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')

def model_forward(xx):
  h_conv1 = tf.nn.relu(conv2d(xx, w0) + b0)
  h_pool1 = max_pool_2x2(h_conv1)

  h_conv2 = tf.nn.relu(conv2d(h_pool1, w1) + b1)
  h_pool2 = max_pool_2x2(h_conv2)

  h_pool2_flat = tf.reshape(h_pool2, [-1, 7 * 7 * 64])
  h_fc1 = tf.nn.relu(tf.matmul(h_pool2_flat, w2) + b2)
  h_fc1_drop = tf.nn.dropout(h_fc1, keep_prob)
  y = tf.matmul(h_fc1_drop, w3) + b3
  return y

yy = model_forward(x_img)
loss = tf.reduce_mean(tf.nn.sigmoid_cross_entropy_with_logits(logits=yy, labels=y))
train_step = tf.train.AdamOptimizer().minimize(loss)
correct_prediction = tf.equal(tf.argmax(yy, 1), tf.argmax(y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name='accuracy')

def main():
  mnist = input_data.read_data_sets("/home/maxim/p/data/mnist-tf", one_hot=True)
  with tf.Session(config=tf.ConfigProto(log_device_placement=True)) as sess:
    sess.run(tf.global_variables_initializer())

    t1_1 = datetime.datetime.now()
    for step in range(0, 10000):
      batch_x, batch_y = mnist.train.next_batch(100)
      sess.run(train_step, feed_dict={x: batch_x, y: batch_y, keep_prob: 0.5})

      if (step % 200) == 0:
        print(step, sess.run(accuracy, feed_dict={x: mnist.test.images, y: mnist.test.labels, keep_prob: 1}))

    t2_1 = datetime.datetime.now()
    print("Computation time: " + str(t2_1 - t1_1))

if __name__ == "__main__":
  main()

现在,如果你真的想要它,你可以做数据或模型并行来利用你的GPU能力(关于它有a great post,但有时由于托管问题它没有正确渲染)。

答案 1 :(得分:1)

除了前两个答案中提到的要点,再看看average_gradients函数中的return average_grads,它是从第一个for循环的第一次迭代返回的,这意味着渐变仅适用于第一个变量(大概是w0)。因此,只有w0会被更新,因此精度会很低,因为其余变量均保持其原始值(随机/零)。

答案 2 :(得分:0)

这是因为该模型没有使用相同的权重和偏向CPU以及其他GPU设备的推理。

例如:

for i in range(0,2):
    with tf.device(('/gpu:{0}').format(i)):
        with tf.variable_scope(('scope_gpu_{0}').format(i)) as infer_scope:
            yy=model_forward(x_dict[('x{0}').format(i)])
            infer_scope.reuse_variables()
            cross_entropy = tf.reduce_mean(-tf.reduce_sum(y_dict[('y{0}').format(i)] * tf.log(yy), reduction_indices=[1]))
            grads.append(opt.compute_gradients(cross_entropy,tf.trainable_variables()))

您获得低精度的原因是,如果没有指定reuse_variables()并且您尝试在每个时期内调用模型推断,则该图将创建具有随机权重&的新模型。偏见初始化,这不是你喜欢的。