类型错误:forward() 需要 2 个位置参数,但给出了 3 个

索伦科赫

您好,我不明白为什么会出现标题中显示的错误,并将以下内容作为我的前向传播函数

    # For function that will activate neurons and perform forward propagation
def forward(self, inputs):
    state, (hx, cx) = inputs # getting separately the input images to the tuple (hidden states, cell states)
    x = F.relu(self.lstm(state)) # forward propagating the signal from the input images to the 1st convolutional layer
    hx, cx = self.lstm(x, (hx, cx)) # the LSTM takes as input x and the old hidden & cell states and ouputs the new hidden & cell states
    x = hx # getting the useful output, which are the hidden states (principle of the LSTM)
    return self.fcL(x), (hx, cx) # returning the output of the actor (Q(S,A)), and the new hidden & cell states ((hx, cx))

这作为我的 action_selection 函数:

    def select_action(self, state):
    #LSTM
    initialise = True # Initialise to zero at first iteration
    if initialise:
        cx = Variable(torch.zeros(1, 30))
        hx = Variable(torch.zeros(1, 30))
    else:  # The hx,cx from the previous iteration
        cx = Variable(cx.data) 
        hx = Variable(hx.data) 
    initialise = False
    q_values, (hx,cx) = self.model(Variable(state), (hx,cx))
    probs = F.softmax((q_values)*self.tau,dim=1)
    #create a random draw from the probability distribution created from softmax
    action = probs.multinomial()
    return action.data[0,0]
使

你在哪里使用你的前向功能?意思是在哪一行抛出错误?我找不到。

但总的来说:如果您不在类中使用 forward 函数,而是将其用作对象,则它实际上需要一个参数。( self 是“自动”给出的,因此您可以在类内部引用其他内容。为了更好的解释,请阅读:https : //docs.python.org/3/tutorial/classes.html)。例如,如果您尝试执行以下操作:

myObject = myClass()
myObject.forward(a,b)

它会抛出这个错误

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

类型错误:forward() 需要 2 个位置参数,但在 pytorch 中给出了 3 个

类型错误:index() 需要 2 个位置参数,但给出了 3 个

类型错误:set_user_agent() 需要 2 个位置参数,但给出了 3 个

类型错误:chunkIt() 需要 2 个位置参数,但给出了 3 个

类型错误:__call__() 需要 2 个位置参数,但给出了 3 个(Gunicorn)

类型错误:forward() 需要 2 个位置参数,但给出了 4 个,Pytorch

TypeError:update()需要2个位置参数,但给出了3个位置:Python

错误:类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

类型错误:hook() 需要 3 个位置参数,但给出了 4 个

类型错误:randint() 需要 3 个位置参数,但给出了 4 个

类型错误:__array__() 需要 1 个位置参数,但给出了 2 个

类型错误:get_object() 需要 1 个位置参数,但给出了 2 个

陷入类型错误:__init__() 需要 1 个位置参数,但给出了 2 个

类型错误:weather_at_place() 需要 2 个位置参数,但给出了 4 个

类型错误:get_stream_position() 需要 1 个位置参数,但给出了 2 个

类型错误:__array__() 需要 1 个位置参数,但给出了 2 个(图像分类 Keras)

Django TypeError:render()需要2个位置参数,但是给出了3个?

spacy 3 NER Scorer() 抛出 TypeError: score() 需要 2 个位置参数,但给出了 3 个

TypeError: __init__() 需要 2 个位置参数,但 Python 3 给出了 3 个?

Pytorch TypeError:forward() 需要 2 个位置参数,但给出了 4 个

python中的多处理错误“需要1个位置参数,但给出了2个”

python super :TypeError: __init__() 需要 2 个位置参数,但给出了 3 个

Odoo 需要 1 到 2 个位置参数,但给出了 3 个

send() 需要 1 到 2 个位置参数,但给出了 3 个 (discord.py)

“predict_classes() 需要 2 到 4 个位置参数,但给出了 5 个”错误

遇到错误TypeError:create_task()需要1到2个位置参数,但是在创建Google Cloud Task时给出了3个

意外错误:replace()接受2个位置参数,但给出了3个

@classmethod错误:TypeError:__call __()接受2个位置参数,但给出了3个

Sklearn错误:predict(x,y)接受2个位置参数,但给出了3个