Example #1
0
def main():

    nn = NN()
    epoch = 50

    x, y = DataUtil.get_dataset("cifar10", "../_Data/cifar10.txt", quantized=True, one_hot=True)

    x = x.reshape(len(x), 3, 32, 32)
    nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("MaxPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((64, 3, 3),))
    nn.add("ConvReLU", ((64, 3, 3),))
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ReLU", (512,))
    nn.add("Identical", (64,), apply_bias=False)
    nn.add("Normalize", activation="ReLU")
    nn.add("Dropout")
    nn.add("CrossEntropy", (y.shape[1],))

    # nn.disable_timing()
    nn.fit(x, y, lr=0.001, epoch=epoch, train_rate=0.8,
           metrics=["acc"], record_period=1, verbose=4)
    nn.evaluate(x, y)
    nn.show_timing_log()
    nn.draw_logs()
Example #2
0
def main():
    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 5
    record_period = 1

    x, y = DataUtil.get_dataset("mnist", "../../../../_Data/mnist.txt", quantized=True, one_hot=True)
    batch_size = 128

    if not load:
        nn.add("ReLU", (x.shape[1], 1024))
        nn.add("ReLU", (1024,))
        nn.add("CrossEntropy", (y.shape[1],))
        nn.optimizer = "Adam"
        nn.preview()
        nn.fit(x, y, lr=lr, lb=lb,
               epoch=epoch, batch_size=batch_size, record_period=record_period,
               show_loss=show_loss, train_only=train_only, do_log=True, verbose=verbose)
        if save:
            nn.save()
        nn.draw_results()
    else:
        nn.load()
        nn.preview()
        print(nn.evaluate(x, y)[0])

    nn.show_timing_log()
Example #3
0
 def __init__(self, im=None, om=None):
     self._im, self._om = im, om
     self._cursor = self._indices = None
     self._x, self._y = DataUtil.get_dataset("mnist", "../../_Data/mnist.txt", quantized=True, one_hot=True)
     self._x = self._x.reshape(-1, 28, 28)
     self._x_train, self._x_test = self._x[:1800], self._x[1800:]
     self._y_train, self._y_test = self._y[:1800], self._y[1800:]
Example #4
0
def main():
    x, y = DataUtil.get_dataset("mnist", "../../../_Data/mnist.txt", quantized=True, one_hot=True)
    x = x.reshape(len(x), 1, 28, 28)

    nn = NNDist()

    # nn.add("ReLU", (x.shape[1], 24))
    # nn.add("ReLU", (24, ))
    # nn.add("CrossEntropy", (y.shape[1], ))

    nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("MaxPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
    nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ReLU", (512,))
    nn.add("Identical", (64,))
    nn.add("Normalize", activation="ReLU")
    nn.add("Dropout")
    nn.add("CrossEntropy", (y.shape[1],))

    nn.optimizer = "Adam"
    nn.preview()
    nn.fit(x, y, verbose=2, do_log=True)
    nn.evaluate(x, y)
    nn.draw_results()
    nn.show_timing_log()
Example #5
0
def main():

    nn = NN()
    epoch = 50

    x, y = DataUtil.get_dataset("cifar10", "../_Data/cifar10.txt", quantized=True, one_hot=True)

    x = x.reshape(len(x), 3, 32, 32)
    nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("MaxPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((64, 3, 3),))
    nn.add("ConvReLU", ((64, 3, 3),))
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("ConvReLU", ((32, 3, 3),))
    nn.add("AvgPool", ((3, 3),), 2)
    nn.add("ReLU", (512,))
    nn.add("Identical", (64,), apply_bias=False)
    nn.add("Normalize", activation="ReLU")
    nn.add("Dropout")
    nn.add("CrossEntropy", (y.shape[1],))

    # nn.disable_timing()
    nn.fit(x, y, lr=0.001, epoch=epoch, train_rate=0.8,
           metrics=["acc"], record_period=1, verbose=4)
    nn.evaluate(x, y)
    nn.show_timing_log()
    nn.draw_logs()
Example #6
0
def main():
    nn = NNDist()
    verbose = 2

    lr = 0.001
    epoch = 50
    record_period = 5

    timing = Timing(enabled=True)
    timing_level = 1
    nn.feed_timing(timing)

    x, y = DataUtil.get_dataset("mnist",
                                "../../_Data/mnist.txt",
                                quantized=True,
                                one_hot=True)

    nn.add(ReLU((x.shape[1], 400)))
    nn.add(CrossEntropy((y.shape[1], )))

    nn.fit(x,
           y,
           lr=lr,
           epoch=epoch,
           record_period=record_period,
           verbose=verbose,
           train_rate=0.8)
    nn.draw_logs()

    timing.show_timing_log(timing_level)
Example #7
0
 def __init__(self, im=None, om=None, one_hot=True):
     super(MnistGenerator, self).__init__(im, om)
     self._x, self._y = DataUtil.get_dataset("mnist",
                                             "../_Data/mnist.txt",
                                             quantized=True,
                                             one_hot=one_hot)
     self._x = self._x.reshape(-1, 28, 28)
     self._x_train, self._x_test = self._x[:1800], self._x[1800:]
     self._y_train, self._y_test = self._y[:1800], self._y[1800:]
Example #8
0
 def __init__(self, im=None, om=None):
     self._im, self._om = im, om
     self._cursor = self._indices = None
     self._x, self._y = DataUtil.get_dataset("mnist",
                                             "../../_Data/mnist.txt",
                                             quantized=True,
                                             one_hot=True)
     self._x = self._x.reshape(-1, 28, 28)
     self._x_train, self._x_test = self._x[:1800], self._x[1800:]
     self._y_train, self._y_test = self._y[:1800], self._y[1800:]
Example #9
0
def main():
    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    visualize = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 10
    record_period = 1

    timing = Timing(enabled=True)
    timing_level = 1

    x, y = DataUtil.get_dataset("mnist", "../../_Data/mnist.txt", quantized=True, one_hot=True)
    x = x.reshape(len(x), 1, 28, 28)

    if not load:
        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 1)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3),))
        nn.add("ConvReLU", ((64, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 1)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 1)
        nn.add("ReLU", (512,))
        nn.add("Identical", (64,))
        nn.add("Normalize")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1],))
        nn.optimizer = "Adam"
        nn.preview()
        nn.feed_timing(timing)
        nn.fit(x, y, lr=lr, lb=lb,
               epoch=epoch, batch_size=32, record_period=record_period,
               show_loss=show_loss, train_only=train_only,
               do_log=True, verbose=verbose, visualize=visualize)
        if save:
            nn.save()
        nn.draw_results()
    else:
        nn.load()
        nn.preview()
        nn.evaluate(x, y)

    timing.show_timing_log(timing_level)
Example #10
0
def main():
    save = False
    load = False
    show_loss = True
    train_only = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 10
    record_period = 1

    x, y = DataUtil.get_dataset("mnist", "../../../_Data/mnist.txt", quantized=True, one_hot=True)
    x = x.reshape(len(x), 1, 28, 28)

    if not load:
        nn = NNDist()

        # nn.add("ReLU", (x.shape[1], 24))
        # nn.add("ReLU", (24, ))
        # nn.add("CrossEntropy", (y.shape[1], ))

        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ReLU", (512,))
        nn.add("Identical", (64,))
        nn.add("Normalize", activation="ReLU")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1],))
        nn.optimizer = "Adam"
        nn.preview(verbose=verbose)
        nn.fit(x, y, lr=lr, lb=lb,
               epoch=epoch, batch_size=256, record_period=record_period,
               show_loss=show_loss, train_only=train_only, do_log=True, tensorboard_verbose=1, verbose=verbose)
        if save:
            nn.save()
    else:
        nn = NNFrozen()
        nn.load()
        nn.preview()
        nn.evaluate(x, y)

    nn.show_timing_log()
Example #11
0
def main():

    # # x, y = DataUtil.gen_xor(100, one_hot=False)
    # x, y = DataUtil.gen_spin(20, 4, 2, 2, one_hot=False)
    # # x, y = DataUtil.gen_two_clusters(n_dim=2, one_hot=False)
    # y[y == 0] = -1
    #
    # svm = SKSVM()
    # # svm = SKSVM(kernel="poly", degree=12)
    # svm.fit(x, y)
    # svm.estimate(x, y)
    # svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm.support_)
    #
    # svm = SVM()
    # _logs = [_log[0] for _log in svm.fit(x, y, metrics=["acc"])]
    # svm.estimate(x, y)
    # svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    (x_train,
     y_train), (x_test,
                y_test), *_ = DataUtil.get_dataset("mushroom",
                                                   "../_Data/mushroom.txt",
                                                   train_num=100,
                                                   quantize=True,
                                                   tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    svm = SKSVM()
    svm.fit(x_train, y_train)
    svm.estimate(x_train, y_train)
    svm.estimate(x_test, y_test)

    svm = SVM()
    _logs = [
        _log[0] for _log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    # svm.fit(x_train, y_train, p=12)
    svm.estimate(x_train, y_train)
    svm.estimate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(_logs)), _logs)
    plt.show()

    svm.show_timing_log()
Example #12
0
def main():
    nn = NN()
    epoch = 10
    x, y = DataUtil.get_dataset("mnist",
                                "../Data/mnist.txt",
                                quantized=True,
                                one_hot=True)
    nn.add("ReLU", (x.shape[1], 24))
    nn.add("ReLU", (24, ))
    nn.add("CrossEntropy", (y.shape[1], ))

    nn.fit(x,
           y,
           lr=0.001,
           epoch=epoch,
           train_rate=0.8,
           metrics=["acc"],
           record_period=1,
           verbose=2)
Example #13
0
def main():

    nn = NNDist()
    verbose = 2

    lr = 0.001
    epoch = 50
    record_period = 5

    timing = Timing(enabled=True)
    timing_level = 1
    nn.feed_timing(timing)

    x, y = DataUtil.get_dataset("cifar10",
                                "../../_Data/cifar10.txt",
                                quantized=True,
                                one_hot=True)
    x = x.reshape(len(x), 3, 32, 32)

    nn.add(ConvReLU((x.shape[1:], (32, 3, 3))))
    nn.add(ConvReLU(((32, 3, 3), )))
    nn.add(MaxPool(((3, 3), ), 2))
    nn.add(ConvReLU(((64, 3, 3), )))
    nn.add(ConvReLU(((64, 3, 3), )))
    nn.add(AvgPool(((3, 3), ), 2))
    nn.add(ConvReLU(((32, 3, 3), )))
    nn.add(ConvReLU(((32, 3, 3), )))
    nn.add(AvgPool(((3, 3), ), 2))
    nn.add(ReLU((512, )))
    nn.add(ReLU((64, )))
    nn.add(CrossEntropy((y.shape[1], )))

    nn.fit(x,
           y,
           lr=lr,
           epoch=epoch,
           record_period=record_period,
           verbose=verbose,
           train_rate=0.8)
    nn.draw_logs()

    timing.show_timing_log(timing_level)
Example #14
0
def main():
    timing = Timing(enabled=True)
    timing_level = 1

    x, y = DataUtil.get_dataset("mnist",
                                "../../_Data/mnist.txt",
                                quantized=True,
                                one_hot=True)
    x = x.reshape(len(x), 1, 28, 28)

    nn = NNDist()

    # nn.add("ReLU", (x.shape[1], 24))
    # nn.add("ReLU", (24, ))
    # nn.add("CrossEntropy", (y.shape[1], ))

    nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
    nn.add("ConvReLU", ((32, 3, 3), ))
    nn.add("MaxPool", ((3, 3), ), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
    nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
    nn.add("AvgPool", ((3, 3), ), 2)
    nn.add("ConvNorm")
    nn.add("ConvDrop")
    nn.add("ConvReLU", ((32, 3, 3), ))
    nn.add("ConvReLU", ((32, 3, 3), ))
    nn.add("AvgPool", ((3, 3), ), 2)
    nn.add("ReLU", (512, ))
    nn.add("Identical", (64, ))
    nn.add("Normalize", activation="ReLU")
    nn.add("Dropout")
    nn.add("CrossEntropy", (y.shape[1], ))

    nn.optimizer = "Adam"
    nn.preview()
    nn.fit(x, y, verbose=2, do_log=True, show_loss=True)
    nn.draw_results()

    timing.show_timing_log(timing_level)
Example #15
0
def main():
    # _x, _y = DataUtil.get_dataset("balloon1.0(en)", "../_Data/balloon1.0(en).txt")
    _x, _y = DataUtil.get_dataset("test", "../_Data/test.txt")
    _fit_time = time.time()
    _tree = CartTree(whether_continuous=[False] * 4)
    _tree.fit(_x, _y, train_only=True)
    _fit_time = time.time() - _fit_time
    _tree.view()
    _estimate_time = time.time()
    _tree.estimate(_x, _y)
    _estimate_time = time.time() - _estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            _fit_time, _estimate_time,
            _fit_time + _estimate_time
        )
    )
    _tree.visualize()

    train_num = 6000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "../_Data/mushroom.txt", tar_idx=0, train_num=train_num)
    _fit_time = time.time()
    _tree = C45Tree()
    _tree.fit(x_train, y_train)
    _fit_time = time.time() - _fit_time
    _tree.view()
    _estimate_time = time.time()
    _tree.estimate(x_train, y_train)
    _tree.estimate(x_test, y_test)
    _estimate_time = time.time() - _estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            _fit_time, _estimate_time,
            _fit_time + _estimate_time
        )
    )
    _tree.visualize()

    _x, _y = DataUtil.gen_xor(one_hot=False)
    _fit_time = time.time()
    _tree = CartTree()
    _tree.fit(_x, _y, train_only=True)
    _fit_time = time.time() - _fit_time
    _tree.view()
    _estimate_time = time.time()
    _tree.estimate(_x, _y)
    _estimate_time = time.time() - _estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            _fit_time, _estimate_time,
            _fit_time + _estimate_time
        )
    )
    _tree.visualize2d(_x, _y)
    _tree.visualize()

    _wc = [False] * 16
    _continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for _cl in _continuous_lst:
        _wc[_cl] = True

    train_num = 2000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "bank1.0", "../_Data/bank1.0.txt", train_num=train_num, quantize=True)
    _fit_time = time.time()
    _tree = CartTree()
    _tree.fit(x_train, y_train)
    _fit_time = time.time() - _fit_time
    _tree.view()
    _estimate_time = time.time()
    _tree.estimate(x_test, y_test)
    _estimate_time = time.time() - _estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            _fit_time, _estimate_time,
            _fit_time + _estimate_time
        )
    )
    _tree.visualize()

    _tree.show_timing_log()
Example #16
0
def main():

    # x, y = DataUtil.gen_xor(100, one_hot=False)
    x, y = DataUtil.gen_spiral(20, 4, 2, 2, one_hot=False)
    # x, y = DataUtil.gen_two_clusters(n_dim=2, one_hot=False)
    y[y == 0] = -1

    animation_params = {
        "show": False, "mp4": False, "period": 50,
        "dense": 400, "draw_background": True
    }

    svm = SVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=600)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    svm = GDSVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=10000)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    if TorchSVM is not None:
        svm = TorchSVM(animation_params=animation_params)
        svm.fit(x, y, kernel="poly", p=12)
        svm.evaluate(x, y)
        svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    svm = TFSVM()
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400)

    svm = SKSVM()
    # svm = SKSVM(kernel="poly", degree=12)
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm.support_)

    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "../_Data/mushroom.txt", n_train=100, quantize=True, tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    svm = SKSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    svm = TFSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    if TorchSVM is not None:
        svm = TorchSVM()
        svm.fit(x_train, y_train)
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        svm = TorchSVM()
        logs = [log[0] for log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test
        )]
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        plt.figure()
        plt.title(svm.title)
        plt.plot(range(len(logs)), logs)
        plt.show()

    svm = SVM()
    logs = [log[0] for log in svm.fit(
        x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test
    )]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm = GDSVM()
    logs = [log[0] for log in svm.fit(
        x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test
    )]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm.show_timing_log()
Example #17
0
            if discrete:
                idx += 1
        return x

if __name__ == '__main__':
    import time

    whether_continuous = [False] * 16
    continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for cl in continuous_lst:
        whether_continuous[cl] = True

    train_num = 40000

    data_time = time.time()
    (x_train, y_train), (x_test, y_test) = DataUtil.get_dataset(
        "bank1.0", "../../_Data/bank1.0.txt", n_train=train_num)
    data_time = time.time() - data_time

    learning_time = time.time()
    nb = MergedNB(whether_continuous=whether_continuous)
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time

    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
    nb.evaluate(x_test, y_test)
    estimation_time = time.time() - estimation_time

    print(
        "Data cleaning   : {:12.6} s\n"
        "Model building  : {:12.6} s\n"
Example #18
0
def main():
    log = ""

    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    visualize = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 50
    record_period = 1

    timing = Timing(enabled=True)
    timing_level = 1

    x, y = DataUtil.get_dataset("cifar10",
                                "../../_Data/cifar10.txt",
                                quantized=True,
                                one_hot=True)
    x = x.reshape(len(x), 3, 32, 32)

    if not load:

        def add_layers(_nn):
            _nn.add("Pipe", 3)
            _nn.add_pipe_layer(0, "ConvReLU", ((32, 1, 3), ))
            _nn.add_pipe_layer(0, "ConvReLU", ((32, 3, 1), ))
            _nn.add_pipe_layer(1, "ConvReLU", ((32, 2, 3), ))
            _nn.add_pipe_layer(1, "ConvReLU", ((32, 3, 2), ))
            _nn.add_pipe_layer(2, "ConvReLU", ((32, 1, 1), ))
            _nn.add_pipe_layer(2, "Pipe", 2)
            _pipe = _nn.get_current_pipe(2)
            _pipe.add_pipe_layer(0, "ConvReLU", ((16, 1, 3), ))
            _pipe.add_pipe_layer(1, "ConvReLU", ((16, 3, 1), ))

        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("MaxPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        add_layers(nn)
        nn.add("MaxPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        add_layers(nn)
        nn.add("AvgPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        add_layers(nn)
        nn.add("ReLU", (512, ))
        nn.add("ReLU", (64, ))
        nn.add("Normalize")
        nn.add("CrossEntropy", (y.shape[1], ))

        nn.optimizer = "Adam"

        nn.preview()
        nn.feed_timing(timing)

        nn.fit(x,
               y,
               lr=lr,
               lb=lb,
               epoch=epoch,
               batch_size=256,
               record_period=record_period,
               show_loss=show_loss,
               train_only=train_only,
               do_log=True,
               verbose=verbose,
               visualize=visualize)
        if save:
            nn.save()
        nn.draw_conv_series(x[:3], (3, 32, 32))
        nn.draw_results()

    else:

        nn.load("Models/Model")
        nn.feed(x, y)
        nn.preview()
        nn.fit(epoch=5, lr=lr, lb=lb, verbose=verbose)
        if visualize:
            nn.visualize2d()
        nn.draw_results()

        acc = nn.evaluate(x, y)[0]
        log += "Whole set Accuracy  : {:12.6} %".format(100 * acc) + "\n"

        print()
        print("=" * 30 + "\n" + "Results\n" + "-" * 30)
        print(log)

    timing.show_timing_log(timing_level)
Example #19
0
    print("=" * 30)
    print("Testing {} ({})...".format(algorithm, clf))
    print('_' * 30)
    t = time.time()
    ensemble = clf_dict[algorithm]()
    ensemble.fit(x, y, None, clf, ensemble, **kwargs)
    ensemble.evaluate(xt, yt)
    print("Time cost:{:8.6} s".format(time.time() - t))

if __name__ == '__main':
    _x, _y = DataUtil.gen_spiral(size=20, n = 4, n_class=2, one_hot=False)
    _y[_y == 0] = -1

    test(_x, _y, algorithm="RF", epoch=30, n_cores = 4)
    test(_x, _y, algorithm="SKAdaBoost")

    train_num = 6000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "/Users/lily/Documents/MachineLearning-master/_Data/mushroom.txt", train_num=train_num, quantize=True, tar_idx=0
    )


    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    cv_test(x_train, y_train, x_test, y_test, clf = "MNB", epoch=1)
    cv_test(x_train, y_train, x_test, y_test, clf = "MNB", epoch=5)
    cv_test(x_train, y_train, x_test, y_test, clf = "MNB", epoch=10)
    cv_test(x_train, y_train, x_test, y_test, clf = "MNB", epoch=15)

    AdaBoost.show_timing_log()
Example #20
0
                plt.bar(tmp_x - 0.35 * c, self._data[j][c, :], width=0.35,
                        facecolor=colors[self.label_dict[c]], edgecolor="white",
                        label=u"class: {}".format(self.label_dict[c]))
            plt.xticks([i for i in range(sj + 2)], [""] + [rev_dict[i] for i in range(sj)] + [""])
            plt.ylim(0, 1.0)
            plt.legend()
            if not save:
                plt.show()
            else:
                plt.savefig("d{}".format(j + 1))

if __name__ == '__main__':
    import time

    train_num = 6000
    (x_train, y_train), (x_test, y_test) = DataUtil.get_dataset(
        "mushroom", "../../_Data/mushroom.txt", n_train=train_num, tar_idx=0)

    learning_time = time.time()
    nb = MultinomialNB()
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time
    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
    nb.evaluate(x_test, y_test)
    estimation_time = time.time() - estimation_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            learning_time, estimation_time,
            learning_time + estimation_time
Example #21
0
 def __init__(self, im=None, om=None, one_hot=True):
     super(MnistGenerator, self).__init__(im, om)
     self._x, self._y = DataUtil.get_dataset("mnist", "../_Data/mnist.txt", quantized=True, one_hot=one_hot)
     self._x = self._x.reshape(-1, 28, 28)
     self._x_train, self._x_test = self._x[:1800], self._x[1800:]
     self._y_train, self._y_test = self._y[:1800], self._y[1800:]
Example #22
0
def main(visualize=True):
    # x, y = DataUtil.get_dataset("balloon1.0(en)", "../_Data/balloon1.0(en).txt")
    x, y = DataUtil.get_dataset(
        "test", "/Users/lily/Documents/MachineLearning-master/_Data/test.txt")
    fit_time = time.time()
    tree = CartTree(whether_continuous=[False] * 4)
    tree.fit(x, y, train_only=True)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x, y)
    estimate_time = time.time() - estimate_time
    print("Model building  : {:12.6} s\n"
          "Estimation      : {:12.6} s\n"
          "Total           : {:12.6} s".format(fit_time, estimate_time,
                                               fit_time + estimate_time))
    if visualize:
        tree.visualize()

    train_num = 6000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom",
        "/Users/lily/Documents/MachineLearning-master/_Data/mushroom.txt",
        tar_idx=0,
        train_num=train_num)
    fit_time = time.time()
    tree = C45Tree()
    tree.fit(x_train, y_train)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x_train, y_train)
    tree.evaluate(x_test, y_test)
    estimate_time = time.time() - estimate_time
    print("Model building  : {:12.6} s\n"
          "Estimation      : {:12.6} s\n"
          "Total           : {:12.6} s".format(fit_time, estimate_time,
                                               fit_time + estimate_time))
    if visualize:
        tree.visualize()

    x, y = DataUtil.gen_xor(one_hot=False)
    fit_time = time.time()
    tree = CartTree()
    tree.fit(x, y, train_only=True)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x, y, n_cores=1)
    estimate_time = time.time() - estimate_time
    print("Model building  : {:12.6} s\n"
          "Estimation      : {:12.6} s\n"
          "Total           : {:12.6} s".format(fit_time, estimate_time,
                                               fit_time + estimate_time))
    if visualize:
        tree.visualize2d(x, y, dense=1000)
        tree.visualize()

    wc = [False] * 16
    continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for _cl in continuous_lst:
        wc[_cl] = True

    train_num = 2000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "bank1.0",
        "/Users/lily/Documents/MachineLearning-master/_Data/bank1.0.txt",
        train_num=train_num,
        quantize=True)
    fit_time = time.time()
    tree = CartTree()
    tree.fit(x_train, y_train)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x_test, y_test)
    estimate_time = time.time() - estimate_time
    print("Model building  : {:12.6} s\n"
          "Estimation      : {:12.6} s\n"
          "Total           : {:12.6} s".format(fit_time, estimate_time,
                                               fit_time + estimate_time))
    if visualize:
        tree.visualize()

    tree.show_timing_log()
