Li Deyang
Li Deyang

Reputation: 33

Normalize output of keras layer, which make the sum of output 1

I want a normalize function like K.l2_normalize, but can make the sum of output 1

L2 normalize formula is:

       x
---------------
sqrt(sum(x**2))

For example, for an input [3, 1, 4, 3, 1] is [3/6, 1/6, 4/6, 3/6, 1/6]=12/6=1/2

But I want:

    x 
---------------
   ||x||

For example, for an input [3, 1, 4, 3, 1] is [3/12, 1/12, 4/12, 3/12, 1/12]=12/12=1

In python, I want something like this:

import tensorflow as tf
from tensorflow.python.keras import backend as K
from tensorflow.python.keras.layers import Lambda

x = tf.keras.layers.Input(tensor=tf.constant([[3, 1, 4, 3, 1]], dtype=tf.float32))
n_layer = Lambda(lambda t: "somefunction" )(x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(n_layer.eval())

---------output--------

[[0.25 0.0833 0.3333 0.25 0.0833 ]]

Upvotes: 3

Views: 1269

Answers (1)

Sayandip Dutta
Sayandip Dutta

Reputation: 15872

What you are looking for is l1-norm, so you need to set the order to 1. You can pass the order of the norm through ord parameter in tf.linalg.norm

from tensorflow.python.keras import backend as K
from tensorflow.python.keras.layers import Lambda

x = tf.keras.layers.Input(tensor=tf.constant([[3, 1, 4, 3, 1]], dtype=tf.float32))
n_layer = Lambda(lambda t: tf.linalg.norm(t,ord=1) )(x)

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(n_layer.eval())

Output:

12.0

Upvotes: 2

Related Questions