从 tensorflow 模型开始,我希望能够检索输出相对于权重的梯度。反向传播的目的是计算损失相对于权重的梯度,以便在代码中的某个位置计算输出相对于权重的梯度。
但是我想知道如何在 API 级别获取这个雅可比行列式,有什么想法吗?
我知道我们可以访问磁带,但我不知道该怎么办,实际上我不需要整个雅可比行列式,我只需要能够计算 J^{*} 的矩阵向量积v 其中 J^{} 是雅可比矩阵的转置,v 是给定向量。
谢谢你, 问候。
最佳答案
如果您只需要计算向量雅可比行列式积,那么只计算矢量雅可比行列式比计算完整雅可比行列式要高效得多。计算 N 个变量的函数的雅可比行列式将花费 O(N) 时间,而矢量雅可比积则需要 O(1) 时间。
那么如何在 TensorFlow 中计算矢量雅可比积呢?诀窍是在 gradient
函数中使用 output_gradients
关键字参数。您将 output_gradients
的值设置为向量雅可比积中的向量。让我们看一个例子。
import tensorflow as tf
with tf.GradientTape() as g:
x = tf.constant([1.0, 2.0])
g.watch(x)
y = x*x # y is a length 2 vector
vec = tf.constant([2.0,3.0]) # vector in vector jacobian product
grad = g.gradient(y,x,output_gradients = vec)
print(grad) # prints the vector-jacobian product, [4.,12.]
注意:如果您尝试计算 tensorflow 中向量值(而不是标量)函数的梯度而不设置output_gradients
,它将计算向量雅可比积,其中向量设置为所有的。例如,
import tensorflow as tf
with tf.GradientTape() as g:
x = tf.constant([1.0, 2.0])
g.watch(x)
y = x*x # y is a length 2 vector
grad = g.gradient(y,x)
print(grad) # prints the vector-jacobian product with a vector of ones, [2.0,4.0]
关于tensorflow - 计算输出相对于权重的梯度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59270621/