首页 > 解决方案 > 我们什么时候输入的?在 TensorFlow/深度学习

问题描述

我正在使用 mnist 数据集。在编码中没有问题一切都很好但是当我想了解代码是如何工作的时,我不能。我对张量流代码的作品有疑问。在我的代码中有 3 个隐藏层和一个隐藏层。

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 = 500
n_classes   = 10
batch_size  = 100

x = tf.placeholder('float',[None,784])
y = tf.placeholder('float',[None,10])

我的 NNA 模型:

def neural_network_model(data):
hidden_1_layer = {'weights':tf.Variable(tf.random_normal([784,n_nodes_hl1])),
                  'biases':tf.Variable(tf.random_normal([n_nodes_hl1]))} #output [None,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]))} #output [None, 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]))}

l1 = tf.add(tf.matmul(data,hidden_1_layer['weights']),hidden_1_layer['biases'])
l1 = tf.nn.relu(l1)

l2 = tf.add(tf.matmul(l1, hidden_2_layer['weights']),hidden_2_layer['biases'])
l2 = tf.nn.relu(l2)

l3 = tf.add(tf.matmul(l2, hidden_3_layer['weights']),hidden_3_layer['biases'])
l3 = tf.nn.relu(l3)

output = tf.add(tf.matmul(l3, output_layer['weights']),output_layer['biases'])
return output

而这列火车

def train_neural_network(x):

prediction = neural_network_model(x)
cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(logits=prediction,labels=y))

#                                 learning_rate = .001
optimizer = tf.train.AdamOptimizer().minimize(cost)

#cycles feed forward + bckprop
hm_epochs = 10

with tf.Session() as sess:

    sess.run(tf.global_variables_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'))
    print('ACCURACY:',accuracy.eval({x:mnist.test.images, y:mnist.test.labels}))

最后一个主要

train_neural_network(x)

问题

现在的问题是,我们从 train_neural_network(x) 开始。然后 x 去预测 = neural_network_model(x)

问题 x,它是一个占位符,没有数据。它还没有喂食。那么神经网络如何给出一个值或输出呢?

输出

EPOCH: 0 COMPLETED OUT OF: 10 LOSS 1700288.9460601807
EPOCH: 1 COMPLETED OUT OF: 10 LOSS 396225.95077705383
EPOCH: 2 COMPLETED OUT OF: 10 LOSS 218223.26675343513
EPOCH: 3 COMPLETED OUT OF: 10 LOSS 127340.1717197299
EPOCH: 4 COMPLETED OUT OF: 10 LOSS 79653.2217836988
EPOCH: 5 COMPLETED OUT OF: 10 LOSS 50255.51314896345
EPOCH: 6 COMPLETED OUT OF: 10 LOSS 33049.792469450236
EPOCH: 7 COMPLETED OUT OF: 10 LOSS 24826.763957113028
EPOCH: 8 COMPLETED OUT OF: 10 LOSS 22047.228584885597
EPOCH: 9 COMPLETED OUT OF: 10 LOSS 17888.153175204916
ACCURACY: 0.9496

Process finished with exit code 0

标签: pythontensorflowdeep-learning

解决方案


sess.run(tf.global_variables_initializer()) 部分可以解决问题。

tf.global_variables_initializer() 用于初始化全局变量(即 tf.Variable 或 tf.placeholder),除非它们在您的代码中被初始化。


推荐阅读