MNIST: x只是一个占位符,MNIST数据如何传入占位符x?

这是来自sentdex教程的代码:MNIST数据集的数据是如何传输到占位符x中的。

请帮助我,考虑到我只是TensorFlow的初学者,如果这与占位符有关,请解释一下。

提前感谢!

"""os.environ removes the warning"""import osos.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'"""tensorflow starts below"""import tensorflow as tffrom tensorflow.examples.tutorials.mnist import input_datamnist = input_data.read_data_sets("/tmp/data/",one_hot=True)# 10 classes , 0-9"""nodes for the hidden layers"""n_nodes_hl1 = 500n_nodes_hl2 = 500n_nodes_hl3 = 500n_classes = 10 # 0-9batch_size = 100"""placeholders"""x = tf.placeholder('float',[None,784]) # 784 is 28*28 ,i.e., the size of mnist imagesy = tf.placeholder('float')# y is the label of datadef neural_network_model(data):    # biases are added so that the some neurons get fired even when input_data is 0    hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784,n_nodes_hl1])),'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))}    hidden_2_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl1,n_nodes_hl2])),'biases':tf.Variable(tf.random_normal([n_nodes_hl2]))}    hidden_3_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl2,n_nodes_hl3])),'biases':tf.Variable(tf.random_normal([n_nodes_hl3]))}    output_layer = {'weights':tf.Variable(tf.random_normal([n_nodes_hl3,n_classes])),    'biases':tf.Variable(tf.random_normal([n_classes]))}    # (input_data * weights) + biases    l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']) , hidden_1_layer['biases'])    l1 = tf.nn.relu(l1) # activation func    l2 = tf.add(tf.matmul(l1,hidden_2_layer['weights']) , hidden_2_layer['biases'])    l2 = tf.nn.relu(l2) # activation func    l3 = tf.add(tf.matmul(l2,hidden_3_layer['weights']) , hidden_3_layer['biases'])    l3 = tf.nn.relu(l3) # activation func    output = tf.matmul(l3,output_layer['weights']) + output_layer['biases']     return output# we now have modeled a neural networkdef train_neural_network(x):    prediction = neural_network_model(x)    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))    # softmax_cross_entropy_with_logits ==> for changing weights    # we wanna minimize the difference    # AdamOptimizer optionally has a learning_reate : 0.0001    optimizer = tf.train.AdamOptimizer().minimize(cost)    hm_epochs = 5 # cycles of feed forward + back    with tf.Session() as sess:        sess.run(tf.global_variables_initializer()) # replace it with global_variable_initializer        for epoch in range(hm_epochs):            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([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})                epoch_loss += c            print('Epoch',epoch,'completed out of',hm_epochs,' loss:',epoch_loss)        correct = tf.equal(tf.argmax(prediction,1),tf.argmax(y,1))        accuracy = tf.reduce_mean(tf.cast(correct,'float')) # cast changes the data type of a tensor        print('Accuracy: ',accuracy.eval({x:mnist.test.images,y:mnist.test.labels}))if __name__ == "__main__":    train_neural_network(x)

回答:

要查看MNIST数据是如何传输到tf.placeholder()张量xy中的,请关注这些行:

for _ in range(int(mnist.train.num_examples/batch_size)):    epoch_x, epoch_y = mnist.train.next_batch(batch_size)     _, c = sess.run([optimizer, cost], feed_dict = {x: epoch_x, y: epoch_y})

数组epoch_xepoch_y是一对(命名上有些令人困惑的)NumPy数组,分别包含来自MNIST训练数据集的batch_size大小的图像和标签批次。它们在for循环的每次迭代中会包含不同的批次。

feed_dict参数传递给sess.run(),告诉TensorFlow用epoch_x的值替换占位符x,用epoch_y的值替换占位符y。因此,TensorFlow将使用这些值来运行优化算法(在本例中是Adam)的一步操作。

请注意,MNIST数据也在这行被使用:

print('Accuracy: ', accuracy.eval({x: mnist.test.images, y: mnist.test.labels}))

…但在这里,程序使用整个测试数据集来评估模型的准确性。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注