Exemplo n.º 1
0
func TestLinearTanh(t *testing.T) {
	s := LinearTanh{}
	sum := 1.23456789
	// const gotten from wolfram alpha
	// http://www.wolframalpha.com/input/?i=1.7159+*+tanh%282%2F3+*+1.23456789%29+%2B+0.01+*+1.23456789
	// http://www.wolframalpha.com/input/?i=1.14393333333333333333333333333333333333333333333333333333333333333333+*+sech%5E2%28%282+*+1.23456789%29%2F3%29+%2B+0.01
	const trueOut = 1.1735362969541956173946145965239159139732343741372935
	const trueDeriv = 0.6300630023283855667915281343653916910151758055270571
	output := s.Activate(sum)
	if math.Abs(output-float64(trueOut)) > 1E-15 {
		t.Errorf("Activation output does not match. %v expected, %v found", trueOut, output)
	}
	deriv := s.DActivateDCombination(sum, output)
	if math.Abs(deriv-float64(trueDeriv)) > 1E-15 {
		t.Errorf("Derivative does not match. %v expected, %v found", trueDeriv, deriv)
	}

	err := common.InterfaceTestMarshalAndUnmarshal(s)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling")
	}

	str := s.String()
	if str != "LinearTanh" {
		t.Error("String doesn't match")
	}
}
Exemplo n.º 2
0
func TestLinear(t *testing.T) {
	s := Linear{}
	sum := 1.23456789
	trueOut := sum
	trueDeriv := 1.0
	output := s.Activate(sum)
	if math.Abs(output-trueOut) > 1E-15 {
		t.Errorf("Activation output does not match. %v expected, %v found", trueOut, output)
	}
	deriv := s.DActivateDCombination(sum, output)
	if math.Abs(deriv-trueDeriv) > 1E-15 {
		t.Errorf("Derivative does not match. %v expected, %v found", trueDeriv, deriv)
	}

	err := common.InterfaceTestMarshalAndUnmarshal(s)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling")
	}

	str := s.String()
	if str != "Linear" {
		t.Error("String doesn't match")
	}

}
Exemplo n.º 3
0
func TestSigmoid(t *testing.T) {
	s := Sigmoid{}
	sum := 1.23456789
	// const gotten from wolfram alpha
	// http://www.wolframalpha.com/input/?i=1%2F%281%2B+exp%28-1.23456789%29%29
	// http://www.wolframalpha.com/input/?i=e%5E1.23456789%2F%281%2Be%5E1.23456789%29%5E2
	const trueOut = 0.7746170617399426534330751940207841531562387807774740
	const trueDeriv = 0.17458546940132052486381952968243494067770801388762
	output := s.Activate(sum)
	if math.Abs(output-float64(trueOut)) > 1E-15 {
		t.Errorf("Activation output does not match. %v expected, %v found", trueOut, output)
	}
	deriv := s.DActivateDCombination(sum, output)
	if math.Abs(deriv-float64(trueDeriv)) > 1E-15 {
		t.Errorf("Derivative does not match. %v expected, %v found", trueDeriv, deriv)
	}

	err := common.InterfaceTestMarshalAndUnmarshal(s)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling")
	}

	str := s.String()
	if str != "Sigmoid" {
		t.Error("String doesn't match")
	}
}
Exemplo n.º 4
0
func TestLogSquared(t *testing.T) {
	prediction := []float64{1, -2, 3}
	truth := []float64{1.1, -2.2, 2.7}
	trueloss := (math.Log(.1*.1+1) + math.Log(.2*.2+1) + math.Log(.3*.3+1)) / 3
	derivative := []float64{0, 0, 0}

	sq := LogSquared{}
	loss := sq.Loss(prediction, truth)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from Loss(). Expected %v, Found: %v", trueloss, loss)
	}

	loss = sq.LossDeriv(prediction, truth, derivative)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from LossDeriv()")
	}
	derivative, fdDerivative := finiteDifferenceLosser(sq, prediction, truth)
	if !floats.EqualApprox(derivative, fdDerivative, FDTol) {
		t.Errorf("Derivative doesn't match. \n deriv: %v \n fdDeriv: %v ", derivative, fdDerivative)
	}
	err := common.InterfaceTestMarshalAndUnmarshal(sq)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling")
	}
}
Exemplo n.º 5
0
func TestRelativeLog(t *testing.T) {
	tol := 1e-2
	prediction := []float64{1, -2, 3}
	truth := []float64{1.1, -2.2, 2.7}
	trueloss := ((.1/(1.1+tol))*(.1/(1.1+tol)) + (.2/(2.2+tol))*(.2/(2.2+tol)) + (.3/(2.7+tol))*(.3/(2.7+tol))) / 3
	trueloss = math.Log(trueloss + 1)
	derivative := []float64{0, 0, 0}

	sq := RelativeLog(tol)
	loss := sq.Loss(prediction, truth)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from Loss(). Expected %v, Found: %v", trueloss, loss)
	}

	loss = sq.LossDeriv(prediction, truth, derivative)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from LossDeriv()")
	}
	derivative, fdDerivative := finiteDifferenceLosser(sq, prediction, truth)
	if !floats.EqualApprox(derivative, fdDerivative, FDTol) {
		t.Errorf("Derivative doesn't match. \n deriv: %v \n fdDeriv: %v ", derivative, fdDerivative)
	}

	err := common.InterfaceTestMarshalAndUnmarshal(sq)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling: " + err.Error())
	}
}
Exemplo n.º 6
0
func TestManhattanDistance(t *testing.T) {
	prediction := []float64{1, 2, 3}
	truth := []float64{1.1, 2.2, 2.7}
	trueloss := (.1 + .2 + .3) / 3
	derivative := []float64{0, 0, 0}

	sq := ManhattanDistance{}
	loss := sq.Loss(prediction, truth)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from Loss()")
	}

	loss = sq.LossDeriv(prediction, truth, derivative)
	if math.Abs(loss-trueloss) > TOL {
		t.Errorf("loss doesn't match from LossDeriv()")
	}
	derivative, fdDerivative := finiteDifferenceLosser(sq, prediction, truth)
	if !floats.EqualApprox(derivative, fdDerivative, FDTol) {
		t.Errorf("Derivative doesn't match. \n deriv: %v \n fdDeriv: %v ", derivative, fdDerivative)
	}

	err := common.InterfaceTestMarshalAndUnmarshal(sq)
	if err != nil {
		t.Errorf("Error marshaling and unmarshaling")
	}

	truth = []float64{1, 2, 3}
	loss = sq.LossDeriv(prediction, truth, derivative)
	if loss != 0 {
		t.Errorf("Non-zero loss for equal pred and truth")
	}
	for _, val := range derivative {
		if val != 0 {
			t.Errorf("Non-zero derivative for equal pred and truth")
		}
	}
}