-
Notifications
You must be signed in to change notification settings - Fork 82
/
get_set_network_params.f90
92 lines (75 loc) · 2.66 KB
/
get_set_network_params.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
program get_set_network_params
use nf, only: dense, input, network
use nf_optimizers, only: sgd
implicit none
type(network) :: net1, net2
real :: x(1), y(1)
real, parameter :: pi = 4 * atan(1.)
integer, parameter :: num_iterations = 100000
integer, parameter :: test_size = 30
real :: xtest(test_size), ytest(test_size)
real :: ypred1(test_size), ypred2(test_size)
integer :: i, n
print '("Getting and setting network parameters")'
print '(60("="))'
print *
print '(a)', 'First, let''s instantiate small dense network net1'
print '(a)', 'of shape (1,5,1) and fit it to a sine function:'
print *
net1 = network([ &
input(1), &
dense(5), &
dense(1) &
])
call net1 % print_info()
xtest = [((i - 1) * 2 * pi / test_size, i=1, test_size)]
ytest = (sin(xtest) + 1) / 2
do n = 0, num_iterations
call random_number(x)
x = x * 2 * pi
y = (sin(x) + 1) / 2
call net1 % forward(x)
call net1 % backward(y)
call net1 % update(sgd(learning_rate=1.))
if (mod(n, 10000) == 0) then
ypred1 = [(net1 % predict([xtest(i)]), i=1, test_size)]
print '(a,i0,1x,f9.6)', 'Number of iterations, loss: ', &
n, sum((ypred1 - ytest)**2) / size(ypred1)
end if
end do
print *
print '(a)', 'Now, let''s see how many network parameters there are'
print '(a)', 'by printing the result of net1 % get_num_params():'
print *
print '("net1 % get_num_params() = ", i0)', net1 % get_num_params()
print *
print '(a)', 'We can see the values of the network parameters'
print '(a)', 'by printing the result of net1 % get_params():'
print *
print '("net1 % get_params() = ", *(g0,1x))', net1 % get_params()
print *
print '(a)', 'Now, let''s create another network of the same shape and set'
print '(a)', 'the parameters from the original network to it'
print '(a)', 'by calling call net2 % set_params(net1 % get_params()):'
net2 = network([ &
input(1), &
dense(5), &
dense(1) &
])
! Set the parameters of net1 to net2
call net2 % set_params(net1 % get_params())
print *
print '(a)', 'We can check that the second network now has the same'
print '(a)', 'parameters as net1:'
print *
print '("net2 % get_params() = ", *(g0,1x))', net2 % get_params()
ypred1 = [(net1 % predict([xtest(i)]), i=1, test_size)]
ypred2 = [(net2 % predict([xtest(i)]), i=1, test_size)]
print *
print '(a)', 'We can also check that the two networks produce the same output:'
print *
print '("net1 output: ", *(g0,1x))', ypred1
print '("net2 output: ", *(g0,1x))', ypred2
print *
print '("Original and cloned network outputs match: ",l)', all(ypred1 == ypred2)
end program get_set_network_params