-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.p6
147 lines (116 loc) · 3.22 KB
/
main.p6
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
use v6.c;
use lib '.';
#use lib './lib/';
use lib::Tools;
# forward declare class
#class Node {...}
class NodeLayer
{
has Vector $.nodeOuts;
has Matrix $.weights;
has Num $.bias;
method new(Vector $nodeOuts, Matrix $weights, Num $bias)
{
return self.bless(:$nodeOuts, :$weights, :$bias);
}
method calc_val(Vector $prevLayerOuts)
{
$.nodeOuts = sigmoid ($prevLayerOuts * $.weights);
}
method calculateWeightDeltas(@input, @expected)
{
}
}
class trainingData
{
has @.inputs;
has @.expected;
method New(@inputs, @expected)
{
return self.bless(:@inputs, :@expected);
}
}
# delta * previous node = dcost/dweight
sub get_layer_weight_dels(@expected, NodeLayer $layer, NodeLayer $prevLayer, @prevDeltas = Nil)
{
my @out = NodeLayer.nodeOuts.values;
return if (@out.elems != @expected.elems);
if (@prevDeltas == Nil)
{
my @deltas = do for ([email protected]) -> i {
-(@expected[i]-@out[i])*@out[i](1-@out[i])
}
}
else
{
}
}
sub createRandList($size, $range)
{
return do for (0..$size)
{
$range.rand
}
}
sub createRandomMatrix ($dims, $range)
{
my @split = $dims.split('x');
die "Bad dim parameter <$dims> ex: 4x4" if @split.elems != 2;
my $rows = (Int)(@split[0]);
my $columns = (Int)(@split[1]);
return Matrix.new(createRandList($rows*$columns,$range), $dims);
}
my @xorTraining = [trainingData.New([0,0],[0]),trainingData.New([0,1],[1]),trainingData.New([0,1],[1]),trainingData.New([1,0],[1]),trainingData.New([1,1],[0])];
sub MAIN()
{
my NodeLayer $firstLayer = NodeLayer.new(Vector.new([0,0]),createRandomMatrix('2x2',(-1..1)),(-1..1).rand);
my NodeLayer $outputLayer = NodeLayer.new(Vector.new([0,0]),createRandomMatrix('2x2',(-1..1)),(-1..1).rand);
my @network = ($firstLayer, $outputLayer);
#Training
#{{{
for @xorTraining
{
my $input = Vector.new($_.inputs);
my $expected = Vector.new($_.expected);
#get values for training
for ([email protected]) -> i
{
if (i == 0){
@network[i].calc_val($input);
}
else
{
@network[i].calc_val(@network[i-1]);
}
}
# *-1 = -> $n { $n - 1 }
my Matrix $weightDeltas = get_layer_weight_dels(@expected, @network[*-1], @network[*-2));
}
#}}}
}
#sub backProp (Node @lastRow, Num @expectedVal)
#{
# return False if @lastRow.elems != @expectedVal.elems;
#
# #dcost/dweight = dTotCost/dnode dnode/dTotInput dTotInput/dnodeWeight
#
# # Gradient decent for output nodes
# for ([email protected]) -> $i
# {
# my $pd_cost_wrt_nodeVal = -(@expectedVal[$i] - @lastRow[$i]);
# my $pd_nodeVal_wrt_netInputToNode = @lastRow[$i] * (1 - @lastRow[$i]);
# my @pd_netInputNode_wrt_weight = do for (@lastRow[$i].connections){ $_.val };
# for (0..@lastRow[$i].connections.elems) ->$ii
# {
#
# }
# }
#}
#sub costFunction(Node @lastRow, @expectedVals)
#{
# my @lastRowVals = do {
# $_.val for @lastRow;
# }
#
# return [+] (@expectedVals <<->> @lastRow).map: { ^2/2};
#}