首页 > 解决方案 > TypeError: __init__() 接受 2 个位置参数,但给出了 3 个位置参数(和 3 个仅关键字参数)

问题描述

手头的任务是将 Opacus 隐私引擎附加到优化器并训练以下模型以使其具有差异化隐私。

import torch.nn as nn
import torch.nn.functional as F


class Model(nn.Module):
    """Class used to initialize model of student/teacher"""

    def __init__(self):

        super(Model, self).__init__()
        self.conv1 = nn.Conv2d(1, 20, 5, 1)
        self.conv2 = nn.Conv2d(20, 50, 5, 1)
        self.fc1 = nn.Linear(4 * 4 * 50, 500)
        self.fc2 = nn.Linear(500, 10)

    def forward(self, x):

        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, 2, 2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, 2, 2)
        x = x.view(-1, 4 * 4 * 50)
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        #return F.log_softmax(x, dim=1)
        return x

我按照 Opacus 的 [文档][1] 中给出的说明阅读了他们的 [隐私引擎][2] 文档,但无法找出错误的原因。

下面给出的是代码

model = Model()
optimizer = optim.SGD(model.parameters(), lr = 0.001)
batch_size = sample_size = 32
privacy_engine = PrivacyEngine(
    model,
    batch_size,
    alphas=[10, 100],
    noise_multiplier= 9.7,
    max_grad_norm = 1.0,
)
privacy_engine.attach(optimizer)

错误

TypeError                                 Traceback (most recent call last)
<ipython-input-8-84493fad0b76> in <module>()
      7     alphas=[10, 100],
      8     noise_multiplier= 9.7,
----> 9     max_grad_norm = 1.0
     10 )
     11 privacy_engine.attach(optimizer)

TypeError: __init__() takes 2 positional arguments but 3 positional arguments (and 3 keyword-only arguments) were given

标签: pythondeep-learningpytorchprivacy

解决方案


推荐阅读