Add federated learning to my code below of using differential prinvacy to train gan opt…
- User
Add federated learning to my code below of using differential prinvacy to train gan opt = parser.parse_args()
try: os.makedirs(opt.outf) except OSError: pass
if opt.manualSeed is None: opt.manualSeed = random.randint(1, 10000) print("Random Seed: ", opt.manualSeed) random.seed(opt.manualSeed) torch.manual_seed(opt.manualSeed)
cudnn.benchmark = True
try: dataset = dset.MNIST( root=opt.data_root, download=True, transform=transforms.Compose( [ transforms.Resize(opt.imageSize), transforms.ToTensor(), transforms.Normalize((0.5,), (0.5,)), ] ), ) idx = dataset.targets == opt.target_digit dataset.targets = dataset.targets[idx] dataset.data = dataset.data[idx] nc = 1 except ValueError: print("Cannot load dataset")
dataloader = torch.utils.data.DataLoader( dataset, num_workers=int(opt.workers), batch_size=opt.batch_size, )
device = torch.device(opt.device) ngpu = int(opt.ngpu) nz = int(opt.nz) ngf = int(opt.ngf) ndf = int(opt.ndf)
custom weights initialization called on netG and netD
def weights_init(m): classname = m.class.name if classname.find("Conv") != -1: m.weight.data.normal_(0.0, 0.02) elif classname.find("BatchNorm") != -1: m.weight.data.normal_(1.0, 0.02) m.bias.data.fill_(0)
class Generator(nn.Module): def init(self, ngpu): super(Generator, self).init() self.ngpu = ngpu self.main = nn.Sequential( # input is Z, going into a convolution nn.ConvTranspose2d(nz, ngf * 8, 4, 1, 0, bias=False), nn.GroupNorm(min(32, ndf * 8), ndf * 8), nn.ReLU(True), # state size. (ngf8) x 4 x 4 nn.ConvTranspose2d(ngf * 8, ngf * 4, 4, 2, 1, bias=False), nn.GroupNorm(min(32, ndf * 4), ndf * 4), nn.ReLU(True), # state size. (ngf4) x 8 x 8 nn.ConvTranspose2d(ngf * 4, ngf * 2, 4, 2, 1, bias=False), nn.GroupNorm(min(32, ndf * 2), ndf * 2), nn.ReLU(True), # state size. (ngf*2) x 16 x 16 nn.ConvTranspose2d(ngf * 2, ngf, 4, 2, 1, bias=False), nn.GroupNorm(min(32, ndf), ndf), nn.ReLU(True), # state size. (ngf) x 32 x 32 nn.ConvTranspose2d(ngf, nc, 4, 2, 1, bias=False), nn.Tanh(), # state size. (nc) x 64 x 64 )
def forward(self, input): if input.is_cuda and self.ngpu > 1: output = nn.parallel.data_parallel(self.main, input, range(self.ngpu)) else: output = self.main(input) return outputnetG = Generator(ngpu) netG = netG.to(device) netG.apply(weights_init) if opt.netG != "": netG.load_state_dict(torch.load(opt.netG))
class Discriminator(nn.Module): def init(self, ngpu): super(Discriminator, self).init() self.ngpu = ngpu self.main = nn.Sequential( # input is (nc) x 64 x 64 nn.Conv2d(nc, ndf, 4, 2, 1, bias=False), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf) x 32 x 32 nn.Conv2d(ndf, ndf * 2, 4, 2, 1, bias=False), nn.GroupNorm(min(32, ndf * 2), ndf * 2), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf4) x 8 x 8 nn.Conv2d(ndf * 4, ndf * 8, 4, 2, 1, bias=False), nn.GroupNorm(min(32, ndf * 8), ndf * 8), nn.LeakyReLU(0.2, inplace=True), # state size. (ndf*8) x 4 x 4 nn.Conv2d(ndf * 8, 1, 4, 1, 0, bias=False), nn.Sigmoid(), )
- Assistant
- User
Giive the complete code, Take training code from the code I gave
- Assistant
- User
My code
Aggregate and update global models
- Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.