Exemplo n.º 1
0
    def _compare_with_sklearn(self, cov_type):
        # sklearn version.
        iterations = 40
        np.random.seed(5)
        sklearn_assignments = np.asarray([0, 0, 1, 0, 0, 0, 1, 0, 0, 1])
        sklearn_means = np.asarray([[144.83417719, 254.20130341],
                                    [274.38754816, 353.16074346]])
        sklearn_covs = np.asarray([[[395.0081194, -4.50389512],
                                    [-4.50389512, 408.27543989]],
                                   [[385.17484203, -31.27834935],
                                    [-31.27834935, 391.74249925]]])

        # skflow version.
        gmm = GMM(self.num_centers,
                  initial_clusters=self.initial_means,
                  covariance_type=cov_type,
                  batch_size=self.num_points,
                  steps=iterations,
                  continue_training=True,
                  config=run_config.RunConfig(tf_random_seed=2))
        gmm.fit(self.points)
        skflow_assignments = gmm.predict(self.points[:10, :]).astype(int)
        self.assertAllClose(sklearn_assignments, np.ravel(skflow_assignments))
        self.assertAllClose(sklearn_means, gmm.clusters())
        if cov_type == 'full':
            self.assertAllClose(sklearn_covs, gmm.covariances(), rtol=0.01)
        else:
            for d in [0, 1]:
                self.assertAllClose(np.diag(sklearn_covs[d]),
                                    gmm.covariances()[d, :],
                                    rtol=0.01)
Exemplo n.º 2
0
 def test_fit(self):
     gmm = GMM(self.num_centers,
               initial_clusters='random',
               batch_size=self.batch_size,
               random_seed=4,
               config=run_config.RunConfig(tf_random_seed=2))
     gmm.fit(x=self.points, steps=1)
     score1 = gmm.score(x=self.points)
     gmm = GMM(self.num_centers,
               initial_clusters='random',
               batch_size=self.batch_size,
               random_seed=4,
               config=run_config.RunConfig(tf_random_seed=2))
     gmm.fit(x=self.points, steps=10)
     score2 = gmm.score(x=self.points)
     self.assertGreater(score1, score2)
     self.assertNear(self.true_score, score2, self.true_score * 0.15)
Exemplo n.º 3
0
 def test_clusters(self):
     """Tests the shape of the clusters."""
     gmm = GMM(self.num_centers,
               initial_clusters=self.initial_means,
               batch_size=self.batch_size,
               steps=40,
               continue_training=True,
               random_seed=4,
               config=run_config.RunConfig(tf_random_seed=2))
     gmm.fit(x=self.points, steps=0)
     clusters = gmm.clusters()
     self.assertAllEqual(list(clusters.shape),
                         [self.num_centers, self.num_dims])
Exemplo n.º 4
0
def main():
    img = mpimg.imread('corgi.png')[:, :, :3]
    img_reshape = np.reshape(img, (-1, 3))
    for m in [3, 5, 10]:
        gmm = GMM(m, steps=10)
        gmm.fit(img_reshape)
        print("w:")
        print(weights(gmm))
        print("mu:")
        print(gmm.clusters())
        print("sigma:")
        print(gmm.covariances())
        clusters = gmm.clusters()
        img_cluster = clusters[gmm.predict(img_reshape)]
        img_cluster = np.reshape(img_cluster,
                                 (img.shape[0], img.shape[1], img.shape[2]))
        plt.imshow(img_cluster)
        plt.title("m = %d" % m)
        plt.show()
Exemplo n.º 5
0
    def test_infer(self):
        gmm = GMM(self.num_centers,
                  initial_clusters=self.initial_means,
                  batch_size=self.batch_size,
                  steps=40,
                  continue_training=True,
                  random_seed=4,
                  config=run_config.RunConfig(tf_random_seed=2))
        gmm.fit(x=self.points, steps=60)
        clusters = gmm.clusters()

        # Make a small test set
        points, true_assignments, true_offsets = (self.make_random_points(
            clusters, 40))

        assignments = np.ravel(gmm.predict(points))
        self.assertAllEqual(true_assignments, assignments)

        # Test score
        score = gmm.score(points)
        self.assertNear(score, np.sum(true_offsets), 4.05)