【问题标题】:Passing a placeholder for an optimizer results in an error "initial_value must have shape specified"为优化器传递占位符会导致错误“initial_value must have shape specified”
【发布时间】:2017-04-11 14:50:24
【问题描述】:

训练网络,希望能够将 beta1 参数作为参数传递,因此将其设为占位符

self.beta1 = tf.placeholder(tf.float32)

几行之后,这一行出现错误

self.train_adam = tf.train.AdamOptimizer(self.eta, beta1=self.beta1, epsilon=1e-15).minimize(self.cost_m)

错误: ValueError: initial_value must have a shape specified: Tensor("Placeholder_5:0", dtype=float32)

取出beta1=self.beta1 会导致错误消失,因此错误一定与此有关。

我不知道为什么会发生这种情况,因为 placeholders 不需要指定形状。我试过用self.beta1 = tf.placeholder(tf.float32, shape=None),还是不行,同样的错误。

有什么想法吗?

【问题讨论】:

  • 一年多后,我遇到了同样的问题,然后记得我以前问过这个问题。多么奇怪的设计决定。

标签: tensorflow placeholder


【解决方案1】:

只需给占位符一个形状[],因为它是一个常量:

self.beta1 = tf.placeholder(tf.float32, shape=[])

【讨论】:

  • 作为备注,添加shape=None没有任何效果,因为python在没有指定参数时已经默认shapeNone
  • 当然。但它不应该首先抱怨placeholders 不需要用形状声明。我尝试这样做只是为了做点什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-10-20
  • 1970-01-01
  • 2018-06-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多