简体   繁体   English

如何在Keras中实现1-Sigmoid?

[英]How to implement 1-sigmoid in Keras?

As I want to implement a structure which is similar to the update gate of GRU: 由于我想实现类似于GRU更新门的结构:

h t = (1-z t )h t-1 + z t h t h t =(1-z t )h t-1 + z t h t

And I am trying to implement it with these code but it doesn't work. 我正在尝试使用这些代码来实现它,但是它不起作用。 I am sure the problem are in the following code: 我确定问题出在以下代码中:

one = K.ones(shape=(1, len, 128))
zt=Subtract([one,zt])
temp_conv2=multiply([reset_conv,zt])
output=Add([temp_conv1,temp_conv2])

I have the following error: 我有以下错误:

AttributeError:'Tensor' object has no attribute '_keras_history' AttributeError:“ Tensor”对象没有属性“ _keras_history”

I have already tried some other method such as using Lambda layer but it doesn't work. 我已经尝试了其他方法,例如使用Lambda层,但是它不起作用。

one is not a Keras Tensor therefore you would get that error. one不是Keras Tensor,因此您会收到该错误。 You can wrap this in a Lambda layer: 您可以将其包装在Lambda层中:

zt = Lambda(lambda x: Subtract([K.ones(shape=(1, len, 128)), x]))(zt)

Even you don't need to construct that Tensor of ones. 即使您不需要构造那个张量。 Simply use 1-x : 只需使用1-x

zt = Lambda(lambda x: 1-x)(zt)

It will be automatically broadcasted and the subtraction would be element-wise. 它会自动广播,减法将是逐元素的。

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

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