Beispiel #1
0
    def __init__(self,
                 player_params,
                 base_model=None,
                 player_models=None,
                 player_names=None):
        """
        Initialize adversarial model. Specify base_model or player_models, not both.
        :param player_params: list of player parameters for each player (shared variables)
        :param base_model: base model will be duplicated for each player to create player models
        :param player_models: model for each player
        :param player_names: names of each player (optional)
        """

        assert (len(player_params) > 0)
        self.player_params = player_params
        self.player_count = len(self.player_params)
        if player_names is None:
            player_names = [
                "player_{}".format(i) for i in range(self.player_count)
            ]
        assert (len(player_names) == self.player_count)
        self.player_names = player_names

        self.generator_optimizer = None
        self.discriminator_optimizer = None
        self.loss = None
        self.total_loss = None
        self.optimizer = None
        self._function_kwargs = None
        if base_model is None and player_models is None:
            raise ValueError(
                "Please specify either base_model or player_models")
        if base_model is not None and player_models is not None:
            raise ValueError("Specify base_model or player_models, not both")
        if base_model is not None:
            self.layers = []
            for i in range(self.player_count):
                # duplicate base model
                model = Model(
                    base_model.inputs,
                    fix_names(base_model(base_model.inputs),
                              base_model.output_names))
                # add model to list
                self.layers.append(model)
        if player_models is not None:
            assert (len(player_models) == self.player_count)
            self.layers = player_models
    def __init__(self, player_params, base_model=None, player_models=None, player_names=None):
        """
        Initialize adversarial model. Specify base_model or player_models, not both.
        :param player_params: list of player parameters for each player (shared variables)
        :param base_model: base model will be duplicated for each player to create player models
        :param player_models: model for each player
        :param player_names: names of each player (optional)
        """

        assert (len(player_params) > 0)
        self.player_params = player_params
        self.player_count = len(self.player_params)
        if player_names is None:
            player_names = ["player_{}".format(i) for i in range(self.player_count)]
        assert (len(player_names) == self.player_count)
        self.player_names = player_names

        self.generator_optimizer = None
        self.discriminator_optimizer = None
        self.loss = None
        self.total_loss = None
        self.optimizer = None
        self._function_kwargs = None
        if base_model is None and player_models is None:
            raise ValueError("Please specify either base_model or player_models")
        if base_model is not None and player_models is not None:
            raise ValueError("Specify base_model or player_models, not both")
        if base_model is not None:
            self.layers = []
            for i in range(self.player_count):
                # duplicate base model
                model = Model(base_model.inputs,
                              fix_names(base_model(base_model.inputs), base_model.output_names))
                # add model to list
                self.layers.append(model)
        if player_models is not None:
            assert (len(player_models) == self.player_count)
            self.layers = player_models
