为优化器传递占位符会导致错误 "initial_value must have shape specified"

Passing a placeholder for an optimizer results in an error "initial_value must have shape specified"

训练网络,希望能够将 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),也不起作用,同样的错误。

有什么想法吗?

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

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