точность примера tensorflow mnist не увеличивается

Я следую этому руководству, чтобы изучить tensorflow и tensorboard. Ниже мой код. Точность застряла на случайном уровне. Я не мог найти, где не так.

Может ли кто-нибудь указать, где ошибка? Я также хотел бы знать, как следует отлаживать тензорный поток. Спасибо.


импорт

from tensorflow.examples.tutorials.mnist import input_data  
mnist = input_data.read_data_sets('MNIST_data', one_hot=True)  
import tensorflow as tf

определить конверсионный слой

def conv_layer(input, size_in, size_out, name="conv"):  
    with tf.name_scope(name):  
        w = tf.Variable(tf.truncated_normal([5, 5, size_in, size_out], stddev=0.1), name="W")
        b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
        conv = tf.nn.conv2d(input, w, strides=[1,1,1,1], padding="SAME")
        act = tf.nn.relu(conv + b)
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", act)
        return tf.nn.max_pool(act, ksize=[1,2,2,1], strides=[1,2,2,1], padding="SAME")

определить слой fc

def fc_layer(input, size_in, size_out, name="fc"):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")
        b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
        act = tf.nn.relu(tf.matmul(input, w) + b)
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", act)
        return act

определить модель

def mnist_model(learning_rate, path):
    tf.reset_default_graph()
    sess = tf.Session()

    x = tf.placeholder(tf.float32, shape=[None, 784], name="x")
    x_image = tf.reshape(x, [-1, 28, 28, 1])
    tf.summary.image('input', x_image, 3)
    y = tf.placeholder(tf.float32, shape=[None, 10], name="labels")

    conv1 = conv_layer(x_image, 1, 32, "conv1")
    conv_out = conv_layer(conv1, 32, 64, "conv2")

    flattened = tf.reshape(conv_out, [-1, 7 * 7 * 64])

    fc1 = fc_layer(flattened, 7 * 7 * 64, 1024, "fc1")
    logits = fc_layer(fc1, 1024, 10, "fc2")

    with tf.name_scope("xent"):
        xent = tf.reduce_mean(
            tf.nn.softmax_cross_entropy_with_logits(
                logits=logits, labels=y), name="xent")
        tf.summary.scalar("xent", xent)

    with tf.name_scope("train"):
        train_step = tf.train.AdamOptimizer(learning_rate).minimize(xent)

    with tf.name_scope("accuracy"):
        correct_prediction = tf.equal(tf.argmax(logits, 1), tf.argmax(y, 1))
        accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
        tf.summary.scalar("accuracy", accuracy)

    summ = tf.summary.merge_all()

    sess.run(tf.global_variables_initializer())
    writer = tf.summary.FileWriter(path)
    writer.add_graph(sess.graph)


    for i in range(2000):
        batch = mnist.train.next_batch(100)
        if i % 50 == 0:
            [train_accuracy, s] = sess.run([accuracy, summ], feed_dict={x: batch[0], y: batch[1]})
            print train_accuracy
            writer.add_summary(s, i)
        sess.run(train_step, feed_dict={x: batch[0], y: batch[1]})

бежать

mnist_model(1e-3, path = "/tmp/mnist_demo/10")

выход

0.09
0.08
0.04
0.07
0.12
0.12
0.09
0.12
0.08
0.1
0.11
0.14
0.11
0.11
0.13
0.11
0.19
0.06

person Q. Li    schedule 05.04.2017    source источник


Ответы (1)


Проблема в том, что вы применяете активацию relu на последнем слое, поэтому все логиты имеют нулевой порог.

Решение:

сдача

def fc_layer(input, size_in, size_out, name="fc"):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")
        b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
        act = tf.nn.relu(tf.matmul(input, w) + b)
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", act)
        return act

to

def fc_layer(input, size_in, size_out, name="fc", activation=tf.nn.relu):
    with tf.name_scope(name):
        w = tf.Variable(tf.truncated_normal([size_in, size_out], stddev=0.1), name="W")
        b = tf.Variable(tf.constant(0.1, shape=[size_out]), name="B")
        act = tf.matmul(input, w) + b
        if activation is not None:
            act = activation(act)
        tf.summary.histogram("weights", w)
        tf.summary.histogram("biases", b)
        tf.summary.histogram("activations", act)
        return act

И передайте None как активацию в последнем полносвязном слое:

logits = fc_layer(fc1, 1024, 10, "fc2", activation=None)
person Dmitriy Danevskiy    schedule 05.04.2017
comment
Благодарю вас! У вас также есть советы по отладке в тензорном потоке? - person Q. Li; 05.04.2017
comment
Ну, афаик, общего решения нет. В Tensorflow 1.0 есть отладчик, но я с ним еще не игрался. Хороший выбор — распечатать (в текстовой или графической форме) как можно больше информации (например, градиенты, активации, веса) и попытаться выяснить, где ошибка. Это помогает найти решение в большинстве случаев. Также помогает изучение графика (в тензорной доске). - person Dmitriy Danevskiy; 05.04.2017