Skip to content

Commit 5cf14eb

Browse files
ricor07jvdp1milancurcic
authored
Locally connected layer (#201)
* changing reshape layer * added tests; to note that they don't work * Now reshape2d works, maxpool still not * Saving changes before rebasing * Resolved merge conflicts * Bug fixed; Added conv1d; Conv1d and maxpool backward still not working * Bug fixes; now everything works * Updated the comments * Implemented locally connected 1d * Bug fix * Bug fix * New bugs * Bug fix * Definitive bug fixes * Adding jvdp1's review * Implemented OneAdder's suggestions * Deleting useless variables * again * Update src/nf/nf_conv1d_layer_submodule.f90 Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com> * locally_connected_1d -> locally_connected1d * Update features table * Fix CMakeLists * Fix CmakeListst * Another one * Tidy up * Acknowledge contributors --------- Co-authored-by: Jeremie Vandenplas <jeremie.vandenplas@gmail.com> Co-authored-by: milancurcic <caomaco@gmail.com>
1 parent e628d1e commit 5cf14eb

27 files changed

+2062
-86
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ add_library(neural-fortran
1818
src/nf.f90
1919
src/nf/nf_activation.f90
2020
src/nf/nf_base_layer.f90
21+
src/nf/nf_conv1d_layer.f90
22+
src/nf/nf_conv1d_layer_submodule.f90
2123
src/nf/nf_conv2d_layer.f90
2224
src/nf/nf_conv2d_layer_submodule.f90
2325
src/nf/nf_cross_attention_layer.f90
@@ -41,12 +43,16 @@ add_library(neural-fortran
4143
src/nf/nf_layernorm_submodule.f90
4244
src/nf/nf_layer.f90
4345
src/nf/nf_layer_submodule.f90
46+
src/nf/nf_locally_connected1d_layer_submodule.f90
47+
src/nf/nf_locally_connected1d_layer.f90
4448
src/nf/nf_linear2d_layer.f90
4549
src/nf/nf_linear2d_layer_submodule.f90
4650
src/nf/nf_embedding_layer.f90
4751
src/nf/nf_embedding_layer_submodule.f90
4852
src/nf/nf_loss.f90
4953
src/nf/nf_loss_submodule.f90
54+
src/nf/nf_maxpool1d_layer.f90
55+
src/nf/nf_maxpool1d_layer_submodule.f90
5056
src/nf/nf_maxpool2d_layer.f90
5157
src/nf/nf_maxpool2d_layer_submodule.f90
5258
src/nf/nf_metrics.f90
@@ -60,6 +66,8 @@ add_library(neural-fortran
6066
src/nf/nf_random.f90
6167
src/nf/nf_reshape_layer.f90
6268
src/nf/nf_reshape_layer_submodule.f90
69+
src/nf/nf_reshape2d_layer.f90
70+
src/nf/nf_reshape2d_layer_submodule.f90
6371
src/nf/nf_self_attention_layer.f90
6472
src/nf/io/nf_io_binary.f90
6573
src/nf/io/nf_io_binary_submodule.f90

README.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,18 @@ Read the paper [here](https://arxiv.org/abs/1902.06714).
3333
| Embedding | `embedding` | n/a | 2 |||
3434
| Dense (fully-connected) | `dense` | `input1d`, `dense`, `dropout`, `flatten` | 1 |||
3535
| Dropout | `dropout` | `dense`, `flatten`, `input1d` | 1 |||
36-
| Convolutional (2-d) | `conv2d` | `input3d`, `conv2d`, `maxpool2d`, `reshape` | 3 || ✅(*) |
36+
| Locally connected (1-d) | `locally_connected1d` | `input2d`, `locally_connected1d`, `conv1d`, `maxpool1d`, `reshape2d` | 2 |||
37+
| Convolutional (1-d) | `conv1d` | `input2d`, `conv1d`, `maxpool1d`, `reshape2d` | 2 |||
38+
| Convolutional (2-d) | `conv2d` | `input3d`, `conv2d`, `maxpool2d`, `reshape` | 3 |||
39+
| Max-pooling (1-d) | `maxpool1d` | `input2d`, `conv1d`, `maxpool1d`, `reshape2d` | 2 |||
3740
| Max-pooling (2-d) | `maxpool2d` | `input3d`, `conv2d`, `maxpool2d`, `reshape` | 3 |||
3841
| Linear (2-d) | `linear2d` | `input2d`, `layernorm`, `linear2d`, `self_attention` | 2 |||
3942
| Self-attention | `self_attention` | `input2d`, `layernorm`, `linear2d`, `self_attention` | 2 |||
4043
| Layer Normalization | `layernorm` | `linear2d`, `self_attention` | 2 |||
4144
| Flatten | `flatten` | `input2d`, `input3d`, `conv2d`, `maxpool2d`, `reshape` | 1 |||
45+
| Reshape (1-d to 2-d) | `reshape2d` | `input2d`, `conv1d`, `locally_connected1d`, `maxpool1d` | 2 |||
4246
| Reshape (1-d to 3-d) | `reshape` | `input1d`, `dense`, `flatten` | 3 |||
4347

44-
(*) See Issue [#145](https://github.com/modern-fortran/neural-fortran/issues/145) regarding non-converging CNN training on the MNIST dataset.
45-
4648
## Getting started
4749

4850
Get the code:
@@ -267,7 +269,9 @@ Thanks to all open-source contributors to neural-fortran:
267269
[jvdp1](https://github.com/jvdp1),
268270
[jvo203](https://github.com/jvo203),
269271
[milancurcic](https://github.com/milancurcic),
272+
[OneAdder](https://github.com/OneAdder),
270273
[pirpyn](https://github.com/pirpyn),
274+
[rico07](https://github.com/ricor07),
271275
[rouson](https://github.com/rouson),
272276
[rweed](https://github.com/rweed),
273277
[Spnetic-5](https://github.com/Spnetic-5),

example/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
foreach(execid
22
cnn_mnist
3+
cnn_mnist_1d
34
dense_mnist
45
get_set_network_params
56
network_parameters

example/cnn_mnist.f90

+3-3
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ program cnn_mnist
1212
real, allocatable :: validation_images(:,:), validation_labels(:)
1313
real, allocatable :: testing_images(:,:), testing_labels(:)
1414
integer :: n
15-
integer, parameter :: num_epochs = 10
15+
integer, parameter :: num_epochs = 250
1616

1717
call load_mnist(training_images, training_labels, &
1818
validation_images, validation_labels, &
@@ -35,9 +35,9 @@ program cnn_mnist
3535
call net % train( &
3636
training_images, &
3737
label_digits(training_labels), &
38-
batch_size=128, &
38+
batch_size=16, &
3939
epochs=1, &
40-
optimizer=sgd(learning_rate=3.) &
40+
optimizer=sgd(learning_rate=0.001) &
4141
)
4242

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

example/cnn_mnist_1d.f90

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
program cnn_mnist_1d
2+
3+
use nf, only: network, sgd, &
4+
input, conv1d, maxpool1d, flatten, dense, reshape, reshape2d, locally_connected1d, &
5+
load_mnist, label_digits, softmax, relu
6+
7+
implicit none
8+
9+
type(network) :: net
10+
11+
real, allocatable :: training_images(:,:), training_labels(:)
12+
real, allocatable :: validation_images(:,:), validation_labels(:)
13+
real, allocatable :: testing_images(:,:), testing_labels(:)
14+
integer :: n
15+
integer, parameter :: num_epochs = 250
16+
17+
call load_mnist(training_images, training_labels, &
18+
validation_images, validation_labels, &
19+
testing_images, testing_labels)
20+
21+
net = network([ &
22+
input(784), &
23+
reshape2d([28, 28]), &
24+
locally_connected1d(filters=8, kernel_size=3, activation=relu()), &
25+
maxpool1d(pool_size=2), &
26+
locally_connected1d(filters=16, kernel_size=3, activation=relu()), &
27+
maxpool1d(pool_size=2), &
28+
dense(10, activation=softmax()) &
29+
])
30+
31+
call net % print_info()
32+
33+
epochs: do n = 1, num_epochs
34+
35+
call net % train( &
36+
training_images, &
37+
label_digits(training_labels), &
38+
batch_size=16, &
39+
epochs=1, &
40+
optimizer=sgd(learning_rate=0.01) &
41+
)
42+
43+
print '(a,i2,a,f5.2,a)', 'Epoch ', n, ' done, Accuracy: ', accuracy( &
44+
net, validation_images, label_digits(validation_labels)) * 100, ' %'
45+
46+
end do epochs
47+
48+
print '(a,f5.2,a)', 'Testing accuracy: ', &
49+
accuracy(net, testing_images, label_digits(testing_labels)) * 100, '%'
50+
51+
contains
52+
53+
real function accuracy(net, x, y)
54+
type(network), intent(in out) :: net
55+
real, intent(in) :: x(:,:), y(:,:)
56+
integer :: i, good
57+
good = 0
58+
do i = 1, size(x, dim=2)
59+
if (all(maxloc(net % predict(x(:,i))) == maxloc(y(:,i)))) then
60+
good = good + 1
61+
end if
62+
end do
63+
accuracy = real(good) / size(x, dim=2)
64+
end function accuracy
65+
66+
end program cnn_mnist_1d
67+

src/nf.f90

+4
Original file line numberDiff line numberDiff line change
@@ -3,6 +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+
conv1d, &
67
conv2d, &
78
dense, &
89
dropout, &
@@ -11,8 +12,11 @@ module nf
1112
input, &
1213
layernorm, &
1314
linear2d, &
15+
locally_connected1d, &
16+
maxpool1d, &
1417
maxpool2d, &
1518
reshape, &
19+
reshape2d, &
1620
self_attention
1721
use nf_loss, only: mse, quadratic
1822
use nf_metrics, only: corr, maxabs

0 commit comments

Comments
 (0)