小编mar*_*lon的帖子

使用 tf.function 时获取梯度

我对以下示例中观察到的行为感到困惑:

import tensorflow as tf

@tf.function
def f(a):
    c = a * 2
    b = tf.reduce_sum(c ** 2 + 2 * c)
    return b, c

def fplain(a):
    c = a * 2
    b = tf.reduce_sum(c ** 2 + 2 * c)
    return b, c


a = tf.Variable([[0., 1.], [1., 0.]])

with tf.GradientTape() as tape:
    b, c = f(a)
    
print('tf.function gradient: ', tape.gradient([b], [c]))

# outputs: tf.function gradient:  [None]

with tf.GradientTape() as tape:
    b, c = fplain(a)
    
print('plain gradient: ', tape.gradient([b], …
Run Code Online (Sandbox Code Playgroud)

python decorator tensorflow2.0 gradienttape

7
推荐指数
1
解决办法
1077
查看次数

标签 统计

decorator ×1

gradienttape ×1

python ×1

tensorflow2.0 ×1