-
Notifications
You must be signed in to change notification settings - Fork 3
/
NeuralNetworkBackPropWithMatrices.html
596 lines (491 loc) · 18.5 KB
/
NeuralNetworkBackPropWithMatrices.html
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Neural network back prop, with matrix fomulation</title>
<script type="text/javascript"
src="https://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({tex2jax: {inlineMath: [['$','$'], ['\\(','\\)']]}});
</script>
<style type="text/css">
<!--
body { background-color:#ededed; font:norm2al 12px/18px Arial, Helvetica, sans-serif; }
h1 { display:block; width:600px; margin:20px auto; paddVing-bottom:20px; font:norm2al 24px/30px Georgia, "Times New Roman", Times, serif; color:#333; text-shadow: 1px 2px 3px #ccc; border-bottom:1px solid #cbcbcb; }
#container { width:600px; margin:0 auto; }
#myCanvas { background:#fff; border:1px solid #cbcbcb; }
#nav { display:block; width:100%; text-align:center; }
#nav li { display:block; font-weight:bold; line-height:21px; text-shadow:1px 1px 1px #fff; width:100px; height:21px; paddVing:5px; margin:0 10px; background:#e0e0e0; border:1px solid #ccc; -moz-border-radius:4px;-webkit-border-radius:4px; border-radius:4px; float:left; }
#nav li a { color:#000; display:block; text-decoration:none; width:100%; height:100%; }
-->
</style>
</head>
<script>
function RandomMat(inp,neurons)
{
var L = []
for(var i=0;i<neurons;i++)
{
L[i] = [];
for(var j=0;j<inp;j++)
{
L[i].push(2*Math.random()-1);
}
}
return L;
}
function PrintMat(m)
{
var str = "[";
for(var j=0;j<m.length;j++)
{
str += "[" + m[j].join(",") + "]";
}
str += "]";
return str;
}
function MulMat(m1, m2)
{
var O = [];
for(var j=0;j<m1.length;j++)
{
O[j]=[];
for(var i=0;i<m2[0].length;i++)
{
var tmp=0;
for(var k=0;k<m1[0].length;k++)
{
tmp += (m1[j][k] * m2[k][i]);
}
O[j].push( tmp);
}
}
return O;
}
function TransposeMat(m)
{
var O = [];
for(var j=0;j<m[0].length;j++)
{
O[j]=[];
for(var i=0;i<m.length;i++)
{
O[j][i] = m[i][j];
}
}
return O;
}
function AddMat(m1, m2)
{
var O = [];
for(var j=0;j<m1.length;j++)
{
O[j]=[];
for(var i=0;i<m1[0].length;i++)
{
O[j][i] = m1[j][i] + m2[j][i];
}
}
return O;
}
function SimpleMulMat(m1, m2)
{
var O = [];
for(var j=0;j<m1.length;j++)
{
O[j]=[];
for(var i=0;i<m1[0].length;i++)
{
O[j][i] = m1[j][i] * m2[j][i];
}
}
return O;
}
function SubMat(m1, m2)
{
var O = [];
for(var j=0;j<m1.length;j++)
{
O[j]=[];
for(var i=0;i<m1[0].length;i++)
{
O[j][i] = m1[j][i] - m2[j][i];
}
}
return O;
}
function Act(x)
{
return 1.0/(1.0+Math.exp(-x));
}
function DerAct(x)
{
return Act(x)*(1-Act(x));
}
function funcMat(func, m )
{
var O = [];
for(var j=0;j<m.length;j++)
{
O[j]=[];
for(var i=0;i<m[0].length;i++)
{
O[j][i] = func(m[j][i]);
}
}
return O;
}
function UpdateWeights(m1,m2, learningRate)
{
for(var i=0;i<m1.length;i++)
{
for(var j=0;j<m1[0].length;j++)
{
m1[i][j] += learningRate * m2[i][j];
}
}
}
//-------------------------------
function GraphAxis()
{
context.beginPath();
context.strokeStyle="#000000";
context.moveTo(0,300-20);
context.lineTo(600,300-20);
context.moveTo(20,0);
context.lineTo(20,300);
context.closePath();
context.stroke();
}
function DrawDecisionBoundary(tx,ty, size)
{
var canvasData = context.getImageData(0, 0, size, size);
for(var y=0;y<size;y++)
{
for(var x=0;x<size;x++)
{
var index = (x + y * size) * 4;
xx = x/size
yy = 1-y/size
var net1 = MulMat(wl_i1, [[xx],[yy],[1]]);
var o1 = funcMat(Act, net1);
var net2 = MulMat(wl_12, o1);
var o2 = funcMat(Act, net2);
v = o2[0][0]>.5?255:0;
canvasData.data[index + 0] = v;
canvasData.data[index + 1] = 255-v;
canvasData.data[index + 2] = 0;
canvasData.data[index + 3] = 128;
}
}
context.putImageData(canvasData, tx, ty);
}
//-------------------------------
var wl_i1 = RandomMat(3,4); //read from 3, ilayer is 4
var wl_12 = RandomMat(4,1);
/*
wl_i1 = TransposeMat([[-0.16595599, 0.44064899, -0.99977125, -0.39533485],
[-0.70648822, -0.81532281, -0.62747958, -0.30887855],
[-0.20646505, 0.07763347, -0.16161097, 0.370439 ]]);
wl_12 = TransposeMat([[-0.5910955 ],
[ 0.75623487],
[-0.94522481],
[ 0.34093502]]);
*/
var iterations = 0;
var oldFitness=1000;
var curFitness=1000;
function iterate()
{
var il = [ [ [0],[0], [1] ], [ [0],[1], [1] ], [ [1],[0], [1] ] , [ [1],[1], [1] ] ]; //input
var t = [ [ [0] ], [ [1] ], [ [1] ], [ [0] ] ]; //target
var learningRate = 1;
var err;
var fitness=100;
iterations++;
var totalErr;
var netResponses = []
for(var j=0;j<1;j++)
{
totalErr = 0;
for(var i=0;i<4;i++)
{
//forward pass
//
var o0 = il[i];
var net1 = MulMat(wl_i1, o0);
var o1 = funcMat(Act, net1);
var net2 = MulMat(wl_12, o1);
var o2 = funcMat(Act, net2);
netResponses[i] = o2;
err = SubMat(o2, t[i]);
//back propagate
//
// outer layer
var sp2 = SimpleMulMat(funcMat(DerAct, net2), err);
var e2 = MulMat(sp2, TransposeMat(o1));
// inner layer
var wsp2 = MulMat(TransposeMat(wl_12), sp2);
var sp1 = SimpleMulMat(funcMat(DerAct, net1), wsp2);
var e1 = MulMat(sp1, TransposeMat(o0));
//update weights
UpdateWeights(wl_12, e2, -learningRate);
UpdateWeights(wl_i1, e1, -learningRate);
totalErr += MulMat( err, TransposeMat(err))[0][0];
}
}
DrawDecisionBoundary(600-100-10,10, 100)
oldFitness = curFitness;
curFitness = totalErr;
context.beginPath();
context.strokeStyle="#ff0000";
context.moveTo((iterations-1)/10+20,300-oldFitness*300-20);
context.lineTo((iterations)/10+20,300-curFitness*300-20);
context.closePath();
context.stroke();
document.getElementById("text").innerHTML = "X axis, epochs: "+ iterations +"<br>Y axis, Error: " + curFitness + "<br>";
document.getElementById("text").innerHTML += " <br> "
for(var i=0;i<4;i++)
{
document.getElementById("text").innerHTML += il[i][0]+" xor "
document.getElementById("text").innerHTML += il[i][1]+" = "
document.getElementById("text").innerHTML += netResponses[i]+"<br>"
}
}
function init()
{
var myCanvas = document.getElementById("myCanvas");
context = myCanvas.getContext('2d');
context.clearRect(0,0,600,300);
GraphAxis();
setInterval(iterate,10);
}
</script>
<body onload="init()">
<h1>Neural network back prop using simple gradient descent, with matrix fomulation</h1>
<div id="container">
<canvas id="myCanvas" width="600" height="300"></canvas>
<div id="text"></div>
<h2>Intro</h2>
The best way to learn something is to teach it, so here I go!<br><br>
This sample uses a 2x4x1 NN to compute learn xor operation.
<br>
This tutorial shows the following:
<ul>
<li>How to compute the derivatives of the Error function of a 2x2 NN using the chain rule</li>
<li>How to express the derivatives using matrices, this helps in many ways:</li>
<ul>
<li>Generalizing the 2x2 configuration into other configuration</li>
<li>Vectorization, by doing many operations at once we can speed up computations.</li>
<li>Batching, by applying the forward pass to the whole set, averaging all the gradients, and using them to update the weights</li>
</ul>
</ul>
<h4>Todo:</h4>
<ul>
<li>batched/mini batching training</li>
<li>dropout</li>
<li>Add momentum to learning</li>
</ul>
<h2>Computing $\frac{\partial{Error}}{\partial{weights}}$</h2>.
Let's consider this simple configuration and later on we'll generalize this for any configuration
<pre>
F H
i1 o-------a--o-------e--o
\ / \ /
\ / \ /
\ b \ f
\ / \ /
\/ \/
/\ /\
/ \ / \
/ c / g
/ \ / \
/ \ / \
i2 o-------d--o-------h--o
G I
</pre>
$$\require{cancel}$$
This is a forward pass:
$$\begin{align*}
F &= \sigma(net_F) &= \sigma(a\cdot i_1 + b\cdot i_2) \\
G &= \sigma(net_G) &= \sigma(c\cdot i_1 + d\cdot i_2) \\
H &= \sigma(net_H) &= \sigma(e\cdot F+f\cdot G) \\
I &= \sigma(net_I) &= \sigma(g\cdot F+h\cdot G)
\end{align*}$$
We are starting to see some matrices here...
$$\begin{pmatrix}net_F\\
net_G\\
\end{pmatrix} = \begin{pmatrix} a & b \\ c & d
\end{pmatrix} \cdot \begin{pmatrix}i_1\\
i_2\\
\end{pmatrix}$$
$$\begin{pmatrix}net_H\\
net_I\\
\end{pmatrix} = \begin{pmatrix} e & f \\ g & h
\end{pmatrix} \cdot \begin{pmatrix}F\\
G\\
\end{pmatrix}$$
And the full forward operation woudl be given by:
$$\begin{pmatrix}H\\
I\\
\end{pmatrix} = \sigma \Bigg( \begin{pmatrix} e & f \\ g & h
\end{pmatrix} \cdot \sigma \Bigg( \begin{pmatrix} a & b \\ c & d
\end{pmatrix} \cdot \begin{pmatrix}i_1\\
i_2\\
\end{pmatrix} \Bigg) \Bigg)
$$
Now we want to see how $H$ and $I$ change when the weights $a ... b$ change. This is given by the derivative.<br>
<br>
For computing derivatives we'll be using the chain rule. We'll need some formulas:
$$ \frac{d\sigma(f(...))}{df(...)} = \sigma'(f(...)) \cdot f'(...)'$$
$$\frac{\partial{H}}{\partial{e}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{e}} =\sigma'(net_H) \frac{\partial{(e\cdot F+f\cdot G)}}{\partial{e}} = \sigma'(net_H) F$$
$$\frac{\partial{H}}{\partial{f}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{f}} =\sigma'(net_H) \frac{\partial{(e\cdot F+f\cdot G)}}{\partial{f}} = \sigma'(net_H) G$$
$$\frac{\partial{I}}{\partial{g}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{g}} =\sigma'(net_I) \frac{\partial{(g\cdot F+h\cdot G)}}{\partial{g}} = \sigma'(net_I) F$$
$$\frac{\partial{I}}{\partial{h}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{h}} =\sigma'(net_I) \frac{\partial{(g\cdot F+h\cdot G)}}{\partial{h}} = \sigma'(net_I) G$$
Starting from the output we immediatelly see a jacobian:
$$\begin{pmatrix}\frac{\partial{H}}{\partial{e}} & \frac{\partial{H}}{\partial{f}} \\
\frac{\partial{I}}{\partial{g}} & \frac{\partial{I}}{\partial{h}}\\
\end{pmatrix} = \begin{pmatrix} \sigma'(net_H) F & \sigma'(net_H) G \\ \sigma'(net_I)F & \sigma'(net_I)G\end{pmatrix} = \sigma'(\begin{pmatrix}net_H\\
net_I\\
\end{pmatrix}) \cdot \begin{pmatrix} F & G \end{pmatrix}$$
Let see how the outputs change with the weights $a .. d$:<br><br>
For H:
$$\frac{\partial{H}}{\partial{a}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{a}} =\sigma'(net_H) \frac{\partial{(e\cdot \sigma(net_F)+f\cdot G)}}{\partial{a}} = \sigma'(net_H) \cdot e\cdot \sigma'(net_F) \cdot i_1$$
$$\frac{\partial{H}}{\partial{b}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{b}} =\sigma'(net_H) \frac{\partial{(e\cdot \sigma(net_F)+f\cdot G)}}{\partial{b}} = \sigma'(net_H) \cdot e\cdot \sigma'(net_F) \cdot i_2$$
$$\frac{\partial{H}}{\partial{c}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{c}} =\sigma'(net_H) \frac{\partial{(e\cdot F+f\cdot \sigma(net_G))}}{\partial{c}} = \sigma'(net_H) \cdot f\cdot \sigma'(net_G) \cdot i_1$$
$$\frac{\partial{H}}{\partial{d}} = \sigma'(net_H) \frac{\partial{(net_H)}}{\partial{d}} =\sigma'(net_H) \frac{\partial{(e\cdot F+f\cdot \sigma(net_G))}}{\partial{d}} = \sigma'(net_H) \cdot f\cdot \sigma'(net_G) \cdot i_2$$
For I:
$$\frac{\partial{I}}{\partial{a}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{a}} =\sigma'(net_I) \frac{\partial{(g\cdot \sigma(net_F)+h\cdot G)}}{\partial{a}} = \sigma'(net_I) \cdot g\cdot \sigma'(net_F) \cdot i_1$$
$$\frac{\partial{I}}{\partial{b}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{b}} =\sigma'(net_I) \frac{\partial{(g\cdot \sigma(net_F)+h\cdot G)}}{\partial{b}} = \sigma'(net_I) \cdot g\cdot \sigma'(net_F) \cdot i_2$$
$$\frac{\partial{I}}{\partial{c}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{c}} =\sigma'(net_I) \frac{\partial{(g\cdot F+h\cdot \sigma(net_G))}}{\partial{c}} = \sigma'(net_I) \cdot h\cdot \sigma'(net_G) \cdot i_1$$
$$\frac{\partial{I}}{\partial{d}} = \sigma'(net_I) \frac{\partial{(net_I)}}{\partial{d}} =\sigma'(net_I) \frac{\partial{(g\cdot F+h\cdot \sigma(net_G))}}{\partial{d}} = \sigma'(net_I) \cdot h\cdot \sigma'(net_G) \cdot i_2$$
Now we have all the ingredients to train our network:
First we need a forward pass, we take the output and we compute how far are we from the target result $t_1$ and $t_2$:
$$Error = (H-t_1)^2 + (I-t_2)^2$$
And it's derivative is:
$$d(Error) = 2(H-t_1)dH + 2(I-t_2)dI= 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
dH\\
dI
\end{pmatrix}$$
Its partial derivatives are:
$$\frac{\partial{Error}}{\partial{e}} = 2(H-t_1)dH + 2(I-t_2)dI= 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\frac{\partial{H}}{\partial{e}}\\
\cancelto{0}{\frac{\partial{I}}{\partial{e}}}
\end{pmatrix} = 2(H-t_1) \frac{\partial{H}}{\partial{e}} $$
$$\frac{\partial{Error}}{\partial{e}} = 2(H-t_1) \frac{\partial{H}}{\partial{e}} = 2(H-t_1) \sigma(net_H)F $$
$$\frac{\partial{Error}}{\partial{f}} = 2(H-t_1) \frac{\partial{H}}{\partial{f}} = 2(H-t_1) \sigma(net_H)G $$
$$\frac{\partial{Error}}{\partial{g}} = 2(I-t_2) \frac{\partial{I}}{\partial{g}} = 2(I-t_2) \sigma(net_I)F $$
$$\frac{\partial{Error}}{\partial{h}} = 2(I-t_2) \frac{\partial{I}}{\partial{h}} = 2(I-t_2) \sigma(net_I)G $$
rearranging:
$$ \begin{pmatrix}\frac{\partial{E}}{\partial{e}} & \frac{\partial{E}}{\partial{f}} \\
\frac{\partial{E}}{\partial{g}} & \frac{\partial{E}}{\partial{h}}\\
\end{pmatrix} = 2 \cdot
\begin{pmatrix}
\sigma'(net_H) \cdot (H-t_1) \\
\sigma'(net_I) \cdot (I-t_2) \\
\end{pmatrix}
\cdot
\begin{pmatrix} F & G \end{pmatrix}
= \begin{pmatrix}
\sigma'(net_H) & 0 \\
0 & \sigma'(net_I)\\
\end{pmatrix}
\cdot
2
\cdot
\begin{pmatrix}
(H-t_1) \\
(I-t_2) \\
\end{pmatrix}
\cdot
\begin{pmatrix} F & G \end{pmatrix}
$$
and in a similar way:
$$\frac{\partial{Error}}{\partial{a}} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\frac{\partial{H}}{\partial{a}}\\
\frac{\partial{I}}{\partial{a}}
\end{pmatrix} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot e\cdot \sigma'(net_F) \cdot i_1\\
\sigma'(net_I) \cdot g\cdot \sigma'(net_F) \cdot i_1
\end{pmatrix} $$
$$\frac{\partial{Error}}{\partial{b}} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\frac{\partial{H}}{\partial{b}}\\
\frac{\partial{I}}{\partial{b}}
\end{pmatrix} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot e\cdot \sigma'(net_F) \cdot i_2\\
\sigma'(net_I) \cdot g\cdot \sigma'(net_F) \cdot i_2
\end{pmatrix} $$
$$\frac{\partial{Error}}{\partial{c}} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\frac{\partial{H}}{\partial{c}}\\
\frac{\partial{I}}{\partial{c}}
\end{pmatrix} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot f\cdot \sigma'(net_G) \cdot i_1\\
\sigma'(net_I) \cdot h\cdot \sigma'(net_G) \cdot i_1
\end{pmatrix} $$
$$\frac{\partial{Error}}{\partial{d}} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\frac{\partial{H}}{\partial{d}}\\
\frac{\partial{I}}{\partial{d}}
\end{pmatrix} = 2\begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot f\cdot \sigma'(net_G) \cdot i_2\\
\sigma'(net_I) \cdot h\cdot \sigma'(net_G) \cdot i_2
\end{pmatrix} $$
simplifying a bit
$$\frac{\partial{Error}}{\partial{a}} =
2\cdot \sigma'(net_F) \cdot i_1 \cdot \begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot e \\
\sigma'(net_I) \cdot g
\end{pmatrix} = 2\cdot \sigma'(net_F) \cdot i_1 \cdot (((H-t_1) \cdot \sigma'(net_H) \cdot e) + ((I-t_2) \cdot \sigma'(net_I) \cdot g))
$$
$$\frac{\partial{Error}}{\partial{b}} =
2\cdot \sigma'(net_F) \cdot i_2 \cdot \begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot e\\
\sigma'(net_I) \cdot g
\end{pmatrix} = 2\cdot \sigma'(net_F) \cdot i_2 \cdot (((H-t_1) \cdot \sigma'(net_H) \cdot e) + ((I-t_2) \cdot \sigma'(net_I) \cdot g))$$
$$\frac{\partial{Error}}{\partial{c}} =
2\cdot \sigma'(net_G) \cdot i_1 \cdot \begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot f \\
\sigma'(net_I) \cdot h
\end{pmatrix} = 2\cdot \sigma'(net_G) \cdot i_1 \cdot (((H-t_1) \cdot \sigma'(net_H) \cdot f) + ((I-t_2) \cdot \sigma'(net_I) \cdot h))$$
$$\frac{\partial{Error}}{\partial{d}} =
2\cdot \sigma'(net_G) \cdot i_2 \cdot \begin{pmatrix} (H-t_1) & (I-t_2) \end{pmatrix} \cdot \begin{pmatrix}
\sigma'(net_H) \cdot f \\
\sigma'(net_I) \cdot h
\end{pmatrix} = 2\cdot \sigma'(net_G) \cdot i_2 \cdot (((H-t_1) \cdot \sigma'(net_H) \cdot f) + ((I-t_2) \cdot \sigma'(net_I) \cdot h))$$
hence
$$ \begin{pmatrix}\frac{\partial{E}}{\partial{a}} & \frac{\partial{E}}{\partial{b}} \\
\frac{\partial{E}}{\partial{c}} & \frac{\partial{E}}{\partial{d}}\\
\end{pmatrix} =
\begin{pmatrix}
\sigma'(net_F) & 0 \\
0 & \sigma'(net_G)\\
\end{pmatrix}
\cdot
\begin{pmatrix}
e & g \\
f & h\\
\end{pmatrix}
\cdot
\Bigg[
2 \cdot
\begin{pmatrix}
\sigma'(net_H) \cdot (H-t_1) \\
\sigma'(net_I) \cdot (I-t_2) \\
\end{pmatrix}
\Bigg]
\cdot
\begin{pmatrix}
i_1 & i_2 \\
\end{pmatrix}
$$
Notice how the expression within aquare brackes was computed early before.
Let's now generalize the above structure for any network..
</br>
</br>
<h2>Contact/Questions:</h2>
<my_github_account_username>[email protected]$.
</br>
</br>
</div>
</body>
</html>