Skip to content

Commit f9ff658

Browse files
authored
Add example program on how to access internal layer parameters (#140)
1 parent e34671e commit f9ff658

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

example/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ foreach(execid
44
dense_mnist
55
dense_from_keras
66
get_set_network_params
7+
network_parameters
78
simple
89
sine
910
quadratic

example/network_parameters.f90

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
program network_parameters
2+
! This program demonstrates how to access network parameters (weights and
3+
! biases) from the layers' internal data structures.
4+
use nf, only: dense, input, layer, network
5+
use nf_conv2d_layer, only: conv2d_layer
6+
use nf_dense_layer, only: dense_layer
7+
8+
implicit none
9+
10+
type(network) :: net
11+
integer :: n
12+
13+
net = network([input(3), dense(5), dense(2)])
14+
15+
do n = 1, size(net % layers)
16+
print *, "Layer ", n, "is " // net % layers(n) % name
17+
select type (this_layer => net % layers(n) % p)
18+
type is (dense_layer)
19+
print *, " with weights of shape", shape(this_layer % weights)
20+
print *, " and ", size(this_layer % biases), " biases"
21+
print *, "Weights are:"
22+
print *, this_layer % weights
23+
type is (conv2d_layer)
24+
print *, " with kernel of shape", shape(this_layer % kernel)
25+
print *, " and ", size(this_layer % biases), " biases"
26+
print *, "Kernel is:"
27+
print *, this_layer % kernel
28+
class default
29+
print *, " with no parameters"
30+
end select
31+
end do
32+
33+
end program network_parameters

0 commit comments

Comments
 (0)