-
-
Notifications
You must be signed in to change notification settings - Fork 431
/
io.go
92 lines (82 loc) · 1.55 KB
/
io.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package main
import (
"encoding/csv"
"io"
"log"
"math/rand"
"os"
"strconv"
"gorgonia.org/tensor"
)
func loadStatic() (w, x, y []float64) {
d0, err := os.Open("testdata/X_ds1.10.csv")
if err == nil {
r := csv.NewReader(d0)
r.Comma = ','
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
for _, f := range record {
fl, _ := strconv.ParseFloat(f, 64)
x = append(x, fl)
}
}
if len(x) != N*feats {
log.Fatalf("Expected %d*%d. Got %d instead", N, feats, len(x))
}
} else {
log.Println("could not read from file")
x = tensor.Random(Float, N*feats).([]float64)
}
w0, err := os.Open("testdata/W_ds1.10.csv")
if err == nil {
r := csv.NewReader(w0)
r.Comma = ' '
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fl, _ := strconv.ParseFloat(record[0], 64)
w = append(w, fl)
}
if len(w) != feats {
log.Fatalf("Expected %d rows. Got %d instead", feats, len(w))
}
} else {
w = tensor.Random(Float, feats).([]float64)
}
y0, err := os.Open("testdata/Y_ds1.10.csv")
if err == nil {
r := csv.NewReader(y0)
r.Comma = ','
for {
record, err := r.Read()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
fl, _ := strconv.ParseFloat(record[0], 64)
y = append(y, fl)
}
if len(y) != N {
log.Fatalf("Expected %d rows. Got %d instead", N, len(y))
}
} else {
y = make([]float64, N)
for i := range y {
y[i] = float64(rand.Intn(2))
}
}
return
}