Tensorflow 中的 VJP 和 JVP

问题描述 投票:0回答:0

可以通过

output_gradients
函数中的
gradient
关键字arg获取Tensorflow中的向量-雅可比积。您可以将
output_gradients
的值设置为向量-雅可比积中的向量,如下例:

import tensorflow as tf

with tf.GradientTape() as tape:  
x  = tf.constant([1.0, 2.0, 3.0])  
tape.watch(x)  
y = x*x 

vec = tf.constant([2.0, 3.0, 4.0]) # vector of the vector-Jacobian product

grads = tape.gradient(y,x,output_gradients = vec)
print(grads) # prints the vector-Jacobian product, [4.,12.,36.]

我的问题是如何在 Tensorflow 上评估雅可比向量积。我知道这个产品没有出现在反向传播中,而是出现在正向传播中。但是,我需要在不显式计算的情况下通过雅可比行列式评估自定义向量的乘积。我应该改用 tf.autodiff.ForwardAccumulator 吗?提前致谢。

tensorflow implementation
© www.soinside.com 2019 - 2024. All rights reserved.