<commit_before>"""TODO: Docstring."""

from __future__ import print_function

import argparse
import keras

import gandlf
import numpy as np

# For repeatability.
np.random.seed(1667)


def get_training_data(num_samples):
    """Generates some training data."""

    x = np.random.randint(0, 2, size=(num_samples, 2))

    y = np.logical_xor(x[:, 0], x[:, 1])
    y = np.cast['int32'](y)
    y = np.eye(2)[y]

    x = np.cast['float32'](x) * 2 - 1  # Scales to [-1, 1].
    x += np.random.normal(scale=0.3, size=x.shape)  # Adds some random noise.

    return x, y


def build_generator(latent_size):
    latent_layer = keras.layers.Input(shape=(latent_size,), name='latent')
    class_input = keras.layers.Input(shape=(2,), name='class')

    input_layer = keras.layers.merge([latent_layer, class_input],
                                     mode='concat')
    hidden_layer = keras.layers.Dense(16, activation='tanh')(input_layer)
    output_layer = keras.layers.Dense(2)(hidden_layer)

    return keras.models.Model([latent_layer, class_input],
                              output_layer, name='generator')


def build_discriminator():
    input_layer = keras.layers.Input(shape=(2,), name='data_input')

    normalized = keras.layers.BatchNormalization()(input_layer)
    hidden_layer = keras.layers.Dense(16, activation='tanh')(normalized)

    real_fake_pred = keras.layers.Dense(1, activation='sigmoid',
                                        name='real_fake')(hidden_layer)
    class_pred = keras.layers.Dense(2, activation='sigmoid',
                                    name='class')(hidden_layer)

    # The first output of this model (real_fake_pred) is treated as
    # the "real / fake" predictor.
    return keras.models.Model(input_layer, [real_fake_pred, class_pred],
                              name='discriminator')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Basic XOR example using a GAN.')

    training_params = parser.add_argument_group('training params')
    training_params.add_argument('--nb_epoch', type=int, default=10,
                                 metavar='INT',
                                 help='number of training epochs')
    training_params.add_argument('--nb_batch', type=int, default=100,
                                 metavar='INT',
                                 help='number of samples per batch')
    training_params.add_argument('--supervised', default=False,
                                 action='store_true',
                                 help='if set, train as an ACGAN')

    model_params = parser.add_argument_group('model params')
    model_params.add_argument('--nb_latent', type=int, default=10,
                              metavar='INT',
                              help='dimensions in the latent vector')
    model_params.add_argument('--nb_samples', type=int, default=10000,
                              metavar='INT',
                              help='total number of training samples')

    args = parser.parse_args()

    model = gandlf.Model(build_generator(args.nb_latent),
                         build_discriminator())
    model.compile(optimizer='adam', loss={
        'class': 'categorical_crossentropy',
        'generator': gandlf.losses.negative_binary_crossentropy,
        'discriminator': 'binary_crossentropy',
    }, metrics=['accuracy'])

    x, y = get_training_data(args.nb_samples)

    model.fit({'latent': 'normal', 'data_input': x, 'class': y},
              {'generator': 'ones', 'discriminator': 'zeros', 'class': y},
              train_auxiliary=args.supervised, nb_epoch=args.nb_epoch,
              batch_size=args.nb_batch)

    print('\n:: Input Data ::')
    print(x[:10])

    print('\n:: Target Data ::')
    print(y[:10])

    if args.supervised:
        print('\n:: Predictions for Real Data ::')
        print(np.round(model.predict({'data_input': x[:10]})[1]))

    print('\n:: Generated Input Data (Knowing Target Data) ::')
    print(model.sample({'latent': 'normal', 'class': y[:10]}))

    if args.supervised:
        print('\n:: Predictions for Generated Data ::')
        print(model.predict({'data_input': 'normal'}, num_samples=10)[1])<commit_msg>minor change to xor model<commit_after>"""TODO: Docstring."""

from __future__ import print_function

import argparse
import keras

import gandlf
import numpy as np

# For repeatability.
np.random.seed(1667)


