-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtestBPTT_GRU.m
More file actions
509 lines (462 loc) · 21 KB
/
testBPTT_GRU.m
File metadata and controls
509 lines (462 loc) · 21 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
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
% This program tests the BPTT process we manually developed for GRU.
% We calculate the gradients of GRU parameters with chain rule, and then
% compare them to the numerical gradients to check whether our chain rule
% derivation is correct.
% Here, we provided 2 versions of BPTT, backward_direct() and backward().
% The former one is the direct idea to calculate gradient within each step
% and add them up (O(sentence_size^2) time). The latter one is optimized to
% calculate the contribution of each step to the overall gradient, which is
% only O(sentence_size) time.
% This is very helpful for people who wants to implement GRU in Caffe since
% Caffe didn't support auto-differentiation. This is also very helpful for
% the people who wants to know the details about Backpropagation Through
% Time algorithm in the Reccurent Neural Networks (such as GRU and LSTM)
% and also get a sense on how auto-differentiation is possible.
% NOTE: We didn't involve SGD training here. With SGD training, this
% program would become a complete implementation of GRU which can be
% trained with sequence data. However, since this is only a CPU serial
% Matlab version of GRU, applying it on large datasets will be dramatically
% slow.
% by Minchen Li, at The University of British Columbia. 2016-04-21
function testBPTT_GRU
% set GRU and data scale
vocabulary_size = 64;
iMem_size = 4;
sentence_size = 20; % number of words in a sentence
%(including start and end symbol)
% since we will only use one sentence for training,
% this is also the total steps during training.
[x y] = getTrainingData(vocabulary_size, sentence_size);
% initialize parameters:
% multiplier for input x_t of intermediate variables
U_z = rand(iMem_size, vocabulary_size);
U_r = rand(iMem_size, vocabulary_size);
U_c = rand(iMem_size, vocabulary_size);
% multiplier for pervious s of intermediate variables
W_z = rand(iMem_size, iMem_size);
W_r = rand(iMem_size, iMem_size);
W_c = rand(iMem_size, iMem_size);
% bias terms of intermediate variables
b_z = rand(iMem_size, 1);
b_r = rand(iMem_size, 1);
b_c = rand(iMem_size, 1);
% decoder for generating output
V = rand(vocabulary_size, iMem_size);
b_V = rand(vocabulary_size, 1); % bias of decoder
% previous s of step 1
s_0 = rand(iMem_size, 1);
% calculate and check gradient
tic
[dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0] = ...
backward_direct(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
toc
tic
checkGradient_GRU(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0, ...
dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0);
toc
tic
[dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0] = ...
backward(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
toc
tic
checkGradient_GRU(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0, ...
dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0);
toc
end
% Forward propagate calculate s, y_hat, loss and intermediate variables for each step
function [s, y_hat, L, z, r, c] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0)
% count sizes
[vocabulary_size, sentence_size] = size(x);
iMem_size = size(V, 2);
% initialize results
s = zeros(iMem_size, sentence_size);
y_hat = zeros(vocabulary_size, sentence_size);
L = zeros(sentence_size, 1);
z = zeros(iMem_size, sentence_size);
r = zeros(iMem_size, sentence_size);
c = zeros(iMem_size, sentence_size);
% calculate result for step 1 since s_0 is not in s
z(:,1) = sigmoid(U_z*x(:,1) + W_z*s_0 + b_z);
r(:,1) = sigmoid(U_r*x(:,1) + W_r*s_0 + b_r);
c(:,1) = tanh(U_c*x(:,1) + W_c*(s_0.*r(:,1)) + b_c);
s(:,1) = (1-z(:,1)).*c(:,1) + z(:,1).*s_0;
y_hat(:,1) = softmax(V*s(:,1) + b_V);
L(1) = sum(-y(:,1).*log(y_hat(:,1)));
% calculate results for step 2 - sentence_size similarly
for wordI = 2:sentence_size
z(:,wordI) = sigmoid(U_z*x(:,wordI) + W_z*s(:,wordI-1) + b_z);
r(:,wordI) = sigmoid(U_r*x(:,wordI) + W_r*s(:,wordI-1) + b_r);
c(:,wordI) = tanh(U_c*x(:,wordI) + W_c*(s(:,wordI-1).*r(:,wordI)) + b_c);
s(:,wordI) = (1-z(:,wordI)).*c(:,wordI) + z(:,wordI).*s(:,wordI-1);
y_hat(:,wordI) = softmax(V*s(:,wordI) + b_V);
L(wordI) = sum(-y(:,wordI).*log(y_hat(:,wordI)));
end
end
% Backward propagate to calculate gradient using chain rule
% (O(sentence_size) time)
function [dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0] = ...
backward(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0)
% forward propagate to get the intermediate and output results
[s, y_hat, L, z, r, c] = forward(x, y, U_z, U_r, U_c, W_z, W_r, W_c, ...
b_z, b_r, b_c, V, b_V, s_0);
% count sentence size
[~, sentence_size] = size(x);
% calculate gradient using chain rule
delta_y = y_hat - y;
db_V = sum(delta_y, 2);
dV = zeros(size(V));
for wordI = 1:sentence_size
dV = dV + delta_y(:,wordI)*s(:,wordI)';
end
ds_0 = zeros(size(s_0));
dU_c = zeros(size(U_c));
dU_r = zeros(size(U_r));
dU_z = zeros(size(U_z));
dW_c = zeros(size(W_c));
dW_r = zeros(size(W_r));
dW_z = zeros(size(W_z));
db_z = zeros(size(b_z));
db_r = zeros(size(b_r));
db_c = zeros(size(b_c));
ds_single = V'*delta_y;
% calculate the derivative contribution of each step and add them up
ds_cur = zeros(size(ds_single,1), 1);
for wordJ = sentence_size:-1:2
ds_cur = ds_cur + ds_single(:,wordJ);
ds_cur_bk = ds_cur;
dtanhInput = (ds_cur.*(1-z(:,wordJ)).*(1-c(:,wordJ).*c(:,wordJ)));
db_c = db_c + dtanhInput;
dU_c = dU_c + dtanhInput*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_c = dW_c + dtanhInput*(s(:,wordJ-1).*r(:,wordJ))';
dsr = W_c'*dtanhInput;
ds_cur = dsr.*r(:,wordJ);
dsigInput_r = dsr.*s(:,wordJ-1).*r(:,wordJ).*(1-r(:,wordJ));
db_r = db_r + dsigInput_r;
dU_r = dU_r + dsigInput_r*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_r = dW_r + dsigInput_r*s(:,wordJ-1)';
ds_cur = ds_cur + W_r'*dsigInput_r;
ds_cur = ds_cur + ds_cur_bk.*z(:,wordJ);
dz = ds_cur_bk.*(s(:,wordJ-1)-c(:,wordJ));
dsigInput_z = dz.*z(:,wordJ).*(1-z(:,wordJ));
db_z = db_z + dsigInput_z;
dU_z = dU_z + dsigInput_z*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_z = dW_z + dsigInput_z*s(:,wordJ-1)';
ds_cur = ds_cur + W_z'*dsigInput_z;
end
% s_1
ds_cur = ds_cur + ds_single(:,1);
dtanhInput = (ds_cur.*(1-z(:,1)).*(1-c(:,1).*c(:,1)));
db_c = db_c + dtanhInput;
dU_c = dU_c + dtanhInput*x(:,1)'; %could be accelerated by avoiding add 0
dW_c = dW_c + dtanhInput*(s_0.*r(:,1))';
dsr = W_c'*dtanhInput;
ds_0 = ds_0 + dsr.*r(:,1);
dsigInput_r = dsr.*s_0.*r(:,1).*(1-r(:,1));
db_r = db_r + dsigInput_r;
dU_r = dU_r + dsigInput_r*x(:,1)'; %could be accelerated by avoiding add 0
dW_r = dW_r + dsigInput_r*s_0';
ds_0 = ds_0 + W_r'*dsigInput_r;
ds_0 = ds_0 + ds_cur.*z(:,1);
dz = ds_cur.*(s_0-c(:,1));
dsigInput_z = dz.*z(:,1).*(1-z(:,1));
db_z = db_z + dsigInput_z;
dU_z = dU_z + dsigInput_z*x(:,1)'; %could be accelerated by avoiding add 0
dW_z = dW_z + dsigInput_z*s_0';
ds_0 = ds_0 + W_z'*dsigInput_z;
end
% A more direct view of backward propagate to calculate gradient using
% chain rule. (O(sentence_size^2) time)
% Instead of calculating how much contribution of derivative each step has,
% here we calculate the gradient within every step.
function [dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0] = ...
backward_direct(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0)
% forward propagate to get the intermediate and output results
[s, y_hat, L, z, r, c] = forward(x, y, U_z, U_r, U_c, W_z, W_r, W_c, ...
b_z, b_r, b_c, V, b_V, s_0);
% count sentence size
[~, sentence_size] = size(x);
% calculate gradient using chain rule
delta_y = y_hat - y;
db_V = sum(delta_y, 2);
dV = zeros(size(V));
for wordI = 1:sentence_size
dV = dV + delta_y(:,wordI)*s(:,wordI)';
end
ds_0 = zeros(size(s_0));
dU_c = zeros(size(U_c));
dU_r = zeros(size(U_r));
dU_z = zeros(size(U_z));
dW_c = zeros(size(W_c));
dW_r = zeros(size(W_r));
dW_z = zeros(size(W_z));
db_z = zeros(size(b_z));
db_r = zeros(size(b_r));
db_c = zeros(size(b_c));
ds_single = V'*delta_y;
% calculate the derivatives in each step and add them up
for wordI = 1:sentence_size
ds_cur = ds_single(:,wordI);
% since in each step t, the derivatives depends on s_0 - s_t,
% we need to trace back from t ot 0 each time
for wordJ = wordI:-1:2
ds_cur_bk = ds_cur;
dtanhInput = (ds_cur.*(1-z(:,wordJ)).*(1-c(:,wordJ).*c(:,wordJ)));
db_c = db_c + dtanhInput;
dU_c = dU_c + dtanhInput*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_c = dW_c + dtanhInput*(s(:,wordJ-1).*r(:,wordJ))';
dsr = W_c'*dtanhInput;
ds_cur = dsr.*r(:,wordJ);
dsigInput_r = dsr.*s(:,wordJ-1).*r(:,wordJ).*(1-r(:,wordJ));
db_r = db_r + dsigInput_r;
dU_r = dU_r + dsigInput_r*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_r = dW_r + dsigInput_r*s(:,wordJ-1)';
ds_cur = ds_cur + W_r'*dsigInput_r;
ds_cur = ds_cur + ds_cur_bk.*z(:,wordJ);
dz = ds_cur_bk.*(s(:,wordJ-1)-c(:,wordJ));
dsigInput_z = dz.*z(:,wordJ).*(1-z(:,wordJ));
db_z = db_z + dsigInput_z;
dU_z = dU_z + dsigInput_z*x(:,wordJ)'; %could be accelerated by avoiding add 0
dW_z = dW_z + dsigInput_z*s(:,wordJ-1)';
ds_cur = ds_cur + W_z'*dsigInput_z;
end
% s_1
dtanhInput = (ds_cur.*(1-z(:,1)).*(1-c(:,1).*c(:,1)));
db_c = db_c + dtanhInput;
dU_c = dU_c + dtanhInput*x(:,1)'; %could be accelerated by avoiding add 0
dW_c = dW_c + dtanhInput*(s_0.*r(:,1))';
dsr = W_c'*dtanhInput;
ds_0 = ds_0 + dsr.*r(:,1);
dsigInput_r = dsr.*s_0.*r(:,1).*(1-r(:,1));
db_r = db_r + dsigInput_r;
dU_r = dU_r + dsigInput_r*x(:,1)'; %could be accelerated by avoiding add 0
dW_r = dW_r + dsigInput_r*s_0';
ds_0 = ds_0 + W_r'*dsigInput_r;
ds_0 = ds_0 + ds_cur.*z(:,1);
dz = ds_cur.*(s_0-c(:,1));
dsigInput_z = dz.*z(:,1).*(1-z(:,1));
db_z = db_z + dsigInput_z;
dU_z = dU_z + dsigInput_z*x(:,1)'; %could be accelerated by avoiding add 0
dW_z = dW_z + dsigInput_z*s_0';
ds_0 = ds_0 + W_z'*dsigInput_z;
end
end
% Sigmoid function for neural network
function val = sigmoid(x)
val = sigmf(x,[1 0]);
end
% Fake a training data set: generate only one sentence for training.
%!!! Only for testing. Needs to be changed to read in training data from files.
function [x_t, y_t] = getTrainingData(vocabulary_size, sentence_size)
assert(vocabulary_size > 2); % for start and end of sentence symbol
assert(sentence_size > 0);
% define start and end of sentence in the vocabulary
SENTENCE_START = zeros(vocabulary_size, 1);
SENTENCE_START(1) = 1;
SENTENCE_END = zeros(vocabulary_size, 1);
SENTENCE_END(2) = 1;
% generate sentence:
x_t = zeros(vocabulary_size, sentence_size-1); % leave one slot for SENTENCE_START
for wordI = 1:sentence_size-1
% generate a random word excludes start and end symbol
x_t(randi(vocabulary_size-2,1,1)+2, wordI) = 1;
end
y_t = [x_t, SENTENCE_END]; % training output
x_t = [SENTENCE_START, x_t]; % training input
end
% Use numerical differentiation to approximate the gradient of each
% parameter and calculate the difference between these numerical results
% and our results calculated by applying chain rule.
function checkGradient_GRU(x, y, U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0, ...
dV, db_V, dU_z, dU_r, dU_c, dW_z, dW_r, dW_c, db_z, db_r, db_c, ds_0)
% Here we use the centre difference formula:
% df(x)/dx = (f(x+h)-f(x-h)) / (2h)
% It is a second order accurate method with error bounded by O(h^2)
h = 1e-5;
% NOTE: h couldn't be too large or too small since large h will
% introduce bigger truncation error and small h will introduce bigger
% roundoff error.
dV_numerical = zeros(size(dV));
% Calculate partial derivative element by element
for rowI = 1:size(dV_numerical,1)
for colI = 1:size(dV_numerical,2)
V_plus = V;
V_plus(rowI,colI) = V_plus(rowI,colI) + h;
V_minus = V;
V_minus(rowI,colI) = V_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V_plus, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V_minus, b_V, s_0);
dV_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dV_numerical-dV)./(abs(dV_numerical)+h))), ...
'dV relative error'); % prevent dividing by 0 by adding h
dU_c_numerical = zeros(size(dU_c));
for rowI = 1:size(dU_c_numerical,1)
for colI = 1:size(dU_c_numerical,2)
U_c_plus = U_c;
U_c_plus(rowI,colI) = U_c_plus(rowI,colI) + h;
U_c_minus = U_c;
U_c_minus(rowI,colI) = U_c_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c_plus, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c_minus, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
dU_c_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dU_c_numerical-dU_c)./(abs(dU_c_numerical)+h))), ...
'dU_c relative error');
dW_c_numerical = zeros(size(dW_c));
for rowI = 1:size(dW_c_numerical,1)
for colI = 1:size(dW_c_numerical,2)
W_c_plus = W_c;
W_c_plus(rowI,colI) = W_c_plus(rowI,colI) + h;
W_c_minus = W_c;
W_c_minus(rowI,colI) = W_c_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c_plus, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c_minus, b_z, b_r, b_c, V, b_V, s_0);
dW_c_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dW_c_numerical-dW_c)./(abs(dW_c_numerical)+h))), ...
'dW_c relative error');
dU_r_numerical = zeros(size(dU_r));
for rowI = 1:size(dU_r_numerical,1)
for colI = 1:size(dU_r_numerical,2)
U_r_plus = U_r;
U_r_plus(rowI,colI) = U_r_plus(rowI,colI) + h;
U_r_minus = U_r;
U_r_minus(rowI,colI) = U_r_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r_plus, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r_minus, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
dU_r_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dU_r_numerical-dU_r)./(abs(dU_r_numerical)+h))), ...
'dU_r relative error');
dW_r_numerical = zeros(size(dW_r));
for rowI = 1:size(dW_r_numerical,1)
for colI = 1:size(dW_r_numerical,2)
W_r_plus = W_r;
W_r_plus(rowI,colI) = W_r_plus(rowI,colI) + h;
W_r_minus = W_r;
W_r_minus(rowI,colI) = W_r_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r_plus, W_c, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r_minus, W_c, b_z, b_r, b_c, V, b_V, s_0);
dW_r_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dW_r_numerical-dW_r)./(abs(dW_r_numerical)+h))), ...
'dW_r relative error');
dU_z_numerical = zeros(size(dU_z));
for rowI = 1:size(dU_z_numerical,1)
for colI = 1:size(dU_z_numerical,2)
U_z_plus = U_z;
U_z_plus(rowI,colI) = U_z_plus(rowI,colI) + h;
U_z_minus = U_z;
U_z_minus(rowI,colI) = U_z_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z_plus, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z_minus, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
dU_z_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dU_z_numerical-dU_z)./(abs(dU_z_numerical)+h))), ...
'dU_z relative error');
dW_z_numerical = zeros(size(dW_z));
for rowI = 1:size(dW_z_numerical,1)
for colI = 1:size(dW_z_numerical,2)
W_z_plus = W_z;
W_z_plus(rowI,colI) = W_z_plus(rowI,colI) + h;
W_z_minus = W_z;
W_z_minus(rowI,colI) = W_z_minus(rowI,colI) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z_plus, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z_minus, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0);
dW_z_numerical(rowI,colI) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
end
display(sum(sum(abs(dW_z_numerical-dW_z)./(abs(dW_z_numerical)+h))), ...
'dW_z relative error');
db_z_numerical = zeros(size(db_z));
for i = 1:length(db_z_numerical)
b_z_plus = b_z;
b_z_plus(i) = b_z_plus(i) + h;
b_z_minus = b_z;
b_z_minus(i) = b_z_minus(i) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z_plus, b_r, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z_minus, b_r, b_c, V, b_V, s_0);
db_z_numerical(i) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
display(sum(abs(db_z_numerical-db_z)./(abs(db_z_numerical)+h)), ...
'db_z relative error');
db_r_numerical = zeros(size(db_r));
for i = 1:length(db_r_numerical)
b_r_plus = b_r;
b_r_plus(i) = b_r_plus(i) + h;
b_r_minus = b_r;
b_r_minus(i) = b_r_minus(i) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r_plus, b_c, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r_minus, b_c, V, b_V, s_0);
db_r_numerical(i) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
display(sum(abs(db_r_numerical-db_r)./(abs(db_r_numerical)+h)), ...
'db_r relative error');
db_c_numerical = zeros(size(db_c));
for i = 1:length(db_c_numerical)
b_c_plus = b_c;
b_c_plus(i) = b_c_plus(i) + h;
b_c_minus = b_c;
b_c_minus(i) = b_c_minus(i) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c_plus, V, b_V, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c_minus, V, b_V, s_0);
db_c_numerical(i) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
display(sum(abs(db_c_numerical-db_c)./(abs(db_c_numerical)+h)), ...
'db_c relative error');
db_V_numerical = zeros(size(db_V));
for i = 1:length(db_V_numerical)
b_V_plus = b_V;
b_V_plus(i) = b_V_plus(i) + h;
b_V_minus = b_V;
b_V_minus(i) = b_V_minus(i) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V_plus, s_0);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V_minus, s_0);
db_V_numerical(i) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
display(sum(abs(db_V_numerical-db_V)./(abs(db_V_numerical)+h)), ...
'db_V relative error');
ds_0_numerical = zeros(size(ds_0));
for i = 1:length(ds_0_numerical)
s_0_plus = s_0;
s_0_plus(i) = s_0_plus(i) + h;
s_0_minus = s_0;
s_0_minus(i) = s_0_minus(i) - h;
[~, ~, L_plus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0_plus);
[~, ~, L_minus] = forward(x, y, ...
U_z, U_r, U_c, W_z, W_r, W_c, b_z, b_r, b_c, V, b_V, s_0_minus);
ds_0_numerical(i) = (sum(L_plus) - sum(L_minus)) / 2 / h;
end
display(sum(abs(ds_0_numerical-ds_0)./(abs(ds_0_numerical)+h)), ...
'ds_0 relative error');
end