Пример #1
0
def test_diag_indices():
    mnp_res = mnp.diag_indices(0)
    onp_res = onp.diag_indices(0)
    match_all_arrays(mnp_res, onp_res)

    mnp_res = mnp.diag_indices(3, 0)
    onp_res = onp.diag_indices(3, 0)
    match_all_arrays(mnp_res, onp_res)

    mnp_res = mnp.diag_indices(5, 7)
    onp_res = onp.diag_indices(5, 7)
    match_all_arrays(mnp_res, onp_res)
Пример #2
0
    def _to_dag(self, train_data):
        """
        threshold: from weaker to stronger in A
        1- If some entries of A less than equal threshold, mask them to zero
        (This can happen with stochastic proximal gradient descent)
        2- Remove edges by threshold until a DAG is obtained.

        Parameters
        ----------
        train_data: NormalizationData
            train samples
        """

        if self.jac_thresh:
            jacobian_adj = compute_jacobian_avg(
                self.model, train_data, train_data.n_samples).transpose()
        else:
            jacobian_adj = self.model.get_w_adj()

        dia_index = msnp.diag_indices(jacobian_adj.shape[0])
        jacobian_adj[dia_index] = 0

        # Find the smallest threshold that removes all cycle-inducing edges
        thresholds = np.unique(jacobian_adj.asnumpy())
        epsilon = 1e-8
        for step, t in enumerate(thresholds):
            to_keep = (jacobian_adj > t + epsilon).astype(dtype=mstype.float32)
            new_adj = self.model.adjacency * to_keep
            if is_acyclic(new_adj):
                self.model.adjacency = new_adj
                break

        return self.model