示例#1
0
ac_gan = ACGAN(100, 10, 1, device)
print(ac_gan)

dataset = datasets.MNIST(root='./data',
                         download=True,
                         transform=transforms.Compose([
                             transforms.Resize(64),
                             transforms.ToTensor(),
                             transforms.Normalize((0.5, ), (0.5, ))
                         ]))

dataloader = torch.utils.data.DataLoader(dataset,
                                         batch_size=100,
                                         shuffle=True,
                                         num_workers=2)

src_criterion = nn.MSELoss()
c_criterion = nn.CrossEntropyLoss()

d_optim = optim.Adam(ac_gan.discriminator.parameters(),
                     lr=0.0002,
                     betas=(0.5, 0.999))
g_optim = optim.Adam(ac_gan.generator.parameters(),
                     lr=0.0002,
                     betas=(0.5, 0.999))

ac_gan.train(dataloader, 50, src_criterion, c_criterion, g_optim, d_optim)

torch.save(ac_gan.generator.state_dict(), './mnist-acgan-g.pth')
torch.save(ac_gan.discriminator.state_dict(), './mnist-acgan-d.pth')
        month_label_train = month_label[:num_train]
        month_label_val = month_label[num_train:]
        day_label_train = day_label[:num_train]
        day_label_val = day_label[num_train:]
        print(x_train.shape, x_val.shape, month_label_train.shape,
              month_label_val.shape, day_label_train.shape,
              day_label_val.shape)

        weight_path = 'weights/pecan' + '_user_' + str(user_id) + '_'
        model = Model(input_dim=2, window_length=96, weight_path=weight_path)

        if train:
            num_epoch = args['num_epoch']
            print(model.discriminator.summary())
            print(model.generator.summary())
            model.train([x_train, month_label_train, day_label_train],
                        [x_val, month_label_val, day_label_val],
                        num_epoch=num_epoch)
        else:
            x_generated = model.generate_by_date(1461)
            usage_generated = x_generated[:, :, 0]
            gen_generated = x_generated[:, :, 1]
            usage_generated_recover = usage_recover(usage_generated)
            gen_generated_recover = gen_recover(gen_generated)
            data = np.stack((usage_generated_recover, gen_generated_recover),
                            axis=-1)
            print('The shape of the synthetic data is {}'.format(data.shape))
            np.savez_compressed('synthetic/user_' + str(user_id) +
                                '_acgan.npz',
                                data=data)