tensorflow - 计算输出相对于权重的梯度

标签 tensorflow machine-learning gradient tensorflow2.0 automatic-differentiation

从 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/

相关文章:

python - 最小化 Tensorflow 中的函数

python - 如何在 Tensorflow 中复制 PyTorch 的 nn.functional.unfold 函数?

python - 为什么 AWS SageMaker 运行 Web 服务器进行批量转换?

python - 数据无法完全加载,因为超过了每张工作表的最大列数

python - 使用逻辑回归进行多点触摸响应模型(python/pandas)?

.net - .NET WinForms 应用程序中的圆形渐变

python - 值错误 : dimension of the inputs to `Dense` should be defined. 发现 `None`

tensorflow - 如何在 Keras 中实现屏蔽 softmax 交叉熵损失函数?

tensorflow - 了解张量板: why 12 tensors sent to optimizer?

gradient - 为什么强化学习的策略梯度方法适用于大型行动空间