假设我有一个简单的单层神经网络:
因此,变量
x = tf.placeholder(tf.float32, [batch_size, input_dim])
W = tf.Variable(tf.random_normal([input_dim, output_dim]))
a = tf.matmul(x, W)
y = tf.nn.softmax(a)
因此,变量
y
的维数为 batch_size
乘以 output_dim
。我想要计算每个批次中每个样本相对于 a
的雅可比矩阵,其维度为 batch_size
乘以 output_dim
乘以 output_dim
。现在,根据数学上的定义,当 i ≠ j 时,雅可比矩阵 (dy/da)_{i,j} = -y_i y_j,否则,(dy/da)_{i,i} = y_i (1 - y_i)。
我想知道如何在 TensorFlow 中计算 softmax 相对于其输入的雅可比矩阵?我知道 tf.gradients
可以计算标量相对于张量的梯度,因此我认为在 TensorFlow 中循环使用 tf.gradients
或者试图实现上述解析式的某种组合应该可以解决问题。但是我不确定如何在 TensorFlow 中使用它的 ops 来完成这个任务,如果有任何代码能帮助我完成这个任务,我将不胜感激!