neuralnetwork_test.go 1.9 KB

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