Skip to content
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

Read & perform inference on networks for which the hidden-layer width varies across layers #166

Merged
merged 14 commits into from
Jul 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 56 additions & 0 deletions example/read-query-infer.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
! Copyright (c), The Regents of the University of California
! Terms of use are as specified in LICENSE.txt
program read_query_infer
!! This program demonstrates how to read a neural network from a JSON file,
!! query the network object for some of its properties, print those properties,
!! and use the network to perform inference.
use inference_engine_m, only : inference_engine_t, relu_t, tensor_t
use julienne_m, only : string_t, command_line_t, file_t
use kind_parameters_m, only : rkind
implicit none

type(command_line_t) command_line
type(inference_engine_t) inference_engine

associate(file_name => string_t(command_line%flag_value("--input-file")))

if (len(file_name%string())==0) then
error stop new_line('a') // new_line('a') // &
'Usage: fpm run --example read-query -- --input-file "<file-name>"'
end if

print *, "Reading an inference_engine_t object from the same JSON file '"//file_name%string()//"'."
associate(inference_engine => inference_engine_t(file_t(file_name)))

print *, "Querying the new inference_engine_t object for several properties:"
associate(activation_name => inference_engine%activation_function_name())
print *, "Activation function: ", activation_name%string()
end associate
print *, "Number of outputs:", inference_engine%num_outputs()
print *, "Number of inputs:", inference_engine%num_inputs()
print *, "Nodes per layer:", inference_engine%nodes_per_layer()
print *, "Performing inference:"

block
integer, parameter :: tensor_size = 2, num_tests = 3
real, parameter :: tensor_range = 11.
real harvest(tensor_size)
integer i

call random_init(repeatable=.false., image_distinct=.true.)

print *, "Inputs | Outputs "

do i = 1, num_tests
call random_number(harvest)
associate(inputs => tensor_t(tensor_range*harvest))
associate(outputs => inference_engine%infer(inputs))
print '(2(2g12.5,a,2x))', inputs%values(), "|", outputs%values()
end associate
end associate
end do

end block
end associate ! associate(inference_engine => ...)
end associate ! associate(file_name => ...)
end program
7 changes: 4 additions & 3 deletions src/inference_engine/inference_engine_m_.f90
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ module inference_engine_m_
use julienne_file_m, only : file_t
use julienne_string_m, only : string_t
use kind_parameters_m, only : rkind
use metadata_m, only : metadata_t
use tensor_m, only : tensor_t
use tensor_range_m, only : tensor_range_t
use differentiable_activation_strategy_m, only :differentiable_activation_strategy_t
Expand All @@ -24,7 +25,7 @@ module inference_engine_m_
!! Encapsulate the minimal information needed to perform inference
private
type(tensor_range_t) input_range_, output_range_
type(string_t) metadata_(size(key))
type(metadata_t) metadata_
real(rkind), allocatable :: weights_(:,:,:), biases_(:,:)
integer, allocatable :: nodes_(:)
class(activation_strategy_t), allocatable :: activation_strategy_ ! Strategy Pattern facilitates elemental activation
Expand All @@ -46,7 +47,7 @@ module inference_engine_m_

type exchange_t
type(tensor_range_t) input_range_, output_range_
type(string_t) metadata_(size(key))
type(metadata_t) metadata_
real(rkind), allocatable :: weights_(:,:,:), biases_(:,:)
integer, allocatable :: nodes_(:)
class(activation_strategy_t), allocatable :: activation_strategy_ ! Strategy Pattern facilitates elemental activation
Expand All @@ -72,7 +73,7 @@ impure module function construct_from_padded_arrays(metadata, weights, biases, n
type(inference_engine_t) inference_engine
end function

impure elemental module function construct_from_json(file_) result(inference_engine)
impure elemental module function from_json(file_) result(inference_engine)
implicit none
type(file_t), intent(in) :: file_
type(inference_engine_t) inference_engine
Expand Down
Loading
Loading