【问题标题】:You must feed a value for placeholder tensor 'Placeholder' with dtype float您必须使用 dtype float 为占位符张量“Placeholder”提供一个值
【发布时间】:2017-05-27 04:54:58
【问题描述】:

我是tensorflow的新手,真的不知道怎么解决。

代码如下:

  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])
    

那就报错

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"]()]]

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

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


代码格式

第一:

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

第二:

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

三是会话:

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()

【问题讨论】:

    标签: python tensorflow


    【解决方案1】:

    一些问题

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

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

    如果名字是images,你的程序中是否有两个imagesplaceholdershuffle data,尝试修改变量名

    【讨论】:

    • 首先,我的bach_size是10,imagesize是32*32*1,所以我把占位符替换为:x = tf.placeholder(tf.float32, [10, 1024]),其次,g也用于 train_op。但是,错误是一样的
    • 输入图像的形状是1D还是3D?如果形状是 3D,则占位符的形状应该类似于 [None, 32, 32, 1]。如果形状是一维的,feed_dict 的keyplaceholder 的名称应该相同。你的 feed_dict 是 {images: e ...},所以有一个 placeholder 命名的图像
    • 我已将图像转换为 3D,并将它们随机播放。 “e”是batch_output之一,它的形状是[10,32,32,1],类型是“float32”。我将“e”喂入火车。 x = tf.placeholder(tf.float32, [10,32,32,1] 用于获取数据,但得到错误“您必须为占位符张量‘占位符’提供一个值,其 dtype 为浮点数和形状 [10,32 ,32,1]"
    • 你在x = tf.place....之前运行sess.run吗?先定义模型然后sess.run 或者你能粘贴更精确的代码吗?
    • 所有参数都按照您的建议同步,显示的问题已更新。但是我得到另一个错误:“unhashable type: 'numpy.ndarray'”,需要你的帮助吗?
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2019-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-04
      相关资源
      最近更新 更多