首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >TypeError:__call__()接受2个位置参数,但给出了3个

TypeError:__call__()接受2个位置参数,但给出了3个
EN

Stack Overflow用户
提问于 2019-09-16 17:31:36
回答 1查看 2.6K关注 0票数 1

当我使用我自己的层时,我得到了这个错误:

代码语言:javascript
复制
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

这是我的代码,希望有人能告诉我如何修复它。

代码语言:javascript
复制
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

关于这个问题,有人知道些什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-09-16 17:37:11

它应该是:

代码语言:javascript
复制
def __call__(self, features, hidden):

而不是:

代码语言:javascript
复制
def call(self, features, hidden):
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/57953999

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档