-
Notifications
You must be signed in to change notification settings - Fork 100
Get set network parameters #111
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
milancurcic
merged 14 commits into
modern-fortran:main
from
milancurcic:get-set-network-parameters
Dec 22, 2022
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
ea65652
Add an exampple to get/set network parameters
milancurcic 565db0e
Bump version
milancurcic 3224282
Add get_num_params, get_params, and set_params implementations
milancurcic 31fc20a
Make get_params() a function;
milancurcic e26007d
Tidy up example
milancurcic a02dcdd
Make layer % get_num_parameters() elemental
milancurcic 026da00
Begin test suite for getting and setting network params
milancurcic 08a6594
Simplify network % get_num_params()
milancurcic 952f373
Warn on stderr if the user attempts to set_params to a non-zero param…
milancurcic 828e13c
Skip no-op layer % set_params() calls
milancurcic 996b42a
Test getting and setting parameters
milancurcic 25c917f
Check that the size of parameters match those of the layer
milancurcic 80dc664
Print number of parameters in layer % print_info()
milancurcic eb9c1a9
Tidy up the exampel
milancurcic File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,6 +3,7 @@ foreach(execid | |
| cnn_from_keras | ||
| dense_mnist | ||
| dense_from_keras | ||
| get_set_network_params | ||
| simple | ||
| sine | ||
| ) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| program get_set_network_params | ||
| use nf, only: dense, input, network | ||
| 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, nparam | ||
| real, allocatable :: parameters(:) | ||
|
|
||
| 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(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 '(a)', 'Original and cloned network outputs match:', all(ypred1 == ypred2) | ||
|
|
||
| end program get_set_network_params | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| name = "neural-fortran" | ||
| version = "0.9.0" | ||
| version = "0.10.0" | ||
| license = "MIT" | ||
| author = "Milan Curcic" | ||
| maintainer = "[email protected]" | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.