【问题标题】:tf.GradientTape() doesn't work on sliced outputstf.GradientTape() 不适用于切片输出
【发布时间】:2020-08-03 08:14:19
【问题描述】:

这是我尝试运行的一段代码:

import tensorflow as tf

a = tf.constant([[1, 2], [2, 3]], dtype=tf.float32)
b = tf.constant([[1, 2], [2, 3]], dtype=tf.float32)

with tf.GradientTape() as tape1, tf.GradientTape() as tape2:
    tape1.watch(a)
    tape2.watch(a)
    
    c = a * b

grad1 = tape1.gradient(c, a)
grad2 = tape2.gradient(c[:, 0], a)
print(grad1)
print(grad2)

这是输出:

tf.Tensor(
[[1. 2.]
 [2. 3.]], shape=(2, 2), dtype=float32)
None

您可以观察到 tf.GradientTape() 不适用于切片输出。有没有办法解决这个问题?

【问题讨论】:

    标签: python tensorflow gradienttape


    【解决方案1】:

    是的,你对张量所做的一切都需要在磁带上下文中发生。您可以像这样相对容易地修复它:

    import tensorflow as tf
    
    a = tf.constant([[1, 2], [2, 3]], dtype=tf.float32)
    b = tf.constant([[1, 2], [2, 3]], dtype=tf.float32)
    
    with tf.GradientTape() as tape1, tf.GradientTape() as tape2:
        tape1.watch(a)
        tape2.watch(a)
        
        c = a * b
        c_sliced = c[:, 0]
    
    grad1 = tape1.gradient(c, a)
    grad2 = tape2.gradient(c_sliced, a)
    print(grad1)
    print(grad2)
    

    【讨论】:

      猜你喜欢
      • 2018-12-24
      • 1970-01-01
      • 2018-05-24
      • 2019-09-03
      • 2020-02-03
      • 1970-01-01
      • 2014-11-17
      • 2016-02-29
      • 1970-01-01
      相关资源
      最近更新 更多