def test3_MDN_with_2d_gaussian_noise_y(self):
        X, Y = self.get_samples(std=0.5)

        with tf.Session():
            model_no_noise = MixtureDensityNetwork("mdn_no_noise_y",
                                                   1,
                                                   1,
                                                   n_centers=1,
                                                   x_noise_std=None,
                                                   y_noise_std=None)
            model_no_noise.fit(X, Y)
            var_no_noise = model_no_noise.covariance(
                x_cond=np.array([[2]]))[0][0][0]

            model_noise = MixtureDensityNetwork("mdn_noise_y",
                                                1,
                                                1,
                                                n_centers=1,
                                                x_noise_std=None,
                                                y_noise_std=1)
            model_noise.fit(X, Y)
            var_noise = model_noise.covariance(x_cond=np.array([[2]]))[0][0][0]

            print("Training w/o noise:", var_no_noise)
            print("Training w/ noise:", var_noise)

            self.assertGreaterEqual(var_noise - var_no_noise, 0.1)
    def test8_data_normalization(self):
        np.random.seed(24)
        mean = 10
        std = 2
        data = np.random.normal([mean, mean, mean, mean], std, size=(2000, 4))
        X = data[:, 0:2]
        Y = data[:, 2:4]

        with tf.Session() as sess:
            model = MixtureDensityNetwork("mdn_data_normalization",
                                          2,
                                          2,
                                          n_centers=2,
                                          x_noise_std=None,
                                          y_noise_std=None,
                                          data_normalization=True,
                                          n_training_epochs=2000,
                                          random_seed=22)
            model.fit(X, Y)
            y_mean, y_std = sess.run([model.mean_y_sym, model.std_y_sym])
            print(y_mean, y_std)
            cond_mean = model.mean_(Y)
            mean_diff = np.abs(mean - np.mean(cond_mean))
            self.assertLessEqual(mean_diff, 0.5)

            cond_cov = np.mean(model.covariance(Y), axis=0)
            print(cond_cov)
            self.assertGreaterEqual(cond_cov[0][0], std**2 * 0.7)
            self.assertLessEqual(cond_cov[0][0], std**2 * 1.3)
            self.assertGreaterEqual(cond_cov[1][1], std**2 * 0.7)
            self.assertLessEqual(cond_cov[1][1], std**2 * 1.3)
    def test_MDN_with_2d_gaussian_sampling(self):
        X, Y = self.get_samples()

        model = MixtureDensityNetwork("mdn_gaussian_sampling",
                                      1,
                                      1,
                                      n_centers=5,
                                      n_training_epochs=200)
        model.fit(X, Y)

        x_cond = np.ones(shape=(10**6, 1))
        _, y_sample = model.sample(x_cond)
        self.assertAlmostEqual(np.mean(y_sample),
                               float(model.mean_(y_sample[1])),
                               places=0)
        self.assertAlmostEqual(np.std(y_sample),
                               float(model.covariance(y_sample[1])),
                               places=0)
Example #4
0
    def test_covariance_mixture(self):
        np.random.seed(24)
        from tensorflow import set_random_seed
        set_random_seed(24)

        scale = 2.0
        data = np.random.normal(loc=[2, 2, 7, -2],
                                scale=scale,
                                size=(10000, 4))
        X = data[:, 0:2]
        Y = data[:, 2:4]

        model = MixtureDensityNetwork("mdn_cov",
                                      2,
                                      2,
                                      n_centers=5,
                                      x_noise_std=0.1,
                                      y_noise_std=0.1)
        model.fit(X, Y)

        cov_est = model.covariance(x_cond=np.array([[0, 1]]))
        print(cov_est)
        self.assertLessEqual(np.abs(cov_est[0][1][0] - 0.0), 0.2)
        self.assertLessEqual(np.abs(np.sqrt(cov_est[0][0][0]) - scale), 1.0)