Skip to content

Commit 34241f4

Browse files
committed
adding locally_connected_1d to neural-fortran environment
1 parent 6fde9da commit 34241f4

File tree

6 files changed

+64
-9
lines changed

6 files changed

+64
-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

@@ -174,6 +174,33 @@ module function maxpool2d(pool_size, stride) result(res)
174174
!! Resulting layer instance
175175
end function maxpool2d
176176

177+
module function locally_connected_1d(filters, kernel_size, activation) result(res)
178+
!! 2-d convolutional layer constructor.
179+
!!
180+
!! This layer is for building 2-d convolutional network.
181+
!! Although the established convention is to call these layers 2-d,
182+
!! the shape of the data is actuall 3-d: image width, image height,
183+
!! and the number of channels.
184+
!! A conv2d layer must not be the first layer in the network.
185+
!!
186+
!! Example:
187+
!!
188+
!! ```
189+
!! use nf, only :: conv2d, layer
190+
!! type(layer) :: conv2d_layer
191+
!! conv2d_layer = dense(filters=32, kernel_size=3)
192+
!! conv2d_layer = dense(filters=32, kernel_size=3, activation='relu')
193+
!! ```
194+
integer, intent(in) :: filters
195+
!! Number of filters in the output of the layer
196+
integer, intent(in) :: kernel_size
197+
!! Width of the convolution window, commonly 3 or 5
198+
class(activation_function), intent(in), optional :: activation
199+
!! Activation function (default sigmoid)
200+
type(layer) :: res
201+
!! Resulting layer instance
202+
end function locally_connected_1d
203+
177204
module function reshape(output_shape) result(res)
178205
!! Rank-1 to rank-any reshape layer constructor.
179206
!! Currently implemented is only rank-3 for the output of the reshape.

src/nf/nf_layer_constructors_submodule.f90

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use nf_input1d_layer, only: input1d_layer
88
use nf_input2d_layer, only: input2d_layer
99
use nf_input3d_layer, only: input3d_layer
10+
use nf_locally_connected_1d_layer, only: locally_connected_1d_layer
1011
use nf_maxpool2d_layer, only: maxpool2d_layer
1112
use nf_reshape_layer, only: reshape3d_layer
1213
use nf_activation, only: activation_function, relu, sigmoid
@@ -103,6 +104,33 @@ module function input3d(dim1, dim2, dim3) result(res)
103104
res % initialized = .true.
104105
end function input3d
105106

107+
<<<<<<< HEAD
108+
=======
109+
module function locally_connected_1d(filters, kernel_size, activation) result(res)
110+
integer, intent(in) :: filters
111+
integer, intent(in) :: kernel_size
112+
class(activation_function), intent(in), optional :: activation
113+
type(layer) :: res
114+
class(activation_function), allocatable :: activation_tmp
115+
116+
res % name = 'locally_connected_1d'
117+
118+
if (present(activation)) then
119+
allocate(activation_tmp, source=activation)
120+
else
121+
allocate(activation_tmp, source=relu())
122+
end if
123+
124+
res % activation = activation_tmp % get_name()
125+
126+
allocate( &
127+
res % p, &
128+
source=locally_connected_1d_layer(filters, kernel_size, activation_tmp) &
129+
)
130+
131+
end function locally_connected_1d
132+
133+
>>>>>>> 0fbbda4 (adding locally_connected_1d to neural-fortran environment)
106134

107135
module function maxpool2d(pool_size, stride) result(res)
108136
integer, intent(in) :: pool_size

src/nf/nf_network_submodule.f90

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,11 @@
66
use nf_input1d_layer, only: input1d_layer
77
use nf_input2d_layer, only: input2d_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_layer, only: layer
12-
use nf_layer_constructors, only: conv2d, dense, flatten, input, maxpool2d, reshape
13+
use nf_layer_constructors, only: conv2d, dense, flatten, input, locally_connected_1d, maxpool2d, reshape
1314
use nf_loss, only: quadratic
1415
use nf_optimizers, only: optimizer_base_type, sgd
1516
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)