simpledescentearlystop.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2020 Alexey Edelev <semlanik@gmail.com>
  5. *
  6. * This file is part of NeuralNetwork project https://git.semlanik.org/semlanik/NeuralNetwork
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy of this
  9. * software and associated documentation files (the "Software"), to deal in the Software
  10. * without restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software, and
  12. * to permit persons to whom the Software is furnished to do so, subject to the following
  13. * conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all copies
  16. * or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  19. * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  20. * PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  21. * FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  22. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  23. * DEALINGS IN THE SOFTWARE.
  24. */
  25. package earlystop
  26. import (
  27. "log"
  28. "math"
  29. "os"
  30. neuralnetwork "git.semlanik.org/semlanik/NeuralNetwork/neuralnetwork"
  31. training "git.semlanik.org/semlanik/NeuralNetwork/training"
  32. )
  33. const tmpFileName = "./.simpleDescentEarlyStop.nnd.tmp"
  34. type simpleDescentEarlyStop struct {
  35. lastFailRate float64
  36. bestFailRate float64
  37. failRateDeltaSum float64
  38. network *neuralnetwork.NeuralNetwork
  39. trainer training.Trainer
  40. glGrowCount int
  41. }
  42. func NewSimpleDescentEarlyStop(network *neuralnetwork.NeuralNetwork, trainer training.Trainer) (es *simpleDescentEarlyStop) {
  43. es = nil
  44. if network == nil || trainer == nil {
  45. return
  46. }
  47. es = &simpleDescentEarlyStop{
  48. lastFailRate: math.MaxFloat64,
  49. bestFailRate: math.MaxFloat64,
  50. failRateDeltaSum: 0.0,
  51. network: network,
  52. trainer: trainer,
  53. glGrowCount: 0,
  54. }
  55. return
  56. }
  57. func (es *simpleDescentEarlyStop) Test() bool {
  58. squareError, fails, total := es.network.Validate(es.trainer)
  59. es.lastFailRate = squareError / float64(total)
  60. log.Printf("Fail count: %v/%v, lastFailRate: %v\n", fails, total, es.lastFailRate)
  61. generalizationLoss := (es.lastFailRate/es.bestFailRate - 1.0)
  62. if es.bestFailRate > es.lastFailRate {
  63. es.bestFailRate = es.lastFailRate
  64. es.network.SaveStateToFile(tmpFileName)
  65. }
  66. if generalizationLoss > 0.0 {
  67. es.glGrowCount++
  68. } else {
  69. es.glGrowCount = 0
  70. }
  71. if es.glGrowCount > 5 {
  72. es.network.LoadStateFromFile(tmpFileName)
  73. os.Remove(tmpFileName)
  74. return true
  75. }
  76. return false
  77. }
  78. func (es *simpleDescentEarlyStop) Reset() {
  79. es.lastFailRate = math.MaxFloat64
  80. es.bestFailRate = math.MaxFloat64
  81. es.glGrowCount = 0
  82. es.failRateDeltaSum = 0.0
  83. }