-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStructure.pde
More file actions
170 lines (147 loc) · 6.03 KB
/
Structure.pde
File metadata and controls
170 lines (147 loc) · 6.03 KB
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
class Structure {
Layer inputLayer;
Layer hiddenLayers[];
Layer outputLayer;
final float MAX_WIDTH = 0.5; // in percentage (0.0 -> 1.0)
final float MAX_HEIGHT = 0.5; // in percentage (0.0 -> 1.0)
float neuronSize;
float inputTab;
float hiddenTabs[];
float outputTab;
float horizontalTab;
final boolean LINK_COLOUR = true;
final boolean LINK_ALPHA = true;
Structure (int nbNeuronsInput, int nbNeuronsOutput) {
inputLayer = new Layer(nbNeuronsInput);
hiddenLayers = new Layer[0];
outputLayer = new Layer(nbNeuronsOutput);
outputLayer.linkTo(inputLayer);
initGraphics();
}
Structure (int nbNeuronsInput, int nbNeuronsHidden[], int nbNeuronsOutput) {
inputLayer = new Layer(nbNeuronsInput);
hiddenLayers = new Layer[nbNeuronsHidden.length];
for (int i=0; i<nbNeuronsHidden.length; i++) {
hiddenLayers[i] = new Layer(nbNeuronsHidden[i]);
}
outputLayer = new Layer(nbNeuronsOutput);
hiddenLayers[0].linkTo(inputLayer);
for (int i=1; i<nbNeuronsHidden.length; i++) {
hiddenLayers[i].linkTo(hiddenLayers[i-1]);
}
outputLayer.linkTo(hiddenLayers[hiddenLayers.length-1]);
initGraphics();
}
void initGraphics () {
// GRAPHICS SIZE
float graphMaxWidth = width * MAX_WIDTH;
float graphMaxHeight = height * MAX_HEIGHT;
float graphWidth;
float graphHeight = graphMaxHeight;
// NEURON SIZE
int maxNeurons = 0;
for (int i=0; i<hiddenLayers.length; i++) {
if (hiddenLayers[i].neurons.length > maxNeurons) {
maxNeurons = hiddenLayers[i].neurons.length;
}
}
if (inputLayer.neurons.length > maxNeurons) {
maxNeurons = inputLayer.neurons.length;
}
if (outputLayer.neurons.length > maxNeurons) {
maxNeurons = outputLayer.neurons.length;
}
neuronSize = graphMaxHeight / maxNeurons;
graphWidth = neuronSize * ((hiddenLayers.length * 2 + 1) + 2);
if (graphWidth > graphMaxWidth) {
neuronSize -= ((graphWidth - graphMaxWidth) / ((hiddenLayers.length * 2 + 1) + 2));
graphHeight = graphMaxHeight - ((graphWidth - graphMaxWidth) * graphMaxHeight / graphWidth);
graphWidth = graphMaxWidth;
}
// TABS
inputTab = (graphMaxHeight - (inputLayer.neurons.length * neuronSize)) / 2;
hiddenTabs = new float[hiddenLayers.length];
for (int i=0; i<hiddenTabs.length; i++) {
hiddenTabs[i] = (graphMaxHeight - (hiddenLayers[i].neurons.length * neuronSize)) / 2;
}
outputTab = (graphMaxHeight - (outputLayer.neurons.length * neuronSize)) / 2;
horizontalTab = (graphMaxWidth - graphWidth) / 2;
}
void setLinkColour (Link link) {
if (LINK_COLOUR) {
float red = 255.0 - (link.weight * 255.0);
float green = link.weight * 255.0;
if (LINK_ALPHA) {
stroke(red, green, 0, 255.0 - (red / 1.2));
}
else {
stroke(red, green, 0);
}
}
else if (LINK_ALPHA) {
float alpha = 255.0 - (link.weight * 255.0);
stroke(0, 0, 0, alpha);
}
else {
stroke(0);
}
}
void show () {
// LINKS
strokeWeight(1);
for (int i=0; i<hiddenLayers.length; i++) {
for (int j=0; j<hiddenLayers[i].neurons.length; j++) {
for (int k=0; k<hiddenLayers[i].previousLayer.neurons.length; k++) {
setLinkColour(hiddenLayers[i].neurons[j].links[k]);
if (i != 0) {
line(horizontalTab + neuronSize * 2 * (i+1), neuronSize * j + hiddenTabs[i] + (neuronSize/2), horizontalTab + neuronSize * 2 * (i+1) - neuronSize, neuronSize * k + hiddenTabs[i-1] + (neuronSize/2));
}
else {
line(horizontalTab + neuronSize * 2 * (i+1), neuronSize * j + hiddenTabs[i] + (neuronSize/2), horizontalTab + neuronSize * 2 * (i+1) - neuronSize, neuronSize * k + inputTab + (neuronSize/2));
}
}
}
}
for (int i=0; i<outputLayer.neurons.length; i++) {
for (int j=0; j<outputLayer.previousLayer.neurons.length; j++) {
setLinkColour(outputLayer.neurons[i].links[j]);
if (hiddenLayers.length != 0) {
line(horizontalTab + neuronSize * 2 * (hiddenLayers.length+1), neuronSize * i + outputTab + (neuronSize/2), horizontalTab + neuronSize * 2 * (hiddenLayers.length+1) - neuronSize, neuronSize * j + hiddenTabs[hiddenLayers.length-1] + (neuronSize/2));
}
else {
line(horizontalTab + neuronSize * 2 * (hiddenLayers.length+1), neuronSize * i + outputTab + (neuronSize/2), horizontalTab + neuronSize * 2 * (hiddenLayers.length+1) - neuronSize, neuronSize * j + inputTab + (neuronSize/2));
}
}
}
// NEURONS
fill(255);
stroke(0);
ellipseMode(CORNER);
for (int i=0; i<inputLayer.neurons.length; i++) {
ellipse(horizontalTab, neuronSize * i + inputTab, neuronSize, neuronSize);
}
for (int i=0; i<hiddenLayers.length; i++) {
for (int j=0; j<hiddenLayers[i].neurons.length; j++) {
ellipse(horizontalTab + neuronSize * 2 * (i+1), neuronSize * j + hiddenTabs[i], neuronSize, neuronSize);
}
}
for (int i=0; i<outputLayer.neurons.length; i++) {
ellipse(horizontalTab + neuronSize * 2 * (hiddenLayers.length+1), neuronSize * i + outputTab, neuronSize, neuronSize);
}
// VALUES
fill(0);
textSize(9);
textAlign(CENTER, CENTER);
for (int i=0; i<inputLayer.neurons.length; i++) {
text(String.format("%.2f", inputLayer.neurons[i].value), horizontalTab + neuronSize / 2, neuronSize * i + inputTab + (neuronSize / 2));
}
for (int i=0; i<hiddenLayers.length; i++) {
for (int j=0; j<hiddenLayers[i].neurons.length; j++) {
text(String.format("%.2f", hiddenLayers[i].neurons[j].value), horizontalTab + neuronSize * 2 * (i+1) + (neuronSize / 2), neuronSize * j + hiddenTabs[i] + (neuronSize / 2));
}
}
for (int i=0; i<outputLayer.neurons.length; i++) {
text(String.format("%.2f", outputLayer.neurons[i].value), horizontalTab + neuronSize * 2 * (hiddenLayers.length+1) + (neuronSize / 2), neuronSize * i + outputTab + (neuronSize / 2));
}
}
}