예제 #1
0
def main():
    np.random.seed(0)
    mnist_digits = load_digits()
    n_images = len(mnist_digits.images)  # 1797
    n_input = np.prod(mnist_digits.images.shape[1:])
    data_images = mnist_digits.images.reshape(n_images,
                                              -1) / 16.  # -> 1797 x 64
    weights, observations = get_data("exact", mnist_digits)
    n_ensemble = 500
    net = nn.NeuralNetworkClassifier(n_input=n_input, n_hidden=10, n_output=10)
    ensembles = sample_ensembles(n_ensemble, weights)
    model_outputs = [
        net.get_output_activation(data_images, *net.flatten_to_net_weights(e))
        for e in ensembles
    ]
    model_outputs = np.array(model_outputs)
    enkf = EnKF(tol=1e-5,
                maxit=3000,
                stopping_crit='',
                online=True,
                shuffle=False,
                n_batches=32,
                converge=True)
    enkf.fit(data=data_images,
             ensemble=ensembles,
             ensemble_size=n_ensemble,
             moments1=np.mean(ensembles, axis=0),
             u_exact=weights,
             model=net,
             observations=observations.T,
             model_output=model_outputs,
             noise=0.0,
             gamma=0.,
             p=None)
    M = enkf.M
    R = enkf.R
    tc = enkf.total_cost
    d = {
        'misfit': {
            "M": M[1:]
        },
        'residuals': {
            'R': R[1:]
        },
        'total_cost': {
            'total_cost': tc
        }
    }
    print("R:", R)
    print("M", M)
    print("TC:", tc)
    weights = np.mean(enkf.ensemble, 0)
    net.set_weights(*net.flatten_to_net_weights(weights))
    # output = net.get_output_activation(data_images, *net.flatten_to_net_weights(weights))
    # print(net.score(data_images, observations))
    plotter = Plotter(d)
    plotter.plot()
    plotter.get_plots()
예제 #2
0
 path_ = './dataloader.npy'
 n_ensembles = 1000
 conv_loss_mnist = []
 np.random.seed(0)
 batch_size = 32
 model = MnistOptimizee(root=root_,
                        batch_size=batch_size,
                        seed=0,
                        n_ensembles=n_ensembles,
                        path=path_)
 conv_ens = None
 gamma = np.eye(10) * 0.01
 enkf = EnKF(tol=1e-5,
             maxit=1,
             stopping_crit='',
             online=False,
             shuffle=False,
             n_batches=1,
             converge=False)
 for i in range(1000):
     model.generation = i + 1
     if i == 0:
         out = model.create_individual()
         conv_ens = out['conv_params']
         out = model.set_parameters(conv_ens)
         print('loss {} generation {}'.format(out['conv_loss'],
                                              model.generation))
     t = time.time()
     enkf.fit(data=out['input'],
              ensemble=conv_ens,
              ensemble_size=n_ensembles,
예제 #3
0
def main():
    np.random.seed(0)
    n_ensemble = 1000
    net = netw.NeuralNetworkClassifier(n_input=784, n_hidden=60, n_output=10)
    root = '/home/yegenoglu/Documents/toolbox/L2L/l2l/optimizees/multitask'
    batch_size = 40
    trainloader, testloader = load_data(root, batch_size)
    dataiter = iter(trainloader)
    testiter = iter(testloader)
    test_images, test_labels = testiter.next()
    test_images = test_images.squeeze().numpy().reshape(batch_size, -1)
    test_labels = test_labels.numpy()
    ensembles = create_ensembles(n_ensemble,
                                 net)  # sample_ensembles(n_ensemble, weights)
    gamma = np.eye(10) * 0.01
    enkf = EnKF(tol=1e-5,
                maxit=1,
                stopping_crit='',
                online=False,
                shuffle=False,
                n_batches=1,
                converge=False)
    cost_train_list = []
    cost_test_list = []
    score_train_list = []
    score_test_list = []
    labels_list = []
    output_train_list = []
    output_test_list = []
    activation_train_list = []
    for i in range(50):
        data_images, observations = dataiter.next()
        data_images = data_images.squeeze().numpy().reshape(batch_size, -1)
        observations = observations.numpy()
        model_outputs = [
            net.get_output_activation(data_images,
                                      *net.flatten_to_net_weights(e))
            for e in ensembles
        ]
        model_outputs = np.array(model_outputs)
        t = time.time()
        enkf.fit(data=data_images,
                 ensemble=ensembles,
                 ensemble_size=n_ensemble,
                 moments1=np.mean(ensembles, axis=0),
                 u_exact=None,
                 observations=observations,
                 model_output=model_outputs,
                 noise=0.0,
                 gamma=gamma,
                 p=None)
        ensembles = enkf.ensemble
        print('done in {} s'.format(time.time() - t))
        weights = np.mean(ensembles, 0)
        net.set_weights(*net.flatten_to_net_weights(weights))
        output = net.get_output_activation(
            data_images, *net.flatten_to_net_weights(weights))
        output_test = net.get_output_activation(
            test_images, *net.flatten_to_net_weights(weights))
        cost = _calculate_cost(_encode_targets(observations, 10),
                               _encode_targets(np.argmax(output, 0), 10),
                               'MSE')
        scores = score(observations, np.argmax(output, 0)) * 100

        cost_test = _calculate_cost(y=test_labels,
                                    y_hat=np.argmax(output_test, 0),
                                    loss_function='MSE')
        score_test = score(test_labels, np.argmax(output_test, 0)) * 100
        print('---- Train -----')
        print('targets: ', observations)
        print('predict: ', np.argmax(output, 0))
        print('score: ', scores)
        print('cost {} in iteration {}/{}'.format(cost, i + 1, 100))
        print('---- Test -----')
        print('accuracy: ', score_test)
        print('-----------------')
        cost_train_list.append(cost)
        cost_test_list.append(cost_test)
        score_train_list.append(scores)
        score_test_list.append(score_test)
        labels_list.extend(observations)
        output_train_list.extend(np.argmax(output, 0))
        output_test_list.extend(np.argmax(output_test, 0))
        activation_train_list.append(output)

    plot_total_cost(cost_train_list, title='Training_loss')
    plot_total_cost(cost_test_list, title='Test_loss')
    plot_total_cost(score_train_list, title='Training_accuracy')
    plot_total_cost(score_test_list, title='Test_accuracy')
    plot_distributions(labels_list, 'Targets')
    plot_distributions(output_train_list, 'Train_prediction')
    plot_distributions(output_test_list, 'Test_prediction')
    params = {
        'cost_train_list': cost_train_list,
        'cost_test_list': cost_test_list,
        'score_train_list': score_train_list,
        'score_test_list': score_test_list,
        'labels_list': labels_list,
        'output_train_list': output_train_list,
        'output_test_list': output_test_list,
        'activation_train_list': activation_train_list,
        'ensembles': ensembles
    }
    np.save('params.npy', params)