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
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
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()
def generate_gmm_classification_data(request): from modshogun import GMM, Math num_classes = int(request.POST['num_classes']) gmm = GMM(num_classes) total = 40 rng = 4.0 num = total/num_classes for i in xrange(num_classes): gmm.set_nth_mean(np.array([Math.random(-rng, rng) for j in xrange(2)]), i) cov_tmp = Math.normal_random(0.2, 0.1) cov = np.array([[1.0, cov_tmp], [cov_tmp, 1.0]], dtype=float) gmm.set_nth_cov(cov, i) data=[] labels=[] for i in xrange(num_classes): coef = np.zeros(num_classes) coef[i] = 1.0 gmm.set_coef(coef) data.append(np.array([gmm.sample() for j in xrange(num)]).T) labels.append(np.array([i for j in xrange(num)])) data = np.hstack(data) data = data / (2.0 * rng) xmin = np.min(data[0,:]) ymin = np.min(data[1,:]) labels = np.hstack(labels) toy_data = [] for i in xrange(num_classes*num): toy_data.append( { 'x': data[0, i] - xmin, 'y': data[1, i] - ymin, 'label': float(labels[i])}) return HttpResponse(json.dumps(toy_data))
def generate_gmm_classification_data(request): from modshogun import GMM, Math num_classes = int(request.POST['num_classes']) gmm = GMM(num_classes) total = 40 rng = 4.0 num = total / num_classes for i in xrange(num_classes): gmm.set_nth_mean(np.array([Math.random(-rng, rng) for j in xrange(2)]), i) cov_tmp = Math.normal_random(0.2, 0.1) cov = np.array([[1.0, cov_tmp], [cov_tmp, 1.0]], dtype=float) gmm.set_nth_cov(cov, i) data = [] labels = [] for i in xrange(num_classes): coef = np.zeros(num_classes) coef[i] = 1.0 gmm.set_coef(coef) data.append(np.array([gmm.sample() for j in xrange(num)]).T) labels.append(np.array([i for j in xrange(num)])) data = np.hstack(data) data = data / (2.0 * rng) xmin = np.min(data[0, :]) ymin = np.min(data[1, :]) labels = np.hstack(labels) toy_data = [] for i in xrange(num_classes * num): toy_data.append({ 'x': data[0, i] - xmin, 'y': data[1, i] - ymin, 'label': float(labels[i]) }) return HttpResponse(json.dumps(toy_data))
from pylab import figure,show,connect,hist,plot,legend from numpy import array, append, arange, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('EM for 1d GMM example') #set the parameters min_cov=1e-9 max_iter=1000 min_change=1e-9 #setup the real GMM real_gmm=GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 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)
from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('SMEM for 2d GMM example') #set the parameters max_iter = 100 max_cand = 5 min_cov = 1e-9 max_em_iter = 1000 min_change = 1e-9 cov_type = 0 #setup the real GMM real_gmm = GMM(3) real_gmm.set_nth_mean(array([2.0, 2.0]), 0) real_gmm.set_nth_mean(array([-2.0, -2.0]), 1) real_gmm.set_nth_mean(array([2.0, -2.0]), 2) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.5]]), 0) real_gmm.set_nth_cov(array([[0.2, 0.1], [0.1, 0.5]]), 1) real_gmm.set_nth_cov(array([[0.3, -0.2], [-0.2, 0.8]]), 2) real_gmm.set_coef(array([0.3, 0.4, 0.3])) #generate training set from real GMM generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0)
from numpy import array, append, arange, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('SMEM for 1d GMM example') #set the parameters max_iter = 100 max_cand = 5 min_cov = 1e-9 max_em_iter = 1000 min_change = 1e-9 #setup the real GMM real_gmm = GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 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)
from pylab import figure,scatter,contour,show,legend,connect from numpy import array, append, arange, reshape, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('EM for 2d GMM example') #set the parameters min_cov=1e-9 max_iter=1000 min_change=1e-9 cov_type=0 #setup the real GMM real_gmm=GMM(2) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2],[0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1],[0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) #generate training set from real GMM generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=0) generated=generated.transpose()
from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('SMEM for 2d GMM example') #set the parameters max_iter=100 max_cand=5 min_cov=1e-9 max_em_iter=1000 min_change=1e-9 cov_type=0 #setup the real GMM real_gmm=GMM(3) real_gmm.set_nth_mean(array([2.0, 2.0]), 0) real_gmm.set_nth_mean(array([-2.0, -2.0]), 1) real_gmm.set_nth_mean(array([2.0, -2.0]), 2) real_gmm.set_nth_cov(array([[1.0, 0.2],[0.2, 0.5]]), 0) real_gmm.set_nth_cov(array([[0.2, 0.1],[0.1, 0.5]]), 1) real_gmm.set_nth_cov(array([[0.3, -0.2],[-0.2, 0.8]]), 2) real_gmm.set_coef(array([0.3, 0.4, 0.3])) #generate training set from real GMM generated=array([real_gmm.sample()]) for i in range(199): generated=append(generated, array([real_gmm.sample()]), axis=0)
from numpy import array, append, arange, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('SMEM for 1d GMM example') #set the parameters max_iter=100 max_cand=5 min_cov=1e-9 max_em_iter=1000 min_change=1e-9 #setup the real GMM real_gmm=GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 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)
from pylab import figure, show, connect, hist, plot, legend from numpy import array, append, arange, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('EM for 1d GMM example') #set the parameters min_cov = 1e-9 max_iter = 1000 min_change = 1e-9 #setup the real GMM real_gmm = GMM(3) real_gmm.set_nth_mean(array([-2.0]), 0) real_gmm.set_nth_mean(array([0.0]), 1) real_gmm.set_nth_mean(array([2.0]), 2) real_gmm.set_nth_cov(array([[0.3]]), 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)
from pylab import figure, scatter, contour, show, legend, connect from numpy import array, append, arange, reshape, empty, exp from modshogun import Gaussian, GMM from modshogun import RealFeatures import util util.set_title('EM for 2d GMM example') #set the parameters min_cov = 1e-9 max_iter = 1000 min_change = 1e-9 cov_type = 0 #setup the real GMM real_gmm = GMM(2) real_gmm.set_nth_mean(array([1.0, 1.0]), 0) real_gmm.set_nth_mean(array([-1.0, -1.0]), 1) real_gmm.set_nth_cov(array([[1.0, 0.2], [0.2, 0.1]]), 0) real_gmm.set_nth_cov(array([[0.3, 0.1], [0.1, 1.0]]), 1) real_gmm.set_coef(array([0.3, 0.7])) #generate training set from real GMM generated = array([real_gmm.sample()]) for i in range(199): generated = append(generated, array([real_gmm.sample()]), axis=0) generated = generated.transpose()