简体   繁体   English

Keras:如何创建自定义的Noisy Relu函数?

[英]Keras: How to create a custom Noisy Relu function?

How could I create a noisy Relu function in Keras? 如何在Keras中创建嘈杂的Relu函数? Especially how do I create the noise Y~N(0,1). 特别是如何创建噪声Y〜N(0,1)。

def relu_noise(x):
return x*(x>0) + N(0,1)

Any ideas? 有任何想法吗? Thanks! 谢谢!

You can use a Lambda layer for that task. 您可以将Lambda图层用于该任务。

Define a function normally, but using the keras backend functions: 通常定义一个函数,但是使用keras后端函数:

def relu_noise(x):

    isPositive = K.greater(x,0) 
    noise = K.random_normal((shape of x), mean=0.5, stddev=0.5)
         #I'm just not sure this is exactly the kind of noise you want. 

    return (x * isPositive) + noise

Then use it in a lambda layer: 然后在lambda层中使用它:

from keras.layers import *

layer = Lambda(relu_noise, output_shape=(shape of x))

Add this layer to a Sequential model as any other layer, or call it with an input in a Model . 将此图层与其他任何图层一样添加到Sequential模型中,或使用Model的输入进行调用。

You can probably use it directly as an activation function as well: 您可能还可以直接将其用作激活功能:

layer = Dense(units, activation=relu_noise)

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

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