Exemplo n.º 1
0
def build_constraint_sets(left_dim, right_dim, nb_factors, sparsity_factor):
    """
    Build constraint set for factors with first factor constant.

    :param left_dim:
    :param right_dim:
    :param nb_factors:
    :param sparsity_factor:
    :return:
    """
    raise DeprecationWarning("should use build constraint set smart instead")

    inner_factor_dim = min(left_dim, right_dim)

    lst_proj_op_by_fac_step = []
    lst_proj_op_desc_by_fac_step = []

    nb_keep_values = sparsity_factor * inner_factor_dim  # sparsity factor = 5
    for k in range(nb_factors - 1):
        nb_values_residual = max(
            nb_keep_values,
            int(inner_factor_dim / 2**(k)) * inner_factor_dim
        )  # k instead of (k+1) for the first, constant matrix
        if k == 0:
            dct_step_lst_proj_op = {
                "split": [constant_proj, lambda mat: mat],
                "finetune": [constant_proj, lambda mat: mat]
            }
            dct_step_lst_nb_keep_values = {
                "split": ["constant_proj", "ident"],
                "finetune": ["constant_proj", "ident"]
            }
        else:
            dct_step_lst_proj_op = {
                "split": [
                    get_lambda_proxsplincol(nb_keep_values),
                    get_lambda_proxsplincol(nb_values_residual)
                ],
                "finetune":
                [constant_proj] + [get_lambda_proxsplincol(nb_keep_values)] *
                (k) + [get_lambda_proxsplincol(nb_values_residual)]
            }

            dct_step_lst_nb_keep_values = {
                "split": [nb_keep_values, nb_values_residual],
                "finetune": ["constant_proj"] + [nb_keep_values] * (k) +
                [nb_values_residual]
            }

        lst_proj_op_by_fac_step.append(dct_step_lst_proj_op)
        lst_proj_op_desc_by_fac_step.append(dct_step_lst_nb_keep_values)

    return lst_proj_op_by_fac_step, lst_proj_op_desc_by_fac_step
Exemplo n.º 2
0
    def build_lst_constraint_from_values(lst_values):
        local_lst_constraints = []
        for val in lst_values:
            if val == "constant_proj":
                local_lst_constraints.append(constant_proj)
            elif val == "ident":
                lambda_func = lambda mat: mat
                lambda_func.__name__ = "ident"
                local_lst_constraints.append(lambda_func)
            else:
                lambda_func = get_lambda_proxsplincol(
                    val, fast_unstable=fast_unstable_proj)
                lambda_func.__name__ = "proxsplincol_{}".format(val)
                local_lst_constraints.append(lambda_func)

        return local_lst_constraints
Exemplo n.º 3
0
        for k, X in data.items():

            d = np.min(X.shape)
            if X.shape[1] == d:
                X = X.T
            nb_factors = int(np.log2(d))
            lst_S_init = []
            for _ in range(nb_factors - 1):
                lst_S_init.append(np.eye(d))
            lst_S_init.append(np.zeros(X.shape))

            nb_keep_values = 2 * d
            nb_values_residual = int(d / 2**nb_factors) * d
            lst_projection_functions = \
                [get_lambda_proxsplincol(nb_keep_values)] * nb_factors \
                + [get_lambda_proxsplincol(nb_values_residual)]

            f_lambda_init = 1
            nb_iter = 10
            update_right_to_left = True
            graphical_display = False
            # f_lambda_ref, lst_S_ref, arr_X_curr_ref, objective_function_ref, \
            # i_iter_ref = \
            #     palm4msa_slow(X,
            #              lst_S_init=lst_S_init,
            #              nb_factors=nb_factors,
            #              lst_projection_functions=lst_projection_functions,
            #              f_lambda_init=f_lambda_init,
            #              nb_iter=nb_iter,
            #              update_right_to_left=update_right_to_left,
Exemplo n.º 4
0
    def test_hierarchical_palm4msa_compare(self):
        for k, X in self.data.items():
            print(k)

            d = np.min(X.shape)
            if X.shape[1] == d:
                X = X.T
            nb_factors = int(np.log2(d))

            nb_iter = 300

            lst_factors = []
            for _ in range(nb_factors - 1):
                lst_factors.append(np.eye(d))
            lst_factors.append(np.zeros(X.shape))
            _lambda = 1.
            # had = hadamard(d)
            # H = had / np.sqrt(32)


            lst_proj_op_by_fac_step = []
            nb_keep_values = 2 * d
            for k in range(nb_factors - 1):
                nb_values_residual = int(d / 2 ** (k + 1)) * d
                dct_step_lst_nb_keep_values = {
                    "split": [get_lambda_proxsplincol(nb_keep_values),
                              get_lambda_proxsplincol(nb_values_residual)],
                    "finetune": [get_lambda_proxsplincol(nb_keep_values)] * (
                            k + 1) + [
                                    get_lambda_proxsplincol(nb_values_residual)]
                }
                lst_proj_op_by_fac_step.append(dct_step_lst_nb_keep_values)

            out1 = hierarchical_palm4msa_fast(
                arr_X_target=X,
                lst_S_init=lst_factors,
                lst_dct_projection_function=lst_proj_op_by_fac_step,
                f_lambda_init=_lambda,
                nb_iter=nb_iter,
                update_right_to_left=True,
                residual_on_right=True,
                return_objective_function=True)

            out0 = hierarchical_palm4msa_slow(
                arr_X_target=X,
                lst_S_init=lst_factors,
                lst_dct_projection_function=lst_proj_op_by_fac_step,
                f_lambda_init=_lambda,
                nb_iter=nb_iter,
                update_right_to_left=True,
                residual_on_right=True,
                graphical_display=False)

            np.testing.assert_almost_equal((out1[0] - out0[0]) / out1[0], 0, err_msg='lambda')

            self.assertEqual(out1[1].n_factors, nb_factors, msg='nb factors')
            for j in range(nb_factors):
                np.testing.assert_array_almost_equal(
                    out1[1].get_factor(j).toarray(), out0[1][j],
                    err_msg='Factor {}'.format(j))
            np.testing.assert_almost_equal(np.linalg.norm(out1[2] - out0[2]) / np.linalg.norm(out1[2]), 0, err_msg='X')
Exemplo n.º 5
0
        for k, X in data.items():

            d = np.min(X.shape)
            if X.shape[1] == d:
                X = X.T
            nb_factors = int(np.log2(d))
            lst_S_init = []
            for _ in range(nb_factors - 1):
                lst_S_init.append(np.eye(d))
            lst_S_init.append(np.zeros(X.shape))

            nb_keep_values = 2 * d
            nb_values_residual = int(d / 2**nb_factors) * d
            lst_projection_functions = \
                [get_lambda_proxsplincol(nb_keep_values, fast_unstable=True)] * nb_factors \
                + [get_lambda_proxsplincol(nb_values_residual, fast_unstable=True)]

            f_lambda_init = 1
            nb_iter = 20
            update_right_to_left = True

            out = palm4msa_fast4(
                X,
                lst_S_init=lst_S_init,
                nb_factors=nb_factors,
                lst_projection_functions=lst_projection_functions,
                # lst_projection_functions=lst_projection_functions_fast,
                f_lambda_init=f_lambda_init,
                nb_iter=nb_iter,
                update_right_to_left=update_right_to_left,
#H =  had / norm(had, ord='fro')
H = had / np.sqrt(32)

lst_proj_op_by_fac_step = []
nb_keep_values = 2 * d
for k in range(nb_factors - 1):
    nb_values_residual = int(d / 2**(k)) * d
    if k == 0:
        dct_step_lst_nb_keep_values = {
            "split": [constant_proj, lambda mat: mat],
            "finetune": [constant_proj, lambda mat: mat]
        }
    else:
        dct_step_lst_nb_keep_values = {
            "split": [
                get_lambda_proxsplincol(nb_keep_values),
                get_lambda_proxsplincol(nb_values_residual)
            ],
            "finetune":
            [constant_proj] + [get_lambda_proxsplincol(nb_keep_values)] * (k) +
            [get_lambda_proxsplincol(nb_values_residual)]
        }
    lst_proj_op_by_fac_step.append(dct_step_lst_nb_keep_values)

#final_lambda, final_factors, final_X = PALM4LED(H, lst_factors, [nb_keep_values for _ in range(nb_factors)], _lambda, nb_iter)
final_lambda, final_factors, final_X, nb_iter_by_factor, _ = hierarchical_palm4msa(
    arr_X_target=H,
    lst_S_init=lst_factors,
    lst_dct_projection_function=lst_proj_op_by_fac_step,
    f_lambda_init=_lambda,
    nb_iter=nb_iter,
Exemplo n.º 7
0
residual_on_right = True
nb_factors = 10
d = min(X.shape)

lst_factors = [np.eye(min(X.shape)) for _ in range(nb_factors)]
lst_factors[-1] = np.random.rand(min(X.shape), X.shape[1])
lst_factors[0] = np.eye(X.shape[0], min(X.shape))
_lambda = 1.

lst_proj_op_by_fac_step = []
factor = 10
nb_keep_values = factor*d
for k in range(nb_factors - 1):
    nb_values_residual = max(nb_keep_values, int(d / 2 ** (k + 1)) * d)
    dct_step_lst_nb_keep_values = {
        "split": [get_lambda_proxsplincol(nb_keep_values), get_lambda_proxsplincol(nb_values_residual)] if residual_on_right else [get_lambda_proxsplincol(nb_values_residual), get_lambda_proxsplincol(nb_keep_values)],
        "finetune": [get_lambda_proxsplincol(nb_keep_values)] * (k+1) + [get_lambda_proxsplincol(nb_values_residual)] if residual_on_right else [get_lambda_proxsplincol(nb_values_residual)] + [get_lambda_proxsplincol(nb_keep_values)] * (k+1)
    }
    lst_proj_op_by_fac_step.append(dct_step_lst_nb_keep_values)

logger.info("Sparsity parameter by factor: {}".format(pformat(lst_proj_op_by_fac_step)))
#final_lambda, final_factors, final_X = PALM4LED(H, lst_factors, [nb_keep_values for _ in range(nb_factors)], _lambda, nb_iter)
final_lambda, final_factors, final_X, nb_iter_by_factor, _ = hierarchical_palm4msa(
    arr_X_target=X,
    lst_S_init=lst_factors,
    lst_dct_projection_function=lst_proj_op_by_fac_step,
    f_lambda_init=_lambda,
    nb_iter=nb_iter,
    update_right_to_left=True,
    residual_on_right=residual_on_right,
    graphical_display=False)