首页 文章

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

提问于
浏览
0

我在这里使用脚本转换了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

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

EDIT 这是工作代码,它使用传统的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 回答

  • 1

    在没有看到你的 tfrecords 文件的情况下,'s difficult to say for sure, but if your data is sorted according to label (i.e. the first 10% of labels are 0s, the second 10% are 1s etc) then failing to shuffle will have a significant effect on your results. 57% accuracy after a single epoch also seems quite surprising (though I'从未在那一点上看过结果),所以它正确(尽管我看不出任何明显的错误) .

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

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

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

    最后, prefetch 大量的数据集元素违背了 prefetching 的目的 . 12 一般就够了 .

  • 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

  • 0

    我发现了自己的错误 . 在解析函数中,我通过使用将标签解码为一个热矢量

    tf.one_hot(label - 1, 10)
    

    它应该是

    tf.one_hot(label, 10)
    

相关问题