tf.batch_jacobian异常行为:切片后返回零而非预期的None
TensorFlow batch_jacobian 切片后梯度不符合预期
import tensorflow as tf with tf.GradientTape() as g: x = tf.constant([[1., 2.], [3., 4.]], dtype=tf.float32) z = tf.constant([[5., 6., 3.], [7., 8., 4.]], dtype=tf.float32) g.watch(x) g.watch(z) y1 = x * x y2 = z * z y = tf.concat([y1, y2], axis=1) batch_jacobian = g.batch_jacobian(y, z[:,0:1],unconnected_gradients=tf.UnconnectedGradients.NONE)
运行上述代码后,输出结果为全零矩阵。按照预期,若切片操作破坏了计算图,应该返回None而非零值。
实际输出:batch_jacobian = [[0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
输出截图:显示上述全零矩阵的TensorFlow运行结果界面
内容的提问来源于stack exchange,提问作者AMIT SINGH
相关产品推荐
相关产品推荐

