python**kwags错误:函数接受6个位置参数,但给出了8个

2024-06-16 13:23:16 发布

您现在位置:Python中文网/ 问答频道 /正文

当我试图发展一个梯度下降,我发现了一个有趣的问题,我不能有效地使用**kwargs。我的功能看起来像

def gradient_descent(g,x,y,alpha,max_its,w,**kwargs):    
    # switch for verbose
    verbose = True
    if 'verbose' in kwargs:
        verbose = kwargs['verbose']

    # determine num train and batch size
    num_train = y.size()[1]
    batch_size = num_train
    if 'batch_size' in kwargs:
        batch_size = kwargs['batch_size']
    ........

错误看起来像:

^{pr2}$

类型错误:gradient_descent()接受6个位置参数,但给出了8个。

有什么我没注意到的吗?在


Tags: in功能alphaverbosesizeifdef错误
1条回答
网友
1楼 · 发布于 2024-06-16 13:23:16

您的函数签名与使用它的参数数目不匹配:

gradient_descent(g,x,y,alpha,max_its,w,**kwargs)

有6个位置参数g,x,y,alpha,max_its,w,但是,在您的调用中:

^{pr2}$

你给了它8g,x_train,y_train,alpha_choice,max_its,w_train,num_pts,batch_size

我猜你想用num_pts作为batch_size参数,所以它看起来像这样:

weight_hist_2,train_hist_2 = gradient_descent(
    g,
    x_train,
    y_train,
    alpha_choice,
    max_its,
    w_train,
    batch_size=num_pts,
    verbose = False)

相关问题 更多 >