textdatareader.go 3.5 KB

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