def evaluation(trainer): num_sample = 10000 xp = gen.xp #xs = [] """ for i in range(0, num_sample, batchsize): z = Variable(xp.asarray(gen.make_hidden(batchsize))) with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): x = gen(z) x = chainer.cuda.to_cpu(x.data) xs.append(x) xs = np.asarray(xs) """ z = Variable(xp.asarray(gen.make_hidden(num_sample))) with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): x = gen(z) xs = chainer.cuda.to_cpu(x.data) real_data = GmmDataset(num_sample, 123, num_cluster=8, std=0.02, scale=2)._data a, b = np.ones((num_sample,)) / num_sample, np.ones((num_sample,)) / num_sample #print(xs) #print(real_data) M = ot.dist(xs, real_data) M /= M.max() distance = ot.emd2(a, b, M) del xs gc.collect() del real_data gc.collect() #print(distance) chainer.reporter.report({ 'EMD': distance, })
def evaluation(trainer): num_sample = 10000 xp = gen.xp #xs = [] """ for i in range(0, num_sample, batchsize): z = Variable(xp.asarray(gen.make_hidden(batchsize))) with chainer.using_config('train', False), chainer.using_config('enable_backprop', False): x = gen(z) x = chainer.cuda.to_cpu(x.data) xs.append(x) xs = np.asarray(xs) """ z = Variable(xp.asarray(gen.make_hidden(num_sample))) with chainer.using_config('train', False), chainer.using_config( 'enable_backprop', False): x = gen(z) xs = chainer.cuda.to_cpu(x.data) real_data = GmmDataset(num_sample, 123, num_cluster=8, std=0.02, scale=2)._data a, b = np.ones((num_sample, )) / num_sample, np.ones( (num_sample, )) / num_sample #print(xs) #print(real_data) M = ot.dist(xs, real_data) M /= M.max() distance = ot.emd2(a, b, M) del xs gc.collect() del real_data gc.collect() #path="/Users/keiikegami/Dropbox/research/Imaizumi-Sensei/paper_result/2dim/emd.pickle" with open("emd2.txt", 'ab') as f: f.write("{%d: %f}\n".encode() % (combination, distance))
def main(): parser = argparse.ArgumentParser(description='WGAN') parser.add_argument('--batchsize', '-b', type=int, default=64, help='Number of images in each mini-batch') parser.add_argument('--datasize', '-d', type=int, default=10000, help='Number of samples') parser.add_argument('--lam', '-l', type=int, default=0.5, help='Hyperparameter of gp') parser.add_argument('--lam2', '-l2', type=int, default=1.0, help='Hyperparameter of gp') parser.add_argument('--n_hidden', type=int, default=100, help='latent variable') parser.add_argument('--seed', '-s', type=int, default=111, help='seed number') parser.add_argument('--epoch', '-e', type=int, default=300, help='Number of sweeps over the dataset to train') parser.add_argument('--gpu', '-g', type=int, default=-1, help='GPU ID (negative value indicates CPU)') parser.add_argument('--out', '-o', default='result', help='Directory to output the result') parser.add_argument('--resume', '-r', default='', help='Resume the training from snapshot') parser.add_argument('--unit', '-u', type=int, default=1000, help='Number of units') parser.add_argument('--setting', '-set', type=int, default=10, help='Number of units') args = parser.parse_args(args=[]) # parameter set ## Optimizers alpha = 0.002 beta1 = 0.9 beta2 = 0.999 ## Network dim = 2 num_nets = 1 wscale = 0.02 # Networks generator = Generator(dim, num_nets, args.n_hidden, wscale) critic = Critic(num_nets, wscale) if args.gpu >= 0: chainer.cuda.get_device(args.gpu).use() generator.to_gpu() critic.to_gpu() # Optimizer set opt_g = chainer.optimizers.Adam(alpha = alpha, beta1 = beta1, beta2 = beta2, weight_decay_rate=0.0001) opt_g.setup(generator) opt_g.add_hook(chainer.optimizer.GradientClipping(1)) opt_c = chainer.optimizers.Adam(alpha = alpha, beta1 = beta1, beta2 = beta2, weight_decay_rate=0.0001) opt_c.setup(critic) opt_c.add_hook(chainer.optimizer.GradientClipping(1)) # Dataset dataset = GmmDataset(args.datasize, args.seed, std=0.02, scale=2) train_iter = chainer.iterators.SerialIterator(dataset, args.batchsize) # Trainer updater = WGANUpdater(train_iter, generator, critic, 5, opt_g, opt_c, args.lam, args.lam2, args.n_hidden, device=args.gpu) trainer = training.Trainer(updater, (args.epoch, 'epoch'), out=args.out) # Extensions trainer.extend(extensions.LogReport()) trainer.extend( extensions.PlotReport(['loss/critic'], 'epoch', file_name='criticloss_2dim%d.png' % args.setting)) trainer.extend( extensions.PlotReport( ['loss/generator'], 'epoch', file_name='genloss_2dim%d.png' % args.setting)) trainer.extend(extensions.PrintReport( ['epoch', 'loss/critic', 'loss/generator', 'elapsed_time'])) out = "/Users/keiikegami/Dropbox/research/Imaizumi-Sensei/paper_result/2dim/result%d" % args.setting trainer.extend(calcEMD(generator), trigger=(1, 'epoch')) trainer.extend(extensions.LogReport(log_name = "log_2dim%d" % args.setting)) trainer.extend(out_generated(generator, args.seed + 10, out, radius=2), trigger=(10, 'epoch')) trainer.extend(extensions.ProgressBar()) if args.resume: chainer.serializers.load_npz(args.resume, trainer) # Run trainer.run()
fig, axes = plt.subplots(1, 1, figsize=(5, 5)) axes.scatter(data[:, 0], data[:, 1], alpha=0.5, color='darkgreen', s=17) axes.set_title('Real Data') axes.set_xlim(-radius-5.0, radius+5.0) axes.set_ylim(-radius-5.0, radius+5.0) fig.tight_layout() fig.savefig(file_path + '/training_data_scatter.jpg') plt.close(fig) if __name__ == "__main__": gmm = GmmDataset(10000, 123, num_cluster=8, std=0.02, scale=2) file = "/Users/keiikegami/Dropbox/research/Imaizumi-Sensei/image_results/2dim" data = gmm._data plot_kde_data_real(data, file, radius = 2) plot_scatter_real_data(data, file, radius = 2) def calcEMD(gen): @chainer.training.make_extension() def evaluation(trainer): num_sample = 10000 xp = gen.xp #xs = [] """ for i in range(0, num_sample, batchsize):