Пример #1
0
def findOptimalParams(model0, regularizerModule, paramsModule, subtrain, valid, n, p):
    X_subtrain, Y_subtrain = subtrain[0], subtrain[1]
    X_valid, Y_valid = valid[0], valid[1]

    model = model0.copy()
    model.X, model.Y_ = X_subtrain, Y_subtrain
    model.N = X_subtrain.shape[0]
    i = 0
    j = 0
    v = np.inf
    model_star = model0.copy()
    i_star = i
    paramsModule.niter = n

    lossClass = lossModule.Loss(model, paramsModule, regularizerModule, Y_subtrain)
    optimizationClass = optimizator.Optimizator(model, paramsModule, args.optimizer)

    while j < p:
        train(model, paramsModule, lossClass, optimizationClass, X_subtrain)

        i = i + n

        Y = classify(X_valid, model)
        accuracy, pr, M = data.eval_perf_multi(Y, Y_valid)
        v_prime = 1. - accuracy

        if v_prime < v:
            j = 0
            model_star = model.copy()
            i_star = i
            v = v_prime
        else:
            j = j + 1

    return model_star, i_star, v
Пример #2
0
    def train(self, X, Yoh_, param_niter, param_print_step=-1):
        if param_print_step < 0:
            param_print_step = param_niter / 10

        # parameter intiailization
        self.session.run(tf.global_variables_initializer())

        # optimization loop
        for i in range(param_niter):
            self.session.run([self.loss, self.train_step],
                             feed_dict={
                                 self.X: X,
                                 self.Yoh_: Yoh_
                             })
            if i % param_print_step == 0:
                accuracy = data.eval_perf_multi(self.eval(X), Yoh_)
                print("Iteration = ", i, ", Loss = ", self.loss,
                      ", Accuracy = ", accuracy)
Пример #3
0
    # instanciraj podatke X i labele Yoh_
    X, Y_ = data.sample_gauss_2d(3, 100)
    X, Y_ = torch.tensor(X, dtype=torch.float), torch.tensor(Y_,
                                                             dtype=torch.long)
    Yoh_ = torch.nn.functional.one_hot(Y_)

    # definiraj model:
    ptlr = PTLogreg(X.shape[1], Yoh_.shape[1])

    # nauči parametre (X i Yoh_ moraju biti tipa torch.Tensor):
    train(ptlr, X, Yoh_, 50000, 1e-2, 1e-6)
    X, Y_ = X.numpy(), Y_.numpy()

    # dohvati vjerojatnosti na skupu za učenje
    probs = eval(ptlr, X)
    Y = np.argmax(probs, axis=1)

    # ispiši performansu (preciznost i odziv po razredima)
    accuracy, recall, precision = data.eval_perf_multi(Y, Y_)
    print(accuracy, recall, precision)

    # iscrtaj rezultate, decizijsku plohu
    decfun = lambda X: eval(ptlr, X)
    bbox = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface_multi(decfun, bbox, offset=0.5)

    data.graph_data(X, Y_, Y, special=[])

    plt.show()
Пример #4
0
    config = [2, 10, 10, 2]

    # instanciraj podatke X i labele Yoh_
    X, Y_ = data.sample_gmm_2d(6, 2, 10)
    Yoh_ = F.one_hot(torch.from_numpy(Y_), config[-1])

    # definiraj model:
    ptdeep = PTDeep(config, torch.relu)

    # nauči parametre (X i Yoh_ moraju biti tipa torch.Tensor):
    train(ptdeep, torch.from_numpy(X), Yoh_, 10_000, 0.1)

    # dohvati vjerojatnosti na skupu za učenje
    probs = eval(ptdeep, X)
    Y = np.argmax(probs, axis=1)

    # ispiši performansu (preciznost i odziv po razredima)
    accuracy, pr, _ = data.eval_perf_multi(Y, Y_)
    print(f'accuracy: {accuracy}, precision: {pr[0]}, recall: {pr[1]}')

    # Ispiši imena i broj parametara
    ptdeep.count_params()

    # iscrtaj rezultate, decizijsku plohu
    decfun = pt_deep_decfun(ptdeep)
    bbox = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface(decfun, bbox, offset=0.5)
    data.graph_data(X, Y_, Y, special=[])

    # Prikaži
    plt.show()
