Ejemplo n.º 1
0
def _get_response_vector(source_index, num_sources, epsilon=0.):
    response_vector = labels_to_one_hot(
        np.array(source_index),
        num_sources,
        dtype=np.float64
    )
    response_vector = np.clip(response_vector, epsilon, 1.)
    return response_vector
Ejemplo n.º 2
0
    def test_cacgmm_sad_init(self):
        samples = 10000
        weight = np.array([0.3, 0.7])
        num_classes, = weight.shape
        covariance = np.array([
            [[10, 1 + 1j, 1 + 1j], [1 - 1j, 5, 1], [1 - 1j, 1, 2]],
            [[2, 0, 0], [0, 3, 0], [0, 0, 2]],
        ])
        covariance /= np.trace(covariance, axis1=-2, axis2=-1)[..., None, None]
        x, labels = sample_cacgmm(samples,
                                  weight,
                                  covariance,
                                  return_label=True)

        affiliations = labels_to_one_hot(labels, num_classes, axis=-2)

        # test initialization
        model = CACGMMTrainer().fit(
            x,
            initialization=affiliations,
            covariance_norm='trace',
        )

        # test initialization with independent
        model = CACGMMTrainer().fit(
            np.array([x]),
            initialization=np.array([affiliations]),
            covariance_norm='trace',
        )

        # test initialization with independent and broadcasted initialization
        model = CACGMMTrainer().fit(
            np.array([x, x, x]),
            initialization=np.array([affiliations]),
            covariance_norm='trace',
        )

        # test initialization with independent
        model = CACGMMTrainer().fit(
            np.array([x, x]),
            initialization=np.array([affiliations, affiliations]),
            covariance_norm='trace',
        )
Ejemplo n.º 3
0
    def predict(self, x):
        """

        Args:
            x: Shape (N, D)

        Returns: Affiliation with shape (K, N)

        """
        N, D = x.shape
        assert np.isrealobj(x), x.dtype

        labels = self.kmeans.predict(x)
        affiliations = labels_to_one_hot(labels,
                                         self.kmeans.n_clusters,
                                         axis=-2,
                                         keepdims=False,
                                         dtype=x.dtype)
        assert affiliations.shape == (self.kmeans.n_clusters, N)
        return affiliations