Skip to content

Commit b1b2cac

Browse files
safegaurd Box-Muller normal random number generation against u=0.0 (#158)
* safegaurd Box-Muller normal random number generation against u=0.0 * Docstrings and formatting * Bump patch version --------- Co-authored-by: milancurcic <[email protected]>
1 parent b119194 commit b1b2cac

6 files changed

+23
-73
lines changed

CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ add_library(neural
5656
src/nf/nf_parallel.f90
5757
src/nf/nf_parallel_submodule.f90
5858
src/nf/nf_random.f90
59-
src/nf/nf_random_submodule.f90
6059
src/nf/nf_reshape_layer.f90
6160
src/nf/nf_reshape_layer_submodule.f90
6261
src/nf/io/nf_io_binary.f90

fpm.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name = "neural-fortran"
2-
version = "0.15.0"
2+
version = "0.15.1"
33
license = "MIT"
44
author = "Milan Curcic"
55
maintainer = "[email protected]"

src/nf/nf_conv2d_layer_submodule.f90

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
submodule(nf_conv2d_layer) nf_conv2d_layer_submodule
22

33
use nf_activation, only: activation_function
4-
use nf_random, only: randn
4+
use nf_random, only: random_normal
55

66
implicit none
77

@@ -40,9 +40,8 @@ module subroutine init(self, input_shape)
4040
self % kernel_size, self % kernel_size))
4141

4242
! Initialize the kernel with random values with a normal distribution.
43-
self % kernel = randn(self % filters, self % channels, &
44-
self % kernel_size, self % kernel_size) &
45-
/ self % kernel_size**2 !TODO kernel_width * kernel_height
43+
call random_normal(self % kernel)
44+
self % kernel = self % kernel / self % kernel_size**2
4645

4746
allocate(self % biases(self % filters))
4847
self % biases = 0

src/nf/nf_dense_layer_submodule.f90

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
use nf_activation, only: activation_function
44
use nf_base_layer, only: base_layer
5-
use nf_random, only: randn
5+
use nf_random, only: random_normal
66

77
implicit none
88

@@ -114,8 +114,8 @@ module subroutine init(self, input_shape)
114114
! Weights are a 2-d array of shape previous layer size
115115
! times this layer size.
116116
allocate(self % weights(self % input_size, self % output_size))
117-
self % weights = randn(self % input_size, self % output_size) &
118-
/ self % input_size
117+
call random_normal(self % weights)
118+
self % weights = self % weights / self % input_size
119119

120120
! Broadcast weights to all other images, if any.
121121
call co_broadcast(self % weights, 1)

src/nf/nf_random.f90

+16-29
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,21 @@ module nf_random
66
implicit none
77

88
private
9-
public :: randn
10-
11-
interface randn
12-
13-
module function randn_1d(i) result(r)
14-
!! Generates i random numbers with a normal distribution,
15-
!! using the Box-Muller method.
16-
implicit none
17-
integer, intent(in) :: i
18-
real :: r(i)
19-
end function randn_1d
20-
21-
module function randn_2d(i, j) result(r)
22-
!! Generates i x j random numbers with a normal distribution,
23-
!! using the Box-Muller method.
24-
implicit none
25-
integer, intent(in) :: i, j
26-
real :: r(i,j)
27-
end function randn_2d
28-
29-
module function randn_4d(i, j, k, l) result(r)
30-
!! Generates i x j x k x l random numbers with a normal distribution,
31-
!! using the Box-Muller method.
32-
implicit none
33-
integer, intent(in) :: i, j, k, l
34-
real :: r(i,j,k,l)
35-
end function randn_4d
36-
37-
end interface randn
9+
public :: random_normal
10+
11+
real, parameter :: pi = 4 * atan(1.d0)
12+
13+
contains
14+
15+
impure elemental subroutine random_normal(x)
16+
!! Sample random numbers from a normal distribution using a Box-Muller
17+
!! formula.
18+
real, intent(out) :: x
19+
!! Scalar or array to be filled with random numbers
20+
real :: u(2)
21+
call random_number(u)
22+
u(1) = 1 - u(1)
23+
x = sqrt(- 2 * log(u(1))) * cos(2 * pi * u(2))
24+
end subroutine random_normal
3825

3926
end module nf_random

src/nf/nf_random_submodule.f90

-35
This file was deleted.

0 commit comments

Comments
 (0)