简体   繁体   English

在需要使用单位L2范数进行输出的回归问题中,如何标准化Keras网络输出?

[英]How to normalize Keras network output in a regression problem that demands output with unit L2 norm?

My regression problem requires that the network output y has unit norm ||y|| = 1. 我的回归问题要求网络输出y具有单位范数||y|| = 1. ||y|| = 1. . ||y|| = 1. I would like to impose that as a Lambda layer after the linear activation: 我想在线性激活后将其强加为Lambda层:

from keras import backend as K  
...  
model.add(Dense(numOutputs, activation='linear'))  
model.add(Lambda(lambda x: K.l2_normalize(x)))  

The backend is TensorFlow. 后端是TensorFlow。 The code compiles but the network predicts output vectors with distinct norms (the norm is not 1 and varies). 代码可以编译,但网络会预测具有不同范数的输出向量(范数不为1,并且变化)。

Any hints regarding what I am doing wrongly? 关于我做错事情的任何提示吗?

The problem is that you haven't passed the axis argument to the K.l2_normalize function. 问题是您尚未将axis参数传递给K.l2_normalize函数。 As a result it would normalize all the elements in the whole batch so that their norm would be equal to one. 结果,它将标准化整个批处理中的所有元素,以使它们的范数等于1。 To resolve this, just pass axis=-1 to normalize over the last axis: 要解决此问题,只需传递axis=-1以对最后一个轴进行归一化:

model.add(Lambda(lambda x: K.l2_normalize(x, axis=-1)))  

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

相关问题 使用 keras 的 L2 归一化输出 - L2 normalised output with keras Keras lambda层为l2范数 - Keras lambda layer for l2 norm 回归模型中成本函数的 L1 范数代替 L2 范数 - L1 norm instead of L2 norm for cost function in regression model Keras L2正则化使网络不学习 - Keras L2 regularization makes the network not learn python sklearn:“ sklearn.preprocessing.normalize(X,norm ='l2')”和“ sklearn.svm.LinearSVC(penalty ='l2')”之间有什么区别 - python sklearn: what is the different between “sklearn.preprocessing.normalize(X, norm='l2')” and “sklearn.svm.LinearSVC(penalty='l2')” sklearn.preprocessing.normalize中的norm ='l2'对于矩阵归一化有什么作用? - What does norm='l2' in sklearn.preprocessing.normalize do for matrix normalization? 如何使用 L2 范数(python)在描述符中查找匹配项? - How to find matches in descriptors using L2 norm (python)? 如何在 Pytorch 的 CNN 中访问卷积层的权重和 L2 范数? - How to access weight and L2 norm of conv layers in a CNN in Pytorch? 使用Keras Lambda层计算L2范数似乎不起作用 - Calculating L2 norm using Keras Lambda layer doesn't seem to work 如何使用 Sklearn 在 Python 中对列表列表进行 L2 规范化 - How to L2 Normalize a list of lists in Python using Sklearn
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM