Exemple #1
0
def main():
	# load MNIST images
	images, labels = dataset.load_train_images()

	# config
	config = model.config

	# settings
	max_epoch = 10000
	num_trains_per_epoch = 5000
	num_validation_data = 10000
	batchsize = 128

	# seed
	np.random.seed(args.seed)
	if args.gpu_device != -1:
		cuda.cupy.random.seed(args.seed)

	# save validation accuracy per epoch
	csv_results = []

	# create semi-supervised split
	training_images, training_labels, validation_images, validation_labels = dataset.split_data(images, labels, num_validation_data, seed=args.seed)
	training_labels = np.random.randint(0, config.num_classes, training_labels.size).astype(np.int32)
	validation_labels = np.random.randint(0, config.num_classes, validation_labels.size).astype(np.int32)

	# training
	progress = Progress()
	for epoch in xrange(1, max_epoch):
		progress.start_epoch(epoch, max_epoch)
		sum_loss = 0

		for t in xrange(num_trains_per_epoch):
			# sample from data distribution
			image_batch, label_batch = dataset.sample_data(training_images, training_labels, batchsize, binarize=False)
			image_batch = np.reshape(image_batch, (-1, 1, 28, 28))
			distribution = model.discriminate(image_batch, apply_softmax=False)
			loss = F.softmax_cross_entropy(distribution, model.to_variable(label_batch))
			sum_loss += float(loss.data)

			model.backprop(loss)

			if t % 10 == 0:
				progress.show(t, num_trains_per_epoch, {})

		model.save(args.model_dir)
		train_accuracy = compute_accuracy(training_images, training_labels)
		validation_accuracy = compute_accuracy(validation_images, validation_labels)
		
		progress.show(num_trains_per_epoch, num_trains_per_epoch, {
			"loss": sum_loss / num_trains_per_epoch,
			"accuracy (validation)": validation_accuracy,
			"accuracy (train)": train_accuracy,
		})

		# write accuracy to csv
		csv_results.append([epoch, train_accuracy, validation_accuracy, progress.get_total_time()])
		data = pd.DataFrame(csv_results)
		data.columns = ["epoch", "train_accuracy", "validation_accuracy", "min"]
		data.to_csv("{}/result.csv".format(args.model_dir))
Exemple #2
0
def main():
	# load MNIST images
	images, labels = dataset.load_train_images()

	# config
	discriminator_config = gan.config_discriminator
	generator_config = gan.config_generator

	# settings
	max_epoch = 1000
	num_updates_per_epoch = 500
	plot_interval = 5
	batchsize_true = 100
	batchsize_fake = batchsize_true

	# seed
	np.random.seed(args.seed)
	if args.gpu_device != -1:
		cuda.cupy.random.seed(args.seed)

	# training
	progress = Progress()
	for epoch in xrange(1, max_epoch + 1):
		progress.start_epoch(epoch, max_epoch)
		sum_loss_critic = 0
		sum_loss_generator = 0

		for t in xrange(num_updates_per_epoch):

			for k in xrange(discriminator_config.num_critic):
				# clamp parameters to a cube
				gan.clip_discriminator_weights()
				# gan.decay_discriminator_weights()

				# sample true data from data distribution
				images_true = dataset.sample_data(images, batchsize_true, binarize=False)
				# sample fake data from generator
				images_fake = gan.generate_x(batchsize_fake)
				images_fake.unchain_backward()

				fw_true, activations_true = gan.discriminate(images_true)
				fw_fake, _ = gan.discriminate(images_fake)

				loss_critic = -F.sum(fw_true - fw_fake) / batchsize_true
				sum_loss_critic += float(loss_critic.data) / discriminator_config.num_critic

				# update discriminator
				gan.backprop_discriminator(loss_critic)

			# generator loss
			images_fake = gan.generate_x(batchsize_fake)
			fw_fake, activations_fake = gan.discriminate(images_fake)
			loss_generator = -F.sum(fw_fake) / batchsize_fake

			# feature matching
			if discriminator_config.use_feature_matching:
				features_true = activations_true[-1]
				features_true.unchain_backward()
				if batchsize_true != batchsize_fake:
					images_fake = gan.generate_x(batchsize_true)
					_, activations_fake = gan.discriminate(images_fake, apply_softmax=False)
				features_fake = activations_fake[-1]
				loss_generator += F.mean_squared_error(features_true, features_fake)

			# update generator
			gan.backprop_generator(loss_generator)
			sum_loss_generator += float(loss_generator.data)
			if t % 10 == 0:
				progress.show(t, num_updates_per_epoch, {})

		gan.save(args.model_dir)

		progress.show(num_updates_per_epoch, num_updates_per_epoch, {
			"wasserstein": -sum_loss_critic / num_updates_per_epoch,
			"loss_g": sum_loss_generator / num_updates_per_epoch,
		})

		if epoch % plot_interval == 0 or epoch == 1:
			plot(filename="epoch_{}_time_{}min".format(epoch, progress.get_total_time()))
