Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我们什么时候输入的?在Tensorflow/深度学习中_Python_Tensorflow_Deep Learning - Fatal编程技术网

Python 我们什么时候输入的?在Tensorflow/深度学习中

Python 我们什么时候输入的?在Tensorflow/深度学习中,python,tensorflow,deep-learning,Python,Tensorflow,Deep Learning,我正在使用mnist数据集。在编码方面没有问题,但当我想了解代码是如何工作的时候,我不能。我对tensorflow代码的工作有问题。在我的代码中有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]

我正在使用mnist数据集。在编码方面没有问题,但当我想了解代码是如何工作的时候,我不能。我对tensorflow代码的工作有问题。在我的代码中有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)
最后一个主节点

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)
问题

现在的问题是,我们从训练神经网络(x)开始。 然后x进入预测=神经网络模型(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
部分sess.run(tf.global\u variables\u initializer())实现了这一点


tf.global\u variables\u initializer()用于初始化全局变量(即tf.Variable或tf.placeholder),除非它们按照代码中的方式初始化。

是的,我知道我们需要启动全局变量时必须使用它,但还是在预测=神经网络\u模型(x)下?因此python编译器还没有出现在sess.Run中,占位符仍然是空的