def build_network(conv_dim, z_size): # define discriminator and generator D = Discriminator(conv_dim) G = Generator(z_size=z_size, conv_dim=conv_dim) # initialize model weights D.apply(weights_init_normal) G.apply(weights_init_normal) return D, G
def train_gan(data_path, save_gen_path, save_disc_path, feedback_fn=None): data_loader = load_dataset(path=data_path, batch_size=128) gen = Generator().to('cuda') disc = Discriminator().to('cuda') criterion = torch.nn.BCEWithLogitsLoss() lr = 3e-4 gen_opt = torch.optim.Adam(gen.parameters(), lr=lr, betas=(0.5, 0.999)) disc_opt = torch.optim.Adam(disc.parameters(), lr=lr, betas=(0.5, 0.999)) gen = gen.apply(weights_init) disc = disc.apply(weights_init) n_epochs = 5 gen, disc = _train_loop(data_loader=data_loader, gen=gen, disc=disc, criterion=criterion, gen_opt=gen_opt, disc_opt=disc_opt, n_epochs=5, feedback_fn=feedback_fn) torch.save(gen.state_dict(), save_gen_path) torch.save(disc.state_dict(), save_disc_path)