Ejemplo n.º 1
0
func main() {
	rand.Seed(time.Now().UnixNano())

	positives := []svm.Sample{
		{V: []float64{0.5, 0}},
		{V: []float64{0.4, 0.1}},
		{V: []float64{0.3, 0.2}},
		{V: []float64{0.5, 0.9}},
		{V: []float64{0.6, 1}},
		{V: []float64{0.4, 0.85}},
		{V: []float64{0.5, 0.5}},
	}

	negatives := []svm.Sample{
		{V: []float64{0, 0.5}},
		{V: []float64{0.1, 0.4}},
		{V: []float64{0.9, 0.5}},
		{V: []float64{1, 0.6}},
		{V: []float64{0, 0.46}},
	}

	problem := &svm.Problem{
		Positives: positives,
		Negatives: negatives,
		Kernel:    svm.PolynomialKernel(1, 2),
	}

	printProblem(problem)

	printSolution("random linear", svm.RandomlySolveLinear(problem, 100000, 5), problem)

	subgradientSolver := &svm.SubgradientSolver{
		Tradeoff: 0.001,
		StepSize: 0.1,
		Steps:    100000,
	}
	printSolution("subgradient descent", subgradientSolver.Solve(problem), problem)

	gradientSolver := &svm.GradientDescentSolver{
		Tradeoff: 0.001,
		Timeout:  time.Minute,
	}
	printSolution("gradient descent", gradientSolver.Solve(problem), problem)
}
Ejemplo n.º 2
0
func main() {
	rand.Seed(time.Now().UnixNano())

	positives := []svm.Sample{
		svm.Sample{V: []float64{0, 0}},
		svm.Sample{V: []float64{0, 0.3}},
		svm.Sample{V: []float64{0.3, 0}},
		svm.Sample{V: []float64{0.1, 0.1}},
	}

	negatives := []svm.Sample{
		svm.Sample{V: []float64{1, 1}},
		svm.Sample{V: []float64{1, 0.7}},
		svm.Sample{V: []float64{0.7, 1}},
		svm.Sample{V: []float64{0.9, 0.9}},
	}

	problem := &svm.Problem{
		Positives: positives,
		Negatives: negatives,
		Kernel:    svm.LinearKernel,
	}

	solution := svm.RandomlySolveLinear(problem, 100000, 5)
	fmt.Println("Solution from random solver:", solution)

	subgradientSolver := &svm.SubgradientSolver{
		Tradeoff: 0.001,
		Steps:    10000,
		StepSize: 0.001,
	}
	solution = subgradientSolver.Solve(problem)

	fmt.Println("Solution from subgradient solver:", solution)

	gradientSolver := &svm.GradientDescentSolver{
		Tradeoff: 0.0001,
		Timeout:  time.Minute,
	}
	solution = gradientSolver.Solve(problem).Linearize()

	fmt.Println("Solution from gradient descent solver:", solution)
}