Hey guys !
I am currently trying to make an implementation of a CNN model with PyTorch and PySyft. But I have an issue that does appear with the optimizer.step() :
“TypeError: add_() takes 1 positional argument but 2 were given”
Here’s my model and the way I launch it :
class Net(Module):
def __init__(self):
super(Net, self).__init__()
self.cnn_layers = Sequential(
# Defining a 2D convolution layer
Conv2d(3, 4, kernel_size=3, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2),
# Defining another 2D convolution layer
Conv2d(4, 4, kernel_size=4, stride=1, padding=1),
BatchNorm2d(4),
ReLU(inplace=True),
MaxPool2d(kernel_size=2),
)
self.linear_layers = Sequential(
Linear(4 * 49 * 49, 1)
)
# Defining the forward pass
def forward(self, x):
x = self.cnn_layers(x)
print("X prev: ", x.shape)
x = x.view(-1, 4*49*49)
print("X after: ", x.shape)
x = self.linear_layers(x)
return x
# defining the model
model = Net()
# defining the optimizer
optimizer = Adam(model.parameters(), lr=0.07)
# defining the loss function
criterion = BCEWithLogitsLoss()
# checking if GPU is available
#if torch.cuda.is_available():
# model = model.to(device)
# criterion = criterion.to(device)
# print("Model & Criterion on GPU")
from torchsummary import summary
#vgg = models.vgg16()
#summary(model.cuda(), (3, 200, 200))
print(model)
def trains(epoch):
model.train()
for batch_idx, (data,target) in enumerate(train_loader):
worker = data.location
model.send(worker)
#if torch.cuda.is_available():
# data = data.to(device)
# target = target.to(device)
optimizer.zero_grad()
# update the model
output_train = model(data)
loss = criterion(output_train, target.unsqueeze(1).type_as(output_train))
loss.backward()
optimizer.step()
model.get()
if batch_idx % args.log_interval == 0:
loss = loss.get()
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * data.shape[0], len(train_loader),
100. * batch_idx / len(train_loader), loss.item()))
import time
t = time.time()
for epoch in range(1, args.epochs + 1):
trains(epoch)
total_time = time.time() - t
print('Total', round(total_time, 20), 's')