TensorFlow:无法将feed_dict键解释为Tensor:Tensor Tensor不是该图的元素

时间:2019-08-04 08:36:10

标签: tensorflow neural-network deep-learning

我试图建立一个具有2个隐藏层的神经网络模型,并使用2048个批处理大小。现在我遇到了一个错误,我的优化程序代码位于:

# Define the optimizer function (train & optimize)
with tf.variable_scope('train', reuse=tf.AUTO_REUSE):
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)

# ... loop running session

        for epoch in range(training_epochs):
            for batch in range(int(n_samples/batch_size)):
                batch_x = X_input[batch*batch_size: (1+batch)*batch_size]
                batch_y = Y_input[batch*batch_size: (1+batch)*batch_size]

                # Reshape
                batch_x = np.reshape(batch_x, (2048, -1))
                batch_y = np.reshape(batch_y, (2048, -1))

                # Run optimizer
                session.run('optimizer', feed_dict={X: batch_x, Y: batch_y})

我已经使用默认图形对其进行了测试,因为我认为这是问题所在,但是即使在tf.Graph()中使用tf.Session()也不起作用。我也重置了整个会话。

浏览了许多相关的问题之后,tf.Graph()似乎已经解决了该问题。我试图弄清楚我在忽略什么。

如果该会话能够正常工作,有哪些最佳实践可以测试各个部分?

代码在下面。

# Define model parameters
learning_rate = 0.005
training_epochs = 5
display_step = 1
training_dropout = 0.90
n_samples = y_train.shape[0]
batch_size = 2048

# Define how many inputs and outputs are in our neural network
number_of_inputs = 30
number_of_outputs = 2

# Maintain fixed ratio of nodes between each layer
multiplier = 1.5

# Number of nodes in each layer
layer_1_nodes = 18
layer_2_nodes = round(layer_1_nodes*multiplier)
layer_3_nodes = round(layer_2_nodes*multiplier)

# Percent of nodes to keep during dropout
dropout_keep = tf.placeholder(tf.float32)

# Define input layer
with tf.variable_scope('input', reuse=tf.AUTO_REUSE):
    X = tf.placeholder(tf.float32, shape=(None, number_of_inputs))

# Layer 1
with tf.variable_scope('layer_1', reuse=tf.AUTO_REUSE):
    weights = tf.get_variable('weights1', shape=[number_of_inputs, layer_1_nodes], initializer=tf.contrib.layers.xavier_initializer())
    biases = tf.get_variable('biases1', shape=[layer_1_nodes], initializer=tf.zeros_initializer)
    layer_1_output = tf.nn.sigmoid(tf.matmul(X, weights) + biases) # matrix multiplication and a standard rectified linear unit 

# Layer 2
with tf.variable_scope('layer_2', reuse=tf.AUTO_REUSE):
    weights = tf.get_variable('weights2', shape=[layer_1_nodes, layer_2_nodes], initializer=tf.contrib.layers.xavier_initializer())
    biases = tf.get_variable('biases2', shape=[layer_2_nodes], initializer=tf.zeros_initializer())
    layer_2_output = tf.nn.sigmoid(tf.matmul(layer_1_output, weights) + biases)

# Layer 3
with tf.variable_scope('layer_3', reuse=tf.AUTO_REUSE):
    weights = tf.get_variable('weights3', shape=[layer_2_nodes, layer_3_nodes], initializer=tf.contrib.layers.xavier_initializer())
    biases = tf.get_variable('biases3', shape=[layer_3_nodes], initializer=tf.zeros_initializer())
    layer_3_output = tf.nn.relu(tf.matmul(layer_2_output, weights) + biases)
    layer_3_output = tf.nn.dropout(layer_3_output, rate=dropout_keep) # keep_prob will be deprecated

# Layer 4 
with tf.variable_scope('layer_4', reuse=tf.AUTO_REUSE):
    weights = tf.get_variable('weights4', shape=[layer_3_nodes, number_of_outputs], initializer=tf.contrib.layers.xavier_initializer())
    biases = tf.get_variable('biases4', shape=[number_of_outputs], initializer=tf.zeros_initializer())
    layer_4_output = tf.nn.softmax(tf.matmul(layer_3_output, weights) + biases)

# Define the cost function
with tf.variable_scope('cost', reuse=tf.AUTO_REUSE):
    Y = tf.placeholder(tf.float32, shape=(batch_size, None))
    cost = -tf.reduce_sum(layer_4_output*tf.log(Y)) # ???

# Define the optimizer function (train & optimize)
with tf.variable_scope('train', reuse=tf.AUTO_REUSE):
    optimizer = tf.train.AdamOptimizer(learning_rate).minimize(cost)


with tf.Graph().as_default():

    # Initialize a training session after defining the model
    with tf.Session() as session:

        # Run the global variable initializer to initialize all variables/layers in the neural network
        session.run(tf.global_variables_initializer()) # executes commands by calling session.run()

        # Training
        for epoch in range(training_epochs):
            for batch in range(int(n_samples/batch_size)):
                batch_x = X_input[batch*batch_size: (1+batch)*batch_size]
                batch_y = Y_input[batch*batch_size: (1+batch)*batch_size]

                # Reshape
                batch_x = np.reshape(batch_x, (2048, -1))
                batch_y = np.reshape(batch_y, (2048, -1))

                # Run optimizer
                session.run('optimizer', feed_dict={X: batch_x, Y: batch_y})

            # Display logs after every epoch
            if (epoch) % display_step == 0:
                train_accuracy, newCost = sess.run([accuracy, cost], feed_dict={X: X_input, Y: Y_input})
                valid_accuracy, newCost = sess.run([accuracy, cost], feed_dict={X: X_input_valid, Y: Y_input_valid})

                print('Epoch: ', epoch,
                      'Acc = {:.5f}'.format(train_accuracy), 
                      'Cost = {:.5f}'.format(newCost),
                      'Valid_Acc = {:.5f}'.format(valid_accuracy), 
                      'Valid_Cost = {:.5f}'.format(valid_newCost))

我希望结果可以优化每个批次并打印出分数。

一旦运行,我想知道我是否正确使用了sess.run([accuracy,cost]),但是由于我首先遇到了当前问题,因此我还无法对其进行检查。

0 个答案:

没有答案