-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
90 lines (80 loc) · 1.68 KB
/
main.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
package main
import (
"bufio"
"encoding/csv"
"fmt"
"io"
"math/rand"
"os"
"strconv"
"time"
)
func main() {
return
}
func mnistTrain(net *Network) {
rand.Seed(time.Now().UTC().UnixNano())
t1 := time.Now()
for epochs := 0; epochs < 5; epochs++ {
testFile, _ := os.Open("mnist_dataset/mnist_train.csv")
r := csv.NewReader(bufio.NewReader(testFile))
for {
record, err := r.Read()
if err == io.EOF {
break
}
inputs := make([]float64, net.inputs)
for i := range inputs {
x, _ := strconv.ParseFloat(record[i], 64)
inputs[i] = (x / 255.0 * 0.99) + 0.01
}
targets := make([]float64, 10)
for i := range targets {
targets[i] = 0.01
}
x, _ := strconv.Atoi(record[0])
targets[x] = 0.99
net.Train(inputs, targets)
}
testFile.Close()
}
elapsed := time.Since(t1)
fmt.Printf("\nTime taken to train %s\n", elapsed)
}
func mnistPredict(net *Network) {
t1 := time.Now()
checkFile, _ := os.Open("mnist_dataset/mnist_test.csv")
defer checkFile.Close()
score := 0
r := csv.NewReader(bufio.NewReader(checkFile))
for {
record, err := r.Read()
if err == io.EOF {
break
}
inputs := make([]float64, net.inputs)
for i := range inputs {
if i == 0 {
inputs[i] = 1.0
}
x, _ := strconv.ParseFloat(record[i], 64)
inputs[i] = (x / 255.0 * 0.99) + 0.01
}
outputs := net.Predict(inputs)
best := 0
highest := 0.0
for i := 0; i < net.outputs; i++ {
if outputs.At(i, 0) > highest {
best = i
highest = outputs.At(i, 0)
}
}
target, _ := strconv.Atoi(record[0])
if best == target {
score++
}
}
elapsed := time.Since(t1)
fmt.Printf("\nTime taken to check %s\n", elapsed)
fmt.Printf("score", score)
}