Skip to content
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
6 changes: 3 additions & 3 deletions demo/app/infer-aerosol.f90
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ program infer_aerosol
use iso_fortran_env, only : int64, real64

! External dependencies:
use inference_engine_m, only : unmapped_engine_t, tensor_t, double_precision, double_precision_file_t
use inference_engine_m, only : unmapped_network_t, tensor_t, double_precision, double_precision_file_t
use julienne_m, only : string_t, command_line_t
use omp_lib

Expand Down Expand Up @@ -49,7 +49,7 @@ subroutine read_stats_and_perform_inference(path)
double precision cube_root
double precision, allocatable, dimension(:,:) :: aerosol_data, input_components, output_components
type(tensor_statistics_t) input_stats, output_stats
type(unmapped_engine_t(double_precision)) inference_engine
type(unmapped_network_t(double_precision)) inference_engine
integer i, j

input_stats = read_tensor_statistics(path // "meanxp.txt", path // "stdxp.txt", num_inputs) !for pre-processing normalization
Expand All @@ -76,7 +76,7 @@ subroutine read_stats_and_perform_inference(path)
!$omp end parallel do

print *, "Reading the neural network from " // network_file_name
inference_engine = unmapped_engine_t(double_precision_file_t(path // network_file_name))
inference_engine = unmapped_network_t(double_precision_file_t(path // network_file_name))

time_inference: &
block
Expand Down
28 changes: 10 additions & 18 deletions demo/app/train-cloud-microphysics.F90
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ program train_on_flat_distribution
use julienne_m, only : string_t, file_t, command_line_t, bin_t
use assert_m, only : assert, intrinsic_array_t
use inference_engine_m, only : &
inference_engine_t, mini_batch_t, input_output_pair_t, tensor_t, trainable_engine_t, tensor_map_t, &
inference_engine_t, mini_batch_t, input_output_pair_t, tensor_t, trainable_network_t, tensor_map_t, &
training_configuration_t, shuffle

!! Internal dependencies:
Expand Down Expand Up @@ -271,7 +271,7 @@ subroutine read_train_write(training_configuration, args, plot_file)

train_network: &
block
type(trainable_engine_t) trainable_engine
type(trainable_network_t) trainable_network
type(mini_batch_t), allocatable :: mini_batches(:)
type(bin_t), allocatable :: bins(:)
type(input_output_pair_t), allocatable :: input_output_pairs(:)
Expand Down Expand Up @@ -319,7 +319,7 @@ subroutine read_train_write(training_configuration, args, plot_file)
read_or_initialize_engine: &
if (io_status==0) then
print *,"Reading network from file " // network_file
trainable_engine = trainable_engine_t(inference_engine_t(file_t(string_t(network_file))))
trainable_network = trainable_network_t(inference_engine_t(file_t(string_t(network_file))))
close(network_unit)
else
close(network_unit)
Expand All @@ -339,9 +339,9 @@ subroutine read_train_write(training_configuration, args, plot_file)
maxima = [maxval(pressure_in), maxval(potential_temperature_in), maxval(temperature_in), &
maxval(qv_in), maxval(qc_in), maxval(qr_in), maxval(qs_in)] &
) )
associate(activation => training_configuration%differentiable_activation_strategy())
associate(activation => training_configuration%differentiable_activation())
associate(residual_network=> string_t(trim(merge("true ", "false", training_configuration%skip_connections()))))
trainable_engine = trainable_engine_t( &
trainable_network = trainable_network_t( &
training_configuration, &
perturbation_magnitude = 0.05, &
metadata = [ &
Expand Down Expand Up @@ -379,7 +379,7 @@ subroutine read_train_write(training_configuration, args, plot_file)
end associate output_extrema

print *,"Normalizing the remaining input and output tensors"
input_output_pairs = trainable_engine%map_to_training_ranges(input_output_pairs)
input_output_pairs = trainable_network%map_to_training_ranges(input_output_pairs)

associate( &
num_pairs => size(input_output_pairs), &
Expand Down Expand Up @@ -417,7 +417,7 @@ subroutine read_train_write(training_configuration, args, plot_file)
if (size(bins)>1) call shuffle(input_output_pairs) ! set up for stochastic gradient descent
mini_batches = [(mini_batch_t(input_output_pairs(bins(b)%first():bins(b)%last())), b = 1, size(bins))]

call trainable_engine%train(mini_batches, cost, adam, learning_rate)
call trainable_network%train(mini_batches, cost, adam, learning_rate)

associate(average_cost => sum(cost)/size(cost))
associate(converged => average_cost <= args%cost_tolerance)
Expand All @@ -428,17 +428,9 @@ subroutine read_train_write(training_configuration, args, plot_file)
print *, epoch, average_cost
write(plot_file%plot_unit,*) epoch, average_cost

block
integer net_unit

open(newunit=net_unit, file=network_file, form='formatted', status='unknown', iostat=io_status, action='write')
associate(inference_engine => trainable_engine%to_inference_engine())
associate(json_file => inference_engine%to_json())
call json_file%write_lines(string_t(network_file))
end associate
end associate
close(net_unit)
end block
associate(json_file => trainable_network%to_json())
call json_file%write_lines(string_t(network_file))
end associate

end if image_1_maybe_writes

Expand Down
29 changes: 14 additions & 15 deletions example/learn-addition.F90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ elemental function y(x_tensor) result(a_tensor)
program learn_addition
!! This trains a neural network to learn the following six polynomial functions of its eight inputs.
use inference_engine_m, only : &
inference_engine_t, trainable_engine_t, mini_batch_t, tensor_t, input_output_pair_t, shuffle
inference_engine_t, trainable_network_t, mini_batch_t, tensor_t, input_output_pair_t, shuffle
use julienne_m, only : string_t, file_t, command_line_t, bin_t
use assert_m, only : assert, intrinsic_array_t
use addition_m, only : y
Expand All @@ -43,15 +43,15 @@ program learn_addition
type(mini_batch_t), allocatable :: mini_batches(:)
type(input_output_pair_t), allocatable :: input_output_pairs(:)
type(tensor_t), allocatable :: inputs(:), desired_outputs(:)
type(trainable_engine_t) trainable_engine
type(trainable_network_t) trainable_network
type(bin_t), allocatable :: bins(:)
real, allocatable :: cost(:), random_numbers(:)

call random_init(image_distinct=.true., repeatable=.true.)
trainable_engine = perturbed_identity_network(perturbation_magnitude=0.05)
call output(trainable_engine%to_inference_engine(), string_t("initial-network.json"))
trainable_network = perturbed_identity_network(perturbation_magnitude=0.05)
call output(trainable_network, string_t("initial-network.json"))

associate(num_inputs => trainable_engine%num_inputs(), num_outputs => trainable_engine%num_outputs())
associate(num_inputs => trainable_network%num_inputs(), num_outputs => trainable_network%num_outputs())

block
integer i, j
Expand All @@ -76,19 +76,18 @@ program learn_addition
call random_number(random_numbers)
call shuffle(input_output_pairs)
mini_batches = [(mini_batch_t(input_output_pairs(bins(b)%first():bins(b)%last())), b = 1, size(bins))]
call trainable_engine%train(mini_batches, cost, adam=.true., learning_rate=1.5)
call trainable_network%train(mini_batches, cost, adam=.true., learning_rate=1.5)
print *,sum(cost)/size(cost)
end do
end block

block
real, parameter :: tolerance = 1.E-06
integer p
#if defined _CRAYFTN || __GFORTRAN__
type(tensor_t), allocatable :: network_outputs(:)
network_outputs = trainable_engine%infer(inputs)
network_outputs = trainable_network%infer(inputs)
#else
associate(network_outputs => trainable_engine%infer(inputs))
associate(network_outputs => trainable_network%infer(inputs))
#endif
print "(a,69x,a)"," Outputs", "| Desired outputs"
do p = 1, num_pairs
Expand All @@ -102,14 +101,14 @@ program learn_addition

end associate

call output(trainable_engine%to_inference_engine(), final_network_file)
call output(trainable_network, final_network_file)

end block

contains

subroutine output(inference_engine, file_name)
type(inference_engine_t), intent(in) :: inference_engine
class(inference_engine_t), intent(in) :: inference_engine
type(string_t), intent(in) :: file_name
type(file_t) json_file
json_file = inference_engine%to_json()
Expand All @@ -123,8 +122,8 @@ pure function e(j,n) result(unit_vector)
unit_vector = real([(merge(1,0,j==k),k=1,n)])
end function

function perturbed_identity_network(perturbation_magnitude) result(trainable_engine)
type(trainable_engine_t) trainable_engine
function perturbed_identity_network(perturbation_magnitude) result(trainable_network)
type(trainable_network_t) trainable_network
real, intent(in) :: perturbation_magnitude
integer, parameter :: n(*) = [8, 64, 64, 64, 6]
integer, parameter :: n_max = maxval(n), layers = size(n)
Expand All @@ -141,10 +140,10 @@ function perturbed_identity_network(perturbation_magnitude) result(trainable_eng

associate(w => identity + perturbation_magnitude*(w_harvest-0.5)/0.5, b => perturbation_magnitude*(b_harvest-0.5)/0.5)

trainable_engine = trainable_engine_t( &
trainable_network = trainable_network_t( inference_engine_t( &
nodes = n, weights = w, biases = b, metadata = &
[string_t("Perturbed Identity"), string_t("Damian Rouson"), string_t("2023-09-23"), string_t("relu"), string_t("false")] &
)
))

end associate
end function
Expand Down
29 changes: 14 additions & 15 deletions example/learn-exponentiation.F90
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ elemental function y(x_tensor) result(a_tensor)
program learn_exponentiation
!! This trains a neural network to learn the following six polynomial functions of its eight inputs.
use inference_engine_m, only : &
inference_engine_t, trainable_engine_t, mini_batch_t, tensor_t, input_output_pair_t, shuffle
inference_engine_t, trainable_network_t, mini_batch_t, tensor_t, input_output_pair_t, shuffle
use julienne_m, only : string_t, file_t, command_line_t, bin_t
use assert_m, only : assert, intrinsic_array_t
use exponentiation_m, only : y
Expand All @@ -43,15 +43,15 @@ program learn_exponentiation
type(mini_batch_t), allocatable :: mini_batches(:)
type(input_output_pair_t), allocatable :: input_output_pairs(:)
type(tensor_t), allocatable :: inputs(:), desired_outputs(:)
type(trainable_engine_t) trainable_engine
type(trainable_network_t) trainable_network
type(bin_t), allocatable :: bins(:)
real, allocatable :: cost(:), random_numbers(:)

call random_init(image_distinct=.true., repeatable=.true.)
trainable_engine = perturbed_identity_network(perturbation_magnitude=0.05)
call output(trainable_engine%to_inference_engine(), string_t("initial-network.json"))
trainable_network = perturbed_identity_network(perturbation_magnitude=0.05)
call output(trainable_network, string_t("initial-network.json"))

associate(num_inputs => trainable_engine%num_inputs(), num_outputs => trainable_engine%num_outputs())
associate(num_inputs => trainable_network%num_inputs(), num_outputs => trainable_network%num_outputs())

block
integer i, j
Expand All @@ -76,19 +76,18 @@ program learn_exponentiation
call random_number(random_numbers)
call shuffle(input_output_pairs)
mini_batches = [(mini_batch_t(input_output_pairs(bins(b)%first():bins(b)%last())), b = 1, size(bins))]
call trainable_engine%train(mini_batches, cost, adam=.true., learning_rate=1.5)
call trainable_network%train(mini_batches, cost, adam=.true., learning_rate=1.5)
print *,sum(cost)/size(cost)
end do
end block

block
real, parameter :: tolerance = 1.E-06
integer p
#if defined _CRAYFTN || __GFORTRAN__
type(tensor_t), allocatable :: network_outputs(:)
network_outputs = trainable_engine%infer(inputs)
network_outputs = trainable_network%infer(inputs)
#else
associate(network_outputs => trainable_engine%infer(inputs))
associate(network_outputs => trainable_network%infer(inputs))
#endif
print "(a,69x,a)"," Outputs", "| Desired outputs"
do p = 1, num_pairs
Expand All @@ -102,14 +101,14 @@ program learn_exponentiation

end associate

call output(trainable_engine%to_inference_engine(), final_network_file)
call output(trainable_network, final_network_file)

end block

contains

subroutine output(inference_engine, file_name)
type(inference_engine_t), intent(in) :: inference_engine
class(inference_engine_t), intent(in) :: inference_engine
type(string_t), intent(in) :: file_name
type(file_t) json_file
json_file = inference_engine%to_json()
Expand All @@ -123,8 +122,8 @@ pure function e(j,n) result(unit_vector)
unit_vector = real([(merge(1,0,j==k),k=1,n)])
end function

function perturbed_identity_network(perturbation_magnitude) result(trainable_engine)
type(trainable_engine_t) trainable_engine
function perturbed_identity_network(perturbation_magnitude) result(trainable_network)
type(trainable_network_t) trainable_network
real, intent(in) :: perturbation_magnitude
integer, parameter :: n(*) = [8, 64, 64, 64, 6] ! nodes per layer (first layer = input, last layer = output)
integer, parameter :: n_max = maxval(n), layers = size(n)
Expand All @@ -141,10 +140,10 @@ function perturbed_identity_network(perturbation_magnitude) result(trainable_eng

associate(w => identity + perturbation_magnitude*(w_harvest-0.5)/0.5, b => perturbation_magnitude*(b_harvest-0.5)/0.5)

trainable_engine = trainable_engine_t( &
trainable_network = trainable_network_t( inference_engine_t( &
nodes = n, weights = w, biases = b, metadata = &
[string_t("Perturbed Identity"), string_t("Damian Rouson"), string_t("2023-09-23"), string_t("relu"), string_t("false")] &
)
))

end associate
end function
Expand Down
Loading