-
Notifications
You must be signed in to change notification settings - Fork 85
/
nf_network_submodule.f90
661 lines (502 loc) · 19.9 KB
/
nf_network_submodule.f90
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
submodule(nf_network) nf_network_submodule
use nf_conv2d_layer, only: conv2d_layer
use nf_dense_layer, only: dense_layer
use nf_flatten_layer, only: flatten_layer
use nf_input1d_layer, only: input1d_layer
use nf_input3d_layer, only: input3d_layer
use nf_maxpool2d_layer, only: maxpool2d_layer
use nf_reshape_layer, only: reshape3d_layer
use nf_io_hdf5, only: get_hdf5_dataset
use nf_keras, only: get_keras_h5_layers, keras_layer
use nf_layer, only: layer
use nf_layer_constructors, only: conv2d, dense, flatten, input, maxpool2d, reshape
use nf_loss, only: quadratic_derivative
use nf_optimizers, only: optimizer_base_type, sgd
use nf_parallel, only: tile_indices
use nf_activation, only: activation_function, &
elu, &
exponential, &
gaussian, &
linear, &
relu, &
leaky_relu, &
sigmoid, &
softmax, &
softplus, &
step, &
tanhf, &
celu
implicit none
contains
module function network_from_layers(layers) result(res)
type(layer), intent(in) :: layers(:)
type(network) :: res
integer :: n
! Error handling
! There must be at least two layers
if (size(layers) < 2) &
error stop 'Error: A network must have at least 2 layers.'
! The first layer must be an input layer
if (.not. layers(1) % name == 'input') &
error stop 'Error: First layer in the network must be an input layer.'
!TODO Ensure that the layers are in allowed sequence:
!TODO input1d -> dense
!TODO dense -> dense
!TODO input3d -> conv2d, maxpool2d, flatten
!TODO conv2d -> conv2d, maxpool2d, flatten
!TODO maxpool2d -> conv2d, maxpool2d, flatten
!TODO flatten -> dense
!TODO reshape -> conv2d, maxpool2d
res % layers = layers
! If connecting a 3-d output layer to a 1-d input layer without a flatten
! layer in between, insert a flatten layer.
n = 2
do while (n <= size(res % layers))
select type(this_layer => res % layers(n) % p)
type is(dense_layer)
select type(prev_layer => res % layers(n-1) % p)
type is(input3d_layer)
res % layers = [res % layers(:n-1), flatten(), res % layers(n:)]
n = n + 1
type is(conv2d_layer)
res % layers = [res % layers(:n-1), flatten(), res % layers(n:)]
n = n + 1
type is(maxpool2d_layer)
res % layers = [res % layers(:n-1), flatten(), res % layers(n:)]
n = n + 1
type is(reshape3d_layer)
res % layers = [res % layers(:n-1), flatten(), res % layers(n:)]
n = n + 1
class default
n = n + 1
end select
class default
n = n + 1
end select
end do
! Loop over each layer in order and call the init methods.
! This will allocate the data internal to each layer (e.g. weights, biases)
! according to the size of the previous layer.
do n = 2, size(res % layers)
call res % layers(n) % init(res % layers(n - 1))
end do
end function network_from_layers
module function network_from_keras(filename) result(res)
character(*), intent(in) :: filename
type(network) :: res
type(keras_layer), allocatable :: keras_layers(:)
type(layer), allocatable :: layers(:)
character(:), allocatable :: layer_name
character(:), allocatable :: object_name
integer :: n
keras_layers = get_keras_h5_layers(filename)
allocate(layers(size(keras_layers)))
do n = 1, size(layers)
select case(keras_layers(n) % class)
case('Conv2D')
if (keras_layers(n) % kernel_size(1) &
/= keras_layers(n) % kernel_size(2)) &
error stop 'Non-square kernel in conv2d layer not supported.'
layers(n) = conv2d( &
keras_layers(n) % filters, &
!FIXME add support for non-square kernel
keras_layers(n) % kernel_size(1), &
get_activation_by_name(keras_layers(n) % activation) &
)
case('Dense')
layers(n) = dense( &
keras_layers(n) % units(1), &
get_activation_by_name(keras_layers(n) % activation) &
)
case('Flatten')
layers(n) = flatten()
case('InputLayer')
if (size(keras_layers(n) % units) == 1) then
! input1d
layers(n) = input(keras_layers(n) % units(1))
else
! input3d
layers(n) = input(keras_layers(n) % units)
end if
case('MaxPooling2D')
if (keras_layers(n) % pool_size(1) &
/= keras_layers(n) % pool_size(2)) &
error stop 'Non-square pool in maxpool2d layer not supported.'
if (keras_layers(n) % strides(1) &
/= keras_layers(n) % strides(2)) &
error stop 'Unequal strides in maxpool2d layer are not supported.'
layers(n) = maxpool2d( &
!FIXME add support for non-square pool and stride
keras_layers(n) % pool_size(1), &
keras_layers(n) % strides(1) &
)
case('Reshape')
layers(n) = reshape(keras_layers(n) % target_shape)
case default
error stop 'This Keras layer is not supported'
end select
end do
res = network(layers)
! Loop over layers and read weights and biases from the Keras h5 file
! for each; currently only dense layers are implemented.
do n = 2, size(res % layers)
layer_name = keras_layers(n) % name
select type(this_layer => res % layers(n) % p)
type is(conv2d_layer)
! Read biases from file
object_name = '/model_weights/' // layer_name // '/' &
// layer_name // '/bias:0'
call get_hdf5_dataset(filename, object_name, this_layer % biases)
! Read weights from file
object_name = '/model_weights/' // layer_name // '/' &
// layer_name // '/kernel:0'
call get_hdf5_dataset(filename, object_name, this_layer % kernel)
type is(dense_layer)
! Read biases from file
object_name = '/model_weights/' // layer_name // '/' &
// layer_name // '/bias:0'
call get_hdf5_dataset(filename, object_name, this_layer % biases)
! Read weights from file
object_name = '/model_weights/' // layer_name // '/' &
// layer_name // '/kernel:0'
call get_hdf5_dataset(filename, object_name, this_layer % weights)
type is(flatten_layer)
! Nothing to do
continue
type is(maxpool2d_layer)
! Nothing to do
continue
type is(reshape3d_layer)
! Nothing to do
continue
class default
error stop 'Internal error in network_from_keras(); ' &
// 'mismatch in layer types between the Keras and ' &
// 'neural-fortran model layers.'
end select
end do
end function network_from_keras
pure function get_activation_by_name(activation_name) result(res)
! Workaround to get activation_function with some
! hardcoded default parameters by its name.
! Need this function since we get only activation name
! from keras files.
character(len=*), intent(in) :: activation_name
class(activation_function), allocatable :: res
select case(trim(activation_name))
case('elu')
allocate ( res, source = elu(alpha = 0.1) )
case('exponential')
allocate ( res, source = exponential() )
case('gaussian')
allocate ( res, source = gaussian() )
case('linear')
allocate ( res, source = linear() )
case('relu')
allocate ( res, source = relu() )
case('leaky_relu')
allocate ( res, source = leaky_relu(alpha = 0.1) )
case('sigmoid')
allocate ( res, source = sigmoid() )
case('softmax')
allocate ( res, source = softmax() )
case('softplus')
allocate ( res, source = softplus() )
case('step')
allocate ( res, source = step() )
case('tanh')
allocate ( res, source = tanhf() )
case('celu')
allocate ( res, source = celu() )
case default
error stop 'activation_name must be one of: ' // &
'"elu", "exponential", "gaussian", "linear", "relu", ' // &
'"leaky_relu", "sigmoid", "softmax", "softplus", "step", "tanh" or "celu".'
end select
end function get_activation_by_name
pure module subroutine backward(self, output)
class(network), intent(in out) :: self
real, intent(in) :: output(:)
integer :: n, num_layers
num_layers = size(self % layers)
! Iterate backward over layers, from the output layer
! to the first non-input layer
do n = num_layers, 2, -1
if (n == num_layers) then
! Output layer; apply the loss function
select type(this_layer => self % layers(n) % p)
type is(dense_layer)
call self % layers(n) % backward( &
self % layers(n - 1), &
quadratic_derivative(output, this_layer % output) &
)
end select
else
! Hidden layer; take the gradient from the next layer
select type(next_layer => self % layers(n + 1) % p)
type is(dense_layer)
call self % layers(n) % backward(self % layers(n - 1), next_layer % gradient)
type is(flatten_layer)
call self % layers(n) % backward(self % layers(n - 1), next_layer % gradient)
type is(conv2d_layer)
call self % layers(n) % backward(self % layers(n - 1), next_layer % gradient)
type is(maxpool2d_layer)
call self % layers(n) % backward(self % layers(n - 1), next_layer % gradient)
end select
end if
end do
end subroutine backward
pure module subroutine forward_1d(self, input)
class(network), intent(in out) :: self
real, intent(in) :: input(:)
integer :: n
! Set the input array into the input layer
select type(input_layer => self % layers(1) % p); type is(input1d_layer)
call input_layer % set(input)
end select
do n = 2, size(self % layers)
call self % layers(n) % forward(self % layers(n - 1))
end do
end subroutine forward_1d
pure module subroutine forward_3d(self, input)
class(network), intent(in out) :: self
real, intent(in) :: input(:,:,:)
integer :: n
! Set the input array into the input layer
select type(input_layer => self % layers(1) % p); type is(input3d_layer)
call input_layer % set(input)
end select
do n = 2, size(self % layers)
call self % layers(n) % forward(self % layers(n - 1))
end do
end subroutine forward_3d
module function predict_1d(self, input) result(res)
class(network), intent(in out) :: self
real, intent(in) :: input(:)
real, allocatable :: res(:)
integer :: num_layers
num_layers = size(self % layers)
call self % forward(input)
select type(output_layer => self % layers(num_layers) % p)
type is(dense_layer)
res = output_layer % output
type is(flatten_layer)
res = output_layer % output
class default
error stop 'network % output not implemented for this output layer'
end select
end function predict_1d
module function predict_3d(self, input) result(res)
class(network), intent(in out) :: self
real, intent(in) :: input(:,:,:)
real, allocatable :: res(:)
integer :: num_layers
num_layers = size(self % layers)
call self % forward(input)
select type(output_layer => self % layers(num_layers) % p)
type is(conv2d_layer)
!FIXME flatten the result for now; find a better solution
res = pack(output_layer % output, .true.)
type is(dense_layer)
res = output_layer % output
type is(flatten_layer)
res = output_layer % output
class default
error stop 'network % output not implemented for this output layer'
end select
end function predict_3d
module function predict_batch_1d(self, input) result(res)
class(network), intent(in out) :: self
real, intent(in) :: input(:,:)
real, allocatable :: res(:,:)
integer :: i, batch_size, num_layers, output_size
num_layers = size(self % layers)
batch_size = size(input, dim=rank(input))
output_size = product(self % layers(num_layers) % layer_shape)
allocate(res(output_size, batch_size))
batch: do concurrent(i = 1:size(res, dim=2))
call self % forward(input(:,i))
select type(output_layer => self % layers(num_layers) % p)
type is(dense_layer)
res(:,i) = output_layer % output
type is(flatten_layer)
res(:,i) = output_layer % output
class default
error stop 'network % output not implemented for this output layer'
end select
end do batch
end function predict_batch_1d
module function predict_batch_3d(self, input) result(res)
class(network), intent(in out) :: self
real, intent(in) :: input(:,:,:,:)
real, allocatable :: res(:,:)
integer :: i, batch_size, num_layers, output_size
num_layers = size(self % layers)
batch_size = size(input, dim=rank(input))
output_size = product(self % layers(num_layers) % layer_shape)
allocate(res(output_size, batch_size))
batch: do concurrent(i = 1:batch_size)
call self % forward(input(:,:,:,i))
select type(output_layer => self % layers(num_layers) % p)
type is(conv2d_layer)
!FIXME flatten the result for now; find a better solution
res(:,i) = pack(output_layer % output, .true.)
type is(dense_layer)
res(:,i) = output_layer % output
type is(flatten_layer)
res(:,i) = output_layer % output
class default
error stop 'network % output not implemented for this output layer'
end select
end do batch
end function predict_batch_3d
module subroutine print_info(self)
class(network), intent(in) :: self
call self % layers % print_info()
end subroutine print_info
pure module function get_num_params(self)
class(network), intent(in) :: self
integer :: get_num_params
get_num_params = sum(self % layers % get_num_params())
end function get_num_params
pure module function get_params(self) result(params)
class(network), intent(in) :: self
real, allocatable :: params(:)
integer :: n, nstart, nend
allocate(params(self % get_num_params()))
nstart = 1
do n = 1, size(self % layers)
if (self % layers(n) % get_num_params() < 1) cycle
nend = nstart + self % layers(n) % get_num_params() - 1
params(nstart:nend) = self % layers(n) % get_params()
nstart = nend + 1
end do
end function get_params
pure module function get_gradients(self) result(gradients)
class(network), intent(in) :: self
real, allocatable :: gradients(:)
integer :: n, nstart, nend
allocate(gradients(self % get_num_params()))
nstart = 1
do n = 1, size(self % layers)
if (self % layers(n) % get_num_params() < 1) cycle
nend = nstart + self % layers(n) % get_num_params() - 1
gradients(nstart:nend) = self % layers(n) % get_gradients()
nstart = nend + 1
end do
end function get_gradients
module subroutine set_params(self, params)
class(network), intent(in out) :: self
real, intent(in) :: params(:)
integer :: n, nstart, nend
! Check that the number of parameters is correct.
if (size(params) /= self % get_num_params()) then
error stop 'network % set_params: number of parameters does not match.'
end if
nstart = 1
do n = 1, size(self % layers)
nend = nstart + self % layers(n) % get_num_params() - 1
if (nend - nstart < 1) cycle
call self % layers(n) % set_params(params(nstart:nend))
nstart = nend + 1
end do
end subroutine set_params
module subroutine train(self, input_data, output_data, batch_size, &
epochs, optimizer)
class(network), intent(in out) :: self
real, intent(in) :: input_data(:,:)
real, intent(in) :: output_data(:,:)
integer, intent(in) :: batch_size
integer, intent(in) :: epochs
class(optimizer_base_type), intent(in), optional :: optimizer
class(optimizer_base_type), allocatable :: optimizer_
real :: pos
integer :: dataset_size
integer :: batch_start, batch_end
integer :: i, j, n
integer :: istart, iend, indices(2)
! Passing the optimizer instance is optional.
! If not provided, we default to SGD with its default settings.
if (present(optimizer)) then
self % optimizer = optimizer
else
self % optimizer = sgd()
end if
call self % optimizer % init(self % get_num_params())
dataset_size = size(output_data, dim=2)
epoch_loop: do n = 1, epochs
batch_loop: do i = 1, dataset_size / batch_size
! Pull a random mini-batch from the dataset
call random_number(pos)
batch_start = int(pos * (dataset_size - batch_size + 1)) + 1
batch_end = batch_start + batch_size - 1
! FIXME shuffle in a way that doesn't require co_broadcast
call co_broadcast(batch_start, 1)
call co_broadcast(batch_end, 1)
! Distribute the batch in nearly equal pieces to all images
indices = tile_indices(batch_size)
istart = indices(1) + batch_start - 1
iend = indices(2) + batch_start - 1
do concurrent(j = istart:iend)
call self % forward(input_data(:,j))
call self % backward(output_data(:,j))
end do
call self % update(batch_size=batch_size)
end do batch_loop
end do epoch_loop
end subroutine train
module subroutine update(self, optimizer, batch_size)
class(network), intent(in out) :: self
class(optimizer_base_type), intent(in), optional :: optimizer
integer, intent(in), optional :: batch_size
class(optimizer_base_type), allocatable :: optimizer_
integer :: batch_size_
real, allocatable :: params(:)
integer :: n
! Passing the optimizer instance is optional. If not provided, and if the
! optimizer has not already been set, we default to the default SGD. The
! instantiation and initialization below of the optimizer is normally done
! at the beginning of the network % train() method. However, if the user
! wants to call network % update() directly, for example if they use their
! own custom mini-batching routine, we initialize the optimizer here as
! well. If it's initialized already, this step is a cheap no-op.
if (.not. allocated(self % optimizer)) then
if (present(optimizer)) then
self % optimizer = optimizer
else
self % optimizer = sgd()
end if
call self % optimizer % init(self % get_num_params())
end if
if (present(batch_size)) then
batch_size_ = batch_size
else
batch_size_ = 1
end if
! Sum weight and bias gradients across images, if any
do n = 2, size(self % layers)
select type(this_layer => self % layers(n) % p)
type is(dense_layer)
call co_sum(this_layer % dw)
call co_sum(this_layer % db)
type is(conv2d_layer)
call co_sum(this_layer % dw)
call co_sum(this_layer % db)
end select
end do
params = self % get_params()
call self % optimizer % minimize(params, self % get_gradients() / batch_size_)
call self % set_params(params)
! Flush network gradients to zero.
do concurrent(n = 2:size(self % layers))
select type(this_layer => self % layers(n) % p)
type is(dense_layer)
this_layer % dw = 0
this_layer % db = 0
type is(conv2d_layer)
this_layer % dw = 0
this_layer % db = 0
end select
end do
end subroutine update
end submodule nf_network_submodule