[英]Sum of second order derivatives in Tensorflow
I have a function in Tensorflow, let's call in f
that takes as input a tensor x
on the form [None, N, M]
, and outputs a number for each row, ie the output is a tensor with form [None]
for some arbitrary number of rows. 我在Tensorflow中有一个函数,让我们调用
f
,它以[None, N, M]
形式的张量x
作为输入,并为每行输出一个数字,即输出是某些形式为[None]
的张量。任意数量的行。
I want to compute the Laplacian of f
, which in my case means that is I want to compute a tensor y
of shape [None]
with rows given by 我想计算
f
的拉普拉斯算子 ,这对我来说意味着要计算形状为[None]
的张量y
,其中行由
I can get the first order gradient the way I want to. 我可以按照自己的方式获得一阶渐变。 For the sake of this example, say my code is like so:
为了这个例子,我的代码是这样的:
import tensorflow as tf
x = tf.Variable([[[0.5, 1, 2], [3, 4, 5]]] , dtype=tf.float64)
y = tf.reduce_sum(x*x*x/3, axis=[1, 2])
grad = tf.gradients(y, x)[0]
which gives as expected 如预期的那样
grad: [[[ 0.25 1. 4. ]
[ 9. 16. 25. ]]]
I thought that I could now do the same on grad
to get the second order: 我以为我现在可以在
grad
做同样的事情来获得第二个订单:
lap = tf.gradients(grad, x)
But this gives 但这给
lap: [-117.125]
which is nothing like what I would expect. 这与我所期望的完全不同。 I would have wanted
我本来想要
lap: [[[ 1 2 4]
[ 6 8 10]]]
or just the sum for each row, like so: 或只是每一行的总和,如下所示:
lap: [ 31 ]
Obviously, this doesn't result in what I want, and I'm a bit stumped on how to fix it. 显然,这并不能满足我的要求,而我对如何解决它有些困惑。 Any help?
有什么帮助吗?
I've also tried tf.hessians
, which kind off works: 我也尝试过
tf.hessians
,它可以起作用:
hess = tf.hessians(y, x)
which gives 这使
hess:
[array([[[[[[ 1., 0., 0.],
[ 0., 0., 0.]]],
[[[ 0., 2., 0.],
[ 0., 0., 0.]]],
[[[ 0., 0., 4.],
[ 0., 0., 0.]]]],
[[[[ 0., 0., 0.],
[ 6., 0., 0.]]],
[[[ 0., 0., 0.],
[ 0., 8., 0.]]],
[[[ 0., 0., 0.],
[ 0., 0., 10.]]]]]])]
This has the correct numbers in there, but this also computes many, many more derivatives than what I need, and picking out numbers from this mess seems very inefficient. 它里面有正确的数字,但是它计算出的导数比我需要的要多得多,从混乱中挑选出数字似乎效率很低。
Secondary question : I think the issue is related to tf.gradients(ys, xs)
returning "derivatives of sum of ys wrt x in xs.". 第二个问题 :我认为问题与
tf.gradients(ys, xs)
返回“ tf.gradients(ys, xs)
中ys wrt x之和的导数”有关。 I don't want derivatives of sums, so I'm thinking I might need to run tf.gradients
several times on subslices of grad
. 我不想要求和的导数,因此我想我可能需要在
grad
tf.gradients
上运行tf.gradients
数次。 But why do I get the full first order gradient with the code above? 但是,为什么用上面的代码得到完整的一阶梯度呢? As far as I can tell, no summing has been made, as I get all the derivatives I want.
据我所知,没有求和,因为我得到了所有想要的导数。
Side note : If it helps if x
is of shape [None, N*M]
, then I can refactor the rest of the code to work with this. 旁注 :如果
x
的形状为[None, N*M]
会有所帮助,那么我可以重构其余代码以使用此格式。
It is kind of amusing because the following works for me perfectly. 这很有趣,因为以下内容非常适合我。
Input Code : 输入代码:
import tensorflow as tf
x = tf.Variable([[[0.5, 1, 2], [3, 4, 5]]] , dtype=tf.float64)
y = tf.reduce_sum(x*x*x/3, axis=[1, 2])
grad = tf.gradients(y, x)[0]
grad2 = tf.gradients(grad, x)
init_op = tf.global_variables_initializer()
with tf.Session() as sess:
sess.run(init_op)
g1, g2 = sess.run([grad, grad2])
print('First order : {}'.format(g1))
print('Second order : {}'.format(g2))
Output : 输出:
First order : [[[ 0.25 1. 4. ]
[ 9. 16. 25. ]]]
Second order : [array([[[ 1., 2., 4.],
[ 6., 8., 10.]]])]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.