简体   繁体   English

Keras CuDNNLSTM 隐式激活函数?

[英]Keras CuDNNLSTM implicit activation function?

While intf.keras.layers.LSTM there is an activation parameter (default tanh )而在tf.keras.layers.LSTM 中有一个activation参数(默认tanh
the CuDNNLSTM doesn't have any, while having a activity_regularizer parameter. CuDNNLSTM没有,但有一个activity_regularizer参数。
Am I missing something?我错过了什么吗?

CuDNNLSTM is not a tf.keras.layers.LSTM wrapper, but a standalone entity, so how do I set the activation function for it? CuDNNLSTM 不是 tf.keras.layers.LSTM 包装器,而是一个独立的实体,那么如何为其设置激活函数?

CuDNNLSTM has a hardcoded tanh activation. CuDNNLSTM 具有硬编码的 tanh 激活。 As far as I am aware, there is no way for this to be changed.据我所知,没有办法改变这一点。 If you need some other activation, you're stuck using a regular LSTM layer.如果您需要其他一些激活,则只能使用常规 LSTM 层。

Alternately, if you only need a specific output activation for your model, eg softmax, you can stick on a Dense layer and then put the activation after that.或者,如果您只需要模型的特定输出激活,例如 softmax,您可以坚持使用 Dense 层,然后在此之后放置激活。 Here's a snippet of how I'm doing that for my particular case:以下是我如何针对我的特定情况执行此操作的片段:

x = CuDNNLSTM(256, return_sequences=True)(x)
x = TimeDistributed(Dense(8))(x)
x = Softmax(axis=2)(x)

Further reading:进一步阅读:

You can reverse the tanh activation, then apply whatever activation u want!您可以反转 tanh 激活,然后应用您想要的任何激活!

for example:例如:

x = LSTM(200,kernel_initializer=VarianceScaling(2))(x)

o = 1-1e-12 #float a little bit smaller than 1 in case LSTM returns -1 or 1
#x = Lambda(lambda x: 0.5*tf.math.log((1+x*o)/(1-x*o)))(x) #Tanh-¹
x = Lambda(lambda x: tf.math.atanh(x*o))(x)

x = LeakyReLU(alpha=0.01)(x) #apply new activation

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

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