Пример #1
0
    def fit(self,X, y):
        n, d = X.shape

        self.w = np.zeros(d)
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMinL1(self.funObj, self.w, self.L1_lambda,
                                        self.maxEvals, X, y, verbose=self.verbose)
Пример #2
0
    def fit(self, X, y):
        n, d = X.shape

        # Initial guess
        self.w = np.zeros(d)
        utils.check_gradient(self, X, y)
        (self.w, f) = minimizers.findMinL1(self.funObj, self.w, self.lammy,
                                           self.maxEvals, self.verbose, X, y)
Пример #3
0
    def fit(self, X, y):
        n, d = X.shape
        self.X = X

        K = self.kernel_fun(X,X, **self.kernel_args)

        utils.check_gradient(self, K, y, n, verbose=self.verbose)
        self.u, f = findMin.findMin(self.funObj, np.zeros(n), self.maxEvals, K, y, verbose=self.verbose)
Пример #4
0
    def fit(self,X, y):
        n, d = X.shape

        # Initial guess
        self.w = np.zeros(d)
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj, self.w,
                                      self.maxEvals, X, y, verbose=self.verbose)
Пример #5
0
    def fit(self, X, y):
        check = np.arange(0, 15)

        n, d = X.shape
        self.n_classes = np.unique(y).size
        self.w = np.zeros((d, self.n_classes))
        self.w = self.w.flatten()
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj, self.w, self.maxEvals, X, y)

        self.w = self.w.reshape((d, self.n_classes))
Пример #6
0
    def fit(self, X, y):
        _, d = X.shape

        # Initial guess
        utils.check_gradient(self, X, y, d, verbose=self.verbose)
        self.w, _ = findMin.findMin(self.funObj,
                                    np.zeros(d),
                                    self.maxEvals,
                                    X,
                                    y,
                                    verbose=self.verbose)
Пример #7
0
    def fit(self,X, y):
        n, d = X.shape
        k = np.unique(y).size

        self.n_classes = k
        self.W = np.zeros(d*k)
        self.w = self.W 
        
        utils.check_gradient(self, X, y)
        (self.W, f) = findMin.findMin(self.funObj, self.W,
                                      self.maxEvals, X, y, verbose=self.verbose)

        self.W = np.reshape(self.W, (k,d))
Пример #8
0
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size

        # Initial guess
        self.w = np.zeros(d*self.n_classes)
        utils.check_gradient(self, X, y)

        self.w, _ = minimizers.findMin(self.funObj, self.w,
                                    self.maxEvals,
                                    self.verbose,
                                    X, y)

        self.w = np.reshape(self.w, (d, self.n_classes))
Пример #9
0
    def fit(self,X, y):
        n, d = X.shape
        k = np.unique(y).size

        self.n_classes = k
        self.W = np.zeros(d*k)
        self.w = self.W # because the gradient checker is implemented in a silly way

        # Initial guess
        utils.check_gradient(self, X, y)
        (self.W, f) = findMin.findMin(self.funObj, self.W,
                                      self.maxEvals, X, y, verbose=self.verbose)

        self.W = np.reshape(self.W, (k,d))
Пример #10
0
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size

        # Initial guess
        self.w = np.zeros(self.n_classes * d)
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj,
                                      self.w,
                                      self.maxEvals,
                                      X,
                                      y,
                                      verbose=self.verbose)
        print("training finished")
Пример #11
0
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size
        # Initial guess
        self.W = np.zeros((self.n_classes, d))
        self.w = np.zeros(self.n_classes * d)

        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj,
                                      self.w,
                                      self.maxEvals,
                                      X,
                                      y,
                                      verbose=self.verbose)
        self.W = np.reshape(self.w, (self.n_classes, d))
Пример #12
0
    def fit(self, X, y):
        n, d = X.shape
        self.X = X

        K = self.kernel_fun(
            X, X, **self.kernel_args
        )  #X-X is 0, so K is 0, why is it XX here, somehow it works for

        utils.check_gradient(self, K, y, n, verbose=self.verbose)
        self.u, f = findMin.findMin(self.funObj,
                                    np.zeros(n),
                                    self.maxEvals,
                                    K,
                                    y,
                                    verbose=self.verbose)
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size

        # Initial guess
        self.w = np.zeros((self.n_classes * d, ))

        # solve the normal equations
        # with a bit of regularization for numerical reasons
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj,
                                      self.w,
                                      self.maxEvals,
                                      X,
                                      y,
                                      verbose=self.verbose)
        self.W = np.reshape(self.w, (self.n_classes, d))
Пример #14
0
    def fit(self, X, y):
        n, d = X.shape
        k = np.unique(y).size

        self.n_classes = k
        self.w = np.zeros(d * k)

        # Initial guess
        utils.check_gradient(self, X, y)
        (self.w, f) = findMin.findMin(self.funObj,
                                      self.w.ravel(),
                                      self.maxEvals,
                                      X,
                                      y,
                                      verbose=self.verbose)

        self.w = np.reshape(self.w, (d, k))
