示例#1
0
        def RunGMMShogun(q):
            totalTimer = Timer()

            try:
                # Load input dataset.
                Log.Info("Loading dataset", self.verbose)
                dataPoints = np.genfromtxt(self.dataset, delimiter=',')
                dataFeat = RealFeatures(dataPoints.T)

                # Get all the parameters.
                g = re.search("-g (\d+)", options)
                n = re.search("-n (\d+)", options)
                s = re.search("-n (\d+)", options)

                g = 1 if not g else int(g.group(1))
                n = 250 if not n else int(n.group(1))

                # Create the Gaussian Mixture Model.
                model = SGMM(g)
                model.set_features(dataFeat)
                with totalTimer:
                    model.train_em(1e-9, n, 1e-9)
            except Exception as e:
                q.put(-1)
                return -1

            time = totalTimer.ElapsedTime()
            q.put(time)
            return time
示例#2
0
    def RunGMMShogun(q):
      totalTimer = Timer()


      try:
        # Load input dataset.
        Log.Info("Loading dataset", self.verbose)
        dataPoints = np.genfromtxt(self.dataset, delimiter=',')
        dataFeat = RealFeatures(dataPoints.T)

        # Get all the parameters.
        g = re.search("-g (\d+)", options)
        n = re.search("-n (\d+)", options)
        s = re.search("-n (\d+)", options)

        g = 1 if not g else int(g.group(1))
        n = 250 if not n else int(n.group(1))

        # Create the Gaussian Mixture Model.
        model = SGMM(g)
        model.set_features(dataFeat)
        with totalTimer:
          model.train_em(1e-9, n, 1e-9)
      except Exception as e:
        q.put(-1)
        return -1

      time = totalTimer.ElapsedTime()
      q.put(time)
      return time
示例#3
0
文件: gmm.py 项目: rcurtin/benchmarks
    def RunGMMShogun():
      totalTimer = Timer()

      try:
        # Load input dataset.
        Log.Info("Loading dataset", self.verbose)
        dataPoints = np.genfromtxt(self.dataset, delimiter=',')
        dataFeat = RealFeatures(dataPoints.T)

        # Get all the parameters.
        if "gaussians" in options:
          g = int(options.pop("gaussians"))
        else:
          Log.Fatal("Required parameter 'gaussians' not specified!")
          raise Exception("missing parameter")
        if "max_iterations" in options:
          n = int(options.pop("max_iterations"))
        else:
          n = 0

        if len(options) > 0:
          Log.Fatal("Unknown parameters: " + str(options))
          raise Exception("unknown parameters")

        # Create the Gaussian Mixture Model.
        model = SGMM(g)
        model.set_features(dataFeat)
        with totalTimer:
          model.train_em(1e-9, n, 1e-9)
      except Exception as e:
        Log.Info("Exception: " + str(e))
        return -1

      return totalTimer.ElapsedTime()
示例#4
0
        def RunGMMShogun():
            totalTimer = Timer()

            try:
                # Load input dataset.
                Log.Info("Loading dataset", self.verbose)
                dataPoints = np.genfromtxt(self.dataset, delimiter=',')
                dataFeat = RealFeatures(dataPoints.T)

                # Get all the parameters.
                if "gaussians" in options:
                    g = int(options.pop("gaussians"))
                else:
                    Log.Fatal("Required parameter 'gaussians' not specified!")
                    raise Exception("missing parameter")
                if "max_iterations" in options:
                    n = int(options.pop("max_iterations"))
                else:
                    n = 0

                if len(options) > 0:
                    Log.Fatal("Unknown parameters: " + str(options))
                    raise Exception("unknown parameters")

                # Create the Gaussian Mixture Model.
                model = SGMM(g)
                model.set_features(dataFeat)
                with totalTimer:
                    model.train_em(1e-9, n, 1e-9)
            except Exception as e:
                Log.Info("Exception: " + str(e))
                return -1

            return totalTimer.ElapsedTime()
示例#5
0
    def fit_gmm(self, samples):
        """
        Runs a couple of em instances on random starting points and returns
        internal GMM representation of best instance
        """
        features = RealFeatures(samples.T)

        gmms = []
        log_likelihoods = zeros(self.num_runs_em)
        for i in range(self.num_runs_em):
            # set up Shogun's GMM class and run em (corresponds to random
            # initialisation)
            gmm = GMM(self.num_components)
            gmm.set_features(features)
            log_likelihoods[i] = gmm.train_em()
            gmms.append(gmm)

        max_idx = log_likelihoods.argmax()

        # construct Gaussian mixture components in internal representation
        components = []
        for i in range(self.num_components):
            mu = gmms[max_idx].get_nth_mean(i)
            Sigma = gmms[max_idx].get_nth_cov(i)
            components.append(Gaussian(mu, Sigma))

        # construct a Gaussian mixture model based on the best EM run
        pie = gmms[max_idx].get_coef()
        proposal = MixtureDistribution(components[0].dimension,
                                       self.num_components, components,
                                       Discrete(pie))

        return proposal
示例#6
0
    def fit_gmm(self, samples):
        """
        Runs a couple of em instances on random starting points and returns
        internal GMM representation of best instance
        """
        features = RealFeatures(samples.T)
        
        gmms = []
        log_likelihoods = zeros(self.num_runs_em)
        for i in range(self.num_runs_em):
            # set up Shogun's GMM class and run em (corresponds to random
            # initialisation)
            gmm = GMM(self.num_components)
            gmm.set_features(features)
            log_likelihoods[i] = gmm.train_em()
            gmms.append(gmm)
            
        
        max_idx = log_likelihoods.argmax()

        # construct Gaussian mixture components in internal representation
        components = []
        for i in range(self.num_components):
            mu = gmms[max_idx].get_nth_mean(i)
            Sigma = gmms[max_idx].get_nth_cov(i)
            components.append(Gaussian(mu, Sigma))
            
        # construct a Gaussian mixture model based on the best EM run
        pie = gmms[max_idx].get_coef()
        proposal = MixtureDistribution(components[0].dimension,
                                     self.num_components, components,
                                     Discrete(pie))
        
        return proposal
示例#7
0
real_gmm.set_nth_cov(array([[0.1]]), 1)
real_gmm.set_nth_cov(array([[0.2]]), 2)

real_gmm.set_coef(array([0.3, 0.5, 0.2]))

#generate training set from real GMM
generated=array([real_gmm.sample()])
for i in range(199):
    generated=append(generated, array([real_gmm.sample()]), axis=1)

feat_train=RealFeatures(generated)

#train GMM using EM
est_gmm=GMM(3)
est_gmm.train(feat_train)
est_gmm.train_em(min_cov, max_iter, min_change)

#get and print estimated means and covariances
est_mean1=est_gmm.get_nth_mean(0)
est_mean2=est_gmm.get_nth_mean(1)
est_mean3=est_gmm.get_nth_mean(2)
est_cov1=est_gmm.get_nth_cov(0)
est_cov2=est_gmm.get_nth_cov(1)
est_cov3=est_gmm.get_nth_cov(2)
est_coef=est_gmm.get_coef()
print est_mean1
print est_cov1
print est_mean2
print est_cov2
print est_mean3
print est_cov3
示例#8
0
#train GMM using EM and bad initial conditions and print log-likelihood
est_em_gmm = GMM(3, cov_type)
est_em_gmm.train(feat_train)

est_em_gmm.set_nth_mean(array([2.0, 0.0]), 0)
est_em_gmm.set_nth_mean(array([-2.0, -2.0]), 1)
est_em_gmm.set_nth_mean(array([-3.0, -3.0]), 2)

est_em_gmm.set_nth_cov(array([[1.0, 0.0], [0.0, 1.0]]), 0)
est_em_gmm.set_nth_cov(array([[1.0, 0.0], [0.0, 1.0]]), 1)
est_em_gmm.set_nth_cov(array([[1.0, 0.0], [0.0, 1.0]]), 2)

est_em_gmm.set_coef(array([0.3333, 0.3333, 0.3334]))

print est_em_gmm.train_em(min_cov, max_em_iter, min_change)

#plot real GMM, data and both estimated GMMs
min_x_gen = min(min(generated[[0]])) - 0.1
max_x_gen = max(max(generated[[0]])) + 0.1
min_y_gen = min(min(generated[[1]])) - 0.1
max_y_gen = max(max(generated[[1]])) + 0.1

plot_real = empty(0)
plot_est_smem = empty(0)
plot_est_em = empty(0)

for i in arange(min_x_gen, max_x_gen, 0.05):
    for j in arange(min_y_gen, max_y_gen, 0.05):
        plot_real = append(plot_real,
                           array([exp(real_gmm.cluster(array([i, j]))[3])]))
示例#9
0
#train GMM using EM and bad initial conditions and print log-likelihood
est_em_gmm=GMM(3, cov_type)
est_em_gmm.train(feat_train)

est_em_gmm.set_nth_mean(array([2.0, 0.0]), 0)
est_em_gmm.set_nth_mean(array([-2.0, -2.0]), 1)
est_em_gmm.set_nth_mean(array([-3.0, -3.0]), 2)

est_em_gmm.set_nth_cov(array([[1.0, 0.0],[0.0, 1.0]]), 0)
est_em_gmm.set_nth_cov(array([[1.0, 0.0],[0.0, 1.0]]), 1)
est_em_gmm.set_nth_cov(array([[1.0, 0.0],[0.0, 1.0]]), 2)

est_em_gmm.set_coef(array([0.3333, 0.3333, 0.3334]))

print est_em_gmm.train_em(min_cov, max_em_iter, min_change)

#plot real GMM, data and both estimated GMMs
min_x_gen=min(min(generated[[0]]))-0.1
max_x_gen=max(max(generated[[0]]))+0.1
min_y_gen=min(min(generated[[1]]))-0.1
max_y_gen=max(max(generated[[1]]))+0.1

plot_real=empty(0)
plot_est_smem=empty(0)
plot_est_em=empty(0)

for i in arange(min_x_gen, max_x_gen, 0.05):
    for j in arange(min_y_gen, max_y_gen, 0.05):
        plot_real=append(plot_real, array([exp(real_gmm.cluster(array([i, j]))[3])]))
        plot_est_smem=append(plot_est_smem, array([exp(est_smem_gmm.cluster(array([i, j]))[3])]))
示例#10
0
real_gmm.set_nth_cov(array([[0.1]]), 1)
real_gmm.set_nth_cov(array([[0.2]]), 2)

real_gmm.set_coef(array([0.3, 0.5, 0.2]))

#generate training set from real GMM
generated = array([real_gmm.sample()])
for i in range(199):
    generated = append(generated, array([real_gmm.sample()]), axis=1)

feat_train = RealFeatures(generated)

#train GMM using EM
est_gmm = GMM(3)
est_gmm.train(feat_train)
est_gmm.train_em(min_cov, max_iter, min_change)

#get and print estimated means and covariances
est_mean1 = est_gmm.get_nth_mean(0)
est_mean2 = est_gmm.get_nth_mean(1)
est_mean3 = est_gmm.get_nth_mean(2)
est_cov1 = est_gmm.get_nth_cov(0)
est_cov2 = est_gmm.get_nth_cov(1)
est_cov3 = est_gmm.get_nth_cov(2)
est_coef = est_gmm.get_coef()
print est_mean1
print est_cov1
print est_mean2
print est_cov2
print est_mean3
print est_cov3