我是TensorFlow新手,我正在使用MNIST数据集遵循初学者的教程,我想只使用0-8 (不包括9级)来训练模型,所以在代码中的哪里是10,我将其替换为9,但是在代码的训练部分,如何要求next_batch()排除9级?如果我想排除多个类呢?
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import argparse
import sys
from tensorflow.examples.tutorials.mnist import input_data
import tensorflow as tf
FLAGS = None
def main(_):
# Import data
mnist = input_data.read_data_sets(FLAGS.data_dir, one_hot=True)
# Create the model
x = tf.placeholder(tf.float32, [None, 784])
W = tf.Variable(tf.zeros([784, 9])) # was 10 instead of 9
b = tf.Variable(tf.zeros([9])) # was 10 instead of 9
y = tf.matmul(x, W) + b
# Define loss and optimizer
y_ = tf.placeholder(tf.float32, [None, 9]) # was 10 instead of 9
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits(labels=y_, logits=y))
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(cross_entropy)
sess = tf.InteractiveSession()
tf.global_variables_initializer().run()
# Train
for _ in range(1000):
batch_xs, batch_ys = mnist.train.next_batch(100) # How to exclude the class 9 ?
sess.run(train_step, feed_dict={x: batch_xs, y_: batch_ys})
# Test trained model
correct_prediction = tf.equal(tf.argmax(y, 1), tf.argmax(y_, 1))
accuracy = tf.reduce_mean(tf.cast(correct_prediction, tf.float32))
print(sess.run(accuracy, feed_dict={x: mnist.test.images,
y_: mnist.test.labels}))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('--data_dir', type=str, default='/tmp/tensorflow/mnist/input_data',
help='Directory for storing input data')
FLAGS, unparsed = parser.parse_known_args()
tf.app.run(main=main, argv=[sys.argv[0]] + unparsed)发布于 2018-01-06 00:53:37
您应该从mnist数据对象中提取训练数据,删除所需的类,然后继续。首先获取不包含9类的数据集:
Xdata_no9 = np.array([x for (x,y) in zip(mnist.train.images,mnist.train.labels) if y[9]==0])
ydata_no9 = np.array([y[0:9] for y in mnist.train.labels if y[9]==0])请注意,y[0:9]将大小从10减小为9。这样就可以了,但是现在您需要构建自己的代码来拉取一个小批量。下面是一种简单的方法:
n = Xdata_no9.shape[0]
batch_size = 100
batch = np.floor(np.random.rand(batch_size)*n).astype(int)
batch_xs = Xdata_no10[batch,:]
batch_ys = ydata_no10[batch,:]请注意,您可以将此代码压缩一点,但我编写它的目的是为了更有指导意义。
注意事项:这样做(从训练集中删除课程)是更好的做法:如果您不想对部分数据进行训练,则应该尽早将其从数据集中删除,而不是要求每次调用该数据时都要记住应该忽略哪些数据部分。在这个例子中,这并不重要,因为你只在训练中使用它,但是如果你试图评估整个训练集的性能,那么它当然会中断(除非你记得再次忽略那个类)。
发布于 2018-01-06 00:56:19
在您获得batch_xs和batch_ys之后,这行代码应该会立即执行此操作
batch_xs, batch_ys = zip(*[(x_, y_[:-1]) for x_, y_ in zip(batch_xs, batch_ys) if np.argmax(y_) not in [9]])https://stackoverflow.com/questions/48117562
复制相似问题