Пример #15
0
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size

        # Initial guess
        self.w = np.zeros((self.n_classes, d))
        #self.w = np.zeros(d)

        for i in range(self.n_classes):
            ytmp = y.copy().astype(float)
            ytmp[y == i] = 1
            ytmp[y != i] = -1

            # solve the normal equations
            # with a bit of regularization for numerical reasons
            self.w[i] = np.linalg.solve(X.T @ X + 0.0001 * np.eye(d),
                                        X.T @ ytmp)
            utils.check_gradient(self, X, y)
    def fit(self, X, y):
        n, d = X.shape

        # Initialize parameter matrix W
        y_labels = np.unique(y)
        k = len(y_labels)
        self.n_classes = len(y_labels)
        self.w = np.zeros(k * d)
        # Check gradient
        utils.check_gradient(self, X, y)
        # Fit the model
        w_vector, f = findMin.findMin(self.funObj,
                                      self.w,
                                      self.maxEvals,
                                      X,
                                      y,
                                      verbose=self.verbose)

        self.W = np.reshape(w_vector, (k, d))
Пример #17
0
    def fit(self, X, y):
        n, d = X.shape
        minimize = lambda ind: findMin.findMin(self.funObj,
                                               np.zeros(len(ind)),
                                               self.maxEvals,
                                               X[:, ind],
                                               y,
                                               verbose=0)
        selected = set()
        selected.add(0)
        minLoss = np.inf
        oldLoss = 0
        bestFeature = -1
        print("Minimize {0}".format(minimize))

        while minLoss != oldLoss:
            oldLoss = minLoss
            print("Epoch %d " % len(selected))
            print("Selected feature: %d" % (bestFeature))
            print("Min Loss: %.3f\n" % minLoss)

            for i in range(d):
                if i in selected:
                    continue

                selected_new = selected | {i}
                # TODO for Q2.3: Fit the model with 'i' added to the features,
                # then compute the loss and update the minLoss/bestFeature

                #L0 norm = # non-zero elements

                w, f = minimize(list(selected_new))
                if (f <= minLoss):
                    minLoss = f
                    bestFeature = i

            selected.add(bestFeature)

        self.w = np.zeros(d)
        self.w[list(selected)], _ = minimize(list(selected))
        utils.check_gradient(self, X, y)
Пример #18
0
    def fit(self, X, y):
        n, d = X.shape
        self.n_classes = np.unique(y).size

        # Initial guess
        self.W = np.zeros((self.n_classes, d))

        for i in range(self.n_classes):
            ytmp = y.copy().astype(float)
            ytmp[y == i] = 1
            ytmp[y != i] = -1
            # solve the normal equations
            # with a bit of regularization for numerical reasons
            (self.W[i], f) = findMin.findMin(self.funObj,
                                             self.W[i],
                                             self.maxEvals,
                                             X,
                                             ytmp,
                                             verbose=self.verbose)
            self.w = self.W[i]
            utils.check_gradient(self, X, ytmp)
Пример #19
0
def exec_task(debug=False):
    # 自编码参数
    sparsity_param = 0.035  # 期望稀疏率
    lambda_ = 3e-3  # 权重衰减比重
    beta = 5  # 稀疏判罚比重

    # 白化参数
    white_epsilon = 0.1  # 正则常数

    if debug:
        print 'Gradient checking...'
        debug_hidden_size = 5
        debug_visible_size = 8
        patches = np.random.rand(8, 10)
        debug_encoder = SparseAutoEncoderLD(debug_visible_size,
                                            debug_hidden_size, sparsity_param,
                                            lambda_, beta)
        cost, grad = debug_encoder.loss_value(debug_encoder.theta, patches)
        check_gradient(lambda x: debug_encoder.loss_value(x, patches),
                       debug_encoder.theta, grad)

    print 'Loading patches and applying whitening...'
    patches = scipy.io.loadmat('data/stlSampledPatches.mat')['patches']

    # 训练参数
    visible_size = patches.shape[0]  # 输入单元数 为 8*8*3
    hidden_size = 400  # 隐藏单元数

    display_color_network(patches[:, 0:100], fn='patches_raw.png')  # 显示原图片数据

    # ZCA白化
    patch_mean = np.mean(patches, axis=1)
    patches -= patch_mean.reshape((-1, 1))
    sigma = patches.dot(patches.transpose()) / patches.shape[1]
    (U, s, V) = np.linalg.svd(sigma)
    zca_white = U.dot(np.diag(1 / (s + white_epsilon))).dot(U.T)
    patches_zca = zca_white.dot(patches)
    # # 上两行代码等价于下面注释掉的三行,效率更高,且模型易保存
    # patch_rot = U.T.dot(patches)
    # pca_whiten = np.diag(1 / (s + white_epsilon)).dot(patch_rot)
    # zca_whiten = U.dot(pca_whiten)
    display_color_network(patches_zca[:, 0:100], fn='patches_zca.png')

    print 'Running sparse auto encoding with linear decoder...'
    encoder = SparseAutoEncoderLD(visible_size, hidden_size, sparsity_param,
                                  lambda_, beta)
    opt_solution = scipy.optimize.minimize(
        encoder.loss_value,
        encoder.theta,
        args=(patches_zca, ),
        method='L-BFGS-B',
        jac=True,
        options={
            'maxiter': 400,
            'disp': True
        })  # after install Theano, this use multi-core.
    opt_theta = opt_solution.x
    w1 = opt_theta[0:hidden_size * visible_size].reshape(
        hidden_size, visible_size)
    display_color_network(
        w1.dot(zca_white).transpose(),
        'linear_decoder_features.png')  # weight after zca processing

    # 保存训练参数,以供之后使用
    params = dict({})
    params['opt_theta'] = opt_theta
    params['zca_white'] = zca_white
    params['mean_patch'] = patch_mean
    joblib.dump(params, "model/ALL_params.pkl", compress=3)