-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(training_configuration_t): add type, JSON I/O
- Loading branch information
Showing
7 changed files
with
182 additions
and
8 deletions.
There are no files selected for viewing
This file contains 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 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 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,53 @@ | ||
module training_configuration_m | ||
use sourcery_m, only : string_t, file_t | ||
use hyperparameters_m, only : hyperparameters_t | ||
use network_configuration_m, only : network_configuration_t | ||
implicit none | ||
|
||
private | ||
public :: training_configuration_t | ||
|
||
type, extends(file_t) :: training_configuration_t | ||
private | ||
type(hyperparameters_t) hyperparameters_ | ||
type(network_configuration_t) network_configuration_ | ||
contains | ||
procedure :: to_json | ||
procedure :: equals | ||
generic :: operator(==) => equals | ||
end type | ||
|
||
interface training_configuration_t | ||
|
||
module function from_components(hyperparameters, network_configuration) result(training_configuration) | ||
implicit none | ||
type(hyperparameters_t), intent(in) :: hyperparameters | ||
type(network_configuration_t), intent(in) :: network_configuration | ||
type(training_configuration_t) training_configuration | ||
end function | ||
|
||
module function from_file(file_object) result(training_configuration) | ||
implicit none | ||
type(file_t), intent(in) :: file_object | ||
type(training_configuration_t) training_configuration | ||
end function | ||
|
||
end interface | ||
|
||
interface | ||
|
||
pure module function to_json(self) result(json_lines) | ||
implicit none | ||
class(training_configuration_t), intent(in) :: self | ||
type(string_t), allocatable :: json_lines(:) | ||
end function | ||
|
||
elemental module function equals(lhs, rhs) result(lhs_eq_rhs) | ||
implicit none | ||
class(training_configuration_t), intent(in) :: lhs, rhs | ||
logical lhs_eq_rhs | ||
end function | ||
|
||
end interface | ||
|
||
end module |
This file contains 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,49 @@ | ||
submodule(training_configuration_m) training_configuration_s | ||
use assert_m, only : assert | ||
implicit none | ||
|
||
character(len=*), parameter :: header="{", footer="}", separator = "," | ||
|
||
contains | ||
|
||
module procedure from_components | ||
|
||
training_configuration%hyperparameters_ = hyperparameters | ||
training_configuration%network_configuration_ = network_configuration | ||
training_configuration%file_t = file_t([ & | ||
string_t(header), & | ||
training_configuration%hyperparameters_%to_json(), & | ||
string_t(separator), & | ||
training_configuration%network_configuration_%to_json(), & | ||
string_t(footer) & | ||
]) | ||
end procedure | ||
|
||
module procedure from_file | ||
integer, parameter :: hyperparameters_start=2, hyperparameters_end=6, separator_line=7 ! line numbers | ||
integer, parameter :: net_config_start=8, net_config_end=12 ! line numbers | ||
integer, parameter :: file_start=hyperparameters_start-1, file_end=net_config_end+1 ! line numbers | ||
|
||
training_configuration%file_t = file_object | ||
|
||
associate(lines => training_configuration%file_t%lines()) | ||
call assert(trim(adjustl(lines(file_start)%string()))==header,"training_configuration_s(from_file): header",lines(file_start)) | ||
training_configuration%hyperparameters_ = hyperparameters_t(lines(hyperparameters_start:hyperparameters_end)) | ||
call assert(trim(adjustl(lines(separator_line)%string()))==separator,"training_configuration_s(from_file): separator", & | ||
lines(file_start)) | ||
training_configuration%network_configuration_= network_configuration_t(lines(net_config_start:net_config_end)) | ||
call assert(trim(adjustl(lines(file_end)%string()))==footer, "training_configuration_s(from_file): footer", lines(file_end)) | ||
end associate | ||
end procedure | ||
|
||
module procedure to_json | ||
json_lines = self%lines() | ||
end procedure | ||
|
||
module procedure equals | ||
lhs_eq_rhs = & | ||
lhs%hyperparameters_ == rhs%hyperparameters_ .and. & | ||
lhs%network_configuration_ == rhs%network_configuration_ | ||
end procedure | ||
|
||
end submodule training_configuration_s |
This file contains 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 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 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,68 @@ | ||
! Copyright (c), The Regents of the University of California | ||
! Terms of use are as specified in LICENSE.txt | ||
module training_configuration_test_m | ||
!! Test training_configuration_t object I/O and construction | ||
|
||
! External dependencies | ||
use assert_m, only : assert | ||
use sourcery_m, only : string_t, test_t, test_result_t, file_t | ||
use inference_engine_m, only : training_configuration_t, hyperparameters_t, network_configuration_t | ||
|
||
! Internal dependencies | ||
use training_configuration_m, only : training_configuration_t | ||
|
||
implicit none | ||
|
||
private | ||
public :: training_configuration_test_t | ||
|
||
type, extends(test_t) :: training_configuration_test_t | ||
contains | ||
procedure, nopass :: subject | ||
procedure, nopass :: results | ||
end type | ||
|
||
contains | ||
|
||
pure function subject() result(specimen) | ||
character(len=:), allocatable :: specimen | ||
specimen = "A training_configuration_t object" | ||
end function | ||
|
||
function results() result(test_results) | ||
type(test_result_t), allocatable :: test_results(:) | ||
|
||
character(len=*), parameter :: longest_description = & | ||
"component-wise construction followed by conversion to and from JSON" | ||
|
||
associate( & | ||
descriptions => & | ||
[ character(len=len(longest_description)) :: & | ||
"component-wise construction followed by conversion to and from JSON" & | ||
], & | ||
outcomes => & | ||
[ construct_and_convert_to_and_from_json() & | ||
] & | ||
) | ||
call assert(size(descriptions) == size(outcomes),"training_configuration_test_m(results): size(descriptions)==size(outcomes)") | ||
test_results = test_result_t(descriptions, outcomes) | ||
end associate | ||
|
||
end function | ||
|
||
function construct_and_convert_to_and_from_json() result(test_passes) | ||
logical test_passes | ||
|
||
|
||
associate(training_configuration => training_configuration_t( & | ||
hyperparameters_t(mini_batches=5, learning_rate=1., optimizer = "adam"), & | ||
network_configuration_t(skip_connections=.false., nodes_per_layer=[2,72,2], activation_function="sigmoid") & | ||
)) | ||
associate(from_json => training_configuration_t(file_t(training_configuration%to_json()))) | ||
test_passes = training_configuration == from_json | ||
end associate | ||
end associate | ||
|
||
end function | ||
|
||
end module training_configuration_test_m |