Numpy 通过tf.Hessians()或tf.gradients()计算Hessians w.r.t更高秩变量不起作用
当我们需要计算双梯度或海森梯度时,在tensorflow中,我们可以使用Numpy 通过tf.Hessians()或tf.gradients()计算Hessians w.r.t更高秩变量不起作用,numpy,machine-learning,tensorflow,Numpy,Machine Learning,Tensorflow,当我们需要计算双梯度或海森梯度时,在tensorflow中,我们可以使用tf.hessians(F(x),x),或者使用tf.gradient(tf.gradients(F(x),x)[0],x)[0]。但是,当x不是秩1时,当使用tf.hessians()时,我被告知以下错误 ValueError:无法计算Hessian,因为xs的元素0没有 排名第一。。张量模型_输入/操作:0必须具有秩1。 接收秩2,形状(?,1) 在以下代码中: with tf.name_scope("1st scope
tf.hessians(F(x),x)
,或者使用tf.gradient(tf.gradients(F(x),x)[0],x)[0]
。但是,当x
不是秩1时,当使用tf.hessians()
时,我被告知以下错误
ValueError:无法计算Hessian,因为xs的元素0没有
排名第一。。张量模型_输入/操作:0必须具有秩1。
接收秩2,形状(?,1)
在以下代码中:
with tf.name_scope("1st scope"):
self.states = tf.placeholder(tf.float32, (None, self.state_dim), name="states")
self.action = tf.placeholder(tf.float32, (None, self.action_dim), name="action")
with tf.name_scope("2nd scope"):
with tf.variable_scope("3rd scope"):
self.policy_outputs = self.policy_network(self.states)
# use tf.gradients twice
self.actor_action_gradients = tf.gradients(self.policy_outputs, self.action)[0]
self.actor_action_hessian = tf.gradients(self.actor_action_gradients, self.action)[0]
# or use tf.hessians
self.actor_action_hessian = tf.hessian(self.policy_outputs, self.action)
使用tf.gradients()
时,也会导致错误:
在create_variables self.actor_action_hessian中=
tf.梯度(self.actor\u action\u梯度,self.action)[0]
AttributeError:“非类型”对象没有属性“数据类型”
我如何解决这个问题,在这种情况下既不能使用
tf.gradients()
也不能使用tf.hessians()
。第二种方法很好,错误在其他地方,即图形未连接
self.actor_action_gradients = tf.gradients(self.policy_outputs, self.action)[0]
self.actor_action_hessian = tf.gradients(self.actor_action_gradients, self.action)[0]
error被抛出到第二行,因为self.actor\u action\u梯度为None,所以您无法计算它的梯度。代码中没有任何内容表明self.policy_的输出取决于self.action(也不应该如此,因为它的操作取决于策略,而不是策略对操作)
一旦你解决了这个问题,你会注意到,“hessian”实际上不是一个hessian,而是一个向量,用来形成f wrt的一个适当的hessian。您必须迭代tf.gradients返回的所有值,并独立地计算每个值的tf.gradients。这是TF的已知限制,目前还没有更简单的方法。非常感谢。你的信息非常有用。因此,当前
tf.hessians()
的返回与应用tf.gradients()
两次相同?