这是我尝试运行的一段代码:
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() 不适用于切片输出。有没有办法解决这个问题?