Beispiel #1
0
def main():
    layers_conf = [[784, 10], [784, 100, 10], [784, 100, 100, 10],
                   [784, 100, 100, 100, 10]]
    results = []
    for layers in layers_conf:
        print(layers)
        clf = TFDeep(layers, 5e-3, 1e-4)
        clf.train_mb(mnist.train.images, mnist.train.labels, 25)
        for digit in digits:
            probs = clf.eval(np.array([[*digit]]))
            print(np.argmax(probs, axis=1))
        Y_ = np.argmax(clf.eval(mnist.test.images), axis=1)
        Y = np.argmax(mnist.test.labels, axis=1)
        # acc, pr, m = data.eval_perf_multi(Y, Y_)
        # print(acc)
        # print(sum([p for p, r in pr]) / 10, [p for p, r in pr])
        # print(sum([r for p, r in pr]) / 10, [r for p, r in pr])
        # print(m)
        # results.append(
        #     (
        #         acc,
        #         (sum([p for p, r in pr]) / 10, [p for p, r in pr]),
        #         (sum([r for p, r in pr]) / 10, [r for p, r in pr]),
        #         m
        #     )
        # )

        Y_best = np.argmax(clf.eval_with_weights(mnist.test.images, clf.best_W,
                                                 clf.best_b),
                           axis=1)
        acc, pr, m = data.eval_perf_multi(Y, Y_best)
        print(acc)
        print(sum([p for p, r in pr]) / 10, [p for p, r in pr])
        print(sum([r for p, r in pr]) / 10, [r for p, r in pr])
        print(m)
        results.append(
            (acc, (sum([p for p, r in pr]) / 10, [p for p, r in pr]),
             (sum([r for p, r in pr]) / 10, [r for p, r in pr]), m))

        if clf.N == 1:
            W = clf.best_W
            for weights in W[0].T:
                plt.imshow(weights.reshape((28, 28)),
                           cmap=plt.get_cmap('gray'),
                           vmin=min(weights),
                           vmax=max(weights))
                plt.show()
    for l, r in zip(layers_conf, results):
        print(l)
        for i in r:
            print(i)
Beispiel #2
0
def svm_sol():
    for kernel in ['linear', 'rbf']:
        print(kernel)
        clf = ksvm_wrap.KSVMWrap(mnist.train.images,
                                 np.argmax(mnist.train.labels, axis=1),
                                 kernel=kernel)
        Y_ = clf.predict(mnist.test.images)
        Y = np.argmax(mnist.test.labels, axis=1)
        print(Y, Y_)
        acc, pr, m = data.eval_perf_multi(Y, Y_)
        print(acc)
        print(sum([p for p, r in pr]) / 10, [p for p, r in pr])
        print(sum([r for p, r in pr]) / 10, [r for p, r in pr])
        print(m)
Beispiel #3
0
def main():
    # inicijaliziraj generatore slučajnih brojeva
    np.random.seed(100)
    tf.set_random_seed(100)

    # instanciraj podatke X i labele Yoh_
    X, Y_ = data.sample_gmm_2d(6, 2, 10)
    Yoh_ = data.class_to_onehot(Y_)

    # izgradi graf:
    tflr = TFDeep([2, 3, 2, 2], 0.1, 1e-4)
    tflr.count_params()

    # nauči parametre:
    tflr.train(X, Yoh_, int(1e4))

    # dohvati vjerojatnosti na skupu za učenje
    probs = tflr.eval(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('acc', accuracy)
    print('recall', recall)
    print('precision', precision)

    # iscrtaj rezultate, decizijsku plohu

    rect = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface(lambda x: np.argmax(tflr.eval(x), axis=1),
                       rect,
                       offset=0.5)

    # graph the data points
    data.graph_data(X, Y_, Y, special=[])

    data.plt.show()
    # instanciraj podatke X i labele Yoh_
    X, Y_ = data.sample_gauss_2d(3, 100)
    Yoh_ = data.class_to_onehot(Y_)

    # izgradi graf:
    tflr = TFLogreg(X.shape[1], Yoh_.shape[1], 0.5, 1e-2)

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

    # dohvati vjerojatnosti na skupu za učenje
    probs = tflr.eval(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('acc', accuracy)
    print('recall', recall)
    print('precision', precision)

    # iscrtaj rezultate, decizijsku plohu

    rect = (np.min(X, axis=0), np.max(X, axis=0))
    data.graph_surface(lambda x: np.argmax(tflr.eval(x), axis=1),
                       rect,
                       offset=0.5)

    # graph the data points
    data.graph_data(X, Y_, Y, special=[])

    data.plt.show()