Exemplo n.º 1
0
def regression_problem(x):

    bias = x[:total_bias_params]
    weight = x[total_bias_params:]

    genome = generate_genome_with_hidden_units(n_input=config.n_input,
                                               n_output=config.n_output,
                                               n_hidden=n_neurons_per_layer)
    nodes = genome.node_genes
    connections = genome.connection_genes

    i = 0
    for key, node in nodes.items():
        node.bias_mean = bias[i]
        node.bias_std = bias[i+1]
        i += 2

    i = 0
    for key, connection in connections.items():
        connection.weight_mean = weight[i]
        connection.weight_std = weight[i + 1]
        i += 2

    evaluation_engine = EvaluationStochasticEngine(batch_size=50000)

    loss = \
        evaluation_engine.evaluate_genome(genome=genome, n_samples=N_SAMPLES, return_all=False)
    print(loss)
    return loss
Exemplo n.º 2
0
    def test_network_structure_siso(self):
        self.config = create_configuration(filename='/regression-siso.json')
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input, n_output=self.config.n_output)

        model = StochasticNetworkOld(genome=genome)

        input = torch.tensor([1.0])
        result = model(input.data)

        self.assertEqual(len(result), self.config.n_output)
Exemplo n.º 3
0
    def test_same_input_gives_different_result(self):
        self.config = create_configuration(filename='/regression-siso.json')
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input, n_output=self.config.n_output)

        model = StochasticNetworkOld(genome=genome)

        input = torch.tensor([[1.0]])
        result_1 = model(input.data)
        result_2 = model(input.data)

        self.assertNotEqual(result_1.item(), result_2.item())
Exemplo n.º 4
0
    def test_remove_connection_that_introduces_cycles(self):
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input,
            n_output=self.config.n_output,
            n_hidden=3)
        possible_connection_set = {(1, 2), (2, 2), (2, 3)}

        possible_connection_set = \
            ArchitectureMutation._remove_connection_that_introduces_cycles(genome=genome,
                                                                           possible_connection_set=possible_connection_set)
        expected_possible_connection_set = {(1, 2), (2, 3)}
        self.assertSetEqual(possible_connection_set,
                            expected_possible_connection_set)
Exemplo n.º 5
0
    def test_calculate_possible_inputs_when_adding_connection(self):
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input,
            n_output=self.config.n_output,
            n_hidden=3)
        out_node = genome.node_genes[0]

        possible_inputs = list(
            ArchitectureMutation.
            _calculate_possible_inputs_when_adding_connection(
                genome=genome, out_node_key=out_node.key, config=self.config))
        expected_possible_inputs = [-2, -1]
        self.assertEqual(expected_possible_inputs, possible_inputs)
Exemplo n.º 6
0
    def test_happy_path_miso(self):
        # Multiple-Input Single-Output
        self.config = create_configuration(
            filename='/classification-miso.json')
        self.config.parallel_evaluation = False
        self.config.n_processes = 1
        genome = generate_genome_with_hidden_units(
            n_input=self.config.n_input, n_output=self.config.n_output)
        population = {1: genome}
        evaluation_engine = EvaluationStochasticEngine()

        population = evaluation_engine.evaluate(population=population)

        self.assertEqual(type(population.get(1).fitness), float)
Exemplo n.º 7
0
def prepare_genome(parameters):
    genome = generate_genome_with_hidden_units(n_input=config.n_input,
                                               n_output=config.n_output,
                                               n_hidden=n_neurons_per_layer)
    nodes = genome.node_genes
    connections = genome.connection_genes

    # output nodes
    output_nodes = [0]
    for output_key in output_nodes:
        nodes[output_key].bias_mean = float(
            parameters['layer_0.bias'].numpy()[output_key])
        nodes[output_key].bias_std = std

    # hidden nodes
    i = 0
    bias_1 = parameters['layer_1.bias'].numpy()
    for key, node in nodes.items():
        if key in output_nodes:
            continue
        node.bias_mean = float(bias_1[i])
        node.bias_std = std
        i += 1

    # WEIGHTS
    hidden_neurons = list(range(1, 11))
    # layer 1 (input-> hidden)
    weights_1 = parameters['layer_1.weight'].numpy()
    for key, connection in connections.items():
        if key[1] not in hidden_neurons:
            continue
        connection.weight_mean = float(weights_1[key[1] - 1, 0])
        connection.weight_std = std

    weights_0 = parameters['layer_0.weight'].numpy()
    for key, connection in connections.items():
        if key[0] not in hidden_neurons:
            continue
        connection.weight_mean = float(weights_0[0, key[0] - 1])
        connection.weight_std = std
    return genome
