Skip to content

Commit 0fbbda4

Browse files
committed
adding locally_connected_1d to neural-fortran environment
1 parent 2514fd0 commit 0fbbda4

File tree

6 files changed

+62
-9
lines changed

6 files changed

+62
-9
lines changed

example/cnn_mnist_1d.f90

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
program cnn_mnist
22

33
use nf, only: network, sgd, &
4-
input, conv2d, maxpool2d, flatten, dense, reshape, &
4+
input, conv2d, maxpool2d, flatten, dense, reshape, locally_connected_1d, &
55
load_mnist, label_digits, softmax, relu
66

77
implicit none
@@ -20,11 +20,8 @@ program cnn_mnist
2020

2121
net = network([ &
2222
input(784), &
23-
reshape([1,28,28]), &
24-
conv2d(filters=8, kernel_size=3, activation=relu()), &
25-
maxpool2d(pool_size=2), &
26-
conv2d(filters=16, kernel_size=3, activation=relu()), &
27-
maxpool2d(pool_size=2), &
23+
reshape([1,784]), &
24+
locally_connected_1d(filters=8, kernel_size=2, activation=relu()), &
2825
dense(10, activation=softmax()) &
2926
])
3027

src/nf.f90

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ module nf
33
use nf_datasets_mnist, only: label_digits, load_mnist
44
use nf_layer, only: layer
55
use nf_layer_constructors, only: &
6-
conv2d, dense, flatten, input, maxpool2d, reshape
6+
conv2d, dense, flatten, input, maxpool2d, reshape, locally_connected_1d
77
use nf_loss, only: mse, quadratic
88
use nf_metrics, only: corr, maxabs
99
use nf_network, only: network

src/nf/nf_layer_constructors.f90

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ module nf_layer_constructors
88
implicit none
99

1010
private
11-
public :: conv2d, dense, flatten, input, maxpool2d, reshape
11+
public :: conv2d, dense, flatten, input, locally_connected_1d, maxpool2d, reshape
1212

1313
interface input
1414

@@ -155,6 +155,33 @@ module function maxpool2d(pool_size, stride) result(res)
155155
!! Resulting layer instance
156156
end function maxpool2d
157157

158+
module function locally_connected_1d(filters, kernel_size, activation) result(res)
159+
!! 2-d convolutional layer constructor.
160+
!!
161+
!! This layer is for building 2-d convolutional network.
162+
!! Although the established convention is to call these layers 2-d,
163+
!! the shape of the data is actuall 3-d: image width, image height,
164+
!! and the number of channels.
165+
!! A conv2d layer must not be the first layer in the network.
166+
!!
167+
!! Example:
168+
!!
169+
!! ```
170+
!! use nf, only :: conv2d, layer
171+
!! type(layer) :: conv2d_layer
172+
!! conv2d_layer = dense(filters=32, kernel_size=3)
173+
!! conv2d_layer = dense(filters=32, kernel_size=3, activation='relu')
174+
!! ```
175+
integer, intent(in) :: filters
176+
!! Number of filters in the output of the layer
177+
integer, intent(in) :: kernel_size
178+
!! Width of the convolution window, commonly 3 or 5
179+
class(activation_function), intent(in), optional :: activation
180+
!! Activation function (default sigmoid)
181+
type(layer) :: res
182+
!! Resulting layer instance
183+
end function locally_connected_1d
184+
158185
module function reshape(output_shape) result(res)
159186
!! Rank-1 to rank-any reshape layer constructor.
160187
!! Currently implemented is only rank-3 for the output of the reshape.

src/nf/nf_layer_constructors_submodule.f90

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use nf_flatten_layer, only: flatten_layer
77
use nf_input1d_layer, only: input1d_layer
88
use nf_input3d_layer, only: input3d_layer
9+
use nf_locally_connected_1d_layer, only: locally_connected_1d_layer
910
use nf_maxpool2d_layer, only: maxpool2d_layer
1011
use nf_reshape_layer, only: reshape3d_layer
1112
use nf_activation, only: activation_function, relu, sigmoid
@@ -91,6 +92,31 @@ module function input3d(layer_shape) result(res)
9192
res % initialized = .true.
9293
end function input3d
9394

95+
module function locally_connected_1d(filters, kernel_size, activation) result(res)
96+
integer, intent(in) :: filters
97+
integer, intent(in) :: kernel_size
98+
class(activation_function), intent(in), optional :: activation
99+
type(layer) :: res
100+
class(activation_function), allocatable :: activation_tmp
101+
102+
res % name = 'locally_connected_1d'
103+
104+
if (present(activation)) then
105+
allocate(activation_tmp, source=activation)
106+
else
107+
allocate(activation_tmp, source=relu())
108+
end if
109+
110+
res % activation = activation_tmp % get_name()
111+
112+
allocate( &
113+
res % p, &
114+
source=locally_connected_1d_layer(filters, kernel_size, activation_tmp) &
115+
)
116+
117+
end function locally_connected_1d
118+
119+
94120
module function maxpool2d(pool_size, stride) result(res)
95121
integer, intent(in) :: pool_size
96122
integer, intent(in), optional :: stride

src/nf/nf_network_submodule.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
use nf_flatten_layer, only: flatten_layer
66
use nf_input1d_layer, only: input1d_layer
77
use nf_input3d_layer, only: input3d_layer
8+
use nf_locally_connected_1d_layer, only: locally_connected_1d_layer
89
use nf_maxpool2d_layer, only: maxpool2d_layer
910
use nf_reshape_layer, only: reshape3d_layer
1011
use nf_layer, only: layer
11-
use nf_layer_constructors, only: conv2d, dense, flatten, input, maxpool2d, reshape
12+
use nf_layer_constructors, only: conv2d, dense, flatten, input, locally_connected_1d, maxpool2d, reshape
1213
use nf_loss, only: quadratic
1314
use nf_optimizers, only: optimizer_base_type, sgd
1415
use nf_parallel, only: tile_indices

src/nf/nf_reshape_layer_submodule.f90

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,6 @@ module subroutine init(self, input_shape)
4848

4949
end subroutine init
5050

51+
52+
5153
end submodule nf_reshape_layer_submodule

0 commit comments

Comments
 (0)