我正在尝试了解渐变磁带渐变。这是我正在尝试生成结果的虚拟模型。我试图理解为什么每列中的所有值都是相同的。这是带有输出的代码:
class A:
def __init__(self):
self.model = self.model_nn()
self.opt = tf.keras.optimizers.Adam()
def model_nn(self):
inputA = Input(3)
d1 = Dense(2)(inputA)
inputB = Input(2)
d2 = Dense(16)(inputB)
d3 = Dense(8)(d2)
d4 = Dense(2)(d3)
c = concatenate([d1, d4], axis=-1)
outputs = Dense(1, activation="linear")(c)
return tf.keras.Model([inputA, inputB], outputs)
def gradients(self, inputA, inputB):
inputB = tf.convert_to_tensor(inputB)
with tf.GradientTape() as tape:
tape.watch(inputB)
values = self.model([inputA, inputB])
values = tf.squeeze(values)
g = tape.gradient(values, inputB)
return g
a = A()
a.gradients(np.array([[1., 2., 3.], [5., 7., 10.], [-2., -30., 1.],
[3., 90., 1.]]), np.array([[4., 2.], [1., 1.], [2., 5.], [0., 8.]]))
输出
<tf.Tensor: shape=(4, 2), dtype=float64, numpy=
array([[0.09073823, 0.08013722],
[0.09073823, 0.08013722],
[0.09073823, 0.08013722],
[0.09073823, 0.08013722]])>
谁能告诉我为什么会这样?