-
Notifications
You must be signed in to change notification settings - Fork 86
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Proposition of API for the method
network % evaluate
(#182)
* proposition of API for the method evaluate * nf_metric -> nf_metrics for consistency with Python frameworks * Add nf_metrics.f90 to the CMake build * Make corr metric public * Formatting * Bump minor version * Make metrics accessible via nf * Evaluate metrics in MNIST example * Add simple tests for metrics * addition of maxabs * Update example * Remove multri-metrics variant of net % evaluate * Mention metrics in README --------- Co-authored-by: Vandenplas, Jeremie <[email protected]> Co-authored-by: milancurcic <[email protected]>
- Loading branch information
1 parent
6dfaed0
commit e82d565
Showing
11 changed files
with
204 additions
and
12 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
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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
name = "neural-fortran" | ||
version = "0.16.1" | ||
version = "0.17.0" | ||
license = "MIT" | ||
author = "Milan Curcic" | ||
maintainer = "[email protected]" | ||
|
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,72 @@ | ||
module nf_metrics | ||
|
||
!! This module provides a collection of metric functions. | ||
|
||
implicit none | ||
|
||
private | ||
public :: metric_type | ||
public :: corr | ||
public :: maxabs | ||
|
||
type, abstract :: metric_type | ||
contains | ||
procedure(metric_interface), nopass, deferred :: eval | ||
end type metric_type | ||
|
||
abstract interface | ||
pure function metric_interface(true, predicted) result(res) | ||
real, intent(in) :: true(:) | ||
real, intent(in) :: predicted(:) | ||
real :: res | ||
end function metric_interface | ||
end interface | ||
|
||
type, extends(metric_type) :: corr | ||
!! Pearson correlation | ||
contains | ||
procedure, nopass :: eval => corr_eval | ||
end type corr | ||
|
||
type, extends(metric_type) :: maxabs | ||
!! Maximum absolute difference | ||
contains | ||
procedure, nopass :: eval => maxabs_eval | ||
end type maxabs | ||
|
||
contains | ||
|
||
pure module function corr_eval(true, predicted) result(res) | ||
!! Pearson correlation function: | ||
!! | ||
real, intent(in) :: true(:) | ||
!! True values, i.e. labels from training datasets | ||
real, intent(in) :: predicted(:) | ||
!! Values predicted by the network | ||
real :: res | ||
!! Resulting correlation value | ||
real :: m_true, m_pred | ||
|
||
m_true = sum(true) / size(true) | ||
m_pred = sum(predicted) / size(predicted) | ||
|
||
res = dot_product(true - m_true, predicted - m_pred) / & | ||
sqrt(sum((true - m_true)**2)*sum((predicted - m_pred)**2)) | ||
|
||
end function corr_eval | ||
|
||
pure function maxabs_eval(true, predicted) result(res) | ||
!! Maximum absolute difference function: | ||
!! | ||
real, intent(in) :: true(:) | ||
!! True values, i.e. labels from training datasets | ||
real, intent(in) :: predicted(:) | ||
!! Values predicted by the network | ||
real :: res | ||
!! Resulting maximum absolute difference value | ||
|
||
res = maxval(abs(true - predicted)) | ||
|
||
end function maxabs_eval | ||
|
||
end module nf_metrics |
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
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,70 @@ | ||
program test_metrics | ||
use iso_fortran_env, only: stderr => error_unit | ||
use nf, only: dense, input, network, sgd, mse | ||
implicit none | ||
type(network) :: net | ||
logical :: ok = .true. | ||
|
||
! Minimal 2-layer network | ||
net = network([ & | ||
input(1), & | ||
dense(1) & | ||
]) | ||
|
||
training: block | ||
real :: x(1), y(1) | ||
real :: tolerance = 1e-3 | ||
integer :: n | ||
integer, parameter :: num_iterations = 1000 | ||
real :: quadratic_loss, mse_metric | ||
real, allocatable :: metrics(:,:) | ||
|
||
x = [0.1234567] | ||
y = [0.7654321] | ||
|
||
do n = 1, num_iterations | ||
call net % forward(x) | ||
call net % backward(y) | ||
call net % update(sgd(learning_rate=1.)) | ||
if (all(abs(net % predict(x) - y) < tolerance)) exit | ||
end do | ||
|
||
! Returns only one metric, based on the default loss function (quadratic). | ||
metrics = net % evaluate(reshape(x, [1, 1]), reshape(y, [1, 1])) | ||
quadratic_loss = metrics(1,1) | ||
|
||
if (.not. all(shape(metrics) == [1, 1])) then | ||
write(stderr, '(a)') 'metrics array is the correct shape (1, 1).. failed' | ||
ok = .false. | ||
end if | ||
|
||
! Returns two metrics, one from the loss function and another specified by the user. | ||
metrics = net % evaluate(reshape(x, [1, 1]), reshape(y, [1, 1]), metric=mse()) | ||
|
||
if (.not. all(shape(metrics) == [1, 2])) then | ||
write(stderr, '(a)') 'metrics array is the correct shape (1, 2).. failed' | ||
ok = .false. | ||
end if | ||
|
||
mse_metric = metrics(1,2) | ||
|
||
if (.not. all(metrics < 1e-5)) then | ||
write(stderr, '(a)') 'value for all metrics is expected.. failed' | ||
ok = .false. | ||
end if | ||
|
||
if (.not. metrics(1,1) == quadratic_loss) then | ||
write(stderr, '(a)') 'first metric should be the same as that of the loss function.. failed' | ||
ok = .false. | ||
end if | ||
|
||
end block training | ||
|
||
if (ok) then | ||
print '(a)', 'test_metrics: All tests passed.' | ||
else | ||
write(stderr, '(a)') 'test_metrics: One or more tests failed.' | ||
stop 1 | ||
end if | ||
|
||
end program test_metrics |