简体   繁体   English

keras 层的归一化输出

[英]Normalized output of keras layer

I want to create a Keras model with Tensorflow background that returns a vector with norm 1. For this purpose, the model ends with the next layer:我想创建一个带有 Tensorflow 背景的 Keras 模型,该模型返回一个范数为 1 的向量。为此,模型以下一层结束:

main_network = Lambda(lambda t: K.l2_normalize(t, axis=1))(x)

I have also created a test in which I only create the model and, without training, I make a random prediction to check that the output has norm 1. But the test fails:我还创建了一个测试,在该测试中我只创建模型,并且在没有训练的情况下进行随机预测以检查输出是否具有范数 1。但测试失败:

AssertionError: 0.37070954 != 1 within 0.1 delta

So the Lambda layer is not working correctly since it is not normalizing the output.因此 Lambda 层无法正常工作,因为它没有对输出进行标准化。 I tried different values for the axis parameter and with all possible values, the test fails.我为轴参数尝试了不同的值,并且使用所有可能的值,测试失败。 But am I missing?但我失踪了吗?

Ok, I fixed the problem.好的,我解决了问题。 For same reason, K.l2_normalize does not work with very small numbers, so I simply changed the line by this one:出于同样的原因,K.l2_normalize 不适用于非常小的数字,所以我简单地将这一行更改为:

main_network = Lambda(lambda t: K.l2_normalize(1000*t, axis=1))(x)

And the now the test works right!!现在测试正常了!!

L2 normalize formula is: L2 normalize公式为:

       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 which indicates the output of L2-normalize is not necessary to be one .例如,对于输入[3, 1, 4, 3, 1][3/6, 1/6, 4/6, 3/6, 1/6] = 12/6表示L2-normalize不一定是一个 If you need something that normalizes the output to the sum of 1, you probably need Softmax :如果您需要将输出归一化为 1 之和的东西,您可能需要Softmax

Here is an example that you can check the output of the softmax is one:这是一个示例,您可以检查 softmax 的输出是否为一个:

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([[1, 2, 3, 4, 5]], dtype=tf.float32))
n_layer = Lambda(lambda t: K.softmax(t, axis=-1))(x)

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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