Skip to content
Open
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
15 changes: 1 addition & 14 deletions src/nf/nf_conv1d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ module function conv1d_layer_cons(filters, kernel_size, activation) result(res)
end function conv1d_layer_cons

module subroutine init(self, input_shape)
implicit none
class(conv1d_layer), intent(in out) :: self
integer, intent(in) :: input_shape(:)

Expand Down Expand Up @@ -54,16 +53,11 @@ module subroutine init(self, input_shape)
end subroutine init

pure module subroutine forward(self, input)
implicit none
class(conv1d_layer), intent(in out) :: self
real, intent(in) :: input(:,:)
integer :: input_channels, input_width
integer :: j, n
integer :: iws, iwe

input_channels = size(input, dim=1)
input_width = size(input, dim=2)

! Loop over output positions.
do j = 1, self % width
! Compute the input window corresponding to output index j.
Expand All @@ -85,14 +79,12 @@ pure module subroutine forward(self, input)
end subroutine forward

pure module subroutine backward(self, input, gradient)
implicit none
class(conv1d_layer), intent(in out) :: self
! 'input' has shape: (channels, input_width)
! 'gradient' (dL/dy) has shape: (filters, output_width)
real, intent(in) :: input(:,:)
real, intent(in) :: gradient(:,:)

integer :: input_channels, input_width, output_width
integer :: j, n, k
integer :: iws, iwe

Expand All @@ -101,11 +93,6 @@ pure module subroutine backward(self, input, gradient)
real :: db_local(self % filters)
real :: dw_local(self % filters, self % channels, self % kernel_size)

! Determine dimensions.
input_channels = size(input, dim=1)
input_width = size(input, dim=2)
output_width = self % width ! Note: output_width = input_width - kernel_size + 1

!--- Compute the local gradient gdz = (dL/dy) * sigma'(z) for each output.
gdz = gradient * self % activation % eval_prime(self % z)

Expand All @@ -120,7 +107,7 @@ pure module subroutine backward(self, input, gradient)
! In the forward pass the window for output index j was:
! iws = j, iwe = j + kernel_size - 1.
do n = 1, self % filters
do j = 1, output_width
do j = 1, self % width
iws = j
iwe = j + self % kernel_size - 1
do k = 1, self % channels
Expand Down
2 changes: 0 additions & 2 deletions src/nf/nf_conv2d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@ end subroutine init


pure module subroutine forward(self, input)
implicit none
class(conv2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:,:)
integer :: input_width, input_height, input_channels
Expand Down Expand Up @@ -113,7 +112,6 @@ end subroutine forward


pure module subroutine backward(self, input, gradient)
implicit none
class(conv2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:,:)
real, intent(in) :: gradient(:,:,:)
Expand Down
19 changes: 3 additions & 16 deletions src/nf/nf_locally_connected2d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
contains

module function locally_connected2d_layer_cons(filters, kernel_size, activation) result(res)
implicit none
integer, intent(in) :: filters
integer, intent(in) :: kernel_size
class(activation_function), intent(in) :: activation
Expand All @@ -21,7 +20,6 @@ module function locally_connected2d_layer_cons(filters, kernel_size, activation)
end function locally_connected2d_layer_cons

module subroutine init(self, input_shape)
implicit none
class(locally_connected2d_layer), intent(in out) :: self
integer, intent(in) :: input_shape(:)

Expand Down Expand Up @@ -52,16 +50,11 @@ module subroutine init(self, input_shape)
end subroutine init

pure module subroutine forward(self, input)
implicit none
class(locally_connected2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:)
integer :: input_channels, input_width
integer :: j, n
integer :: iws, iwe

input_channels = size(input, dim=1)
input_width = size(input, dim=2)

do j = 1, self % width
iws = j
iwe = j + self % kernel_size - 1
Expand All @@ -73,27 +66,21 @@ pure module subroutine forward(self, input)
end subroutine forward

pure module subroutine backward(self, input, gradient)
implicit none
class(locally_connected2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:)
real, intent(in) :: gradient(:,:)
integer :: input_channels, input_width, output_width
integer :: j, n, k
integer :: iws, iwe
real :: gdz(self % filters, self % width)
real :: db_local(self % filters, self % width)
real :: dw_local(self % filters, self % width, self % channels, self % kernel_size)

input_channels = size(input, dim=1)
input_width = size(input, dim=2)
output_width = self % width

do j = 1, output_width
do j = 1, self % width
gdz(:, j) = gradient(:, j) * self % activation % eval_prime(self % z(:, j))
end do

do n = 1, self % filters
do j = 1, output_width
do j = 1, self % width
db_local(n, j) = gdz(n, j)
end do
end do
Expand All @@ -102,7 +89,7 @@ pure module subroutine backward(self, input, gradient)
self % gradient = 0.0

do n = 1, self % filters
do j = 1, output_width
do j = 1, self % width
iws = j
iwe = j + self % kernel_size - 1
do k = 1, self % channels
Expand Down
4 changes: 0 additions & 4 deletions src/nf/nf_maxpool1d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
contains

pure module function maxpool1d_layer_cons(pool_size, stride) result(res)
implicit none
integer, intent(in) :: pool_size
integer, intent(in) :: stride
type(maxpool1d_layer) :: res
Expand All @@ -15,7 +14,6 @@ end function maxpool1d_layer_cons


module subroutine init(self, input_shape)
implicit none
class(maxpool1d_layer), intent(in out) :: self
integer, intent(in) :: input_shape(:)

Expand All @@ -34,7 +32,6 @@ module subroutine init(self, input_shape)
end subroutine init

pure module subroutine forward(self, input)
implicit none
class(maxpool1d_layer), intent(in out) :: self
real, intent(in) :: input(:,:)
integer :: input_width
Expand Down Expand Up @@ -70,7 +67,6 @@ pure module subroutine forward(self, input)
end subroutine forward

pure module subroutine backward(self, input, gradient)
implicit none
class(maxpool1d_layer), intent(in out) :: self
real, intent(in) :: input(:,:)
real, intent(in) :: gradient(:,:)
Expand Down
4 changes: 0 additions & 4 deletions src/nf/nf_maxpool2d_layer_submodule.f90
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
contains

pure module function maxpool2d_layer_cons(pool_size, stride) result(res)
implicit none
integer, intent(in) :: pool_size
integer, intent(in) :: stride
type(maxpool2d_layer) :: res
Expand All @@ -15,7 +14,6 @@ end function maxpool2d_layer_cons


module subroutine init(self, input_shape)
implicit none
class(maxpool2d_layer), intent(in out) :: self
integer, intent(in) :: input_shape(:)

Expand All @@ -39,7 +37,6 @@ end subroutine init


pure module subroutine forward(self, input)
implicit none
class(maxpool2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:,:)
integer :: input_width, input_height
Expand Down Expand Up @@ -86,7 +83,6 @@ end subroutine forward


pure module subroutine backward(self, input, gradient)
implicit none
class(maxpool2d_layer), intent(in out) :: self
real, intent(in) :: input(:,:,:)
real, intent(in) :: gradient(:,:,:)
Expand Down