如何在tensorflow2中的keras稠密层中逐元素相减



假设我在Dense(2)层。如何添加层,首先记录整个张量,然后从第一列中逐元素减去第二列?非常感谢。

array([[1,2],
[3,4],
[5,6]])
becomes
array([[log(2)-log(1)],
[log(4)-log(3)],
[log(6)-log(5)]])

我会这样做:

input = tf.keras.layers.Input(shape=(2,), dtype=tf.float32)
x = tf.keras.layers.Dense(2)(input)
x = tf.math.log(x[-1][0]) - tf.math.log(x[-1][1])
model = tf.keras.Model(inputs=input, outputs=x)

或者您必须创建一个自定义图层。

最新更新