Exemple #1
0
    def compute_join_dist_mat(self, list_expert_id):
        """ Reset global variables. """
        joint_data = []
        joint_structures = []
        for i in list_expert_id:
            joint_data += self.training_data[i]
            # joint_structures += self.training_structures[expert_id]
        _global_training_data[f"{self.name}_join"] = joint_data
        _global_training_structures[f"{self.name}_join"] = joint_labels
        kmat = kernel_distance_mat(
            self.hyps,
            self.name + "_join",
            self.energy_kernel,
            self.cutoffs,
            self.hyps_mask,
            self.n_cpus,
            self.n_sample,
        )
        for i in range(kmat.shape[0]):
            norm = np.sqrt(kmat[i, i])
            kmat[i, :] /= norm
            kmat[:, i] /= norm
        print(np.min(kmat), np.max(kmat))
        kmat = np.max(kmat) - kmat

        del _global_training_data[f"{self.name}_join"]
        del _global_training_structures[f"{self.name}_join"]
        del joint_data
        del joint_structures
        return kmat
Exemple #2
0
    def redistribute_training_data(self, figure_name="default"):
        """ redistribute data """

        joint_data = []
        joint_structures = []
        joint_labels = []
        for i in range(self.n_experts):
            joint_data += self.training_data[i]
            joint_labels += self.training_labels[i]
            self.unsync_experts_data(i)

        self.n_experts = 1
        self.reset_container()

        _global_training_data[f"{self.name}_join"] = joint_data
        _global_training_structures[f"{self.name}_join"] = joint_structures

        kmat = kernel_distance_mat(
            self.hyps,
            self.name + "_join",
            self.energy_kernel,
            self.cutoffs,
            self.hyps_mask,
            self.n_cpus,
            self.n_sample,
        )
        for i in range(kmat.shape[0]):
            norm = np.sqrt(kmat[i, i])
            kmat[i, :] /= norm
            kmat[:, i] /= norm
        kmat = np.max(kmat) - kmat
        print(np.min(kmat), np.max(kmat))

        plot_mat(kmat, f"{figure_name}_unsorted_dmat.png")
        # np.save(f"{figure_name}_unsorted_dmat.npy", kmat)

        iu1 = np.triu_indices(kmat.shape[0], 1)
        upper_triang = kmat[iu1]
        Z = linkage(upper_triang, method="average")
        K = fcluster(Z, t=0.5, criterion="distance")

        ncluster = int(np.max(K))
        new_indices = []
        sizes = []
        for i in range(ncluster):
            index = np.where(K == (i + 1))[0]
            new_indices += [index]
            sizes += [len(index)]
        new_indices = np.hstack(new_indices)

        plot_mat(sort_matrix(kmat, new_indices),
                 f"{figure_name}_sorted_dmat.png")
        # np.save(f"{figure_name}_sorted_dmat.npy", kmat)

        for i in new_indices:
            self.add_one_env(joint_data[i],
                             joint_labels[i],
                             train=False,
                             expert_id=None)

        del _global_training_data[f"{self.name}_join"]
        del _global_training_structures[f"{self.name}_join"]
        del joint_data
        del joint_structures
        del joint_labels

        self.set_L_alpha()

        return kmat