使用自己的图层时出现此错误:
Traceback (most recent call last):
File "E:/fffan/try.py", line 40, in <module>
run(input, 5000)
File "E:/fffan/try.py", line 36, in run
out = SelfAttention(nclass)(output, state)
TypeError: __call__() takes 2 positional arguments but 3 were given
这是我的代码,伙计可以告诉我如何解决。
from keras.engine.topology import Layer
from keras.layers.core import Dense
from keras import backend as K
from keras.layers import Input,CuDNNGRU
from keras.activations import softmax
class SelfAttention(Layer): #### 对于长序列效果较差
def __init__(self,units,**kwargs):
self.W1 = Dense(units)
self.W2 = Dense(units)
self.V = Dense(1)
super(SelfAttention, self).__init__(**kwargs)
def call(self,features, hidden):
hidden_with_time_axis = K.expand_dims(hidden, 1)
score = self.V(K.tanh(self.W1(features) + self.W2(hidden_with_time_axis)))
attention_weights = softmax(score, axis=1)
context_vector = attention_weights * features
return context_vector
def GRU(units):
return CuDNNGRU(units, return_sequences=True,
return_state=True,
recurrent_initializer='glorot_uniform')
def run(input,nclass):
output, state = GRU(nclass)(input)
out = SelfAttention(nclass)(output, state)
if __name__ == '__main__':
input = Input(shape=(35, 512), name='the_input')
run(input, 5000)
我的tensorflow版本是1.14.0,我的keras是2.1.5
有人知道这个问题吗?
答案 0 :(得分:2)
应该是:
def __call__(self, features, hidden):
代替:
def call(self, features, hidden):