Example #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
Example #2
0
def main():
    model_filename = 'network-regression_1.pt'
    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(f'./../../deep_learning/models/{model_filename}')
    network.load_state_dict(parameters)

    std = -3.1
    genome = get_genome_from_standard_network(network, std=std)

    evaluation_engine = EvaluationStochasticEngine(testing=False)


    x, y_true, y_pred, kl_posterior = \
        evaluation_engine.evaluate_genome(genome, n_samples=1, is_gpu=False, return_all=True)

    print()
    print(f'KL Posterior: {kl_posterior}')

    x = x.numpy()
    x = evaluation_engine.dataset.input_scaler.inverse_transform(x)
    y_pred = evaluation_engine.dataset.output_scaler.inverse_transform(
        y_pred.numpy())
    y_true = evaluation_engine.dataset.output_scaler.inverse_transform(
        y_true.numpy())

    # plot results
    plt.figure(figsize=(20, 20))
    plt.plot(x, y_true, 'r*')
    plt.plot(x, y_pred, 'b*')
    plt.show()

    print(f'MSE: {mean_squared_error(y_true, y_pred) * 100} %')
Example #3
0
def main():
    model_filename = 'network-classification.pt'
    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(f'./../../deep_learning/models/{model_filename}')
    network.load_state_dict(parameters)

    std = -3.1
    genome = get_genome_from_standard_network(network, std=std)

    # genome = generate_genome_with_hidden_units(2, 2, n_hidden=1)

    evaluation_engine = EvaluationStochasticEngine(testing=False)

    x, y_true, y_pred, kl_posterior = \
        evaluation_engine.evaluate_genome(genome, n_samples=100, is_gpu=False, return_all=True)

    print()
    print(f'KL Posterior: {kl_posterior}')

    x = x.numpy()
    x = evaluation_engine.dataset.input_scaler.inverse_transform(x)
    y_true = y_true.numpy()

    # plot results
    y_pred = np.argmax(y_pred.numpy(), 1)
    df = pd.DataFrame(x, columns=['x1', 'x2'])
    df['y'] = y_pred

    x1_limit, x2_limit = evaluation_engine.dataset.get_separation_line()

    plt.figure()
    ax = sns.scatterplot(x='x1', y='x2', hue='y', data=df)
    ax.plot(x1_limit, x2_limit, 'g-', linewidth=2.5)
    plt.show()


    print('Confusion Matrix:')
    print(confusion_matrix(y_true, y_pred))

    print(f'Accuracy: {accuracy_score(y_true, y_pred) * 100} %')
Example #4
0
def regression_problem_learn_from_nn():

    # 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 = prepare_genome(parameters)
    print(genome)
    evaluation_engine = EvaluationStochasticEngine(testing=False,
                                                   batch_size=None)

    x, y_true, y_pred, kl_posterior = \
        evaluation_engine.evaluate_genome(genome, n_samples=100, 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}')