Exemple #1
0
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))
Exemple #2
0
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))
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)

feat_train=RealFeatures(generated)

#train GMM using EM
est_gmm=GMM(3)
est_gmm.train(feat_train)
Exemple #4
0
#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)

generated = generated.transpose()
feat_train = RealFeatures(generated)

#train GMM using SMEM and print log-likelihood
est_smem_gmm = GMM(3, cov_type)
Exemple #5
0
#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)

feat_train = RealFeatures(generated)

#train GMM using SMEM and print log-likelihood
est_smem_gmm = GMM(3)
est_smem_gmm.train(feat_train)
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()
feat_train=RealFeatures(generated)

#train GMM using EM
est_gmm=GMM(2, cov_type)
est_gmm.train(feat_train)
#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)

generated=generated.transpose()
feat_train=RealFeatures(generated)

#train GMM using SMEM and print log-likelihood
est_smem_gmm=GMM(3, cov_type)
Exemple #8
0
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()
feat_train = RealFeatures(generated)

#train GMM using EM
est_gmm = GMM(2, cov_type)
est_gmm.train(feat_train)