The task at hand is to attach the Opacus privacy engine to the optimizer and train the following model to make it differentially private.
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
I followed the instructions given in Opacus's [Documentation][1] and read through their documentation of [privacy engine][2], but wasn't able to figure out the cause of the error.
Given below is the code
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)
Error
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