def get_training_data(num_samples):
    """Generates some training data."""

    x = np.random.randint(0, 2, size=(num_samples, 2))

    y = np.logical_xor(x[:, 0], x[:, 1])
    y = np.cast['int32'](y)

    x = np.cast['float32'](x) * 2 - 1  # Scales to [-1, 1].
    x += np.random.normal(scale=0.3, size=x.shape)  # Adds some random noise.

    return x, y


def build_generator(latent_size):
    latent_layer = keras.layers.Input(shape=(latent_size,), name='latent')
    class_input = keras.layers.Input(shape=(1,), name='class')
    embedded = keras.layers.Embedding(2, latent_size,
                                      init='glorot_normal')(class_input)
    flat_embedded = keras.layers.Flatten()(embedded)

    input_layer = keras.layers.merge([latent_layer, flat_embedded],
                                     mode='mul')
    hidden_layer = keras.layers.Dense(16, activation='tanh')(input_layer)
    output_layer = keras.layers.Dense(2)(hidden_layer)

    return keras.models.Model([latent_layer, class_input],
                              output_layer, name='generator')


def build_discriminator():
    input_layer = keras.layers.Input(shape=(2,), name='data_input')

    normalized = keras.layers.BatchNormalization()(input_layer)
    hidden_layer = keras.layers.Dense(16, activation='tanh')(normalized)

    real_fake_pred = keras.layers.Dense(1, activation='sigmoid',
                                        name='real_fake')(hidden_layer)
    class_pred = keras.layers.Dense(2, activation='sigmoid',
                                    name='class')(hidden_layer)

    # The first output of this model (real_fake_pred) is treated as
    # the "real / fake" predictor.
    return keras.models.Model(input_layer, [real_fake_pred, class_pred],
                              name='discriminator')


if __name__ == '__main__':
    parser = argparse.ArgumentParser(
        description='Basic XOR example using a GAN.')

    training_params = parser.add_argument_group('training params')
    training_params.add_argument('--nb_epoch', type=int, default=10,
                                 metavar='INT',
                                 help='number of training epochs')
    training_params.add_argument('--nb_batch', type=int, default=100,
                                 metavar='INT',
                                 help='number of samples per batch')
    training_params.add_argument('--supervised', default=False,
                                 action='store_true',
                                 help='if set, train as an ACGAN')

    model_params = parser.add_argument_group('model params')
    model_params.add_argument('--nb_latent', type=int, default=10,
                              metavar='INT',
                              help='dimensions in the latent vector')
    model_params.add_argument('--nb_samples', type=int, default=10000,
                              metavar='INT',
                              help='total number of training samples')

    args = parser.parse_args()

    model = gandlf.Model(build_generator(args.nb_latent),
                         build_discriminator())
    model.compile(optimizer='adam', loss={
        'class': 'categorical_crossentropy',
        'generator': gandlf.losses.negative_binary_crossentropy,
        'discriminator': 'binary_crossentropy',
    }, metrics=['accuracy'])

    x, y = get_training_data(args.nb_samples)
    y_ohe = np.eye(2)[y]
    y = np.expand_dims(y, -1)

    model.fit({'latent': 'normal', 'data_input': x, 'class': y},
              {'generator': 'ones', 'discriminator': 'zeros', 'class': y_ohe},
              train_auxiliary=args.supervised, nb_epoch=args.nb_epoch,
              batch_size=args.nb_batch)

    print('\n:: Input Data ::')
    print(x[:10])

    print('\n:: Target Data ::')
    print(y[:10])

    if args.supervised:
        print('\n:: Predictions for Real Data ::')
        print(np.round(model.predict({'data_input': x[:10]})[1]))

    print('\n:: Generated Input Data (Knowing Target Data) ::')
    print(model.sample({'latent': 'normal', 'class': y[:10]}))

    if args.supervised:
        print('\n:: Predictions for Generated Data ::')
        print(model.predict({'data_input': 'normal'}, num_samples=10)[1])
{'fraction_non_alphanumeric': 0.13695627091265722, 'fraction_numerical': 0.012461059190031152, 'mean_word_length': 2.502222222222222, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 19, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3548740', 'n_tokens_mistral': 2721, 'n_tokens_neox': 2598, 'n_words': 506}