You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.20 18:48:41