Skip to content

Commit 65d74e9

Browse files
authored
Merge pull request #51 from rouson/submodules
Refactor: move procedure definitions submodules
2 parents f49d95f + bc13121 commit 65d74e9

17 files changed

+1038
-677
lines changed

CMakeLists.txt

+17-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,23 @@ if(CMAKE_Fortran_COMPILER_ID MATCHES Cray)
7878
endif()
7979

8080
# library to archive (libneural.a)
81-
add_library(neural src/mod_activation.f90 src/mod_io.f90 src/mod_kinds.f90 src/mod_layer.f90 src/mod_mnist.f90 src/mod_network.f90 src/mod_parallel.f90 src/mod_random.f90)
81+
add_library(neural
82+
src/mod_activation.f90
83+
src/mod_activation_submodule.f90
84+
src/mod_io.f90
85+
src/mod_io_submodule.f90
86+
src/mod_kinds.f90
87+
src/mod_layer.f90
88+
src/mod_layer_submodule.f90
89+
src/mod_mnist.f90
90+
src/mod_mnist_submodule.f90
91+
src/mod_network.f90
92+
src/mod_network_submodule.f90
93+
src/mod_parallel.f90
94+
src/mod_parallel_submodule.f90
95+
src/mod_random.f90
96+
src/mod_random_submodule.f90
97+
)
8298

8399
# Remove leading or trailing whitespace
84100
string(REGEX REPLACE "^ | $" "" LIBS "${LIBS}")

README.md

+6
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ Dependencies:
4343
* OpenCoarrays (optional, for parallel execution, GFortran only)
4444
* BLAS, MKL (optional)
4545

46+
Compilers tested include:
47+
48+
* gfortran-10.3.0
49+
* ifort-2021.4
50+
* ifx-2021.4
51+
4652
### Building with fpm
4753

4854
#### Building in serial mode

fpm.toml

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "neural-fortran"
2-
version = "0.1.0"
2+
version = "0.2.0"
33
license = "MIT"
44
author = "Milan Curcic"
55
maintainer = "[email protected]"
6-
copyright = "Copyright 2018-2021, neural-fortran contributors"
6+
copyright = "Copyright 2018-2022, neural-fortran contributors"

src/mod_activation.f90

+75-83
Original file line numberDiff line numberDiff line change
@@ -16,94 +16,86 @@ module mod_activation
1616
public :: tanhf, tanh_prime
1717

1818
interface
19+
1920
pure function activation_function(x)
2021
import :: rk
2122
real(rk), intent(in) :: x(:)
2223
real(rk) :: activation_function(size(x))
2324
end function activation_function
24-
end interface
2525

