textdatareader.go 4.4 KB

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