-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgym.adept
104 lines (79 loc) · 3.83 KB
/
gym.adept
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
import 'sys/cstdio.adept'
import 'sys/cstdlib.adept'
import 'neuralnet.adept'
GYM_VERBOSE == false
GYM_EXERCISE_MIRROR == 0x01
GYM_EXERCISE_XOR == 0x02
GYM_EXERCISE_OR == 0x03
func workout(network *NeuralNetwork, exercise uint) void {
i usize; inputs *double
printf('Training Neural Network...\n')
if exercise == GYM_EXERCISE_MIRROR {
inputs = malloc(sizeof double * 1); defer free(inputs)
i = 0; while i != 100000, trainMirror(network, inputs); i += 1
printf('Completed Training...\n')
// Test when given 1.0
inputs[0] = 1.0; network.feedForward(inputs, 1)
printf('%f ===> %f\n', inputs[0], network.getOutputLayer().neurons[0].output)
// Test when given 0.0
inputs[0] = 0.0; network.feedForward(inputs, 1)
printf('%f ===> %f\n', inputs[0], network.getOutputLayer().neurons[0].output)
return
}
if exercise == GYM_EXERCISE_XOR {
inputs = malloc(sizeof double * 2); defer free(inputs)
i = 0; while i != 100000, trainXOR(network, inputs); i += 1
printf('Completed Training...\n')
// Test when given 0.0 and 0.0
inputs[0] = 0.0; inputs[1] = 0.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 0.0 and 1.0
inputs[0] = 0.0; inputs[1] = 1.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 1.0 and 0.0
inputs[0] = 1.0; inputs[1] = 0.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 1.0 and 1.0
inputs[0] = 1.0; inputs[1] = 1.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
return
}
if exercise == GYM_EXERCISE_OR {
inputs = malloc(sizeof double * 2); defer free(inputs)
i = 0; while i != 100000, trainOR(network, inputs); i += 1
printf('Completed Training...\n')
// Test when given 0.0 and 0.0
inputs[0] = 0.0; inputs[1] = 0.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 0.0 and 1.0
inputs[0] = 0.0; inputs[1] = 1.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 1.0 and 0.0
inputs[0] = 1.0; inputs[1] = 0.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
// Test when given 1.0 and 1.0
inputs[0] = 1.0; inputs[1] = 1.0; network.feedForward(inputs, 2)
printf('%f %f ===> %f\n', inputs[0], inputs[1], network.getOutputLayer().neurons[0].output)
return
}
}
func trainMirror(network *NeuralNetwork, inputs *double) void {
inputs[0] = cast double (normalizedRandom() > 0.5)
target double = inputs[0]
network.feedForward(inputs, 1)
network.backPropagate(&target, 1)
}
func trainXOR(network *NeuralNetwork, inputs *double) void {
inputs[0] = cast double (normalizedRandom() > 0.5)
inputs[1] = cast double (normalizedRandom() > 0.5)
target double = cast double (inputs[0] != inputs[1])
network.feedForward(inputs, 2)
network.backPropagate(&target, 1)
}
func trainOR(network *NeuralNetwork, inputs *double) void {
inputs[0] = cast double (normalizedRandom() > 0.5)
inputs[1] = cast double (normalizedRandom() > 0.5)
target double = cast double (cast bool inputs[0] || cast bool inputs[1])
network.feedForward(inputs, 2)
network.backPropagate(&target, 1)
}