Skip to content

Commit 1bef0aa

Browse files
committed
wip: A simple RNN example
1 parent c77f3eb commit 1bef0aa

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

example/simple_rnn.f90

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
program simple_rnn
2+
use nf, only: dense, input, network, rnn, sgd
3+
implicit none
4+
type(network) :: net
5+
real, allocatable :: x(:), y(:), p(:)
6+
integer, parameter :: num_iterations = 1000
7+
integer :: n, l
8+
9+
allocate(p(2))
10+
11+
print '("Simple RNN")'
12+
print '(60("="))'
13+
14+
net = network([ &
15+
input(3), &
16+
rnn(5), &
17+
rnn(1) &
18+
])
19+
20+
call net % print_info()
21+
22+
x = [0.2, 0.4, 0.6]
23+
y = [0.123456, 0.246802]
24+
25+
do n = 0, num_iterations
26+
27+
do l = 1, size(net % layers)
28+
if (net % layers(l) % name == 'rnn') call net % layers(l) % set_state()
29+
end do
30+
31+
if (mod(n, 100) == 0) then
32+
p(1:1) = net % predict(x)
33+
p(2:2) = net % predict(x)
34+
print '(i4,2(3x,f8.6))', n, p
35+
36+
else
37+
38+
call net % forward(x)
39+
call net % backward(y(1:1))
40+
call net % update(optimizer=sgd(learning_rate=.001))
41+
call net % forward(x)
42+
call net % backward(y(2:2))
43+
call net % update(optimizer=sgd(learning_rate=.001))
44+
end if
45+
46+
end do
47+
48+
end program simple_rnn

0 commit comments

Comments
 (0)