张量流中的MNIST分类,RecursionError:超出最大递归深度

zer*_*vty 1 python neural-network python-3.x deep-learning tensorflow

我为MNIST分类运行了一个神经网络模型并收到了错误 -

RecursionError: maximum recursion depth exceeded
Run Code Online (Sandbox Code Playgroud)

我检查了stackoverflow上的一些问题,并试图将递归限制增加到1500但是没有用.我该如何增加限额?我怎么知道什么限制会导致堆栈溢出?

我从这里开始学习

我的Windows 10机器上有Anaconda 3.5发行版.

完整的代码在这里 -

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data

mnist= input_data.read_data_sets("/tmp/data/", one_hot=True)

n_nodes_hl1 = 500
n_nodes_hl2 = 500
n_nodes_hl3 =500

n_classes = 10
batch_size = 100

#height x weight
x = tf.placeholder('float', [None, 784])
y = tf.placeholder('float')

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]))
                 }
    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_hl1, n_nodes_hl2])),
                  '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]))
                }

#our model= (input_data x weights) + biases

    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.matmul(l3, output_layer['weights']) + output_layer['biases']

    return output


def train_neural_network(x):
    prediction = train_neural_network(x)
    cost = tf.reduce_mean(tf.nn.softmax_cross_entropy_with_logits(prediction,y))
    optimizer= tf.train.AdamOptimizer().minimize(cost) #default learning rate for adamoptimizer= 0.001

    hm_epochs = 5
    with tf.Session() as sess:
        sess.run(tf.initialize_all_variables())
        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)
Run Code Online (Sandbox Code Playgroud)

Rég*_* B. 6

我不知道确切的代码应该是什么,但我很确定以下行是错误的:

def train_neural_network(x):
    prediction = train_neural_network(x)
Run Code Online (Sandbox Code Playgroud)

这将导致无限递归,并且增加递归限制将无法解决问题.