def test_map_transformer(): post_data = np.array([[1, 2, 2], [2, 1, 2], [7, 8, 9], [7, 7, 8], [7, 9, 7]]) test_data = np.array([[1, 1, 1], [1, 1, 2], [8, 9, 9], [8, 8, 8]]) n_gaussians = 2 n_features = 3 prior_machine = GMMMachine(n_gaussians) prior_machine.means = np.array([[2, 2, 2], [8, 8, 8]]) prior_machine.variances = np.ones_like(prior_machine.means) prior_machine.weights = np.array([0.5, 0.5]) machine = GMMMachine( n_gaussians, trainer="map", ubm=prior_machine, update_means=True, update_variances=True, update_weights=True, ) for transform in (to_numpy, to_dask_array): post_data = transform(post_data) machine = machine.fit(post_data) expected_means = np.array([[1.83333333, 1.83333333, 2.0], [7.57142857, 8, 8]]) np.testing.assert_almost_equal(machine.means, expected_means) eps = np.finfo(float).eps expected_vars = np.array([[eps, eps, eps], [eps, eps, eps]]) np.testing.assert_almost_equal(machine.variances, expected_vars) expected_weights = np.array([0.46226415, 0.53773585]) np.testing.assert_almost_equal(machine.weights, expected_weights) stats = machine.acc_stats(test_data) expected_stats = GMMStats(n_gaussians, n_features) expected_stats.init_fields( log_likelihood=-1.3837590691807108e16, t=test_data.shape[0], n=np.array([2, 2], dtype=float), sum_px=np.array([[2, 2, 3], [16, 17, 17]], dtype=float), sum_pxx=np.array([[2, 2, 5], [128, 145, 145]], dtype=float), ) assert stats.is_similar_to(expected_stats)
def test_ml_transformer(): data = np.array([[1, 2, 2], [2, 1, 2], [7, 8, 9], [7, 7, 8], [7, 9, 7]]) test_data = np.array([[1, 1, 1], [1, 1, 2], [8, 9, 9], [8, 8, 8]]) n_gaussians = 2 n_features = 3 machine = GMMMachine( n_gaussians, update_means=True, update_variances=True, update_weights=True, ) machine.means = np.array([[2, 2, 2], [8, 8, 8]]) machine.variances = np.ones_like(machine.means) for transform in (to_numpy, to_dask_array): data = transform(data) machine = machine.fit(data) expected_means = np.array([[1.5, 1.5, 2.0], [7.0, 8.0, 8.0]]) np.testing.assert_almost_equal(machine.means, expected_means) expected_weights = np.array([2 / 5, 3 / 5]) np.testing.assert_almost_equal(machine.weights, expected_weights) eps = np.finfo(float).eps expected_variances = np.array([[1 / 4, 1 / 4, eps], [eps, 2 / 3, 2 / 3]]) np.testing.assert_almost_equal(machine.variances, expected_variances) stats = machine.acc_stats(test_data) expected_stats = GMMStats(n_gaussians, n_features) expected_stats.init_fields( log_likelihood=-6755399441055685.0, t=test_data.shape[0], n=np.array([2, 2], dtype=float), sum_px=np.array([[2, 2, 3], [16, 17, 17]], dtype=float), sum_pxx=np.array([[2, 2, 5], [128, 145, 145]], dtype=float), ) assert stats.is_similar_to(expected_stats)