Exemplo n.º 8
0
def main():

    # standard network
    network = FeedForward(n_input=config.n_input,
                          n_output=config.n_output,
                          n_neurons_per_layer=n_neurons_per_layer,
                          n_hidden_layers=1)
    parameters = torch.load('./../deep_learning/models/network.pt')
    network.load_state_dict(parameters)

    genome = generate_genome_with_hidden_units(n_input=config.n_input,
                                               n_output=config.n_output,
                                               n_hidden=n_neurons_per_layer)

    # genome = prepare_genome(parameters)
    print(genome)

    print('KL (q(w)||p(w)')
    kl_qw_pw = compute_kl_qw_pw(genome=genome)
    print(kl_qw_pw)

    evaluation_engine = EvaluationEngine(testing=False, batch_size=1)

    # setup network
    network = StochasticNetworkOld(genome=genome)
    network.eval()

    x, y_true, y_pred, kl_posterior, kl_qw_pw = \
        evaluation_engine.evaluate_genome(genome, n_samples=10, is_gpu=False, return_all=True)
    plt.figure(figsize=(20, 20))
    plt.plot(x.numpy().reshape(-1), y_true.numpy().reshape(-1), 'b*')
    plt.plot(x.numpy().reshape(-1), y_pred.numpy().reshape(-1), 'r*')
    plt.show()
    print(f'KL Div - Posterior: {kl_posterior}')
    print(f'KL Div - Prior: {kl_qw_pw}')
    print(f'MSE: {kl_posterior - kl_qw_pw}')
Exemplo n.º 9
0
 def setUp(self) -> None:
     path = get_config_files_path()
     filename = ''.join([path, '/regression-miso.json'])
     config = get_configuration(filename=filename)
     self.genome = generate_genome_with_hidden_units(
         n_input=config.n_input, n_output=config.n_output)
Exemplo n.º 10
0
def regression_problem(x):
    bias = x[:8]
    weight = x[8:]

    genome = generate_genome_with_hidden_units(n_input=config.n_input,
                                               n_output=config.n_output)
    nodes = genome.node_genes
    connections = genome.connection_genes

    i = 0
    for key, node in nodes.items():
        node.bias_mean = bias[i]
        node.bias_std = bias[i + 1]
        i += 2

    i = 0
    for key, connection in connections.items():
        connection.weight_mean = weight[i]
        connection.weight_std = weight[i + 1]
        i += 2

    evaluation_engine = EvaluationEngine(testing=True)
    dataset = evaluation_engine.dataset
    data_loader = evaluation_engine.data_loader
    loss = evaluation_engine.loss
    # setup network
    network = StochasticNetworkOld(genome=genome)
    network.eval()

    # calculate Data log-likelihood (p(y*|x*,D))
    mses = []
    xs = []
    y_preds = []
    y_trues = []
    for i in range(N_SAMPLES):
        for x_batch, y_batch in data_loader:
            x_batch = x_batch.reshape((-1, genome.n_input))
            x_batch = x_batch.float()
            y_batch = y_batch.float()

            with torch.no_grad():
                # forward pass
                output = network(x_batch)

                mse = loss(y_pred=output,
                           y_true=y_batch,
                           kl_qw_pw=0,
                           beta=evaluation_engine.get_beta())
                mses.append(mse)
            x = x_batch.numpy().reshape(-1)
            y_true_unnormalized = dataset.unnormalize_output(y_pred=y_batch)
            y_true = y_true_unnormalized.numpy()
            output_unnormalized = dataset.unnormalize_output(y_pred=output)
            y_pred = output_unnormalized.numpy().reshape(-1)
            xs.append(x)
            y_trues.append(y_true)
            y_preds.append(y_pred)
    x = np.concatenate(xs)
    y_true = np.concatenate(y_trues)
    y_pred = np.concatenate(y_preds)
    plt.figure(figsize=(20, 20))
    plt.plot(x, y_true, 'b*')
    plt.plot(x, y_pred, 'r*')
    plt.show()
    print(mse)