Example #1
0
 def _update_beta_jac_bcd(X,
                          y,
                          beta,
                          dbeta,
                          r,
                          dr,
                          hyperparam,
                          L,
                          compute_jac=True):
     """
         beta : dual variable of the svm
         r : primal used for cheap updates
         dbeta : jacobian of the dual variables
         dr : jacobian of the primal variable
     """
     C = hyperparam[0]
     epsilon = hyperparam[1]
     n_samples = X.shape[0]
     for j in range(2 * n_samples):
         if j < n_samples:
             F = np.sum(r * X[j, :]) + epsilon - y[j]
             beta_old = beta[j]
             zj = beta[j] - F / L[j]
             beta[j] = proj_box_svm(zj, C)
             r += (beta[j] - beta_old) * X[j, :]
             if compute_jac:
                 dF = np.array([
                     np.sum(dr[:, 0].T * X[j, :]),
                     epsilon + np.sum(dr[:, 1].T * X[j, :])
                 ])
                 dbeta_old = dbeta[j, :]
                 dzj = dbeta[j, :] - dF / L[j]
                 dbeta[j, :] = ind_box(zj, C) * dzj
                 dbeta[j, 0] += C * (C <= zj)
                 dr[:, 0] += (dbeta[j, 0] - dbeta_old[0]) * X[j, :]
                 dr[:, 1] += (dbeta[j, 1] - dbeta_old[1]) * X[j, :]
         if j >= n_samples:
             F = - np.sum(r * X[j - n_samples, :]) + \
                 epsilon + y[j - n_samples]
             beta_old = beta[j]
             zj = beta[j] - F / L[j - n_samples]
             beta[j] = proj_box_svm(zj, C)
             r -= (beta[j] - beta_old) * X[j - n_samples, :]
             if compute_jac:
                 dF = np.array([
                     -np.sum(dr[:, 0].T * X[j - n_samples, :]),
                     -np.sum(dr[:, 1].T * X[j - n_samples, :] + epsilon)
                 ])
                 dbeta_old = dbeta[j, :]
                 dzj = dbeta[j, :] - dF / L[j - n_samples]
                 dbeta[j, :] = ind_box(zj, C) * dzj
                 dbeta[j, 0] += C * (C <= zj)
                 dr[:, 0] -= (dbeta[j, 0] - dbeta_old[0]) * \
                     X[j - n_samples, :]
                 dr[:, 1] -= (dbeta[j, 1] - dbeta_old[1]) * \
                     X[j - n_samples, :]
Example #2
0
 def _update_beta_jac_bcd(X,
                          y,
                          beta,
                          dbeta,
                          dual_var,
                          ddual_var,
                          C,
                          L,
                          compute_jac=True):
     """
         beta : dual variable of the svm
         r : primal used for cheap updates
         dbeta : jacobian of the dual variables
         dr : jacobian of the primal variable
     """
     C = C[0]
     n_samples = X.shape[0]
     for j in range(n_samples):
         F = y[j] * np.sum(beta * X[j, :]) - 1.0
         dual_var_old = dual_var[j]
         zj = dual_var[j] - F / L[j]
         dual_var[j] = proj_box_svm(zj, C)
         beta += (dual_var[j] - dual_var_old) * y[j] * X[j, :]
         if compute_jac:
             dF = y[j] * np.sum(dbeta * X[j, :])
             ddual_var_old = ddual_var[j]
             dzj = ddual_var[j] - dF / L[j]
             ddual_var[j] = ind_box(zj, C) * dzj
             ddual_var[j] += C * (C <= zj)
             dbeta += (ddual_var[j] - ddual_var_old) * y[j] * X[j, :]
Example #3
0
 def _update_beta_jac_bcd_sparse(data,
                                 indptr,
                                 indices,
                                 y,
                                 n_samples,
                                 n_features,
                                 beta,
                                 dbeta,
                                 dual_var,
                                 ddual_var,
                                 C,
                                 L,
                                 compute_jac=True):
     # data needs to be a row sparse matrix
     C = C[0]
     for j in range(n_samples):
         # get the i-st row of X in sparse format
         Xis = data[indptr[j]:indptr[j + 1]]
         # get the non zero indices
         idx_nz = indices[indptr[j]:indptr[j + 1]]
         # Compute the gradient
         F = y[j] * np.sum(beta[idx_nz] * Xis) - 1.0
         dual_var_old = dual_var[j]
         zj = dual_var[j] - F / L[j]
         dual_var[j] = proj_box_svm(zj, C)
         beta[idx_nz] += (dual_var[j] - dual_var_old) * y[j] * Xis
         if compute_jac:
             dF = y[j] * np.sum(dbeta[idx_nz] * Xis)
             ddual_var_old = ddual_var[j]
             dzj = ddual_var[j] - dF / L[j]
             ddual_var[j] = ind_box(zj, C) * dzj
             ddual_var[j] += C * (C <= zj)
             dbeta[idx_nz] += (ddual_var[j] - ddual_var_old) * y[j] * Xis
Example #4
0
def _update_beta_jac_bcd_aux(X, y, epsilon, beta, dbeta, dual_var, ddual_var,
                             L, C, j1, j2, sign, compute_jac):
    F = sign * np.sum(beta * X[j1, :]) + epsilon - sign * y[j1]
    dual_var_old = dual_var[j2]
    zj = dual_var[j2] - F / L[j1]
    dual_var[j2] = proj_box_svm(zj, C)
    beta += sign * ((dual_var[j2] - dual_var_old) * X[j1, :])
    if compute_jac:
        _compute_jac_aux(X, epsilon, dbeta, ddual_var, zj, L, C, j1, j2, sign)
Example #5
0
def _update_beta_jac_bcd_aux_sparse(data, indptr, indices, y, epsilon, beta,
                                    dbeta, dual_var, ddual_var, L, C, j1, j2,
                                    sign, compute_jac):

    # get the i-st row of X in sparse format
    Xjs = data[indptr[j1]:indptr[j1 + 1]]
    # get the non zero indices
    idx_nz = indices[indptr[j1]:indptr[j1 + 1]]

    F = sign * np.sum(beta[idx_nz] * Xjs) + epsilon - sign * y[j1]
    dual_var_old = dual_var[j2]
    zj = dual_var[j2] - F / L[j1]
    dual_var[j2] = proj_box_svm(zj, C)
    beta[idx_nz] += sign * (dual_var[j2] - dual_var_old) * Xjs
    if compute_jac:
        _compute_jac_aux_sparse(data, indptr, indices, epsilon, dbeta,
                                ddual_var, zj, L, C, j1, j2, sign)