Beispiel #3
0
def example_aae(adversarial_optimizer):
    # z \in R^100
    latent_dim = 200
    # x \in R^{28x28}
    input_shape = (28, 28)

    # generator (z -> x)
    generator = model_generator(latent_dim, input_shape)
    # encoder (x ->z)
    encoder = model_encoder(latent_dim, input_shape)
    # autoencoder (x -> x')
    print (encoder.inputs)
    e = encoder(encoder.inputs)
    g = generator(e)
    autoencoder = Model(encoder.inputs, g)
    # discriminator (z -> y)
    discriminator = model_discriminator(latent_dim)

    # assemple AAE
    x = encoder.inputs[0]
    z = encoder(x)
    xpred = generator(z)
    zreal = normal_latent_sampling((latent_dim,))(x)
    yreal = discriminator(zreal)
    yfake = discriminator(z)
    aae = Model(x, fix_names([xpred, yfake, yreal], ["xpred", "yfake", "yreal"]))

    # print summary of models
    generator.summary()
    encoder.summary()
    discriminator.summary()
    autoencoder.summary()

    # build adversarial model
    generative_params = generator.trainable_weights + encoder.trainable_weights
    model = AdversarialModel(base_model=aae,
                             player_params=[generative_params, discriminator.trainable_weights],
                             player_names=["generator", "discriminator"])
    model.adversarial_compile(adversarial_optimizer=adversarial_optimizer,
                              player_optimizers=[Adam(1e-4, decay=1e-4), Adam(1e-3, decay=1e-4)],
                              loss={"yfake": "binary_crossentropy", "yreal": "binary_crossentropy",
                                    "xpred": "mean_squared_error"},
                              player_compile_kwargs=[{"loss_weights": {"yfake": 1e-2, "yreal": 1e-2, "xpred": 1}}] * 2)

    # load mnist data
    xtrain, xtest = mnist_data()

    # callback for image grid of generated samples
    def generator_sampler():
        zsamples = np.random.normal(size=(10 * 10, latent_dim))
        return generator.predict(zsamples).reshape((10, 10, 28, 28))

    # callback   for image grid of autoencoded samples
    def autoencoder_sampler():
        xsamples = n_choice(xtest, 10)
        xrep = np.repeat(xsamples, 9, axis=0)
        xgen = autoencoder.predict(xrep).reshape((10, 9, 28, 28))
        xsamples = xsamples.reshape((10, 1, 28, 28))
        samples = np.concatenate((xsamples, xgen), axis=1)
        return samples
    
    # train network
    # generator, discriminator; pred, yfake, yreal
    n = xtrain.shape[0]
    y = [xtrain, np.ones((n, 1)), np.zeros((n, 1)), xtrain, np.zeros((n, 1)), np.ones((n, 1))]
    ntest = xtest.shape[0]
    ytest = [xtest, np.ones((ntest, 1)), np.zeros((ntest, 1)), xtest, np.zeros((ntest, 1)), np.ones((ntest, 1))]
    history = model.fit(x=xtrain, y=y, validation_data=(xtest, ytest), nb_epoch=50, batch_size=32)

    # save model
    encoder.save("aae-lstm-encoder.h5")
    generator.save("aae-lstm-generator.h5")
    discriminator.save("aae-lstm-discriminator.h5")
Beispiel #4
0
    def adversarial_compile(self, adversarial_optimizer, player_optimizers,
                            loss, **kwargs):
        """
        Configures the learning process.
        :param adversarial_optimizer: instance of AdversarialOptimizer
        :param player_optimizers: list of optimizers for each player
        :param loss: loss function or function name
        :param kwargs: additional arguments to function compilation
        :return:
        """
        self._function_kwargs = kwargs
        self.adversarial_optimizer = adversarial_optimizer
        assert (len(player_optimizers) == self.player_count)

        self.optimizers = [
            optimizers.get(optimizer) for optimizer in player_optimizers
        ]
        self.loss = objectives.get(loss)
        self.optimizer = None

        # Build player models
        self.layers = []
        for i in range(self.player_count):
            # duplicate base model
            model = Model(
                self.base_model.inputs,
                fix_names(self.base_model(self.base_model.inputs),
                          self.base_model.output_names))
            # compile model
            model.compile(self.optimizers[i], loss=self.loss)
            # add model to list
            self.layers.append(model)

        self.train_function = None
        self.test_function = None

        # Inputs are same as base model
        self.internal_input_shapes = self.base_model.internal_input_shapes
        self.input_names = self.base_model.input_names
        self.inputs = self.base_model.inputs

        # Outputs are concatenated player models
        models = self.layers

        def collect(f):
            return list(itertools.chain.from_iterable(f(m) for m in models))

        self.internal_output_shapes = collect(
            lambda m: m.internal_output_shapes)
        self.loss_functions = collect(lambda m: m.loss_functions)
        self.targets = collect(lambda m: m.targets)
        self.outputs = collect(lambda m: m.outputs)
        self.sample_weights = collect(lambda m: m.sample_weights)
        self.sample_weight_modes = collect(lambda m: m.sample_weight_modes)
        # for each target, output name is {player}_{target}
        self.output_names = []
        for i in range(self.player_count):
            for name in models[i].output_names:
                self.output_names.append("{}_{}".format(
                    self.player_names[i], name))
        # for each metric, metric name is {player}_{metric}
        self.metrics_names = ["loss"]
        for i in range(self.player_count):
            for name in models[i].metrics_names:
                self.metrics_names.append("{}_{}".format(
                    self.player_names[i], name))

        # total loss is sum of losses
        self.total_loss = np.float32(0)
        for model in models:
            self.total_loss += model.total_loss