Пример #5
0
    scores1 = np.dot(X, w1.transpose()) + b1  # NxH
    #ReLU
    h1 = np.maximum(0, scores1)  # NxH
    scores2 = np.dot(h1, w2.transpose()) + b2  # NxC

    #softmax
    expscores = np.exp(scores2)  # NxC
    sumexp = expscores.sum(axis=1)  # Nx1

    return (expscores.transpose() / sumexp).transpose()


if __name__ == '__main__':
    np.random.seed(32)
    X, Y = sample_gmm_2d(6, 2, 10)
    ##X=np.array([[1,3],[1,1],[3,2], [3,3]]);Y=np.array([1,0,2,2])
    w1, b1, w2, b2 = fcann2_train(X, Y)

    Y_ = np.empty((0, 0), dtype=np.int64)
    # get class by doing argmax on vector (highest value = predicted class)
    ##for v in fcann2_classify(X, w1, b1, w2, b2):
    ##    Y_ = np.append((Y_), np.argmax(v))
    Y_ = np.argmax(fcann2_classify(X, w1, b1, w2, b2),
                   axis=1)  # axis=1 - horizontally

    accuracy, conf_mat, prec_recall = data.eval_perf_multi(Y, Y_)
    print('accuracy:', accuracy)

    plt.scatter(X[:, 0], X[:, 1], c=Y)
    plt.show()
Пример #6
0
tf.set_random_seed(100)
np.random.seed(100)
mnist = input_data.read_data_sets(tf.app.flags.FLAGS.data_dir, one_hot=True)

N = mnist.train.images.shape[0]
D = mnist.train.images.shape[1]
C = mnist.train.labels.shape[1]

network = TFDeep([D, C], param_delta=0.5)

print(":::::::::::::::::::::::")
print(":::::::::::::::::::::::")
print(":::::::::::::::::::::::")
network.train_batch(mnist.train.images,
                    mnist.train.labels,
                    500,
                    param_print_step=25,
                    param_batch_size=1000)
print(":::::::::::::::::::::::")
print(":::::::::::::::::::::::")
print(":::::::::::::::::::::::")

# predict probabilities of the data points
probs = network.eval(mnist.test.images)
Y = probs.argmax(axis=1)
Y_ = mnist.test.labels.argmax(axis=1)

# print performance (per-class precision and recall)
accuracy, _, _ = data.eval_perf_multi(Y_, Y)
print("Accuracy = ", accuracy)
Пример #7
0
    # initialize the random number generator
    np.random.seed(100)
    tf.set_random_seed(100)

    # instantiate the data X and the labels Yoh_
    X, Y_ = data.sample_gmm_2d(5, 3, 40)
    Yoh_ = data.class_to_one_hot(Y_)

    # build the graph:
    tfdeep = TFDeep([2, 10, 3], param_delta=0.01)
    tfdeep.count_params()

    # perform the training with given hyper-parameters:
    tfdeep.train(X, Yoh_, 100000)

    # predict probabilities of the data points
    probs = tfdeep.eval(X)
    Y = probs.argmax(axis=1)

    # print performance (per-class precision and recall)
    accuracy = data.eval_perf_multi(Y, Y_)

    print("Accuracy = ", accuracy)

    # draw results, decision surface
    decfun = lambda x: tfdeep.eval(x).max(axis=1)
    bbox = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface(decfun, bbox, offset=0.5)
    data.graph_data(X, Y_, Y, special=[])
    plt.show()