Exemple #3
0
def main():
    # settings
    max_epoch = 1000
    num_updates_per_epoch = 500
    batchsize_u = 256
    batchsize_l = min(100, args.num_labeled_data)

    # seed
    np.random.seed(args.seed)
    if args.gpu_device != -1:
        cuda.cupy.random.seed(args.seed)

    # training
    progress = Progress()
    for epoch in range(1, max_epoch + 1):
        progress.start_epoch(epoch, max_epoch)
        sum_loss = 0
        sum_entropy = 0
        sum_conditional_entropy = 0
        sum_rsat = 0

        for t in range(num_updates_per_epoch):
            x_u = dataset.sample_data(train_images_u, batchsize_u)
            p = imsat.classify(x_u, apply_softmax=True)
            hy = imsat.compute_marginal_entropy(p)
            hy_x = F.sum(imsat.compute_entropy(p)) / batchsize_u
            Rsat = -F.sum(imsat.compute_lds(x_u)) / batchsize_u

            # semi-supervised
            loss_semisupervised = 0
            if args.num_labeled_data > 0:
                x_l, t_l = dataset.sample_labeled_data(train_images_l,
                                                       train_labels_l,
                                                       batchsize_l)
                log_p = imsat.classify(x_l, apply_softmax=False)
                loss_semisupervised = F.softmax_cross_entropy(
                    log_p, imsat.to_variable(t_l))

            loss = Rsat - config.lam * (
                config.mu * hy - hy_x) + config.sigma * loss_semisupervised
            imsat.backprop(loss)

            sum_loss += float(loss.data)
            sum_entropy += float(hy.data)
            sum_conditional_entropy += float(hy_x.data)
            sum_rsat += float(Rsat.data)

            if t % 10 == 0:
                progress.show(t, num_updates_per_epoch, {})

        imsat.save(args.model_dir)

        counts_train, accuracy_train = compute_accuracy(
            train_images, train_labels)
        counts_test, accuracy_test = compute_accuracy(test_images, test_labels)
        progress.show(
            num_updates_per_epoch, num_updates_per_epoch, {
                "loss": sum_loss / num_updates_per_epoch,
                "hy": sum_entropy / num_updates_per_epoch,
                "hy_x": sum_conditional_entropy / num_updates_per_epoch,
                "Rsat": sum_rsat / num_updates_per_epoch,
                "acc_test": accuracy_test,
                "acc_train": accuracy_test,
            })
        print(counts_train)
        print(counts_test)
        plot(counts_train, "train")
        plot(counts_test, "test")
Exemple #4
0
def main():
    # load MNIST images
    images, labels = dataset.load_train_images()

    # config
    discriminator_config = gan.config_discriminator
    generator_config = gan.config_generator

    # labels
    a = discriminator_config.a
    b = discriminator_config.b
    c = discriminator_config.c

    # settings
    max_epoch = 1000
    num_updates_per_epoch = 500
    plot_interval = 5
    batchsize_true = 100
    batchsize_fake = batchsize_true

    # seed
    np.random.seed(args.seed)
    if args.gpu_device != -1:
        cuda.cupy.random.seed(args.seed)

    # training
    progress = Progress()
    for epoch in xrange(1, max_epoch + 1):
        progress.start_epoch(epoch, max_epoch)
        sum_loss_d = 0
        sum_loss_g = 0

        for t in xrange(num_updates_per_epoch):
            # sample true data from data distribution
            images_true = dataset.sample_data(images,
                                              batchsize_true,
                                              binarize=False)
            # sample fake data from generator
            images_fake = gan.generate_x(batchsize_fake)
            images_fake.unchain_backward()

            d_true = gan.discriminate(images_true, return_activations=False)
            d_fake = gan.discriminate(images_fake, return_activations=False)

            loss_d = 0.5 * (F.sum((d_true - b)**2) + F.sum(
                (d_fake - a)**2)) / batchsize_true
            sum_loss_d += float(loss_d.data)

            # update discriminator
            gan.backprop_discriminator(loss_d)

            # generator loss
            images_fake = gan.generate_x(batchsize_fake)
            d_fake = gan.discriminate(images_fake, return_activations=False)
            loss_g = 0.5 * (F.sum((d_fake - c)**2)) / batchsize_fake
            sum_loss_g += float(loss_g.data)

            # update generator
            gan.backprop_generator(loss_g)

            if t % 10 == 0:
                progress.show(t, num_updates_per_epoch, {})

        gan.save(args.model_dir)

        progress.show(
            num_updates_per_epoch, num_updates_per_epoch, {
                "loss_d": sum_loss_d / num_updates_per_epoch,
                "loss_g": sum_loss_g / num_updates_per_epoch,
            })

        if epoch % plot_interval == 0 or epoch == 1:
            plot(filename="epoch_{}_time_{}min".format(
                epoch, progress.get_total_time()))