func TestShouldBeCompatibleWeightPanicsForMatricsWithDifferentDimensions(t *testing.T) { test := weightUpdateTest{ old: dense.Zeros(4, 8), update: dense.Zeros(4, 10), } defer func() { if p := recover(); p != INCOMPATIBLE_WEIGHTS_PANIC { t.Fatalf("%s should be caused, but %s causes", INCOMPATIBLE_WEIGHTS_PANIC, p) } }() ShouldBeCompatibleWeights(test.old, test.update) }
func TestShouldBeCompatibleWeightSucceedsForSameShapeMatrices(t *testing.T) { test := weightUpdateTest{ old: dense.Zeros(4, 8), update: dense.Zeros(4, 8), } defer func() { if p := recover(); p != nil { t.Fatalf( "Weights validation should not cause any panic for the same shape one: %s", p, ) } }() ShouldBeCompatibleWeights(test.old, test.update) }
func TestShouldBeFeaturePanicsForNonFeature(t *testing.T) { test := dense.Zeros(4, 1) defer func() { if p := recover(); p != NON_FEATURE_MATRIX_PANIC { t.Fatalf("%s should be caused, but %s causes", NON_FEATURE_MATRIX_PANIC, p) } }() ShouldBeFeature(test) }
// Create a new classifier with the given number of classes and dimensions of features. // The generate classifier has a weight matrix with zeros. func New(classSize, dimensions int) *Classifier { validates.ShouldBeOneOrMoreClasses(classSize) c := &Classifier{ initialized: true, // TODO: Replace the weights with the immutable version of dense matrix. weights: dense.Zeros(classSize, dimensions), } return c }
func TestWeightsReturnsTheWeightsMatrix(t *testing.T) { test := constructionTest{ classSize: 4, dimensions: 8, } c := New(test.classSize, test.dimensions) zeroMatrix := dense.Zeros(test.classSize, test.dimensions) if weights := c.Weights(); !weights.Equal(zeroMatrix) { t.Fatalf("A %d x %d matrix should be returned as the weights matrix.", 4, 8) } }
func TestShouldBeFeatureSucceedsForFeature(t *testing.T) { test := dense.Zeros(1, 1) defer func() { if p := recover(); p != nil { t.Fatalf( "Feature validation should not cause any panic for feature matirx: %s", p, ) } }() ShouldBeFeature(test) }
func TestUnmarshalJSONFailsWithIncompatibleVersion(t *testing.T) { c := &classifierJson{ Version: 99999, Weights: dense.Zeros(4, 8), } d := &Classifier{} b, _ := json.Marshal(c) if err := json.Unmarshal(b, d); err == nil || err.Error() != IncompatibleVersionError { t.Fatalf("Unmarshal can be applied to compatible-version classifier.") } }
func TestClassifyPanicsByNonFeatureMatrix(t *testing.T) { classSize, dimensions := 4, 8 test := dense.Zeros(classSize, dimensions+1) defer func() { if p := recover(); p == NON_FEATURE_MATRIX_PANIC { return } t.Fatal("Update should use \"validate.ShouldBeFeature\".") }() New(classSize, dimensions).Classify(test) }
func TestUpdatePanicsByIncompatibleWeights(t *testing.T) { classSize, dimensions := 4, 8 test := dense.Zeros(classSize+1, dimensions) c := New(classSize, dimensions) defer func() { if p := recover(); p == INCOMPATIBLE_WEIGHTS_PANIC { return } t.Fatal("Update should use \"validate.ShouldBeCompatibleWeights\".") }() c.Update(test) }
func TestUpdateSucceedsForMatricesWithSameShape(t *testing.T) { classSize, dimensions := 4, 8 test := dense.Zeros(classSize, dimensions) c := New(classSize, dimensions) defer func() { if p := recover(); p == nil { return } t.Fatal("Update should not panic for the weights as same shape as the old.") }() c.Update(test) }