繁体   English   中英

从 tensorflow_datasets 加载 mnist 数据集的问题

[英]Issues loading mnist dataset from tensorflow_datasets

我目前正在阅读 Tom Hope、Yehezkel S. Resheff 和 Itay Lieder 所著的《学习 TensorFlow:构建深度学习系统指南》一书。 这是一本较旧的书,使用 tensorflow 1.x 版本和来自 tensorflow.examples.tutorials.mnist 的 mnist 数据集。 由于我安装了最新版本的 tensorflow,因此我尝试修改第 4 章中的代码,并且我即将让它运行,但我在正确加载 mnist 进行训练时遇到了问题。 这是我的代码修改代码:

import tensorflow_datasets as tfds
import tensorflow as tf
import numpy as np

from layers import conv_layer, max_pool_2x2, full_layer

tf.compat.v1.disable_eager_execution()

DATA_DIR = '../data/'
MINIBATCH_SIZE = 50
STEPS = 5000


mnist = tfds.load(name='mnist', split=['train', 'test'], data_dir=DATA_DIR)

x = tf.compat.v1.placeholder(tf.float32, shape=[None, 784])
y_ = tf.compat.v1.placeholder(tf.float32, shape=[None, 10])

x_image = tf.reshape(x, [-1, 28, 28, 1])
conv1 = conv_layer(x_image, shape=[5, 5, 1, 32])
conv1_pool = max_pool_2x2(conv1)

conv2 = conv_layer(conv1_pool, shape=[5, 5, 32, 64])
conv2_pool = max_pool_2x2(conv2)

conv2_flat = tf.reshape(conv2_pool, [-1, 7*7*64])
full_1 = tf.nn.relu(full_layer(conv2_flat, 1024))

keep_prob = tf.compat.v1.placeholder(tf.float32)
full1_drop = tf.nn.dropout(full_1, rate=1-keep_prob)

y_conv = full_layer(full1_drop, 10)

cross_entropy = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=y_conv, labels=y_))
train_step = tf.compat.v1.train.AdamOptimizer(1e-4).minimize(cross_entropy)
correct_prediction = tf.equal(tf.argmax(y_conv, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))

with tf.compat.v1.Session() as sess:
    sess.run(tf.compat.v1.global_variables_initializer())

    for i in range(STEPS):
        batch = mnist.train.next_batch(MINIBATCH_SIZE)

        if i % 100 == 0:
            train_accuracy = sess.run(accuracy, feed_dict={x: batch[0], y_: batch[1],
                                                           keep_prob: 1.0})
            print("step {}, training accuracy {}".format(i, train_accuracy))

        sess.run(train_step, feed_dict={x: batch[0], y_: batch[1], keep_prob: 0.5})

    X = mnist.test.images.reshape(10, 1000, 784)
    Y = mnist.test.labels.reshape(10, 1000, 10)
    test_accuracy = np.mean(
        [sess.run(accuracy, feed_dict={x: X[i], y_: Y[i], keep_prob: 1.0}) for i in range(10)])

print("test accuracy: {}".format(test_accuracy))

并在此处提供原始代码以供参考:

https://github.com/Hezi-Resheff/Oreilly-Learning-TensorFlow/blob/master/04__convolutional_neural_networks/mnist_cnn.py

当我运行修改后的代码时,出现此错误:

File "/home/af/Dokumenter/Programs/LearningTensorFlow/Chapter 4/mnist_cnn.py", line 43, in <module>
    batch = mnist.train.next_batch(MINIBATCH_SIZE)
AttributeError: 'list' object has no attribute 'train'

现在我正在从 tensorflow_datasets 加载 mnist 数据集,我不确定如何修改该行。 欢迎任何建议或提示。

mnist.traindataSet数据集的一个实例,在您的情况下似乎缺少。 可以在read_data_sets() function here中看到,所以我的建议是读取如下数据:

mnist = input_data.read_data_sets(DATA_DIR, one_hot=True)

在这种情况下,label 向量y的大小为[batchsize,10] (2D numpy 数组)。 但是,如果one_hot=False ,则 label 向量的大小将等于[batchsize]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM