简体   繁体   中英

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)]])

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM