Example #1
0
def main():

    x, y = DataUtil.gen_two_clusters(n_dim=2, dis=2.5, center=5, one_hot=False)
    y[y == 0] = -1

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

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

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

    if TorchLinearSVM is not None:
        svm = TorchLinearSVM(animation_params=animation_params)
        svm.fit(x, y)
        svm.evaluate(x, y)
        svm.visualize2d(x, y, padding=0.1, dense=400)

    perceptron = Perceptron()
    perceptron.fit(x, y)
    perceptron.evaluate(x, y)
    perceptron.visualize2d(x, y, padding=0.1, dense=400)

    perceptron.show_timing_log()
Example #2
0
def main():

    x, y = DataUtil.gen_two_clusters(n_dim=2, dis=2.5, center=5, one_hot=False)
    y[y == 0] = -1

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

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

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

    if TorchLinearSVM is not None:
        svm = TorchLinearSVM(animation_params=animation_params)
        svm.fit(x, y)
        svm.evaluate(x, y)
        svm.visualize2d(x, y, padding=0.1, dense=400)

    perceptron = Perceptron()
    perceptron.fit(x, y)
    perceptron.evaluate(x, y)
    perceptron.visualize2d(x, y, padding=0.1, dense=400)

    perceptron.show_timing_log()
Example #3
0
def main():

    x, y = DataUtil.gen_two_clusters(n_dim=2, dis=2.5, center=5, one_hot=False)
    y[y == 0] = -1

    svm = LinearSVM()
    svm.fit(x, y, epoch=10 ** 5, lr=1e-3)
    svm.estimate(x, y)
    svm.visualize2d(x, y, padding=0.1, dense=400)

    perceptron = Perceptron()
    perceptron.fit(x, y)
    perceptron.estimate(x, y)
    perceptron.visualize2d(x, y)

    perceptron.show_timing_log()
Example #4
0
        self._opt = opt

    def fit(self, x, y):
        self._func_cache = self._func(x.shape[1], x, y)
        line_search = None if self._line_search is None else self._line_search(self._func_cache)
        self._opt_cache = self._opt(self._func_cache, line_search)
        self._opt_cache.opt()
        self._beta = self._func_cache._beta

    def predict(self, x, get_raw_results=False, **kwargs):
        pi = 1 / (1 + np.exp(-np.atleast_2d(x).dot(self._beta)))
        if get_raw_results:
            return pi
        return (pi >= 0.5).astype(np.double)

data, labels = DataUtil.gen_two_clusters(one_hot=False)
lr = LR(LM)
lr.fit(data, labels)
lr.evaluate(data, labels)
lr["func_cache"].refresh_cache(lr["beta"])
print("Loss:", lr["func_cache"].loss(lr["beta"]))
lr.visualize2d(data, labels, dense=400)
# Draw Training Curve
plt.figure()
plt.plot(np.arange(len(lr["opt_cache"].log))+1, lr["opt_cache"].log)
plt.show()


# Example3: RBFN Regression with BFGS & Armijo using "Automatic Differentiation"
# "RBFN" represents "Radial Basis Function Network". Typically, we use Gaussian Function for this example
class RBFN(Function):
Example #5
0
        self._opt = opt

    def fit(self, x, y):
        self._func_cache = self._func(x.shape[1], x, y)
        line_search = None if self._line_search is None else self._line_search(self._func_cache)
        self._opt_cache = self._opt(self._func_cache, line_search)
        self._opt_cache.opt()
        self._beta = self._func_cache._beta

    def predict(self, x, get_raw_results=False):
        pi = 1 / (1 + np.exp(-np.atleast_2d(x).dot(self._beta)))
        if get_raw_results:
            return pi
        return (pi >= 0.5).astype(np.double)

data, labels = DataUtil.gen_two_clusters(one_hot=False)
lr = LR(LM)
lr.fit(data, labels)
lr.evaluate(data, labels)
lr["func_cache"].refresh_cache(lr["beta"])
print("Loss:", lr["func_cache"].loss(lr["beta"]))
lr.visualize2d(data, labels, dense=400)
# Draw Training Curve
plt.figure()
plt.plot(np.arange(len(lr["opt_cache"].log))+1, lr["opt_cache"].log)
plt.show()


# Example3: RBFN Regression with BFGS & Armijo using "Automatic Differentiation"
# "RBFN" represents "Radial Basis Function Network". Typically, we use Gaussian Function for this example
class RBFN(Function):
Example #6
0
            for j, indices in enumerate(labels == arange):
                self._centers[j] = np.average(x[indices], axis=0)
            counter += 1
            animation_params["extra"] = self._centers
            self._handle_animation(i, x, labels, ims, animation_params, *animation_properties)
            bar.update()
        self._counter = counter
        self._handle_mp4(ims, animation_properties)

    def predict(self, x, get_raw_results=False, high_dim=False):
        if not high_dim:
            x = x[:, None, ...]
        dis = np.abs(x - self._centers) if self._params["norm"] == "l1" else (x - self._centers) ** 2
        return np.argmin(np.sum(dis, axis=2), axis=1)

if __name__ == '__main__':
    _x, _y = DataUtil.gen_random(size=2000, scale=6)
    k_means = KMeans(n_clusters=8, animation_params={
        "show": False, "mp4": True, "period": 1, "draw_background": True
    })
    k_means.fit(_x)
    k_means.visualize2d(_x, _y, dense=400, extra=k_means["centers"])
    _x, _y = DataUtil.gen_two_clusters()
    k_means = KMeans()
    k_means.fit(_x)
    k_means.visualize2d(_x, _y, dense=400, extra=k_means["centers"])
    _x, _y = DataUtil.gen_two_clusters(n_dim=3)
    k_means = KMeans()
    k_means.fit(_x)
    k_means.visualize3d(_x, _y, dense=100, extra=k_means["centers"])
Example #7
0
            for j, indices in enumerate(labels == arange):
                self._centers[j] = np.average(x[indices], axis=0)
            counter += 1
            animation_params["extra"] = self._centers
            self._handle_animation(i, x, labels, ims, animation_params, *animation_properties)
            bar.update()
        self._counter = counter
        self._handle_mp4(ims, animation_properties)

    def predict(self, x, get_raw_results=False, high_dim=False):
        if not high_dim:
            x = x[:, None, ...]
        dis = np.abs(x - self._centers) if self._params["norm"] == "l1" else (x - self._centers) ** 2
        return np.argmin(np.sum(dis, axis=2), axis=1)

if __name__ == '__main__':
    _x, _y = DataUtil.gen_random(size=2000, scale=6)
    k_means = KMeans(n_clusters=8, animation_params={
        "show": False, "mp4": True, "period": 1, "draw_background": True
    })
    k_means.fit(_x)
    k_means.visualize2d(_x, _y, dense=400, extra=k_means["centers"])
    _x, _y = DataUtil.gen_two_clusters()
    k_means = KMeans()
    k_means.fit(_x)
    k_means.visualize2d(_x, _y, dense=400, extra=k_means["centers"])
    _x, _y = DataUtil.gen_two_clusters(n_dim=3)
    k_means = KMeans()
    k_means.fit(_x)
    k_means.visualize3d(_x, _y, dense=100, extra=k_means["centers"])