【问题标题】:InvalidArgumentError when running tf.global_variables_initializer()运行 tf.global_variables_initializer() 时出现 InvalidArgumentError
【发布时间】:2017-10-11 13:07:17
【问题描述】:

基本上,我有一个函数需要一个张量 x 和两个占位符 zc

def error_robust(x,z,c):
  zz = tf.reshape(z, [-1, 28, 28, 1])
  var = tf.reduce_mean(x-zz)
  out = tf.cond( tf.abs(var) <= c, lambda: (c*c/6.0)*(1 - tf.pow(1-tf.pow(var/c,2),3)), lambda: tf.Variable(c*c/6.0) )
  return out

我定义了我要使用的占位符和张量:

# TENSORFLOW PLACEHOLDERS
sess  = tf.Session(config=tf.ConfigProto(allow_soft_placement=True))
flat_mnist_data = tf.placeholder(tf.float32, [None, 28*28])
dropout_keep_prob = tf.placeholder(tf.float32)
param_robust = tf.placeholder(tf.float32, shape=())

调用定义的函数不会产生任何错误:

error_r = error_robust(layer1_b.reconstruction, flat_mnist_data, param_robust)

这会产生错误:

sess.run(tf.global_variables_initializer())

InvalidArgumentError(请参阅上面的回溯):您必须为占位符张量“Placeholder”提供一个 dtype float 的值 [[节点:Placeholder = Placeholderdtype=DT_FLOAT, shape=[], _device="/job:localhost/replica:0/task:0/gpu:0"]]

我真的不明白为什么会这样。关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    好的,我明白了。我最初期望 c 是一个简单的标量。所以我使用 tf.Variable 作为 tf.cond 的第二个参数。 更新error_robust函数即可解决:

    def error_robust(x,z,c):
      zz = tf.reshape(z, [-1, 28, 28, 1])
      var = tf.reduce_mean(x-zz)
      out = tf.cond( tf.abs(var) <= c, lambda: (c*c/6.0)*(1 - tf.pow(1-tf.pow(var/c,2),3)), lambda: c*c/6.0 )
      return out
    

    【讨论】:

      猜你喜欢
      • 2021-06-30
      • 2018-09-27
      • 2018-02-06
      • 1970-01-01
      • 2021-09-04
      • 1970-01-01
      • 2018-10-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多