-
Notifications
You must be signed in to change notification settings - Fork 1
/
validate.go
81 lines (76 loc) · 1.67 KB
/
validate.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
package main
import (
"fmt"
"os"
"strconv"
)
func doTest(m Executor) {
testData, err := ReadCSV(*testdatapath, 0)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
resultFP, err := os.OpenFile("/home/kjs/testing/example_data/result.csv",
os.O_WRONLY|os.O_CREATE|os.O_TRUNC, os.FileMode(0644))
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
defer resultFP.Close()
resultFP.WriteString(fmt.Sprintln("ImageId,Label"))
for _, input := range testData.Input {
prediction := maxValIDX(m.Execute(input))
resultFP.WriteString(strconv.Itoa(prediction) + "\n")
}
}
func doValidation(m Executor) {
right := 0
wrong := 0
validationData, err := ReadCSV(*validatedatapath, *outputs)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
for idx, input := range validationData.Input {
prediction := maxValIDX(m.Execute(input))
actual := maxValIDX(validationData.Output[idx])
switch prediction {
case actual:
right++
default:
wrong++
}
}
fmt.Printf("%.4f%% out of sample accuracy\n", 100.0*float64(right)/float64(right+wrong))
if false {
right = 0
wrong = 0
sampleData, err := ReadCSV(*traindatapath, *outputs)
if err != nil {
fmt.Printf("%s\n", err)
os.Exit(1)
}
for idx, input := range sampleData.Input {
prediction := maxValIDX(m.Execute(input))
actual := maxValIDX(sampleData.Output[idx])
switch prediction {
case actual:
right++
default:
wrong++
}
}
fmt.Printf("%.4f%% in sample accuracy\n", 100.0*float64(right)/float64(right+wrong))
}
}
func maxValIDX(in []float64) int {
maxval := 0.0
maxidx := 0
for idx, val := range in {
if val > maxval {
maxval = val
maxidx = idx
}
}
return maxidx
}