具有TFRecord和数据集的Tensorflow MNIST精度低

时间:2018-04-15 02:05:02

标签: python tensorflow

我在这里使用脚本转换了MNIST数据集: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/how_tos/reading_data/convert_to_records.py

下面是我用来读取TFRecord,构建模型和训练的代码。

import tensorflow as tf

BATCH_SIZE = 32
epoch = 20

n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)

def parse_func(serialized_data):
    keys_to_features = {'image_raw': tf.FixedLenFeature([],tf.string),
                        'label': tf.FixedLenFeature([], tf.int64)}

    parsed_features = tf.parse_single_example(serialized_data, keys_to_features)
    prices = tf.decode_raw(parsed_features['image_raw'],tf.float32)
    label = tf.cast(parsed_features['label'], tf.int32)
    return prices,tf.one_hot(label - 1, 10)

def input_fn(filenames):
    dataset = tf.data.TFRecordDataset(filenames=filenames)
    dataset = dataset.map(parse_func,num_parallel_calls=8)
    dataset = dataset.batch(BATCH_SIZE).prefetch(50)
    # dataset = dataset.shuffle(2000)

    return dataset.make_initializable_iterator()


weights = {
    'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}

# Create model
def neural_net(x):
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.nn.relu(layer_1)
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    layer_2 = tf.nn.relu(layer_2)
    # Output fully connected layer with a neuron for each class
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

def inference(input):
    input = tf.reshape(input,[-1,784])
    dense = tf.layers.dense(inputs=input, units=1024, activation=tf.nn.relu)

    # Logits Layer
    output = tf.layers.dense(inputs=dense, units=10)
    return output


train_iter = input_fn('train_mnist.tfrecords')
valid_iter = input_fn('validation_mnist.tfrecords')

is_training  = tf.placeholder(shape=[],dtype=tf.bool)

img,labels = tf.cond(is_training,lambda :train_iter.get_next(),lambda :valid_iter.get_next())
# img,labels = train_iter.get_next()

logits = neural_net(img)
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=labels))
train_op = tf.train.AdamOptimizer().minimize(loss_op)

prediction = tf.nn.softmax(logits)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(labels, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for e in range(epoch):
        epoch_loss = 0
        sess.run(train_iter.initializer)
        count = 0
        while True:
            try:
                count +=1
                _,c = sess.run([train_op,loss_op],feed_dict={is_training:True})
                epoch_loss += c
            except tf.errors.OutOfRangeError:
                break

        print('Epoch', e, ' completed out of ', epoch, ' Epoch loss: ',epoch_loss,' count :',count)

        total_acc = 0
        count = 0
        sess.run(valid_iter.initializer)
        while True:
            try:
                count += 1
                acc = sess.run(accuracy,feed_dict={is_training:False})
                total_acc += acc
            except tf.errors.OutOfRangeError:
                break

        print('Accuracy: ', total_acc/count,' count ',count)

我不知道我做错了什么,但是在几个时代之后,损失和准确性都没有提高。我用传统方式feed_dict方法测试了上面的模型。一切都很好,我可以用这个模型达到85%的准确率。这是上面代码的输出

Epoch 0  completed out of  20  Epoch loss:  295472940.19140625  count : 1720
Accuracy:  0.5727848101265823  count  158
Epoch 1  completed out of  20  Epoch loss:  2170057598.328125  count : 1720
Accuracy:  0.22231012658227847  count  158
Epoch 2  completed out of  20  Epoch loss:  6578130587.9375  count : 1720
Accuracy:  0.29944620253164556  count  158
Epoch 3  completed out of  20  Epoch loss:  13321823489.0  count : 1720
Accuracy:  0.13310917721518986  count  158
Epoch 4  completed out of  20  Epoch loss:  22460952288.75  count : 1720
Accuracy:  0.20787183544303797  count  158
Epoch 5  completed out of  20  Epoch loss:  34615459125.0  count : 1720
Accuracy:  0.28560126582278483  count  158
Epoch 6  completed out of  20  Epoch loss:  50057282083.0  count : 1720
Accuracy:  0.11748417721518987  count  158  

我检查了数据集的输出。一切看起来都很正常,形状正确。有人能指出我在这里做错了什么吗?

修改 这是工作代码,它使用传统的feed_dict方法

# Import MNIST data
from tensorflow.examples.tutorials.mnist import input_data
mnist = input_data.read_data_sets("/tmp/data/", one_hot=True)

import tensorflow as tf

BATCH_SIZE = 32
epoch = 5

# Network Parameters
n_hidden_1 = 256 # 1st layer number of neurons
n_hidden_2 = 256 # 2nd layer number of neurons
num_input = 784 # MNIST data input (img shape: 28*28)
num_classes = 10 # MNIST total classes (0-9 digits)

# tf Graph input
X = tf.placeholder("float", [None, num_input])
Y = tf.placeholder("float", [None, num_classes])

# Store layers weight & bias
weights = {
    'h1': tf.Variable(tf.random_normal([num_input, n_hidden_1])),
    'h2': tf.Variable(tf.random_normal([n_hidden_1, n_hidden_2])),
    'out': tf.Variable(tf.random_normal([n_hidden_2, num_classes]))
}
biases = {
    'b1': tf.Variable(tf.random_normal([n_hidden_1])),
    'b2': tf.Variable(tf.random_normal([n_hidden_2])),
    'out': tf.Variable(tf.random_normal([num_classes]))
}


# Create model
def neural_net(x):
    # Hidden fully connected layer with 256 neurons
    layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
    # Hidden fully connected layer with 256 neurons
    layer_2 = tf.add(tf.matmul(layer_1, weights['h2']), biases['b2'])
    # Output fully connected layer with a neuron for each class
    out_layer = tf.matmul(layer_2, weights['out']) + biases['out']
    return out_layer

# Construct model
logits = neural_net(X)
loss_op = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits_v2(logits=logits, labels=Y))
train_op = tf.train.AdamOptimizer().minimize(loss_op)

