python - 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
解决方案
推荐阅读
- angular - 如何使用 ngClass
- ios - ObjectiveC:在导航控制器中添加/执行多个 segue
- delphi - 如何使用 TAdapterBindSource 刷新 TListView LiveBinding
- tomcat - 了解具有 JMX 相关属性的连接池
- entity-framework-core - aspnetboilerplate框架中如何处理大量记录
- javascript - JS Google Cloud 函数错误:Reference.update 失败:第一个参数在属性中包含未定义
- python - 过度拟合问题,验证成功率远低于训练。我能改变什么?
- python - 整个 Python 列表未写入 CSV 文件
- google-cloud-platform - 如何部署具有两个网络接口的 Windows 2016 服务器实例
- android - 如何通过获取 SQLite 数据自动填充编辑文本?