示例#1
0
    def __init__(self,
                 midi_path_list,
                 batch_size=20,
                 seq_len=8,
                 time_fraction=1.0,
                 learning_rate=1e-10,
                 hidden_dim=15200,
                 generative_model=None,
                 discriminative_model=None,
                 gans_value_function=None):
        '''
        Init.

        Args:
            midi_path_list:         `list` of paths to MIDI files.
            batch_size:             Batch size.
            seq_len:                The length of sequence that LSTM networks will observe.
            time_fraction:          Time fraction or time resolution (seconds).

            learning_rate:          Learning rate in `Generator` and `Discriminator`.

            hidden_dim:             The number of units in hidden layer of `DiscriminativeModel`.

            true_sampler:           is-a `TrueSampler`.
            noise_sampler:          is-a `NoiseSampler`.
            generative_model:       is-a `GenerativeModel`.
            discriminative_model:   is-a `DiscriminativeModel`.
            gans_value_function:    is-a `GANsValueFunction`.
        '''
        self.__midi_controller = MidiController()
        self.__midi_df_list = [
            self.__midi_controller.extract(midi_path)
            for midi_path in midi_path_list
        ]

        bar_gram = BarGram(midi_df_list=self.__midi_df_list,
                           time_fraction=time_fraction)
        self.__bar_gram = bar_gram
        dim = self.__bar_gram.dim

        true_sampler = BarGramTrueSampler(bar_gram=bar_gram,
                                          midi_df_list=self.__midi_df_list,
                                          batch_size=batch_size,
                                          seq_len=seq_len,
                                          time_fraction=time_fraction)

        noise_sampler = BarGramNoiseSampler(bar_gram=bar_gram,
                                            midi_df_list=self.__midi_df_list,
                                            batch_size=batch_size,
                                            seq_len=seq_len,
                                            time_fraction=time_fraction)

        if generative_model is None:
            conv_activation_function = TanhFunction()
            conv_activation_function.batch_norm = BatchNorm()

            channel = noise_sampler.channel

            convolution_layer_list = [
                ConvolutionLayer1(
                    ConvGraph1(activation_function=conv_activation_function,
                               filter_num=batch_size,
                               channel=channel,
                               kernel_size=3,
                               scale=0.01,
                               stride=1,
                               pad=1))
            ]

            deconv_activation_function = SoftmaxFunction()

            deconvolution_layer_list = [
                DeconvolutionLayer(
                    DeCNNGraph(activation_function=deconv_activation_function,
                               filter_num=batch_size,
                               channel=channel,
                               kernel_size=3,
                               scale=0.01,
                               stride=1,
                               pad=1))
            ]

            opt_params_deconv = Adam()
            deconvolution_model = DeconvolutionModel(
                deconvolution_layer_list=deconvolution_layer_list,
                opt_params=opt_params_deconv)

            opt_params = Adam()
            opt_params.dropout_rate = 0.0

            generative_model = Generator(
                batch_size=batch_size,
                layerable_cnn_list=convolution_layer_list,
                deconvolution_model=deconvolution_model,
                condition_noise_sampler=UniformNoiseSampler(
                    low=-0.1,
                    high=0.1,
                    output_shape=(batch_size, channel, seq_len, dim)),
                learning_rate=learning_rate,
            )

        generative_model.noise_sampler = noise_sampler

        if discriminative_model is None:
            activation_function = LogisticFunction()
            activation_function.batch_norm = BatchNorm()

            # First convolution layer.
            conv2 = ConvolutionLayer2(
                # Computation graph for first convolution layer.
                ConvGraph2(
                    # Logistic function as activation function.
                    activation_function=activation_function,
                    # The number of `filter`.
                    filter_num=batch_size,
                    # The number of channel.
                    channel=noise_sampler.channel * 2,
                    # The size of kernel.
                    kernel_size=3,
                    # The filter scale.
                    scale=0.001,
                    # The nubmer of stride.
                    stride=1,
                    # The number of zero-padding.
                    pad=1))

            # Stack.
            layerable_cnn_list = [conv2]

            opt_params = Adam()
            opt_params.dropout_rate = 0.0

            if hidden_dim is None:
                hidden_dim = channel * seq_len * dim

            cnn_output_activating_function = LogisticFunction()

            cnn_output_graph = CNNOutputGraph(
                hidden_dim=hidden_dim,
                output_dim=1,
                activating_function=cnn_output_activating_function,
                scale=0.01)

            discriminative_model = Discriminator(
                batch_size=batch_size,
                layerable_cnn_list=layerable_cnn_list,
                cnn_output_graph=cnn_output_graph,
                opt_params=opt_params,
                computable_loss=CrossEntropy(),
                learning_rate=learning_rate)

        if gans_value_function is None:
            gans_value_function = MiniMax()

        GAN = GenerativeAdversarialNetworks(
            gans_value_function=gans_value_function,
            feature_matching=FeatureMatching(lambda1=0.01, lambda2=0.99))

        self.__noise_sampler = noise_sampler
        self.__true_sampler = true_sampler
        self.__generative_model = generative_model
        self.__discriminative_model = discriminative_model
        self.__GAN = GAN
        self.__time_fraction = time_fraction
示例#2
0
    def __init__(self,
                 midi_path_list,
                 target_program=0,
                 batch_size=10,
                 seq_len=4,
                 time_fraction=1.0,
                 true_sampler=None,
                 noise_sampler=None,
                 generative_model=None,
                 discriminative_model=None,
                 gans_value_function=None):
        '''
        Init.

        Args:
            midi_path_list:         `list` of paths to MIDI files.
            target_program:         Program in generated MIDI.
            batch_size:             Batch size.
            seq_len:                The length of sequence that LSTM networks will observe.
            time_fraction:          Time fraction or time resolution (seconds).
            true_sampler:           is-a `TrueSampler`.
            noise_sampler:          is-a `NoiseSampler`.
            generative_model:       is-a `GenerativeModel`.
            discriminative_model:   is-a `DiscriminativeModel`.
            gans_value_function:    is-a `GANsValueFunction`.
        '''
        self.__midi_controller = MidiController()
        self.__midi_df_list = [
            self.__midi_controller.extract(midi_path)
            for midi_path in midi_path_list
        ]

        # The dimension of observed or feature points.
        dim = 12

        if true_sampler is None:
            true_sampler = MidiTrueSampler(midi_path_list=midi_path_list,
                                           batch_size=batch_size,
                                           seq_len=seq_len)

        if noise_sampler is None:
            noise_sampler = MidiNoiseSampler(batch_size=batch_size)

        if generative_model is None:
            hidden_activating_function = TanhFunction()
            hidden_activating_function.batch_norm = BatchNorm()
            generative_model = Generator(
                batch_size=batch_size,
                seq_len=seq_len,
                input_neuron_count=dim,
                hidden_neuron_count=dim,
                hidden_activating_function=hidden_activating_function)

        generative_model.noise_sampler = noise_sampler

        if discriminative_model is None:
            discriminative_model = Discriminator(batch_size=batch_size,
                                                 seq_len=seq_len,
                                                 input_neuron_count=dim,
                                                 hidden_neuron_count=dim)

        if gans_value_function is None:
            gans_value_function = MiniMax()

        GAN = GenerativeAdversarialNetworks(
            gans_value_function=gans_value_function)

        self.__true_sampler = true_sampler
        self.__generative_model = generative_model
        self.__discriminative_model = discriminative_model
        self.__GAN = GAN
        self.__time_fraction = time_fraction
        self.__target_program = target_program