InvalidArgumentError:您必须为占位符张量提供一个值' ground_truth' dtype double

时间:2017-05-02 11:24:17

标签: python numpy tensorflow

我试图通过Tensorflow了解转移学习。但是我得到了规定的错误 这是我的代码

def add_final_training_ops(graph, class_count, final_tensor_name,
                           ground_truth_tensor_name):
    """Adds a new softmax and fully-connected layer for training.
    We need to retrain the top layer to identify our new classes, so this function
    adds the right operations to the graph, along with some variables to hold the
    weights, and then sets up all the gradients for the backward pass.
    The set up for the softmax and fully-connected layers is based on:
    https://tensorflow.org/versions/master/tutorials/mnist/beginners/index.html
    Args:
      graph: Container for the existing model's Graph.
      class_count: Integer of how many categories of things we're trying to
      recognize.
      final_tensor_name: Name string for the new final node that produces results.
      ground_truth_tensor_name: Name string of the node we feed ground truth data
      into.
    Returns:
      Nothing.
    """
    bottleneck_tensor1 = graph.get_tensor_by_name(ensure_name_has_port(
        BOTTLENECK_TENSOR_NAME))
    bottleneck_tensor = tf.placeholder_with_default(bottleneck_tensor1, shape=[None, 2048])
    layer_weights = tf.Variable(
        tf.truncated_normal([BOTTLENECK_TENSOR_SIZE, class_count], stddev=0.001),
        name='final_weights')
    layer_biases = tf.Variable(tf.zeros([class_count]), name='final_biases')
    logits = tf.matmul(bottleneck_tensor, layer_weights,
                       name='final_matmul') + layer_biases
    tf.nn.softmax(logits, name=final_tensor_name)
    ground_truth_placeholder = tf.placeholder(tf.float64,
                                              [None, class_count],
                                              name=ground_truth_tensor_name)
    cross_entropy = tf.nn.softmax_cross_entropy_with_logits(
        logits=logits, labels=ground_truth_placeholder)
    cross_entropy_mean = tf.reduce_mean(cross_entropy)
    train_step = tf.train.GradientDescentOptimizer(FLAGS.learning_rate).minimize(
        cross_entropy_mean)
    return train_step, cross_entropy_mean

def do_train(sess,X_input, Y_input, X_validation, Y_validation):
    ground_truth_tensor_name = 'ground_truth'
    mini_batch_size = 10
    n_train = X_input.shape[0]

    graph = create_graph()

    train_step, cross_entropy = add_final_training_ops(
        graph, len(classes), FLAGS.final_tensor_name,
        ground_truth_tensor_name)

    init = tf.initialize_all_variables()
    sess.run(init)

    evaluation_step = add_evaluation_step(graph, FLAGS.final_tensor_name, ground_truth_tensor_name)

    # Get some layers we'll need to access during training.
    bottleneck_tensor1 = graph.get_tensor_by_name(ensure_name_has_port(BOTTLENECK_TENSOR_NAME))
    bottleneck_tensor = tf.placeholder_with_default(bottleneck_tensor1, shape=[None, 2048])
    ground_truth_tensor1 = graph.get_tensor_by_name(ensure_name_has_port(ground_truth_tensor_name))
    ground_truth_tensor = tf.placeholder_with_default(ground_truth_tensor1, shape=[None, len(classes)])

    i=0
    epocs = 1
    for epoch in range(epocs):
        shuffledRange = np.random.permutation(n_train)
        y_one_hot_train = encode_one_hot(len(classes), Y_input)
        y_one_hot_validation = encode_one_hot(len(classes), Y_validation)
        shuffledX = X_input[shuffledRange,:]
        shuffledY = y_one_hot_train[shuffledRange]
        for Xi, Yi in iterate_mini_batches(shuffledX, shuffledY, mini_batch_size):
            print Xi.shape
            print type(Xi)
            print type(Yi)
            print Yi.shape
            print Yi.dtype
            print Yi[0]
            sess.run(train_step,
                     feed_dict={bottleneck_tensor: Xi,
                                ground_truth_tensor: Yi})  

Print语句具有以下输出:

(10, 2048)
<type 'numpy.ndarray'>
<type 'numpy.ndarray'>
(10, 5)
float64
[ 0.  0.  0.  1.  0.]  

我收到错误:

sess.run(train_step,feed_dict={bottleneck_tensor: Xi,ground_truth_tensor: Yi})  

有人可以告诉我为什么我会遇到这个错误吗?

1 个答案:

答案 0 :(得分:1)

问题是您在add_final_training_ops中创建了一个您不提供的占位符。您可能认为在ground_truth_tensor中创建的占位符add_final_training_ops是相同的,但它不是,它是一个新的,即使它是由前者初始化的。

最简单的修复方法可能是从add_final_training_ops返回占位符并改为使用此占位符。