Skip to content

Commit

Permalink
Merge pull request #60 from BerkeleyLab/fix-construction-from-json
Browse files Browse the repository at this point in the history
Fix construction from json
  • Loading branch information
rouson authored May 30, 2023
2 parents 843faa5 + 55c6420 commit 1fd4a06
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 72 deletions.
11 changes: 9 additions & 2 deletions src/inference_engine/inference_engine_s.f90
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,22 @@
end block

inference_engine%input_weights_ = transpose(hidden_layers%input_weights())
call assert(hidden_layers%next_allocated(), "inference_engine_t%from_json: next layer exists")

block
type(layer_t), pointer :: next_layer
real(rkind), allocatable :: transposed(:,:,:)
integer layer

next_layer => hidden_layers%next_pointer()
hidden_weights = next_layer%hidden_weights()

if (hidden_layers%next_allocated()) then
hidden_weights = next_layer%hidden_weights()
else
associate(neurons_per_layer => size(inference_engine%input_weights_,1)) ! keep consistent with the eponymous function
allocate(hidden_weights(neurons_per_layer, neurons_per_layer, 0))
end associate
end if

inference_engine%biases_ = hidden_layers%hidden_biases()

allocate(transposed(size(hidden_weights,2), size(hidden_weights,1), size(hidden_weights,3)))
Expand Down
52 changes: 31 additions & 21 deletions test/asymmetric_engine_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
! Terms of use are as specified in LICENSE.txt
module asymmetric_engine_test_m
!! Define asymmetric tests and procedures required for reporting results
use assert_m, only : assert
use string_m, only : string_t
use test_m, only : test_t
use test_result_m, only : test_result_t
Expand Down Expand Up @@ -36,27 +37,36 @@ pure function subject() result(specimen)
function results() result(test_results)
type(test_result_t), allocatable :: test_results(:)

test_results = test_result_t( &
[ character(len=len("mapping (false,false) to false using the concurrent_dot_products_t() inference strategy")) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to false using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy", &
"counting the number of hidden layers", &
"counting the number of neurons per layer", &
"counting the number of inputs", &
"counting the number of outputs", &
"getting the activation function name" &
], &
[ xor_and_2nd_input_truth_table(concurrent_dot_products_t()), xor_and_2nd_input_truth_table(matmul_t()), &
test_num_hidden_layers(), test_neurons_per_layer(), test_num_inputs(), test_num_outputs(), &
test_activation_name() &
] &
)
character(len=*), parameter :: longest_description = &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy"

associate( &
descriptions => &
[ character(len=len(longest_description)) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to false using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy", &
"counting the number of hidden layers", &
"counting the number of neurons per layer", &
"counting the number of inputs", &
"counting the number of outputs", &
"getting the activation function name" &
], &
outcomes => &
[ xor_and_2nd_input_truth_table(concurrent_dot_products_t()), xor_and_2nd_input_truth_table(matmul_t()), &
test_num_hidden_layers(), test_neurons_per_layer(), test_num_inputs(), test_num_outputs(), &
test_activation_name() &
] &
)
call assert(size(descriptions) == size(outcomes),"assymetric_engine_test_m(results): size(descriptions) == size(outcomes)")
test_results = test_result_t(descriptions, outcomes)
end associate

end function

function xor_and_2nd_input_network() result(inference_engine)
Expand Down
59 changes: 40 additions & 19 deletions test/inference_engine_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
! Terms of use are as specified in LICENSE.txt
module inference_engine_test_m
!! Define inference tests and procedures required for reporting results
use assert_m, only : assert
use string_m, only : string_t
use test_m, only : test_t
use test_result_m, only : test_result_t
Expand Down Expand Up @@ -36,24 +37,33 @@ pure function subject() result(specimen)
function results() result(test_results)
type(test_result_t), allocatable :: test_results(:)

test_results = test_result_t( &
[ character(len=len("mapping (false,false) to false using the concurrent_dot_products_t() inference strategy")) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to true using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy", &
"converting to and from JSON format", &
"performing inference with encapsulated inputs and outputs", &
"performing inference with a single-layer perceptron" &
], &
[ convert_to_and_from_json(), xor_truth_table(concurrent_dot_products_t()), xor_truth_table(matmul_t()), &
elemental_inference(), single_layer_inference() &
] &
character(len=*), parameter :: longest_description = &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy"

associate( &
descriptions => &
[ character(len=len(longest_description)) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to true using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy", &
"performing elemental inference with encapsulated inputs and outputs", &
"performing inference with a single-hidden-layer network", &
"converting a single-hidden-layer network to and from JSON format", &
"converting a multi-hidden-layer network to and from JSON format" &
], &
outcomes => &
[ xor_truth_table(concurrent_dot_products_t()), xor_truth_table(matmul_t()), elemental_inference(), &
single_hidden_layer_inference(), single_hidden_layer_net_to_from_json(), multi_hidden_layer_net_to_from_json() &
] &
)
call assert(size(descriptions) == size(outcomes), "inference_engine_test(results): size(descriptions) == size(outcomes)")
test_results = test_result_t(descriptions, outcomes)
end associate
end function

function single_layer_perceptron() result(inference_engine)
Expand All @@ -73,7 +83,7 @@ function single_layer_perceptron() result(inference_engine)
)
end function

function single_layer_inference() result(test_passes)
function single_hidden_layer_inference() result(test_passes)
logical, allocatable :: test_passes(:)
type(inference_engine_t) inference_engine

