Skip to content

Commit e416673

Browse files
committed
Connected locally_connected_submodule to environment; as for now, it does not still work
1 parent 5697175 commit e416673

File tree

6 files changed

+230
-258
lines changed

6 files changed

+230
-258
lines changed

example/cnn_mnist_1d.f90

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ program cnn_mnist
2020

2121
net = network([ &
2222
input(784), &
23-
reshape_generalized([28, 28]), &
23+
reshape([1,28,28]), &
2424
locally_connected_1d(filters=8, kernel_size=3, activation=relu()), &
25+
maxpool2d(pool_size=2), &
26+
locally_connected_1d(filters=16, kernel_size=3, activation=relu()), &
27+
maxpool2d(pool_size=2), &
2528
dense(10, activation=softmax()) &
2629
])
2730

@@ -34,7 +37,7 @@ program cnn_mnist
3437
label_digits(training_labels), &
3538
batch_size=16, &
3639
epochs=1, &
37-
optimizer=sgd(learning_rate=1) &
40+
optimizer=sgd(learning_rate=0.1) &
3841
)
3942

4043
print '(a,i2,a,f5.2,a)', 'Epoch ', n, ' done, Accuracy: ', accuracy( &

src/nf/nf_conv2d_layer_submodule.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,4 @@ module subroutine set_params(self, params)
243243

244244
end subroutine set_params
245245

246-
end submodule nf_conv2d_layer_submodule
246+
end submodule nf_conv2d_layer_submodule

src/nf/nf_layer_submodule.f90

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ pure module subroutine backward_1d(self, previous, gradient)
4444
call this_layer % backward(prev_layer % output, gradient)
4545
type is(conv2d_layer)
4646
call this_layer % backward(prev_layer % output, gradient)
47+
type is(locally_connected_1d_layer)
48+
call this_layer % backward(prev_layer % output, gradient)
4749
type is(maxpool2d_layer)
4850
call this_layer % backward(prev_layer % output, gradient)
4951
end select
@@ -73,6 +75,8 @@ pure module subroutine backward_3d(self, previous, gradient)
7375
call this_layer % backward(prev_layer % output, gradient)
7476
type is(conv2d_layer)
7577
call this_layer % backward(prev_layer % output, gradient)
78+
type is(locally_connected_1d_layer)
79+
call this_layer % backward(prev_layer % output, gradient)
7680
type is(reshape3d_layer)
7781
call this_layer % backward(prev_layer % output, gradient)
7882
end select
@@ -87,6 +91,8 @@ pure module subroutine backward_3d(self, previous, gradient)
8791
call this_layer % backward(prev_layer % output, gradient)
8892
type is(input3d_layer)
8993
call this_layer % backward(prev_layer % output, gradient)
94+
type is(locally_connected_1d_layer)
95+
call this_layer % backward(prev_layer % output, gradient)
9096
type is(reshape3d_layer)
9197
call this_layer % backward(prev_layer % output, gradient)
9298
end select
@@ -135,6 +141,8 @@ pure module subroutine forward(self, input)
135141
call this_layer % forward(prev_layer % output)
136142
type is(conv2d_layer)
137143
call this_layer % forward(prev_layer % output)
144+
type is(locally_connected_1d_layer)
145+
call this_layer % forward(prev_layer % output)
138146
type is(maxpool2d_layer)
139147
call this_layer % forward(prev_layer % output)
140148
type is(reshape3d_layer)
@@ -149,6 +157,8 @@ pure module subroutine forward(self, input)
149157
call this_layer % forward(prev_layer % output)
150158
type is(conv2d_layer)
151159
call this_layer % forward(prev_layer % output)
160+
type is(locally_connected_1d_layer)
161+
call this_layer % forward(prev_layer % output)
152162
type is(maxpool2d_layer)
153163
call this_layer % forward(prev_layer % output)
154164
type is(reshape3d_layer)
@@ -163,6 +173,8 @@ pure module subroutine forward(self, input)
163173
call this_layer % forward(prev_layer % output)
164174
type is(conv2d_layer)
165175
call this_layer % forward(prev_layer % output)
176+
type is(locally_connected_1d_layer)
177+
call this_layer % forward(prev_layer % output)
166178
type is(maxpool2d_layer)
167179
call this_layer % forward(prev_layer % output)
168180
type is(reshape3d_layer)
@@ -218,6 +230,8 @@ pure module subroutine get_output_3d(self, output)
218230
allocate(output, source=this_layer % output)
219231
type is(conv2d_layer)
220232
allocate(output, source=this_layer % output)
233+
type is(locally_connected_1d_layer)
234+
allocate(output, source=this_layer % output)
221235
type is(maxpool2d_layer)
222236
allocate(output, source=this_layer % output)
223237
type is(reshape3d_layer)
@@ -247,6 +261,8 @@ impure elemental module subroutine init(self, input)
247261
select type(this_layer => self % p)
248262
type is(conv2d_layer)
249263
self % layer_shape = shape(this_layer % output)
264+
type is(locally_connected_1d_layer)
265+
self % layer_shape = shape(this_layer % output)
250266
type is(maxpool2d_layer)
251267
self % layer_shape = shape(this_layer % output)
252268
type is(flatten_layer)
@@ -288,6 +304,8 @@ elemental module function get_num_params(self) result(num_params)
288304
num_params = this_layer % get_num_params()
289305
type is (conv2d_layer)
290306
num_params = this_layer % get_num_params()
307+
type is (locally_connected_1d_layer)
308+
num_params = this_layer % get_num_params()
291309
type is (maxpool2d_layer)
292310
num_params = 0
293311
type is (flatten_layer)
@@ -315,6 +333,8 @@ module function get_params(self) result(params)
315333
params = this_layer % get_params()
316334
type is (conv2d_layer)
317335
params = this_layer % get_params()
336+
type is (locally_connected_1d_layer)
337+
params = this_layer % get_params()
318338
type is (maxpool2d_layer)
319339
! No parameters to get.
320340
type is (flatten_layer)
@@ -342,6 +362,8 @@ module function get_gradients(self) result(gradients)
342362
gradients = this_layer % get_gradients()
343363
type is (conv2d_layer)
344364
gradients = this_layer % get_gradients()
365+
type is (locally_connected_1d_layer)
366+
gradients = this_layer % get_gradients()
345367
type is (maxpool2d_layer)
346368
! No gradients to get.
347369
type is (flatten_layer)
@@ -391,6 +413,9 @@ module subroutine set_params(self, params)
391413

392414
type is (conv2d_layer)
393415
call this_layer % set_params(params)
416+
417+
type is (locally_connected_1d_layer)
418+
call this_layer % set_params(params)
394419

395420
type is (maxpool2d_layer)
396421
! No parameters to set.

src/nf/nf_locally_connected_1d.f90

Lines changed: 116 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -1,112 +1,119 @@
11
module nf_locally_connected_1d_layer
2-
!! This module provides a locally connected 1d layer type.
3-
4-
use nf_activation, only: activation_function
5-
use nf_base_layer, only: base_layer
6-
implicit none
7-
8-
private
9-
public :: locally_connected_1d_layer
10-
11-
type, extends(base_layer) :: locally_connected_1d_layer
12-
! For a 1D layer, we assume an input shape of [channels, input_length]
13-
integer :: channels ! number of input channels
14-
integer :: input_length ! length of the 1D input
15-
integer :: output_length ! computed as input_length - kernel_size + 1
16-
integer :: kernel_size ! size of the 1D window
17-
integer :: filters ! number of filters (output channels)
18-
19-
! Parameters (unshared weights)
20-
! Kernel shape: (filters, output_length, channels, kernel_size)
21-
real, allocatable :: kernel(:,:,:,:)
22-
! Biases shape: (filters, output_length)
23-
real, allocatable :: biases(:,:)
24-
25-
! Forward-pass arrays
26-
! Pre-activation values: shape (filters, output_length)
27-
real, allocatable :: z(:,:)
28-
! Activated output: shape (filters, output_length)
29-
real, allocatable :: output(:,:)
30-
31-
! Gradients for backpropagation
32-
! Gradient for kernel, same shape as kernel
33-
real, allocatable :: dw(:,:,:,:)
34-
! Gradient for biases, same shape as biases
35-
real, allocatable :: db(:,:)
36-
! Gradient with respect to the input, shape (channels, input_length)
37-
real, allocatable :: gradient(:,:)
38-
39-
! Activation function
40-
class(activation_function), allocatable :: activation
41-
contains
42-
procedure :: forward
43-
procedure :: backward
44-
procedure :: get_gradients
45-
procedure :: get_num_params
46-
procedure :: get_params
47-
procedure :: init
48-
procedure :: set_params
49-
end type locally_connected_1d_layer
50-
51-
interface locally_connected_1d_layer
52-
module function locally_connected_1d_layer_cons(filters, kernel_size, activation) result(res)
53-
!! Constructor for the locally connected 1d layer.
54-
integer, intent(in) :: filters
55-
integer, intent(in) :: kernel_size
56-
class(activation_function), intent(in):: activation
57-
type(locally_connected_1d_layer) :: res
58-
end function locally_connected_1d_layer_cons
59-
end interface locally_connected_1d_layer
60-
61-
interface
62-
module subroutine init(self, input_shape)
63-
!! Initialize the layer data structures.
64-
!! input_shape: integer array of length 2, where
65-
!! input_shape(1) = number of channels
66-
!! input_shape(2) = input length
67-
class(locally_connected_1d_layer), intent(inout) :: self
68-
integer, intent(in) :: input_shape(:)
69-
end subroutine init
70-
71-
pure module subroutine forward(self, input)
72-
!! Apply the forward pass.
73-
!! Input shape: (channels, input_length)
74-
class(locally_connected_1d_layer), intent(inout) :: self
75-
real, intent(in) :: input(:,:)
76-
end subroutine forward
77-
78-
pure module subroutine backward(self, input, gradient)
79-
!! Apply the backward pass.
80-
!! input: shape (channels, input_length)
81-
!! gradient: gradient w.r.t. output, shape (filters, output_length)
82-
class(locally_connected_1d_layer), intent(inout) :: self
83-
real, intent(in) :: input(:,:)
84-
real, intent(in) :: gradient(:,:)
85-
end subroutine backward
86-
87-
pure module function get_num_params(self) result(num_params)
88-
!! Get the total number of parameters (kernel + biases)
89-
class(locally_connected_1d_layer), intent(in) :: self
90-
integer :: num_params
91-
end function get_num_params
92-
93-
module function get_params(self) result(params)
94-
!! Return a flattened vector of parameters (kernel then biases).
95-
class(locally_connected_1d_layer), intent(in), target :: self
96-
real, allocatable :: params(:)
97-
end function get_params
98-
99-
module function get_gradients(self) result(gradients)
100-
!! Return a flattened vector of gradients (dw then db).
101-
class(locally_connected_1d_layer), intent(in), target :: self
102-
real, allocatable :: gradients(:)
103-
end function get_gradients
104-
105-
module subroutine set_params(self, params)
106-
!! Set the parameters from a flattened vector.
107-
class(locally_connected_1d_layer), intent(inout) :: self
108-
real, intent(in) :: params(:)
109-
end subroutine set_params
110-
end interface
2+
!! This modules provides a 1-d convolutional `locally_connected_1d` type.
3+
4+
use nf_activation, only: activation_function
5+
use nf_base_layer, only: base_layer
6+
implicit none
7+
8+
private
9+
public :: locally_connected_1d_layer
10+
11+
type, extends(base_layer) :: locally_connected_1d_layer
12+
13+
integer :: width
14+
integer :: height
15+
integer :: channels
16+
integer :: kernel_size
17+
integer :: filters
18+
19+
real, allocatable :: biases(:) ! size(filters)
20+
real, allocatable :: kernel(:,:,:) ! filters x channels x window x window
21+
real, allocatable :: output(:,:) ! filters x output_width * output_height
22+
real, allocatable :: z(:,:) ! kernel .dot. input + bias
23+
24+
real, allocatable :: dw(:,:,:) ! weight (kernel) gradients
25+
real, allocatable :: db(:) ! bias gradients
26+
real, allocatable :: gradient(:,:)
27+
28+
class(activation_function), allocatable :: activation
29+
30+
contains
31+
32+
procedure :: forward
33+
procedure :: backward
34+
procedure :: get_gradients
35+
procedure :: get_num_params
36+
procedure :: get_params
37+
procedure :: init
38+
procedure :: set_params
39+
40+
end type locally_connected_1d_layer
41+
42+
interface locally_connected_1d_layer
43+
module function locally_connected_1d_layer_cons(filters, kernel_size, activation) &
44+
result(res)
45+
!! `locally_connected_1d_layer` constructor function
46+
integer, intent(in) :: filters
47+
integer, intent(in) :: kernel_size
48+
class(activation_function), intent(in) :: activation
49+
type(locally_connected_1d_layer) :: res
50+
end function locally_connected_1d_layer_cons
51+
end interface locally_connected_1d_layer
52+
53+
interface
54+
55+
module subroutine init(self, input_shape)
56+
!! Initialize the layer data structures.
57+
!!
58+
!! This is a deferred procedure from the `base_layer` abstract type.
59+
class(locally_connected_1d_layer), intent(in out) :: self
60+
!! A `locally_connected_1d_layer` instance
61+
integer, intent(in) :: input_shape(:)
62+
!! Input layer dimensions
63+
end subroutine init
64+
65+
pure module subroutine forward(self, input)
66+
!! Apply a forward pass on the `locally_connected_1d` layer.
67+
class(locally_connected_1d_layer), intent(in out) :: self
68+
!! A `locally_connected_1d_layer` instance
69+
real, intent(in) :: input(:,:)
70+
!! Input data
71+
end subroutine forward
72+
73+
pure module subroutine backward(self, input, gradient)
74+
!! Apply a backward pass on the `locally_connected_1d` layer.
75+
class(locally_connected_1d_layer), intent(in out) :: self
76+
!! A `locally_connected_1d_layer` instance
77+
real, intent(in) :: input(:,:)
78+
!! Input data (previous layer)
79+
real, intent(in) :: gradient(:,:)
80+
!! Gradient (next layer)
81+
end subroutine backward
82+
83+
pure module function get_num_params(self) result(num_params)
84+
!! Get the number of parameters in the layer.
85+
class(locally_connected_1d_layer), intent(in) :: self
86+
!! A `locally_connected_1d_layer` instance
87+
integer :: num_params
88+
!! Number of parameters
89+
end function get_num_params
90+
91+
module function get_params(self) result(params)
92+
!! Return the parameters (weights and biases) of this layer.
93+
!! The parameters are ordered as weights first, biases second.
94+
class(locally_connected_1d_layer), intent(in), target :: self
95+
!! A `locally_connected_1d_layer` instance
96+
real, allocatable :: params(:)
97+
!! Parameters to get
98+
end function get_params
99+
100+
module function get_gradients(self) result(gradients)
101+
!! Return the gradients of this layer.
102+
!! The gradients are ordered as weights first, biases second.
103+
class(locally_connected_1d_layer), intent(in), target :: self
104+
!! A `locally_connected_1d_layer` instance
105+
real, allocatable :: gradients(:)
106+
!! Gradients to get
107+
end function get_gradients
108+
109+
module subroutine set_params(self, params)
110+
!! Set the parameters of the layer.
111+
class(locally_connected_1d_layer), intent(in out) :: self
112+
!! A `locally_connected_1d_layer` instance
113+
real, intent(in) :: params(:)
114+
!! Parameters to set
115+
end subroutine set_params
116+
117+
end interface
111118

112119
end module nf_locally_connected_1d_layer

0 commit comments

Comments
 (0)