bliu
bliu

Reputation: 153

How to element-wise subtract to in a keras Dense layer in tensorflow2?

Let say I'm at the Dense(2) layer. How to add layers to first log the whole tensor and then element-wise subtract the second column from the first column? Thank you.

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

becomes

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

Upvotes: 0

Views: 490

Answers (1)

Andrey
Andrey

Reputation: 6367

I would do this:

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)

Or you have to create a custom layer.

Upvotes: 1

Related Questions