コード例 #1
0
ファイル: gaussian_test.go プロジェクト: henrylee2cn/gjoa
// Train using sampler.
func BenchmarkTrain2(b *testing.B) {

	dim := 8
	numSamp := 2000000

	// Use a FloatObserver.
	fs, err := model.NewFloatObserver(make([][]float64, numSamp, numSamp),
		make([]model.SimpleLabel, numSamp, numSamp))
	if err != nil {
		b.Fatal(err)
	}
	mean := []float64{0.1, 0.2, 0.3, 0.4, 1, 1, 1, 1}
	std := []float64{0.5, 0.5, 0.5, 0.5, 0.1, 0.2, 0.3, 0.4}
	g := NewModel(dim, Name("test training"))

	r := rand.New(rand.NewSource(33))
	for i := 0; i < numSamp; i++ {
		rv := model.RandNormalVector(r, mean, std)
		fs.Values[i] = rv
	}
	for i := 0; i < b.N; i++ {
		g.Update(fs, model.NoWeight)
		g.Estimate()
		g.Clear()
	}
}
コード例 #2
0
ファイル: gaussian_test.go プロジェクト: henrylee2cn/gjoa
func TestTrainGaussian2(t *testing.T) {

	if testing.Short() {
		t.Skip("skipping test in short mode.")
	}
	dim := 8
	numSamp := 2000000

	// Use a FloatObserver.
	values := make([][]float64, numSamp, numSamp)
	labels := make([]model.SimpleLabel, numSamp, numSamp)
	mean := []float64{0.1, 0.2, 0.3, 0.4, 1, 1, 1, 1}
	std := []float64{0.5, 0.5, 0.5, 0.5, 0.1, 0.2, 0.3, 0.4}
	g := NewModel(dim, Name("test training"))

	r := rand.New(rand.NewSource(33))
	for i := 0; i < numSamp; i++ {
		rv := model.RandNormalVector(r, mean, std)
		values[i] = rv
	}

	fo, err := model.NewFloatObserver(values, labels)
	if err != nil {
		t.Fatal(err)
	}

	g.Update(fo, model.NoWeight)
	g.Estimate()
	t.Logf("Mean: \n%+v", g.Mean)
	t.Logf("STD: \n%+v", g.StdDev)

	for i, _ := range mean {
		if !gjoa.Comparef64(mean[i], g.Mean[i], tolerance) {
			t.Errorf("Wrong Mean[%d]. Expected: [%f], Got: [%f]",
				i, mean[i], g.Mean[i])
		}
		if !gjoa.Comparef64(std[i], g.StdDev[i], tolerance) {
			t.Errorf("Wrong STD[%d]. Expected: [%f], Got: [%f]",
				i, std[i], g.StdDev[i])
		}
	}
}