Example #23
0
                w3[leaf_id_cursor] = rs / np.sum(rs)
                leaf_id_cursor += 1

        w2 *= max_route_length
        self._transform_ws = [w1, w2, w3]
        self._transform_bs = [b]


if __name__ == '__main__':
    from Util.Util import DataUtil

    centers = (1, 1)
    slopes = (0.5, -2)
    x, y = DataUtil.gen_x_set(1000, centers, slopes, one_hot=False)
    x_test, y_test = DataUtil.gen_x_set(100, centers, slopes, one_hot=False)

    (x, y), (x_test,
             y_test), *_ = DataUtil.get_dataset("cifar10",
                                                "../../../_Data/cifar10.txt",
                                                400,
                                                quantized=True)

    DT2NN().fit(x, y, x_test, y_test).scatter2d(x,
                                                y,
                                                padding=0.1,
                                                title="X set").visualize2d(
                                                    x,
                                                    y).visualize2d(x_test,
                                                                   y_test,
                                                                   padding=1)
    def _transfer_x(self, x):
        # 遍历元素
        for j, char in enumerate(x):
            x[j] = self._feat_dics[j][char]
        return x

if __name__=='__main__':
    # 导入 标准库,读取数据
    import time
    from Util.Util import DataUtil
    # 遍历数据集
    #dataset = "mushroom"
    for dataset in ("balloon1.0", "balloon1.5"):
        # 读入数据TypeError: 'NoneType' object is not subscriptable
        _x, _y = DataUtil.get_dataset(dataset, "../../data/{}.txt".format(dataset))
        # 实例化模型,训练,记录时间
        learning_time = time.time()
        nb = MultinomialNB()
        nb.fit(_x, _y)
        learning_time = time.time() - learning_time
        # 评估模型表现,记录话费时间
        estimation_time = time.time()
        nb.evaluate(_x, _y)
        estimation_time = time.time() - estimation_time
        # 打印
        print(
            "Model Building : {:12.6} s\n"
            "Estimation     : {:12.6} s\n"
            "Total          : {:12.6} s\n".format(learning_time, estimation_time, learning_time+estimation_time)
        )
Example #25
0
def main(visualize=True):
    # x, y = DataUtil.get_dataset("balloon1.0(en)", "../_Data/balloon1.0(en).txt")
    x, y = DataUtil.get_dataset("test", "../_Data/test.txt")
    fit_time = time.time()
    tree = CartTree(whether_continuous=[False] * 4)
    tree.fit(x, y, train_only=True)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x, y)
    estimate_time = time.time() - estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            fit_time, estimate_time,
            fit_time + estimate_time
        )
    )
    if visualize:
        tree.visualize()

    train_num = 6000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "../_Data/mushroom.txt", tar_idx=0, n_train=train_num)
    fit_time = time.time()
    tree = C45Tree()
    tree.fit(x_train, y_train)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x_train, y_train)
    tree.evaluate(x_test, y_test)
    estimate_time = time.time() - estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            fit_time, estimate_time,
            fit_time + estimate_time
        )
    )
    if visualize:
        tree.visualize()

    x, y = DataUtil.gen_xor(one_hot=False)
    fit_time = time.time()
    tree = CartTree()
    tree.fit(x, y, train_only=True)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x, y, n_cores=1)
    estimate_time = time.time() - estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            fit_time, estimate_time,
            fit_time + estimate_time
        )
    )
    if visualize:
        tree.visualize2d(x, y, dense=1000)
        tree.visualize()

    wc = [False] * 16
    continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for _cl in continuous_lst:
        wc[_cl] = True

    train_num = 2000
    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "bank1.0", "../_Data/bank1.0.txt", n_train=train_num, quantize=True)
    fit_time = time.time()
    tree = CartTree()
    tree.fit(x_train, y_train)
    fit_time = time.time() - fit_time
    if visualize:
        tree.view()
    estimate_time = time.time()
    tree.evaluate(x_test, y_test)
    estimate_time = time.time() - estimate_time
    print(
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            fit_time, estimate_time,
            fit_time + estimate_time
        )
    )
    if visualize:
        tree.visualize()

    tree.show_timing_log()
def main():

    # x, y = DataUtil.gen_xor(100, one_hot=False)
    x, y = DataUtil.gen_spiral(20, 4, 2, 2, one_hot=False)
    # x, y = DataUtil.gen_two_clusters(n_dim=2, one_hot=False)
    y[y == 0] = -1

    animation_params = {
        "show": False,
        "mp4": False,
        "period": 50,
        "dense": 400,
        "draw_background": True
    }

    svm = SVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=600)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    svm = GDSVM(animation_params=animation_params)
    svm.fit(x, y, kernel="poly", p=12, epoch=10000)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm["alpha"] > 0)

    if TorchSVM is not None:
        svm = TorchSVM(animation_params=animation_params)
        svm.fit(x, y, kernel="poly", p=12)
        svm.evaluate(x, y)
        svm.visualize2d(x,
                        y,
                        padding=0.1,
                        dense=400,
                        emphasize=svm["alpha"] > 0)

    svm = TFSVM()
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400)

    svm = SKSVM()
    # svm = SKSVM(kernel="poly", degree=12)
    svm.fit(x, y)
    svm.evaluate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400, emphasize=svm.support_)

    (x_train,
     y_train), (x_test,
                y_test), *_ = DataUtil.get_dataset("mushroom",
                                                   "../_Data/mushroom.txt",
                                                   n_train=100,
                                                   quantize=True,
                                                   tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    svm = SKSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    svm = TFSVM()
    svm.fit(x_train, y_train)
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    if TorchSVM is not None:
        svm = TorchSVM()
        svm.fit(x_train, y_train)
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        svm = TorchSVM()
        logs = [
            log[0] for log in svm.fit(x_train,
                                      y_train,
                                      metrics=["acc"],
                                      x_test=x_test,
                                      y_test=y_test)
        ]
        svm.evaluate(x_train, y_train)
        svm.evaluate(x_test, y_test)

        plt.figure()
        plt.title(svm.title)
        plt.plot(range(len(logs)), logs)
        plt.show()

    svm = SVM()
    logs = [
        log[0] for log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm = GDSVM()
    logs = [
        log[0] for log in svm.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    svm.evaluate(x_train, y_train)
    svm.evaluate(x_test, y_test)

    plt.figure()
    plt.title(svm.title)
    plt.plot(range(len(logs)), logs)
    plt.show()

    svm.show_timing_log()
Example #27
0
                         label="class: {}".format(self.label_dict[c]))
                plt.xlim(x_min - 0.2 * gap, x_max + 0.2 * gap)
                plt.legend()
                if not save:
                    plt.show()
                else:
                    plt.savefig("d{}".format(j + 1))


if __name__ == "__main__":
    import time

    train_num = 6000
    (x_train, y_train), (x_test,
                         y_test) = DataUtil.get_dataset("mushroom",
                                                        "../Data/mushroom.txt",
                                                        n_train=train_num,
                                                        tar_idx=0)

    learning_time = time.time()
    nb = GaussianNB()
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time
    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
    nb.evaluate(x_test, y_test)
    estimation_time = time.time() - estimation_time
    print("Model build  :  {:12.6} s\n"
          "Estimation   :  {:12.6} s\n"
          "Total        :  {:12.6} s\n".format(
              learning_time, estimation_time, learning_time + estimation_time))
Example #28
0
                        label=u"class: {}".format(self.label_dic[c]))
            plt.xticks([i for i in range(sj + 2)],
                       [""] + [_rev_dic[i] for i in range(sj)] + [""])
            plt.ylim(0, 1.0)
            plt.legend()
            if not save:
                plt.show()
            else:
                plt.savefig("d{}".format(j + 1))


if __name__ == '__main__':
    import time

    for dataset in ("balloon1.0", "balloon1.5"):
        _x, _y = DataUtil.get_dataset(dataset,
                                      "../../_Data/{}.txt".format(dataset))
        learning_time = time.time()
        nb = MultinomialNB()
        nb.fit(_x, _y)
        learning_time = time.time() - learning_time
        print("=" * 30)
        print(dataset)
        print("-" * 30)
        estimation_time = time.time()
        nb.evaluate(_x, _y)
        estimation_time = time.time() - estimation_time
        print("Model building  : {:12.6} s\n"
              "Estimation      : {:12.6} s\n"
              "Total           : {:12.6} s".format(
                  learning_time, estimation_time,
                  learning_time + estimation_time))
Example #29
0
    #     "Model building  : {:12.6} s\n"
    #     "Estimation      : {:12.6} s\n"
    #     "Total           : {:12.6} s".format(
    #         learning_time, estimation_time,
    #         learning_time + estimation_time
    #     )
    # )

    whether_continuous = [False] * 16
    continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for cl in continuous_lst:
        whether_continuous[cl] = True

    train_num = 40000
    data_time = time.time()
    (x_train, y_train), (x_test, y_test) = DataUtil.get_dataset(
        "bank1.0", "../../_Data/bank1.0.txt", train_num=train_num)
    data_time = time.time() - data_time
    learning_time = time.time()
    nb = MergedNB(whether_continuous=whether_continuous)
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time
    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
    nb.evaluate(x_test, y_test)
    estimation_time = time.time() - estimation_time
    print(
        "Data cleaning   : {:12.6} s\n"
        "Model building  : {:12.6} s\n"
        "Estimation      : {:12.6} s\n"
        "Total           : {:12.6} s".format(
            data_time, learning_time, estimation_time,
Example #30
0
def main():
    log = ""

    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    visualize = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 10
    record_period = 1

    timing = Timing(enabled=True)
    timing_level = 1

    x, y = DataUtil.get_dataset("mnist",
                                "../../_Data/mnist.txt",
                                quantized=True,
                                one_hot=True)

    if not load:

        # nn.add("ReLU", (x.shape[1], 24))
        # nn.add("ReLU", (24, ))
        # nn.add("CrossEntropy", (y.shape[1], ))

        x = x.reshape(len(x), 1, 28, 28)
        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("MaxPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
        nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
        nn.add("AvgPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("AvgPool", ((3, 3), ), 2)
        nn.add("ReLU", (512, ))
        nn.add("Identical", (64, ))
        nn.add("Normalize", activation="ReLU")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1], ))

        nn.optimizer = "Adam"

        nn.preview()
        nn.feed_timing(timing)

        nn.fit(x,
               y,
               lr=lr,
               lb=lb,
               epoch=epoch,
               batch_size=256,
               record_period=record_period,
               show_loss=show_loss,
               train_only=train_only,
               do_log=True,
               verbose=verbose,
               visualize=visualize)
        if save:
            nn.save()
        nn.draw_results()

    else:

        nn.load("Models/Model")
        nn.feed(x, y)
        nn.preview()
        nn.fit(epoch=5, lr=lr, lb=lb, verbose=verbose)
        if visualize:
            nn.visualize2d()
        nn.draw_results()

        acc = nn.estimate(x, y)[0]
        log += "Whole set Accuracy  : {:12.6} %".format(100 * acc) + "\n"

        print()
        print("=" * 30 + "\n" + "Results\n" + "-" * 30)
        print(log)

    timing.show_timing_log(timing_level)
Example #31
0
def main():
    log = ""

    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    do_log = True
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 5
    record_period = 1
    weight_scale = 0.001
    optimizer = "Adam"
    nn.optimizer = optimizer

    timing = Timing(enabled=True)
    timing_level = 1

    x, y = DataUtil.get_dataset("cifar10", "../../_Data/cifar10.txt", quantized=True, one_hot=True)

    draw = False
    img_shape = (3, 32, 32)
    x = x.reshape(len(x), *img_shape)

    if not load:
        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ReLU", (512, ))
        nn.add("Identical", (64, ), apply_bias=False)
        nn.add("Normalize", activation="ReLU")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1], ))

        nn.preview()
        nn.feed_timing(timing)

        nn.fit(x, y,
               lr=lr, lb=0, epoch=epoch, weight_scale=weight_scale,
               record_period=record_period, show_loss=show_loss, train_only=train_only,
               do_log=do_log, verbose=verbose)
        # nn.draw_results()

        if save:
            nn.save()
        if draw:
            # nn.draw_conv_weights()
            nn.draw_conv_series(x[:3], img_shape)

    else:

        nn.load()
        nn.feed(x, y)
        print("Optimizer: " + nn.optimizer)
        nn.preview()
        nn.fit(epoch=1, lr=lr, lb=lb, verbose=verbose)
        # nn.fit(x, y, x_cv, y_cv, x_test, y_test, epoch=1, lr=lr, lb=lb, verbose=verbose)
        if draw:
            # nn.draw_conv_weights()
            nn.draw_conv_series(x[:3], img_shape)
        nn.draw_results()

        acc = nn.evaluate(x, y)[0]
        log += "Test set Accuracy  : {:12.6} %".format(100 * acc) + "\n"
        print("=" * 30 + "\n" + "Results\n" + "-" * 30)
        print(log)

    timing.show_timing_log(timing_level)
Example #32
0
def main():
    save = False
    load = False
    show_loss = True
    train_only = False
    verbose = 2

    lr = 0.001
    lb = 0.001
    epoch = 10
    record_period = 1

    x, y = DataUtil.get_dataset("mnist",
                                "../../_Data/mnist.txt",
                                quantized=True,
                                one_hot=True)
    x = x.reshape(len(x), 1, 28, 28)

    if not load:
        nn = NNDist()

        # nn.add("ReLU", (x.shape[1], 24))
        # nn.add("ReLU", (24, ))
        # nn.add("CrossEntropy", (y.shape[1], ))

        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("MaxPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
        nn.add("ConvReLU", ((64, 3, 3), ), std=0.01)
        nn.add("AvgPool", ((3, 3), ), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("ConvReLU", ((32, 3, 3), ))
        nn.add("AvgPool", ((3, 3), ), 2)
        nn.add("ReLU", (512, ))
        nn.add("Identical", (64, ))
        nn.add("Normalize", activation="ReLU")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1], ))
        nn.optimizer = "Adam"
        nn.preview(verbose=verbose)
        nn.fit(x,
               y,
               lr=lr,
               lb=lb,
               epoch=epoch,
               batch_size=256,
               record_period=record_period,
               show_loss=show_loss,
               train_only=train_only,
               do_log=True,
               tensorboard_verbose=1,
               verbose=verbose)
        if save:
            nn.save()
    else:
        nn = NNFrozen()
        nn.load()
        nn.preview()
        nn.evaluate(x, y)

    nn.show_timing_log()
Example #33
0
                plt.bar(tmp_x - 0.35 * c, self._data[j][c, :], width=0.35,
                        facecolor=colors[self.label_dict[c]], edgecolor="white",
                        label=u"class: {}".format(self.label_dict[c]))
            plt.xticks([i for i in range(sj + 2)], [""] + [rev_dict[i] for i in range(sj)] + [""])
            plt.ylim(0, 1.0)
            plt.legend()
            if not save:
                plt.show()
            else:
                plt.savefig("d{}".format(j + 1))

if __name__ == '__main__':
    import time

    for dataset in ("balloon1.0", "balloon1.5"):
        _x, _y = DataUtil.get_dataset(dataset, "../../_Data/{}.txt".format(dataset))
        learning_time = time.time()
        nb = MultinomialNB()
        nb.fit(_x, _y)
        learning_time = time.time() - learning_time
        print("=" * 30)
        print(dataset)
        print("-" * 30)
        estimation_time = time.time()
        nb.evaluate(_x, _y)
        estimation_time = time.time() - estimation_time
        print(
            "Model building  : {:12.6} s\n"
            "Estimation      : {:12.6} s\n"
            "Total           : {:12.6} s".format(
                learning_time, estimation_time,
Example #34
0
    animation_params = {
        "show": False, "mp4": False, "period": 50,
        "dense": 400, "draw_background": True
    }

    kp = KP(animation_params=animation_params)
    kp.fit(xs, ys, kernel="poly", p=12, epoch=200)
    kp.evaluate(xs, ys)
    kp.visualize2d(xs, ys, dense=400)

    kp = GDKP(animation_params=animation_params)
    kp.fit(xs, ys, kernel="poly", p=12, epoch=10000)
    kp.evaluate(xs, ys)
    kp.visualize2d(xs, ys, dense=400)

    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "../_Data/mushroom.txt", n_train=100, quantize=True, tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    kp = KP()
    logs = [log[0] for log in kp.fit(
        x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test
    )]
    kp.evaluate(x_train, y_train)
    kp.evaluate(x_test, y_test)

    plt.figure()
    plt.title(kp.title)
    plt.plot(range(len(logs)), logs)
    plt.show()
Example #35
0
    # # xs, ys = DataUtil.gen_xor(one_hot=False)
    # ys[ys == 0] = -1
    # perceptron = KernelPerceptron()
    # _logs = [_log[0] for _log in perceptron.fit(
    #     xs, ys, metrics=["acc"], epoch=10 ** 5
    # )]
    # # perceptron.fit(xs, ys, kernel="rbf", epoch=10 ** 6)
    # # perceptron.fit(xs, ys, p=12, epoch=10 ** 5)
    # perceptron.evaluate(xs, ys)
    # perceptron.visualize2d(xs, ys, dense=400)

    (x_train,
     y_train), (x_test,
                y_test), *_ = DataUtil.get_dataset("mushroom",
                                                   "../_Data/mushroom.txt",
                                                   train_num=100,
                                                   quantize=True,
                                                   tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    perceptron = KernelPerceptron()
    _logs = [
        _log[0] for _log in perceptron.fit(
            x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test)
    ]
    perceptron.evaluate(x_train, y_train)
    perceptron.evaluate(x_test, y_test)

    plt.figure()
    plt.title(perceptron.title)
Example #36
0
    animation_params = {
        "show": False, "mp4": False, "period": 50,
        "dense": 400, "draw_background": True
    }

    kp = KP(animation_params=animation_params)
    kp.fit(xs, ys, kernel="poly", p=12, epoch=200)
    kp.evaluate(xs, ys)
    kp.visualize2d(xs, ys, dense=400)

    kp = GDKP(animation_params=animation_params)
    kp.fit(xs, ys, kernel="poly", p=12, epoch=10000)
    kp.evaluate(xs, ys)
    kp.visualize2d(xs, ys, dense=400)

    (x_train, y_train), (x_test, y_test), *_ = DataUtil.get_dataset(
        "mushroom", "../_Data/mushroom.txt", n_train=100, quantize=True, tar_idx=0)
    y_train[y_train == 0] = -1
    y_test[y_test == 0] = -1

    kp = KP()
    logs = [log[0] for log in kp.fit(
        x_train, y_train, metrics=["acc"], x_test=x_test, y_test=y_test
    )]
    kp.evaluate(x_train, y_train)
    kp.evaluate(x_test, y_test)

    plt.figure()
    plt.title(kp.title)
    plt.plot(range(len(logs)), logs)
    plt.show()
Example #37
0
            plt.figure()
            plt.title(title)
            for c in range(len(self.label_dic)):
                plt.plot(tmp_x, [self._data[j][c](xx) for xx in tmp_x],
                         c=colors[self.label_dic[c]], label="class: {}".format(self.label_dic[c]))
            plt.xlim(x_min-0.2*gap, x_max+0.2*gap)
            plt.legend()
            if not save:
                plt.show()
            else:
                plt.savefig("d{}".format(j + 1))

if __name__ == '__main__':
    import time

    xs, ys = DataUtil.get_dataset("mushroom", "../../_Data/mushroom.txt", tar_idx=0)
    nb = MultinomialNB()
    nb.feed_data(xs, ys)
    xs, ys = nb["x"].tolist(), nb["y"].tolist()

    train_num = 6000
    x_train, x_test = xs[:train_num], xs[train_num:]
    y_train, y_test = ys[:train_num], ys[train_num:]

    learning_time = time.time()
    nb = GaussianNB()
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time

    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
Example #38
0
                idx += 1
        return x


if __name__ == '__main__':
    import time

    whether_continuous = [False] * 16
    continuous_lst = [0, 5, 9, 11, 12, 13, 14]
    for cl in continuous_lst:  #查看数据集,设置对应的特征是否连续还是离散
        whether_continuous[cl] = True

    train_num = 40000

    data_time = time.time()
    (x_train, y_train), (x_test,
                         y_test) = DataUtil.get_dataset("bank1.0",
                                                        "../Data/bank1.0.txt",
                                                        n_train=train_num)
    data_time = time.time() - data_time

    learning_time = time.time()
    nb = MergedNB(whether_continuous=whether_continuous)
    nb.fit(x_train, y_train)
    learning_time = time.time() - learning_time

    estimation_time = time.time()
    nb.evaluate(x_train, y_train)
    nb.evaluate(x_test, y_test)
    estimation_time = time.time() - estimation_time
    nb.show_timing_log()
Example #39
0
    def predict(self, x, bound=None):
        if bound is None:
            _matrix = np.array([_tree.predict(x) for _tree in self._trees]).T
        else:
            _matrix = np.array(
                [_tree.predict(x) for _tree in self._trees[:bound]]).T
        return np.array([RandomForest.most_appearance(rs) for rs in _matrix])


if __name__ == '__main__':
    import time

    train_num = 100
    (x_train,
     y_train), (x_test, y_test) = DataUtil.get_dataset("mushroom",
                                                       "../_Data/mushroom.txt",
                                                       train_num=train_num,
                                                       tar_idx=0)

    learning_time = time.time()
    forest = RandomForest()
    forest.fit(x_train, y_train)
    learning_time = time.time() - learning_time
    estimation_time = time.time()
    forest.estimate(x_train, y_train)
    forest.estimate(x_test, y_test)
    estimation_time = time.time() - estimation_time
    print("Model building  : {:12.6} s\n"
          "Estimation      : {:12.6} s\n"
          "Total           : {:12.6} s".format(
              learning_time, estimation_time, learning_time + estimation_time))
    forest.show_timing_log()
Example #40
0
def main():
    log = ""

    nn = NNDist()
    save = False
    load = False
    show_loss = True
    train_only = False
    do_log = True
    verbose = 4

    lr = 0.001
    lb = 0.001
    epoch = 10
    record_period = 1
    weight_scale = 0.001
    optimizer = "Adam"
    nn.optimizer = optimizer

    x, y = DataUtil.get_dataset("cifar10", "../../../_Data/cifar10.txt", quantized=True, one_hot=True)

    draw = True
    img_shape = (3, 32, 32)
    x = x.reshape(len(x), *img_shape)

    if not load:
        nn.add("ConvReLU", (x.shape[1:], (32, 3, 3)))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("MaxPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("ConvReLU", ((64, 3, 3),), std=0.01)
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ConvNorm")
        nn.add("ConvDrop")
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("ConvReLU", ((32, 3, 3),))
        nn.add("AvgPool", ((3, 3),), 2)
        nn.add("ReLU", (512, ))
        nn.add("Identical", (64, ), apply_bias=False)
        nn.add("Normalize", activation="ReLU")
        nn.add("Dropout")
        nn.add("CrossEntropy", (y.shape[1], ))

        nn.preview()
        nn.fit(x, y,
               lr=lr, lb=0, epoch=epoch, weight_scale=weight_scale,
               record_period=record_period, show_loss=show_loss, train_only=train_only,
               do_log=do_log, verbose=verbose)
        nn.draw_results()

        if save:
            nn.save()
        if draw:
            # nn.draw_conv_weights()
            nn.draw_conv_series(x[:3], img_shape)
    else:
        nn.load()
        print("Optimizer: " + nn.optimizer)
        nn.preview()
        nn.fit(x, y, epoch=1, lr=lr, lb=lb, verbose=verbose)
        # nn.fit(x, y, x_cv, y_cv, x_test, y_test, epoch=1, lr=lr, lb=lb, verbose=verbose)
        if draw:
            # nn.draw_conv_weights()
            nn.draw_conv_series(x[:3], img_shape)
        nn.draw_results()

        acc = nn.evaluate(x, y)[0]
        log += "Test set Accuracy  : {:12.6} %".format(100 * acc) + "\n"
        print("=" * 30 + "\n" + "Results\n" + "-" * 30)
        print(log)

    nn.show_timing_log()