Python 您必须使用 dtype float 为占位符张量“Placeholder”提供一个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41607155/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 01:19:08  来源:igfitidea点击:

You must feed a value for placeholder tensor 'Placeholder' with dtype float

pythontensorflow

提问by judyzha

I'm a newer to tensorflow, I really don't know how to solve the problem.

我是 tensorflow 的新手,我真的不知道如何解决这个问题。

The code is like:

代码是这样的:

  1. Feed the train with values:

    sess.run(train_op, feed_dict={images: e, labels: l, keep_prob_fc2: 0.5})
    
  2. Use the value in CNN:

    x = tf.placeholder(tf.float32, [None, 10 * 1024])
    
  1. 用值喂火车:

    sess.run(train_op, feed_dict={images: e, labels: l, keep_prob_fc2: 0.5})
    
  2. 使用 CNN 中的值:

    x = tf.placeholder(tf.float32, [None, 10 * 1024])
    

Then have the error

然后有错误

InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]
InvalidArgumentError (see above for traceback): You must feed a value for placeholder tensor 'Placeholder' with dtype float
     [[Node: Placeholder = Placeholder[dtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]()]]

I print the input valuetypes using print(e.dtype)and the result is float32and e.shape:(10, 32, 32, 1).

我使用打印输入值类型print(e.dtype),结果是float32and e.shape:(10, 32, 32, 1)

I really don't know why this error is happening.

我真的不知道为什么会发生这个错误。



The code format

代码格式

First:

第一的:

 define the CNN model 
       "image = tf.placeholder(tf.float32, [FLAGS.batch_size, 32,32,1])" is here

Second:

第二:

 loss funtion and train_op is here
       "label = tf.placeholder(tf.float32, [None, FLAGS.batch_size])" is here

Third is the session:

三是座谈会:

images, labels = getShuffleimage()#here will get shuffle data
num_examples = 0
init = tf.initialize_local_variables()

with tf.Session() as sess:
    # Start populating the filename queue.
    sess.run(init)
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(coord=coord, sess=sess)

    try:
        step = 0
        while not coord.should_stop():
            start_time = time.time()
            image, label = sess.run([images, labels])#get shuffle images
            print(image.shape)
            print(image.dtype)
            sess.run(train_op, feed_dict={image: image, label: label , keep_prob_fc2: 0.5})
            duration = time.time() - start_time

    except tf.errors.OutOfRangeError:
        print('Done training after reading all data')
    finally:
        # When done, ask the threads to stop.
        coord.request_stop()

        # Wait for threads to finish.
        coord.join(threads)
        sess.close()

采纳答案by xxi

Some questions

一些问题

first
why you use sess = tf.InteractiveSession()and with tf.Session() as sess:at same time, just curious

首先
你为什么使用sess = tf.InteractiveSession()with tf.Session() as sess:同时,只是好奇

second what is your placeholder name xor images?
if name is x, {images: x_data...}won't feed x_datato x, it override(?) images
I think feed_dict should be {x: x_data...}

第二你有什么占位符的名称ximages
如果 name 是x{images: x_data...}则不会提供x_datax它,它会覆盖 (?)images
我认为 feed_dict 应该是{x: x_data...}

if name is images,do you have two imagesin your program, placeholderand shuffle data, try to modify name of variable

如果名称是imagesimages您的程序中是否有两个,placeholder以及shuffle data,尝试修改变量名称

回答by Targo

I saw one problem with the code. There are two variables with the same name label. One of them refers to a Tensor, and the other one refers to some data. When you set label: labelin the feed_dict, you need to distinguish between the two variables. Maybe you can try changing the name for one of the variables?

我看到代码有一个问题。有两个同名的变量label。其中一个是指一个张量,另一个是指一些数据。在 中设置label: labelfeed_dict,需要区分这两个变量。也许您可以尝试更改其中一个变量的名称?