Skip to content

Commit 7d81f7b

Browse files
authored
Merge pull request #14 from modern-fortran/12-loading-network-problems
add example that shows saving a trained network to a file, then loadi…
2 parents a6bbeff + ef33c15 commit 7d81f7b

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ foreach(execid mnist network_save network_sync set_activation_function)
7878
add_test(test_${execid} bin/test_${execid})
7979
endforeach()
8080

81-
foreach(execid mnist simple sine)
81+
foreach(execid mnist save_and_load simple sine)
8282
add_executable(example_${execid} src/tests/example_${execid}.f90)
8383
target_link_libraries(example_${execid} neural ${LIBS})
8484
add_test(example_${execid} bin/example_${execid})
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
program example_save_and_load
2+
3+
use mod_network, only: network_type
4+
implicit none
5+
6+
type(network_type) :: net1, net2
7+
real, allocatable :: input(:), output(:)
8+
integer :: i
9+
10+
net1 = network_type([3, 5, 2])
11+
12+
input = [0.2, 0.4, 0.6]
13+
output = [0.123456, 0.246802]
14+
15+
! train network 1
16+
do i = 1, 500
17+
call net1 % train(input, output, eta=1.0)
18+
end do
19+
20+
! save network 1 to file
21+
call net1 % save('my_simple_net.txt')
22+
23+
! load network 2 from file
24+
!net2 = network_type([3, 5, 2])
25+
call net2 % load('my_simple_net.txt')
26+
call net2 % set_activation('sigmoid')
27+
28+
print *, 'Network 1 output: ', net1 % output(input)
29+
print *, 'Network 2 output: ', net2 % output(input)
30+
print *, 'Outputs match: ', all(net1 % output(input) == net2 % output(input))
31+
32+
end program example_save_and_load

0 commit comments

Comments
 (0)