main.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "os"
  6. neuralnetwork "./neuralnetworkbase"
  7. remotecontrol "./remotecontrol"
  8. teach "./teach"
  9. )
  10. func main() {
  11. sizes := []int{13, 8, 8, 8, 8, 3}
  12. nn, _ := neuralnetwork.NewNeuralNetwork(sizes, neuralnetwork.NewRPropInitializer(neuralnetwork.RPropConfig{
  13. NuPlus: 1.2,
  14. NuMinus: 0.5,
  15. DeltaMax: 50.0,
  16. DeltaMin: 0.000001,
  17. }))
  18. rc := &remotecontrol.RemoteControl{}
  19. nn.SetStateWatcher(rc)
  20. // inFile, err := os.Open("./networkstate")
  21. // if err != nil {
  22. // log.Fatal(err)
  23. // }
  24. // defer inFile.Close()
  25. // nn.LoadState(inFile)
  26. // nn, _ := neuralnetwork.NewNeuralNetwork(sizes, neuralnetwork.NewBackPropInitializer(0.1))
  27. // for i := 0; i < nn.Count; i++ {
  28. // if i > 0 {
  29. // fmt.Printf("Weights before:\n%v\n\n", mat.Formatted(nn.Weights[i], mat.Prefix(""), mat.Excerpt(0)))
  30. // fmt.Printf("Biases before:\n%v\n\n", mat.Formatted(nn.Biases[i], mat.Prefix(""), mat.Excerpt(0)))
  31. // fmt.Printf("Z before:\n%v\n\n", mat.Formatted(nn.Z[i], mat.Prefix(""), mat.Excerpt(0)))
  32. // }
  33. // fmt.Printf("A before:\n%v\n\n", mat.Formatted(nn.A[i], mat.Prefix(""), mat.Excerpt(0)))
  34. // }
  35. go func() {
  36. // teacher := teach.NewMNISTReader("./minst.data", "./mnist.labels")
  37. teacher := teach.NewTextDataReader("wine.data", 7)
  38. nn.Teach(teacher, 1000)
  39. // for i := 0; i < nn.Count; i++ {
  40. // if i > 0 {
  41. // fmt.Printf("Weights after:\n%v\n\n", mat.Formatted(nn.Weights[i], mat.Prefix(""), mat.Excerpt(0)))
  42. // fmt.Printf("Biases after:\n%v\n\n", mat.Formatted(nn.Biases[i], mat.Prefix(""), mat.Excerpt(0)))
  43. // fmt.Printf("Z after:\n%v\n\n", mat.Formatted(nn.Z[i], mat.Prefix(""), mat.Excerpt(0)))
  44. // }
  45. // fmt.Printf("A after:\n%v\n\n", mat.Formatted(nn.A[i], mat.Prefix(""), mat.Excerpt(0)))
  46. // }
  47. outFile, err := os.OpenFile("./data", os.O_CREATE|os.O_TRUNC|os.O_WRONLY, 0666)
  48. if err != nil {
  49. log.Fatal(err)
  50. }
  51. defer outFile.Close()
  52. nn.SaveState(outFile)
  53. outFile.Close()
  54. failCount := 0
  55. teacher.Reset()
  56. for teacher.NextValidator() {
  57. dataSet, expect := teacher.GetValidator()
  58. index, _ := nn.Predict(dataSet)
  59. if expect.At(index, 0) != 1.0 {
  60. failCount++
  61. // fmt.Printf("Fail: %v, %v\n\n", teacher.ValidationIndex(), expect.At(index, 0))
  62. }
  63. }
  64. fmt.Printf("Fail count: %v\n\n", failCount)
  65. }()
  66. // nn = &neuralnetwork.NeuralNetwork{}
  67. // inFile, err := os.Open("./data")
  68. // if err != nil {
  69. // log.Fatal(err)
  70. // }
  71. // defer inFile.Close()
  72. // nn.LoadState(inFile)
  73. // inFile.Close()
  74. // failCount = 0
  75. // teacher.Reset()
  76. // for teacher.NextValidator() {
  77. // dataSet, expect := teacher.GetValidator()
  78. // index, _ := nn.Predict(dataSet)
  79. // if expect.At(index, 0) != 1.0 {
  80. // failCount++
  81. // // fmt.Printf("Fail: %v, %v\n\n", teacher.ValidationIndex(), expect.At(index, 0))
  82. // }
  83. // }
  84. // fmt.Printf("Fail count: %v\n\n", failCount)
  85. rc.Run()
  86. }