首页 文章

Tensorflow:针对每个样本计算Hessian

提问于
浏览
1

我的张量 X 大小 M x D . 我们可以将 X 的每一行解释为训练样本,并将每列作为特征 .

X 用于计算大小为 M x 1 的张量 u (换句话说, u 取决于计算图中的 X ) . 我们可以将其解释为预测的向量;每个样本一个 . 特别是, u 的第m行仅使用 X 的第m行计算 .

现在,如果我运行 tensor.gradients(u, X)[0] ,我获得了 M x D 张量,对应于 u 的"per-sample"的"per-sample"梯度 .

我怎样才能类似地计算"per-sample" Hessian张量? (即 M x D x D 数量)


Addendum :Peter 's answer below is correct. I also found a different approach using stacking and unstacking (using Peter' s表示法:

hess2 = tf.stack([
    tf.gradients( tmp, a )[ 0 ]
    for tmp in tf.unstack( grad, num=5, axis=1 )
], axis = 2)

在Peter的例子中, D = 5是特征的数量 . 我怀疑(但我没有检查过)上面对于 M 更大更快,因为它跳过了彼得答案中提到的零条目 .

1 回答

  • 2

    tf.hessians()正在计算所提供的 ysxs 的Hessian维度 . 由于维度 M x Dxs 维度为 M x D ,因此结果将为维度 M x D x M x D . 但由于每个示例的输出彼此独立,因此大多数Hessian将为零,即第三维中只有一个切片将具有任何值 . 因此,为了得到你想要的结果,你应该采用两个 M 维度的对角线,或者更容易,你应该简单地总结并消除第三个维度,如下所示:

    hess2 = tf.reduce_sum( hess, axis = 2 )
    

    示例代码(已测试):

    import tensorflow as tf
    
    a = tf.constant( [ [ 1.0, 1, 1, 1, 1 ], [ 2, 2, 2, 2, 2 ], [ 3, 3, 3, 3, 3 ] ] )
    b = tf.constant( [ [ 1.0 ], [ 2 ], [ 3 ], [ 4 ], [ 5 ] ] )
    c = tf.matmul( a, b )
    c_sq = tf.square( c )
    
    grad = tf.gradients( c_sq, a )[ 0 ]
    
    hess = tf.hessians( c_sq, a )[ 0 ]
    hess2 = tf.reduce_sum( hess, axis = 2 )
    
    
    with tf.Session() as sess:
        res = sess.run( [ c_sq, grad, hess2 ] )
    
        for v in res:
            print( v.shape )
            print( v )
            print( "=======================")
    

    将输出:

    (3,1)[[225.] [900.] [2025.]] =======================(3,5)[[ 30. 60. 90. 120. 150.] [60. 120. 180. 240. 300.] [90. 180. 270. 360. 450.]] ============== =========(3,5,5)[[[2. 4. 6. 8. 10.] [4. 8. 12. 16. 20.] [6. 12. 18. 24 .30 . ] [8. 16. 24. 32. 40.] [10 . 20. 30. 50. 50.]] [[2. 4. 6. 8. 10.] [4. 8. 12. 16. 20.] [6. 12. 18. 24. 30.] [8 . 16. 24. 32. 40.] [10 . 20. 30. 50. 50.]] [[2. 4. 6. 8. 10.] [4. 8. 12. 16. 20.] [6. 12. 18. 24. 30.] [8 . 16. 24. 32. 40.] [10 . 20. 30. 40. 50.]]] =======================

相关问题