Пример #8
0
            pt_deep.train(model,
                          x_train,
                          y_train_oh,
                          args.param_niter,
                          args.param_delta,
                          trace=loss_trace,
                          param_lambda=args.param_lambda,
                          X_val=x_val,
                          Y_val_oh_=y_val_oh)

        x_test = x_test.reshape(-1, 784).to(device)
        probs = model(x_test)
        Y = probs.cpu().detach().numpy().argmax(axis=1)

        print('Test scores:')
        test_accuracy, recall, precision = data.eval_perf_multi(Y, y_test)
        print(test_accuracy, recall, '\n', precision)

        probs = model(x_train)
        Y = probs.cpu().detach().numpy().argmax(axis=1)

        print('Train scores:')
        train_accuracy, recall, precision = data.eval_perf_multi(Y, y_train)
        print(train_accuracy, recall, '\n', precision)

        run_time = time.time() - t0
        print('Train and eval run in %.3f s' % run_time)

        if args.model_idx == 0 and args.show_weights:
            show_model_weights(model)
Пример #9
0
    # instanciraj podatke X i labele Yoh_
    X, Y_ = data.sample_gauss(3, 100)
    Yoh_ = data.class_to_onehot(Y_)

    # izgradi graf:
    tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.004)

    # nauči parametre:
    tflr.train(X, Yoh_, 20000)

    # dohvati vjerojatnosti na skupu za učenje
    probs = tflr.eval(X)
    Y = [np.argmax(ps) for ps in probs]

    # ispiši performansu (preciznost i odziv po razredima)
    accuracy, recall, precision = data.eval_perf_multi(Y_, np.array(Y))
    print('acc:', accuracy, '\n rec', recall)
    plt.show()

    # iscrtaj rezultate, decizijsku plohu
    bbox = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface(logreg_classify_function(tflr),
                       bbox,
                       offset=0.5,
                       width=256,
                       height=256)
    # graph the data points
    data.graph_data(X, Y_, Y)
    # show the plot
    plt.show()
Пример #10
0
    def classify(X):
        return logreg_classify(X, w, b)

    return classify


if __name__ == "__main__":
    np.random.seed(100)

    # get the training dataset
    X, Y_ = data.sample_gmm(5, 2, 100)

    # train the model
    W, b = logreg_train(X, Y_)

    # evaluate the model on the training dataset
    probs, logprobs = logreg_classify(X, W, b)
    Y = np.argmax(probs, axis=1)

    # report performance
    accuracy, confusion_matrix, recall = data.eval_perf_multi(Y, Y_)
    print(accuracy, confusion_matrix, recall)

    data.graph_data(X, Y_, Y, special=[])

    decfun = logreg_decfun(W, b)
    bbox = (np.min(X, axis=0), np.max(X, axis=0))
    #data.graph_surface(decfun, bbox, offset=0.5)

    plt.show()
Пример #11
0
                                            download=True)

    x_train, y_train = mnist_train.data, mnist_train.targets
    x_test, y_test = mnist_test.data, mnist_test.targets
    x_train, x_test = x_train.float().div_(255.0), x_test.float().div_(255.0)

    y_oh_train = F.one_hot(y_train, config[-1])
    y_oh_test = F.one_hot(y_test, config[-1])

    N = x_train.shape[0]
    D = x_train.shape[1] * x_train.shape[2]
    C = y_train.max().add_(1).item()

    model = PTDeep(config, torch.relu)
    train(model, x_train.view(-1, D), y_oh_train, 100, 0.1)
    # train_mb(model, x_train.view(-1, D), y_oh_train, 100, 100, 1e-4)

    # Prikazi slike koje najvise doprinose funkciji gubitka
    # show_high_loss_pics(model, x_train, y_oh_train)

    # dohvati vjerojatnosti na skupu za učenje
    probs = eval(model, x_test.view(-1, D))
    Y_pred = np.argmax(probs, axis=1)

    # ispiši performansu (preciznost i odziv po razredima)
    accuracy, pr, _ = data.eval_perf_multi(Y_pred, y_test)
    print(f'accuracy: {accuracy}, precision: {pr[0]}, recall: {pr[1]}')

    # Ispiši imena i broj parametara
    model.count_params()
    # show_weight_matrix(model)