26-
contains
27-
28-
pure function gaussian(x) result(res)
29-
!! Gaussian activation function.
30-
real(rk), intent(in) :: x(:)
31-
real(rk) :: res(size(x))
32-
res = exp(-x**2)
33-
end function gaussian
34-
35-
pure function gaussian_prime(x) result(res)
36-
!! First derivative of the Gaussian activation function.
37-
real(rk), intent(in) :: x(:)
38-
real(rk) :: res(size(x))
39-
res = -2 * x * gaussian(x)
40-
end function gaussian_prime
41-
42-
pure function relu(x) result(res)
43-
!! REctified Linear Unit (RELU) activation function.
44-
real(rk), intent(in) :: x(:)
45-
real(rk) :: res(size(x))
46-
res = max(0., x)
47-
end function relu
48-
49-
pure function relu_prime(x) result(res)
50-
!! First derivative of the REctified Linear Unit (RELU) activation function.
51-
real(rk), intent(in) :: x(:)
52-
real(rk) :: res(size(x))
53-
where (x > 0)
54-
res = 1
55-
elsewhere
56-
res = 0
57-
end where
58-
end function relu_prime
59-
60-
pure function sigmoid(x) result(res)
61-
!! Sigmoid activation function.
62-
real(rk), intent(in) :: x(:)
63-
real(rk) :: res(size(x))
64-
res = 1 / (1 + exp(-x))
65-
endfunction sigmoid
66-
67-
pure function sigmoid_prime(x) result(res)
68-
!! First derivative of the sigmoid activation function.
69-
real(rk), intent(in) :: x(:)
70-
real(rk) :: res(size(x))
71-
res = sigmoid(x) * (1 - sigmoid(x))
72-
end function sigmoid_prime
73-
74-
pure function step(x) result(res)
75-
!! Step activation function.
76-
real(rk), intent(in) :: x(:)
77-
real(rk) :: res(size(x))
78-
where (x > 0)
79-
res = 1
80-
elsewhere
81-
res = 0
82-
end where
83-
end function step
84-
85-
pure function step_prime(x) result(res)
86-
!! First derivative of the step activation function.
87-
real(rk), intent(in) :: x(:)
88-
real(rk) :: res(size(x))
89-
res = 0
90-
end function step_prime
91-
92-
pure function tanhf(x) result(res)
93-
!! Tangent hyperbolic activation function.
94-
!! Same as the intrinsic tanh, but must be
95-
!! defined here so that we can use procedure
96-
!! pointer with it.
97-
real(rk), intent(in) :: x(:)
98-
real(rk) :: res(size(x))
99-
res = tanh(x)
100-
end function tanhf
101-
102-
pure function tanh_prime(x) result(res)
103-
!! First derivative of the tanh activation function.
104-
real(rk), intent(in) :: x(:)
105-
real(rk) :: res(size(x))
106-
res = 1 - tanh(x)**2
107-
end function tanh_prime
26+
pure module function gaussian(x) result(res)
27+
!! Gaussian activation function.
28+
implicit none
29+
real(rk), intent(in) :: x(:)
30+
real(rk) :: res(size(x))
31+
end function gaussian
32+
33+
pure module function gaussian_prime(x) result(res)
34+
!! First derivative of the Gaussian activation function.
35+
implicit none
36+
real(rk), intent(in) :: x(:)
37+
real(rk) :: res(size(x))
38+
end function gaussian_prime
39+
40+
pure module function relu(x) result(res)
41+
!! REctified Linear Unit (RELU) activation function.
42+
implicit none
43+
real(rk), intent(in) :: x(:)
44+
real(rk) :: res(size(x))
45+
end function relu
46+
47+
pure module function relu_prime(x) result(res)
48+
!! First derivative of the REctified Linear Unit (RELU) activation function.
49+
implicit none
50+
real(rk), intent(in) :: x(:)
51+
real(rk) :: res(size(x))
52+
end function relu_prime
53+
54+
pure module function sigmoid(x) result(res)
55+
!! Sigmoid activation function.
56+
implicit none
57+
real(rk), intent(in) :: x(:)
58+
real(rk) :: res(size(x))
59+
end function sigmoid
60+
61+
pure module function sigmoid_prime(x) result(res)
62+
!! First derivative of the sigmoid activation function.
63+
implicit none
64+
real(rk), intent(in) :: x(:)
65+
real(rk) :: res(size(x))
66+
end function sigmoid_prime
67+
68+
pure module function step(x) result(res)
69+
!! Step activation function.
70+
implicit none
71+
real(rk), intent(in) :: x(:)
72+
real(rk) :: res(size(x))
73+
end function step
74+
75+
pure module function step_prime(x) result(res)
76+
!! First derivative of the step activation function.
77+
implicit none
78+
real(rk), intent(in) :: x(:)
79+
real(rk) :: res(size(x))
80+
end function step_prime
81+
82+
pure module function tanhf(x) result(res)
83+
!! Tangent hyperbolic activation function.
84+
!! Same as the intrinsic tanh, but must be
85+
!! defined here so that we can use procedure
86+
!! pointer with it.
87+
implicit none
88+
real(rk), intent(in) :: x(:)
89+
real(rk) :: res(size(x))
90+
end function tanhf
91+
92+
pure module function tanh_prime(x) result(res)
93+
!! First derivative of the tanh activation function.
94+
implicit none
95+
real(rk), intent(in) :: x(:)
96+
real(rk) :: res(size(x))
97+
end function tanh_prime
98+
99+
end interface
108100

109101
end module mod_activation

src/mod_activation_submodule.f90

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
submodule(mod_activation) mod_activation_submodule
2+
3+
!! A collection of activation functions and their derivatives.
4+
5+
implicit none
6+
7+
contains
8+
9+
pure module function gaussian(x) result(res)
10+
real(rk), intent(in) :: x(:)
11+
real(rk) :: res(size(x))
12+
res = exp(-x**2)
13+
end function gaussian
14+
15+
pure module function gaussian_prime(x) result(res)
16+
real(rk), intent(in) :: x(:)
17+
real(rk) :: res(size(x))
18+
res = -2 * x * gaussian(x)
19+
end function gaussian_prime
20+
21+
pure module function relu(x) result(res)
22+
real(rk), intent(in) :: x(:)
23+
real(rk) :: res(size(x))
24+
res = max(0., x)
25+
end function relu
26+
27+
pure module function relu_prime(x) result(res)
28+
real(rk), intent(in) :: x(:)
29+
real(rk) :: res(size(x))
30+
where (x > 0)
31+
res = 1
32+
elsewhere
33+
res = 0
34+
end where
35+
end function relu_prime
36+
37+
pure module function sigmoid(x) result(res)
38+
real(rk), intent(in) :: x(:)
39+
real(rk) :: res(size(x))
40+
res = 1 / (1 + exp(-x))
41+
endfunction sigmoid
42+
43+
pure module function sigmoid_prime(x) result(res)
44+
real(rk), intent(in) :: x(:)
45+
real(rk) :: res(size(x))
46+
res = sigmoid(x) * (1 - sigmoid(x))
47+
end function sigmoid_prime
48+
49+
pure module function step(x) result(res)
50+
real(rk), intent(in) :: x(:)
51+
real(rk) :: res(size(x))
52+
where (x > 0)
53+
res = 1
54+
elsewhere
55+
res = 0
56+
end where
57+
end function step
58+
59+
pure module function step_prime(x) result(res)
60+
real(rk), intent(in) :: x(:)
61+
real(rk) :: res(size(x))
62+
res = 0
63+
end function step_prime
64+
65+
pure module function tanhf(x) result(res)
66+
real(rk), intent(in) :: x(:)
67+
real(rk) :: res(size(x))
68+
res = tanh(x)
69+
end function tanhf
70+
71+
pure module function tanh_prime(x) result(res)
72+
real(rk), intent(in) :: x(:)
73+
real(rk) :: res(size(x))
74+
res = 1 - tanh(x)**2
75+
end function tanh_prime
76+
77+
end submodule mod_activation_submodule

