textdatareader.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /*
  2. * MIT License
  3. *
  4. * Copyright (c) 2019 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 training
  26. import (
  27. "bufio"
  28. "fmt"
  29. "log"
  30. "math/rand"
  31. "os"
  32. "strconv"
  33. "strings"
  34. "sync"
  35. "time"
  36. mat "gonum.org/v1/gonum/mat"
  37. )
  38. type TextDataReader struct {
  39. dataSet []*mat.Dense
  40. result []*mat.Dense
  41. dataCount int
  42. mutex *sync.Mutex
  43. }
  44. func NewTextDataReader(filename string, validationPart int) *TextDataReader {
  45. r := &TextDataReader{
  46. mutex: &sync.Mutex{},
  47. }
  48. r.readData(filename)
  49. r.dataCount = int((float64(len(r.dataSet)) * float64(100.0 - validationPart)) / 100.0)
  50. return r
  51. }
  52. func (r *TextDataReader) readData(filename string) {
  53. inputFile, err := os.Open(filename)
  54. if err != nil {
  55. log.Fatal(err)
  56. }
  57. defer inputFile.Close()
  58. scanner := bufio.NewScanner(inputFile)
  59. scanner.Split(bufio.ScanLines)
  60. var results []string
  61. var uniqueResults []string
  62. var max []float64
  63. for scanner.Scan() {
  64. dataLine := scanner.Text()
  65. data := strings.Split(dataLine, ",")
  66. dataSetSize := len(data) - 1
  67. if len(max) <= 0 {
  68. max = make([]float64, dataSetSize)
  69. }
  70. if dataSetSize != len(max) {
  71. fmt.Printf("Garbage record: %s\n", dataLine)
  72. continue
  73. }
  74. var dataRaw []float64
  75. for i := 0; i < dataSetSize; i++ {
  76. val, err := strconv.ParseFloat(data[i], 64)
  77. if err != nil {
  78. break
  79. }
  80. dataRaw = append(dataRaw, val)
  81. if max[i] < val {
  82. max[i] = val
  83. }
  84. }
  85. if len(dataRaw) < dataSetSize {
  86. fmt.Printf("Garbage record: %s\n", dataLine)
  87. continue
  88. }
  89. r.dataSet = append(r.dataSet, mat.NewDense(dataSetSize, 1, dataRaw))
  90. found := false
  91. for _, uniqueResult := range uniqueResults {
  92. if uniqueResult == data[dataSetSize] {
  93. found = true
  94. break
  95. }
  96. }
  97. if !found {
  98. uniqueResults = append(uniqueResults, data[dataSetSize])
  99. }
  100. results = append(results, data[dataSetSize])
  101. }
  102. for i, result := range results {
  103. k := 0
  104. for k, _ = range uniqueResults {
  105. if uniqueResults[k] == result {
  106. break
  107. }
  108. }
  109. r.result = append(r.result, mat.NewDense(len(uniqueResults), 1, nil))
  110. r.result[i].Set(k, 0, 1.0)
  111. }
  112. //normalize
  113. for i := 0; i < len(r.dataSet); i++ {
  114. r.dataSet[i].Apply(func(r, _ int, val float64) float64 {
  115. return val / max[r]
  116. }, r.dataSet[i])
  117. }
  118. rand.Seed(time.Now().UnixNano())
  119. for k := 0; k < 25; k++ {
  120. rand.Shuffle(len(r.dataSet), func(i, j int) {
  121. r.result[i], r.result[j] = r.result[j], r.result[i]
  122. r.dataSet[i], r.dataSet[j] = r.dataSet[j], r.dataSet[i]
  123. })
  124. }
  125. }
  126. func (r *TextDataReader) GetData(i int) (*mat.Dense, *mat.Dense) {
  127. if i >= r.dataCount {
  128. return nil, nil
  129. }
  130. return r.dataSet[i], r.result[i]
  131. }
  132. func (r *TextDataReader) DataCount() int {
  133. return r.dataCount;
  134. }
  135. func (r *TextDataReader) GetValidator(i int) (*mat.Dense, *mat.Dense) {
  136. if i >= len(r.result) - r.dataCount {
  137. return nil, nil
  138. }
  139. return r.dataSet[r.dataCount + i], r.result[r.dataCount + i]
  140. }
  141. func (r *TextDataReader) ValidatorCount() int {
  142. return len(r.result) - r.dataCount;
  143. }