-
Notifications
You must be signed in to change notification settings - Fork 0
/
samples.go
68 lines (56 loc) · 1.42 KB
/
samples.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package feedforward
import (
"bufio"
"os"
"strconv"
"strings"
)
// Type which models a standard floating point sample,
// consisting of any number of floating point inputs and any number of floating point outputs
type Sample struct {
Input []float64
Output []float64
}
type Delimiters struct {
InputValues string
InputOutput string
OutputValues string
}
// Function for loading samples from a text file into a slice
func Load(path string, delimiters Delimiters) ([]Sample, error) {
var samples []Sample
file, err := os.Open(path)
if err != nil {
panic(err)
}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
if err := scanner.Err(); err != nil {
return nil, err
}
rawSample := strings.Split(scanner.Text(), delimiters.InputOutput)
rawInput := strings.Split(rawSample[0], delimiters.InputValues)
rawOutput := strings.Split(rawSample[1], delimiters.OutputValues)
input := make([]float64, len(rawInput))
output := make([]float64, len(rawOutput))
for i := 0; i < len(input); i++ {
val, err := strconv.ParseFloat(rawInput[i], 64)
if err != nil {
return nil, err
}
input[i] = val
}
for i := 0; i < len(output); i++ {
val, err := strconv.ParseFloat(rawOutput[i], 64)
if err != nil {
return nil, err
}
output[i] = val
}
samples = append(samples, Sample{Input: input, Output: output})
}
if err := file.Close(); err != nil {
return nil, err
}
return samples, nil
}