src/mod_io.f90

+15-29
Original file line numberDiff line numberDiff line change
@@ -9,35 +9,21 @@ module mod_io
99
public :: read_binary_file
1010

1111
interface read_binary_file
12-
module procedure :: read_binary_file_1d, read_binary_file_2d
13-
end interface read_binary_file
1412

15-
contains
16-
17-
subroutine read_binary_file_1d(filename, dtype, nrec, array)
18-
character(len=*), intent(in) :: filename
19-
integer(ik), intent(in) :: dtype, nrec
20-
real(rk), allocatable, intent(in out) :: array(:)
21-
integer(ik) :: fileunit
22-
allocate(array(nrec))
23-
open(newunit=fileunit, file=filename, access='direct',&
24-
action='read', recl=dtype * nrec, status='old')
25-
read(fileunit, rec=1) array
26-
close(fileunit)
27-
end subroutine read_binary_file_1d
28-
29-
subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
30-
character(len=*), intent(in) :: filename
31-
integer(ik), intent(in) :: dtype, dsize, nrec
32-
real(rk), allocatable, intent(in out) :: array(:,:)
33-
integer(ik) :: fileunit, i
34-
allocate(array(dsize, nrec))
35-
open(newunit=fileunit, file=filename, access='direct',&
36-
action='read', recl=dtype * dsize, status='old')
37-
do i = 1, nrec
38-
read(fileunit, rec=i) array(:,i)
39-
end do
40-
close(fileunit)
41-
end subroutine read_binary_file_2d
13+
module subroutine read_binary_file_1d(filename, dtype, nrec, array)
14+
implicit none
15+
character(len=*), intent(in) :: filename
16+
integer(ik), intent(in) :: dtype, nrec
17+
real(rk), allocatable, intent(in out) :: array(:)
18+
end subroutine read_binary_file_1d
19+
20+
module subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
21+
implicit none
22+
character(len=*), intent(in) :: filename
23+
integer(ik), intent(in) :: dtype, dsize, nrec
24+
real(rk), allocatable, intent(in out) :: array(:,:)
25+
end subroutine read_binary_file_2d
26+
27+
end interface read_binary_file
4228

4329
end module mod_io

src/mod_io_submodule.f90

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
submodule(mod_io) mod_io_submodule
2+
3+
implicit none
4+
5+
contains
6+
7+
module subroutine read_binary_file_1d(filename, dtype, nrec, array)
8+
character(len=*), intent(in) :: filename
9+
integer(ik), intent(in) :: dtype, nrec
10+
real(rk), allocatable, intent(in out) :: array(:)
11+
integer(ik) :: fileunit
12+
allocate(array(nrec))
13+
open(newunit=fileunit, file=filename, access='direct',&
14+
action='read', recl=dtype * nrec, status='old')
15+
read(fileunit, rec=1) array
16+
close(fileunit)
17+
end subroutine read_binary_file_1d
18+
19+
module subroutine read_binary_file_2d(filename, dtype, dsize, nrec, array)
20+
character(len=*), intent(in) :: filename
21+
integer(ik), intent(in) :: dtype, dsize, nrec
22+
real(rk), allocatable, intent(in out) :: array(:,:)
23+
integer(ik) :: fileunit, i
24+
allocate(array(dsize, nrec))
25+
open(newunit=fileunit, file=filename, access='direct',&
26+
action='read', recl=dtype * dsize, status='old')
27+
do i = 1, nrec
28+
read(fileunit, rec=i) array(:,i)
29+
end do
30+
close(fileunit)
31+
end subroutine read_binary_file_2d
32+
33+
end submodule mod_io_submodule

0 commit comments

Comments
 (0)