Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/tensorflow/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 张量流中二阶导数的和_Python_Tensorflow_Derivative - Fatal编程技术网

Python 张量流中二阶导数的和

Python 张量流中二阶导数的和,python,tensorflow,derivative,Python,Tensorflow,Derivative,我在Tensorflow中有一个函数,让我们调用f,它以[None,N,M]的形式输入一个张量x,并为每行输出一个数字,也就是说,对于任意数量的行,输出是一个带有[None]形式的张量 我想计算f的张量,在我的例子中,这意味着我想计算一个y形状[None]的张量,行由 我可以用我想要的方法得到一阶梯度。在本例中,假设我的代码如下: import tensorflow as tf x = tf.Variable([[[0.5, 1, 2], [3, 4, 5]]] , dtype=tf.floa

我在Tensorflow中有一个函数,让我们调用
f
,它以
[None,N,M]
的形式输入一个张量
x
,并为每行输出一个数字,也就是说,对于任意数量的行,输出是一个带有
[None]
形式的张量

我想计算
f
的张量,在我的例子中,这意味着我想计算一个
y
形状
[None]
的张量,行由

我可以用我想要的方法得到一阶梯度。在本例中,假设我的代码如下:

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]
这与预期相符

grad: [[[ 0.25  1.    4.  ]
        [ 9.   16.   25.  ]]]
我想我现在可以在
grad
上做同样的事情来获得第二个订单:

lap = tf.gradients(grad, x)
但这给了

lap: [-117.125]
这与我所期望的完全不同。我会想要的

lap: [[[ 1  2  4]
       [ 6  8 10]]]
或者只是每行的总和,如下所示:

lap: [ 31 ]
显然,这并没有得到我想要的结果,我有点困惑于如何修复它。有什么帮助吗

我也试过
tf.hessians
,哪种方法有效:

hess = tf.hessians(y, x)

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.]]]]]])]
这里面有正确的数字,但这也计算出了比我需要的多得多的导数,从这些乱七八糟的数字中挑选出来似乎效率很低

第二个问题:我认为这个问题与
tf.梯度(ys,xs)
返回“ys w.r.t.x与xs之和的导数”有关。我不想求和的导数,所以我想我可能需要在
grad
的子片上运行几次
tf.gradients
。但是为什么我要用上面的代码得到完整的一阶梯度呢?据我所知,没有求和,因为我得到了我想要的所有导数


旁注:如果
x
的形状
[None,N*M]
有帮助,那么我可以重构其余的代码来处理这个问题。

这有点有趣,因为下面的内容对我来说非常适合

输入代码:

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))
输出:

First order : [[[ 0.25  1.    4.  ]
  [ 9.   16.   25.  ]]]
Second order : [array([[[ 1.,  2.,  4.],
        [ 6.,  8., 10.]]])]