-
Notifications
You must be signed in to change notification settings - Fork 0
/
dppn_test.go
110 lines (90 loc) · 2.21 KB
/
dppn_test.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
package main
import (
"fmt"
"github.com/gonum/matrix/mat64"
//"image"
//"image/color"
//"image/png"
"log"
"math/rand"
//"os"
"testing"
)
// DPPNUnitTest performs a series of white box testing.
func DPPNUnitTest() {
fmt.Println("=== DPPN Unit Test ===")
// Test NewDPPN()
fmt.Println("=== Test NewDPPN() ===")
n, err := NewDPPN(NewGenome(0, 2, 3, 1), 4)
if err != nil {
log.Fatal("Error: %s\n", err)
}
fmt.Println(n.ToString())
fmt.Printf("Expected number of inputs: %d\n", 2)
fmt.Printf("Actual number of inputs: %d\n", n.NumInputs)
fmt.Printf("Expected number of outputs: %d\n", 1)
fmt.Printf("Actual number of outputs: %d\n", n.NumOutputs)
fmt.Printf("Expected number of nodes: %d\n", 6)
fmt.Printf("Actual number of nodes: %d\n", len(n.Nodes))
numEdges := 0
for _, node := range n.Nodes {
numEdges += len(node.Inputs)
}
fmt.Printf("Expected number of edges: %d\n", 9)
fmt.Printf("Actual number of edges: %d\n", numEdges)
if n.NumInputs != 2 || n.NumOutputs != 1 ||
len(n.Nodes) != 6 || numEdges != 9 {
fmt.Println("TEST \033[1;31mFAILED\033[0m")
} else {
fmt.Println("TEST \033[1;32mPASSED\033[0m")
}
}
// DPPNAcceptanceTest performs a series of black box testing.
func DPPNAcceptanceTest() {
fmt.Println("=== DPPN Acceptance Test ===")
// DPPN XOR Test
fmt.Println("=== DPPN XOR Test ===")
n1, err := NewDPPN(NewGenome(1, 3, 4, 1), 4)
if err != nil {
log.Fatal("Error: %s\n", err)
}
inputs := mat64.NewDense(
4, 3, []float64{
1.0, 1.0, 1.0,
1.0, 0.0, 1.0,
0.0, 1.0, 1.0,
0.0, 0.0, 1.0,
},
)
target := mat64.NewDense(
4, 1, []float64{
0.0,
1.0,
1.0,
0.0,
},
)
for i := 0; i < 1000; i++ {
mse, err := n1.Backprop(inputs, target, 0.5)
if err != nil {
log.Fatal("Error: %s\n", err)
}
// average MSE
if i%100 == 0 {
fmt.Printf("Average MSE at epoch %d: %f\n", i, mse)
}
}
outputs, err := n1.FeedForward(inputs)
if err != nil {
log.Fatal("Error: %s\n", err)
}
outputFmt := mat64.Formatted(outputs, mat64.Prefix(" "), mat64.Squeeze())
fmt.Printf("a = %v\n\n", outputFmt)
}
func TestDPPN(t *testing.T) {
rand.Seed(0)
// Perform white box testing on DPPN
DPPNUnitTest()
// Perform black box testing on DPPN
DPPNAcceptanceTest()
}