コード例 #1
0
ファイル: generator.py プロジェクト: Saurabh7/shogun-demo
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))
コード例 #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))
コード例 #3
0
def gen_data(ftype, num_samples, show_data = False):
	from modshogun import Math
	from modshogun import FactorType, Factor, TableFactorType, FactorGraph
	from modshogun import FactorGraphObservation, FactorGraphLabels, FactorGraphFeatures
	from modshogun import MAPInference, TREE_MAX_PROD

	Math.init_random(17)

	samples = FactorGraphFeatures(num_samples)
	labels = FactorGraphLabels(num_samples)

	for i in xrange(num_samples):
		vc = np.array([2,2,2], np.int32)
		fg = FactorGraph(vc)

		data1 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind1 = np.array([0,1], np.int32)
		fac1 = Factor(ftype[0], vind1, data1)
		fg.add_factor(fac1)

		data2 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind2 = np.array([1,2], np.int32)
		fac2 = Factor(ftype[0], vind2, data2)
		fg.add_factor(fac2)

		data3 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind3 = np.array([0], np.int32)
		fac3 = Factor(ftype[1], vind3, data3)
		fg.add_factor(fac3)

		data4 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind4 = np.array([1], np.int32)
		fac4 = Factor(ftype[1], vind4, data4)
		fg.add_factor(fac4)

		data5 = np.array([2.0*Math.random(0.0,1.0)-1.0 for i in xrange(2)])
		vind5 = np.array([2], np.int32)
		fac5 = Factor(ftype[1], vind5, data5)
		fg.add_factor(fac5)

		data6 = np.array([1.0])
		vind6 = np.array([0], np.int32)
		fac6 = Factor(ftype[2], vind6, data6)
		fg.add_factor(fac6)

		data7 = np.array([1.0])
		vind7 = np.array([2], np.int32)
		fac7 = Factor(ftype[2], vind7, data7)
		fg.add_factor(fac7)

		samples.add_sample(fg)
		fg.connect_components()
		fg.compute_energies()

		infer_met = MAPInference(fg, TREE_MAX_PROD)
		infer_met.inference()

		fg_obs = infer_met.get_structured_outputs()
		labels.add_label(fg_obs)

		if show_data:
			state = fg_obs.get_data()
			print state

	return samples, labels
コード例 #4
0
def gen_data(ftype, num_samples, show_data=False):
    from modshogun import Math
    from modshogun import FactorType, Factor, TableFactorType, FactorGraph
    from modshogun import FactorGraphObservation, FactorGraphLabels, FactorGraphFeatures
    from modshogun import MAPInference, TREE_MAX_PROD

    Math.init_random(17)

    samples = FactorGraphFeatures(num_samples)
    labels = FactorGraphLabels(num_samples)

    for i in range(num_samples):
        vc = np.array([2, 2, 2], np.int32)
        fg = FactorGraph(vc)

        data1 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind1 = np.array([0, 1], np.int32)
        fac1 = Factor(ftype[0], vind1, data1)
        fg.add_factor(fac1)

        data2 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind2 = np.array([1, 2], np.int32)
        fac2 = Factor(ftype[0], vind2, data2)
        fg.add_factor(fac2)

        data3 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind3 = np.array([0], np.int32)
        fac3 = Factor(ftype[1], vind3, data3)
        fg.add_factor(fac3)

        data4 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind4 = np.array([1], np.int32)
        fac4 = Factor(ftype[1], vind4, data4)
        fg.add_factor(fac4)

        data5 = np.array([2.0 * Math.random(0.0, 1.0) - 1.0 for i in range(2)])
        vind5 = np.array([2], np.int32)
        fac5 = Factor(ftype[1], vind5, data5)
        fg.add_factor(fac5)

        data6 = np.array([1.0])
        vind6 = np.array([0], np.int32)
        fac6 = Factor(ftype[2], vind6, data6)
        fg.add_factor(fac6)

        data7 = np.array([1.0])
        vind7 = np.array([2], np.int32)
        fac7 = Factor(ftype[2], vind7, data7)
        fg.add_factor(fac7)

        samples.add_sample(fg)
        fg.connect_components()
        fg.compute_energies()

        infer_met = MAPInference(fg, TREE_MAX_PROD)
        infer_met.inference()

        fg_obs = infer_met.get_structured_outputs()
        labels.add_label(fg_obs)

        if show_data:
            state = fg_obs.get_data()
            print(state)

    return samples, labels