neuralnetwork_test.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. package neuralnetwork
  2. import (
  3. "testing"
  4. "gonum.org/v1/gonum/mat"
  5. )
  6. func TestNewNeuralNetwork(t *testing.T) {
  7. nn, err := NewNeuralNetwork([]int{}, nil)
  8. if nn != nil || err == nil {
  9. t.Error("nn initialized, but shouldn't ", err)
  10. }
  11. nn, err = NewNeuralNetwork([]int{0, 0, 0, 0}, nil)
  12. if nn != nil || err == nil {
  13. t.Error("nn initialized, but shouldn't ", err)
  14. }
  15. nn, err = NewNeuralNetwork([]int{1, 1, 1, 1}, nil)
  16. if nn != nil || err == nil {
  17. t.Error("nn initialized, but shouldn't ", err)
  18. }
  19. nn, err = NewNeuralNetwork([]int{5, 5}, nil)
  20. if nn != nil || err == nil {
  21. t.Error("nn initialized, but shouldn't ", err)
  22. }
  23. nn, err = NewNeuralNetwork([]int{5, 1, 5, 5}, nil)
  24. if nn != nil || err == nil {
  25. t.Error("nn initialized, but shouldn't ", err)
  26. }
  27. nn, err = NewNeuralNetwork([]int{5, 4, 4, 5}, nil)
  28. if nn == nil || err != nil {
  29. t.Error("nn is not initialized, but should be ", err)
  30. }
  31. }
  32. func TestNeuralNetworkPredict(t *testing.T) {
  33. nn, _ := NewNeuralNetwork([]int{3, 4, 4, 2}, nil)
  34. aIn := &mat.Dense{}
  35. index, max := nn.Predict(aIn)
  36. if index != -1 || max != 0.0 {
  37. t.Error("Prediction when empty aIn shouldn't be possibe but predicted", index, max)
  38. }
  39. aIn = mat.NewDense(2, 1, []float64{0.1, 0.2})
  40. index, max = nn.Predict(aIn)
  41. if index != -1 || max != 0.0 {
  42. t.Error("Prediction aIn has invalid size shouldn't be possibe but predicted", index, max)
  43. }
  44. aIn = mat.NewDense(3, 1, []float64{0.1, 0.2, 0.3})
  45. index, max = nn.Predict(aIn)
  46. if index == -1 || max == 0.0 {
  47. t.Error("Prediction of aIn valid size should be predicted", index, max)
  48. }
  49. aIn = mat.NewDense(4, 1, []float64{0.1, 0.2, 0.3, 0.4})
  50. index, max = nn.Predict(aIn)
  51. if index != -1 || max != 0.0 {
  52. t.Error("Prediction aIn has invalid size shouldn't be possibe but predicted", index, max)
  53. }
  54. }