prediction = tf.nn.softmax(logits)
correct_pred = tf.equal(tf.argmax(prediction, 1), tf.argmax(Y, 1))
accuracy = tf.reduce_mean(tf.cast(correct_pred, "float"))

# Start training
with tf.Session() as sess:

    # Run the initializer
    sess.run(tf.global_variables_initializer())

    for e in range(epoch):
        epoch_loss = 0
        for _ in range(int(mnist.train.num_examples / BATCH_SIZE)):
            epoch_x, epoch_y = mnist.train.next_batch(BATCH_SIZE)
            _, c = sess.run([train_op, loss_op], feed_dict={X: epoch_x, Y: epoch_y})
            epoch_loss += c

        print('Epoch', e, ' completed out of ', epoch, ' Epoch loss: ', epoch_loss)

        # Calculate accuracy for MNIST test images
        print("Testing Accuracy:",sess.run(accuracy, feed_dict={X: mnist.test.images,Y: mnist.test.labels}))

3 个答案:

答案 0 :(得分:1)

如果没有看到您的tfrecords文件,则很难确定,但如果您的数据按照标签排序(即标签的前10%是0,则第二个10%是1等)然后没有洗牌会对你的结果产生重大影响。单个时代后57%的准确率似乎也相当令人惊讶(虽然我从未在那一点上看过结果),所以你的评估指标(准确度)可能不正确(尽管我可以& #39;看不出任何明显的错误。)

如果你还没有看到你的输入(即实际的图像和标签,而不仅仅是形状),那肯定是第一步。

除了你的问题之外,代码的一个明显缺点是缺乏非线性 - 紧跟线性层的线性层相当于线性层。要获得更复杂的行为/更好的结果,请添加非线性,例如:每一层之后的tf.nn.relu除了最后一层之外,例如

layer_1 = tf.add(tf.matmul(x, weights['h1']), biases['b1'])
layer_1 = tf.nn.relu(layer_1)

最后,prefetch大量数据集元素违背了prefetching的目的。 12通常就足够了。

答案 1 :(得分:0)

@Thien,我下载了所有文件并运行它们以生成tfrecords,然后加载tf记录。我检查了你的tf记录,图像批次返回32,194的形状(14x14,而不是28x28)。然后我使用matplotlib来查看图像,它们看起来根本不像数字,看起来不像原始的mnist数据。你的编码/解码成tfrecords是问题。考虑为你的tf记录编写一个编码函数,为你的tf记录编写一个解码函数,然后测试tfdecode(tfencode(a))== a。

    x,y = train_iter.get_next()
    a = sess.run(x)
    import matplotlib.pyplot as plt
    plt.imshow( a[0].reshape(14,14) )
    plt.gray()
    plt.show()

enter image description here

答案 2 :(得分:0)

我发现了自己的错误。在解析函数中,我使用

将标签解码为一个热矢量
tf.one_hot(label - 1, 10)

应该是

tf.one_hot(label, 10)