From bf9d0200b1f03bcf549d620d32a76648d1b4449b Mon Sep 17 00:00:00 2001 From: Damian Rouson Date: Fri, 5 Jan 2024 16:34:40 -0800 Subject: [PATCH] chore(examples): match program names to file names and fix the usage message correspondingly --- example/concurrent-inferences.f90 | 6 +- example/fit-polynomials.f90 | 137 ---------------------- example/learn-addition.f90 | 6 +- example/learn-exponentiation.f90 | 6 +- example/learn-microphysics-procedures.f90 | 2 +- example/learn-multiplication.f90 | 4 +- example/learn-power-series.f90 | 4 +- example/write-read-infer.f90 | 2 +- 8 files changed, 15 insertions(+), 152 deletions(-) delete mode 100644 example/fit-polynomials.f90 diff --git a/example/concurrent-inferences.f90 b/example/concurrent-inferences.f90 index f284e0e11..6fdfb1a4c 100644 --- a/example/concurrent-inferences.f90 +++ b/example/concurrent-inferences.f90 @@ -1,6 +1,6 @@ ! Copyright (c), The Regents of the University of California ! Terms of use are as specified in LICENSE.txt -program concurrent_inference +program concurrent_inferences !! This program demonstrates how to read a neural network from a JSON file !! and use the network to perform concurrent inferences. use inference_engine_m, only : inference_engine_t, tensor_t @@ -16,12 +16,12 @@ program concurrent_inference if (len(network_file_name%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example identity -- --network ""' + 'Usage: ./build/run-fpm.sh run --example concurrent-inferences -- --network ""' end if block type(inference_engine_t) network, inference_engine - type(tensor_t), allocatable :: inputs(:,:,:), outputs(:,:,:) + type(tensor_t), allocatable :: inputs(:,:,:), outputs(:,:,:) real, allocatable :: input_components(:,:,:,:) integer, parameter :: lat=263, lon=317, lev=15 ! latitudes, longitudes, levels (elevations) integer i, j, k diff --git a/example/fit-polynomials.f90 b/example/fit-polynomials.f90 deleted file mode 100644 index b7b135fe1..000000000 --- a/example/fit-polynomials.f90 +++ /dev/null @@ -1,137 +0,0 @@ -! Copyright (c), The Regents of the University of California -! Terms of use are as specified in LICENSE.txt -program train_polynomials - !! 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, relu_t - use sourcery_m, only : string_t, file_t, command_line_t, bin_t - use assert_m, only : assert, intrinsic_array_t - implicit none - - type(string_t) intial_network_file, final_network_file - type(command_line_t) command_line - - final_network_file = string_t(command_line%flag_value("--output-file")) - - if (len(final_network_file%string())==0) then - error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example train-polynomials -- --output-file ""' - end if - - block - integer, parameter :: num_pairs = 5, num_epochs = 500, num_mini_batches= 3 ! num_pairs = # input/output pairs in training data - - 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(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.1) - call output(trainable_engine%to_inference_engine(), string_t("initial-network.json")) - - associate(num_inputs => trainable_engine%num_inputs(), num_outputs => trainable_engine%num_outputs()) - - block - integer i, j - inputs = [(tensor_t(real([(j*i, j = 1,num_inputs)])/(num_inputs*num_pairs)), i = 1, num_pairs)] - desired_outputs = y(inputs) - call assert(num_outputs == size(desired_outputs(1)%values()), & - "fit-polynomials: # outputs", intrinsic_array_t([num_outputs, size(desired_outputs(1)%values())]) & - ) - end block - input_output_pairs = input_output_pair_t(inputs, desired_outputs) - block - integer b - bins = [(bin_t(num_items=num_pairs, num_bins=num_mini_batches, bin_number=b), b = 1, num_mini_batches)] - end block - - allocate(random_numbers(2:size(input_output_pairs))) - - print *,"Cost" - block - integer e, b - do e = 1,num_epochs - 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) - print *,sum(cost)/size(cost) - end do - end block - - block - real, parameter :: tolerance = 1.E-06 - integer p - - associate(network_outputs => trainable_engine%infer(inputs)) - print *," Outputs | Desired outputs" - do p = 1, num_pairs - print *,network_outputs(p)%values(), "|", desired_outputs(p)%values() - end do - end associate - end block - - end associate - - call output(trainable_engine%to_inference_engine(), final_network_file) - - end block - -contains - - subroutine output(inference_engine, file_name) - type(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() - call json_file%write_lines(file_name) - end subroutine - - pure function e(j,n) result(unit_vector) - integer, intent(in) :: j, n - integer k - real, allocatable :: unit_vector(:) - unit_vector = real([(merge(1,0,j==k),k=1,n)]) - end function - - elemental function y(x_tensor) result(a_tensor) - type(tensor_t), intent(in) :: x_tensor - type(tensor_t) a_tensor - associate(x => x_tensor%values()) - call assert(ubound(x,1)>=7 .and. lbound(x,1)<=2,"y(x) :: sufficient input") - a_tensor = tensor_t([x(1), x(2) + x(3), x(4), x(5), 0.5*x(6) + 1.5*x(7), x(8)]) - end associate - end function - - function perturbed_identity_network(perturbation_magnitude) result(trainable_engine) - type(trainable_engine_t) trainable_engine - real, intent(in) :: perturbation_magnitude - integer, parameter :: n(*) = [8, 8, 8, 8, 6] - integer, parameter :: n_max = maxval(n), layers = size(n) - integer j, k, l - real, allocatable :: identity(:,:,:), w_harvest(:,:,:), b_harvest(:,:) - - identity = reshape( [( [(e(k,n_max), k=1,n_max)], l = 1, layers-1 )], [n_max, n_max, layers-1]) - - allocate(w_harvest, mold = identity) - allocate(b_harvest(size(identity,1), size(identity,3))) - - call random_number(w_harvest) - call random_number(b_harvest) - - 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( & - nodes = n, weights = w, biases = b, differentiable_activation_strategy = relu_t(), & - metadata = & - [string_t("Perturbed Identity"), string_t("Damian Rouson"), string_t("2023-09-23"), string_t("relu"), string_t("false")] & - ) - - end associate - end function - -end program \ No newline at end of file diff --git a/example/learn-addition.f90 b/example/learn-addition.f90 index 9df9bb72a..984b2638c 100644 --- a/example/learn-addition.f90 +++ b/example/learn-addition.f90 @@ -1,6 +1,6 @@ ! Copyright (c), The Regents of the University of California ! Terms of use are as specified in LICENSE.txt -module add_inputs +module addition_m !! Define a function that produces the desired network output for a given network input use inference_engine_m, only : tensor_t use assert_m, only : assert @@ -18,7 +18,7 @@ elemental function y(x_tensor) result(a_tensor) end module -program train_polynomials +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, relu_t @@ -34,7 +34,7 @@ program train_polynomials if (len(final_network_file%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example train-polynomials -- --output-file ""' + 'Usage: ./build/run-fpm.sh run --example learn-addition -- --output-file ""' end if block diff --git a/example/learn-exponentiation.f90 b/example/learn-exponentiation.f90 index 4d39e96e7..2313d7455 100644 --- a/example/learn-exponentiation.f90 +++ b/example/learn-exponentiation.f90 @@ -1,6 +1,6 @@ ! Copyright (c), The Regents of the University of California ! Terms of use are as specified in LICENSE.txt -module raise_inputs_to_a_power +module exponentiation_m !! Define a function that produces the desired network output for a given network input use inference_engine_m, only : tensor_t use assert_m, only : assert @@ -18,13 +18,13 @@ elemental function y(x_tensor) result(a_tensor) end module -program train_polynomials +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, relu_t use sourcery_m, only : string_t, file_t, command_line_t, bin_t, csv use assert_m, only : assert, intrinsic_array_t - use raise_inputs_to_a_power, only : y + use exponentiation_m, only : y implicit none type(string_t) intial_network_file, final_network_file diff --git a/example/learn-microphysics-procedures.f90 b/example/learn-microphysics-procedures.f90 index ad2306a1d..eec4cef8f 100644 --- a/example/learn-microphysics-procedures.f90 +++ b/example/learn-microphysics-procedures.f90 @@ -19,7 +19,7 @@ program learn_microphysics_procedures if (len(network_file%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run learn_microphysics_procedures -- --output-file ""' + 'Usage: ./build/run-fpm.sh run learn-microphysics-procedures -- --output-file ""' end if call system_clock(counter_start, clock_rate) diff --git a/example/learn-multiplication.f90 b/example/learn-multiplication.f90 index a9a074b72..18ba510fc 100644 --- a/example/learn-multiplication.f90 +++ b/example/learn-multiplication.f90 @@ -18,7 +18,7 @@ elemental function y(x_tensor) result(a_tensor) end module -program train_polynomials +program learn_multiplication !! 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, relu_t @@ -34,7 +34,7 @@ program train_polynomials if (len(final_network_file%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example train-polynomials -- --output-file ""' + 'Usage: ./build/run-fpm.sh run --example learn-multiplication -- --output-file ""' end if block diff --git a/example/learn-power-series.f90 b/example/learn-power-series.f90 index 553e868aa..b10cfc2c3 100644 --- a/example/learn-power-series.f90 +++ b/example/learn-power-series.f90 @@ -18,7 +18,7 @@ elemental function y(x_in) result(a) end module -program train_polynomials +program learn_power_series !! 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, relu_t @@ -34,7 +34,7 @@ program train_polynomials if (len(final_network_file%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example train-polynomials -- --output-file ""' + 'Usage: ./build/run-fpm.sh run --example learn-power-series -- --output-file ""' end if block diff --git a/example/write-read-infer.f90 b/example/write-read-infer.f90 index 6401b2936..1a9854333 100644 --- a/example/write-read-infer.f90 +++ b/example/write-read-infer.f90 @@ -19,7 +19,7 @@ program write_read_infer if (len(file_name%string())==0) then error stop new_line('a') // new_line('a') // & - 'Usage: ./build/run-fpm.sh run --example identity -- --output-file ""' + 'Usage: ./build/run-fpm.sh run --example write-read-infer -- --output-file ""' end if call write_read_query_infer(file_name)