Expand Down Expand Up @@ -114,7 +124,7 @@ function xor_network() result(inference_engine)
)
end function

function convert_to_and_from_json() result(test_passes)
function multi_hidden_layer_net_to_from_json() result(test_passes)
logical, allocatable :: test_passes
type(inference_engine_t) xor, difference
real, parameter :: tolerance = 1.0E-06
Expand All @@ -124,6 +134,17 @@ function convert_to_and_from_json() result(test_passes)
test_passes = difference%norm() < tolerance
end function

function single_hidden_layer_net_to_from_json() result(test_passes)
logical, allocatable :: test_passes
type(inference_engine_t) one_hidden_layer_network, difference

real, parameter :: tolerance = 1.0E-06

one_hidden_layer_network = single_layer_perceptron()
difference = inference_engine_t(one_hidden_layer_network%to_json()) - one_hidden_layer_network
test_passes = difference%norm() < tolerance
end function

function xor_truth_table(inference_strategy) result(test_passes)
logical, allocatable :: test_passes(:)
class(inference_strategy_t), intent(in), optional :: inference_strategy
Expand Down
35 changes: 21 additions & 14 deletions test/skip_connections_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -33,20 +33,27 @@ pure function subject() result(specimen)
function results() result(test_results)
type(test_result_t), allocatable :: test_results(:)

test_results = test_result_t( &
[ character(len=len("mapping (false,false) to false using the concurrent_dot_products_t() inference strategy")) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to false using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy" &
], &
[ not_1st_and_2nd_truth_table(concurrent_dot_products_t()), not_1st_and_2nd_truth_table(matmul_t()) &
] &
)
character(len=*), parameter :: longest_description = &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy"

associate( &
descriptions => &
[ character(len=len(longest_description)) :: &
"mapping (true,true) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (false,true) to true using the concurrent_dot_products_t() inference strategy", &
"mapping (false,false) to false using the concurrent_dot_products_t() inference strategy", &
"mapping (true,true) to false using the matmul_t() inference strategy", &
"mapping (true,false) to false using the matmul_t() inference strategy", &
"mapping (false,true) to true using the matmul_t() inference strategy", &
"mapping (false,false) to false using the matmul_t() inference strategy" &
], &
outcomes => [not_1st_and_2nd_truth_table(concurrent_dot_products_t()), not_1st_and_2nd_truth_table(matmul_t()) ] &
)
call assert(size(descriptions) == size(outcomes), "skip_connections_test_m(results): size(descriptions) == size(outcomes)")
test_results = test_result_t(descriptions, outcomes)
end associate

end function


Expand Down
35 changes: 19 additions & 16 deletions test/trainable_engine_test_m.f90
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
! Terms of use are as specified in LICENSE.txt
module trainable_engine_test_m
!! Define inference tests and procedures required for reporting results
use assert_m, only : assert
use intrinsic_array_m, only : intrinsic_array_t
use string_m, only : string_t
use test_m, only : test_t
use test_result_m, only : test_result_t
Expand Down Expand Up @@ -29,30 +31,31 @@ module trainable_engine_test_m

pure function subject() result(specimen)
character(len=:), allocatable :: specimen
specimen = "A trainable_engine_t (this will take a few minutes)"
specimen = "A trainable_engine_t"
end function

function results() result(test_results)
type(test_result_t), allocatable :: test_results(:)

character(len=*), parameter :: longest_description = &
"learning the mapping (false,false) -> false when trained on a fixed input/output pair"
"learning the mapping (false,false) -> false when trained on a fixed input/output pair"

test_results = test_result_t( &
[character(len=len(longest_description)) :: &
"learning the mapping (true,true) -> false when trained on a fixed input/output pair", &
"learning the mapping (false,true) -> true when trained on a fixed input/output pair", &
"learning the mapping (true,false) -> true when trained on a fixed input/output pair", &
"learning the mapping (false,false) -> false when trained on a fixed input/output pair", &
"learning the mapping (true,true) -> false trained on mini-batches", &
"learning the mapping (false,true) -> true trained on mini-batches", &
"learning the mapping (true,false) -> true trained on mini-batches", &
"learning the mapping (false,false) -> false trained on mini-batches" &
], &
[train_on_fixed_input_output_pair(), &
train_on_truth_table_mini_batch() &
] &
associate( &
descriptions => &
[ character(len=len(longest_description)) :: &
"learning the mapping (true,true) -> false when trained on a fixed input/output pair", &
"learning the mapping (false,true) -> true when trained on a fixed input/output pair", &
"learning the mapping (true,false) -> true when trained on a fixed input/output pair", &
"learning the mapping (false,false) -> false when trained on a fixed input/output pair", &
"learning the mapping (true,true) -> false trained on mini-batches", &
"learning the mapping (false,true) -> true trained on mini-batches", &
"learning the mapping (true,false) -> true trained on mini-batches", &
"learning the mapping (false,false) -> false trained on mini-batches" &
], outcomes => [train_on_fixed_input_output_pair(), train_on_truth_table_mini_batch()] &
)
call assert(size(descriptions) == size(outcomes), "trainable_engine_test_m(results): size(descritions) == size(outcomes)")
test_results = test_result_t(descriptions, outcomes)
end associate
end function

function trainable_hidden_layer() result(trainable_engine)
Expand Down

0 comments on commit 1fd4a06

Please sign in to comment.