Skip to content
Closed
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
4 changes: 4 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@ add_library(neural-fortran
src/nf/nf_layer_constructors_submodule.f90
src/nf/nf_layer.f90
src/nf/nf_layer_submodule.f90
src/nf/nf_locally_connected_1d.f90
src/nf/nf_locally_connected_1d_submodule.f90
src/nf/nf_loss.f90
src/nf/nf_loss_submodule.f90
src/nf/nf_maxpool2d_layer.f90
Expand All @@ -47,6 +49,8 @@ add_library(neural-fortran
src/nf/nf_parallel.f90
src/nf/nf_parallel_submodule.f90
src/nf/nf_random.f90
src/nf/nf_reshape_generalized.f90
src/nf/nf_reshape_generalized_submodule.f90
src/nf/nf_reshape_layer.f90
src/nf/nf_reshape_layer_submodule.f90
src/nf/io/nf_io_binary.f90
Expand Down
1 change: 1 addition & 0 deletions example/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
foreach(execid
cnn_mnist
cnn_mnist_1d
dense_mnist
get_set_network_params
network_parameters
Expand Down
6 changes: 3 additions & 3 deletions example/cnn_mnist.f90
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ program cnn_mnist
real, allocatable :: validation_images(:,:), validation_labels(:)
real, allocatable :: testing_images(:,:), testing_labels(:)
integer :: n
integer, parameter :: num_epochs = 10
integer, parameter :: num_epochs = 20

call load_mnist(training_images, training_labels, &
validation_images, validation_labels, &
Expand All @@ -35,9 +35,9 @@ program cnn_mnist
call net % train( &
training_images, &
label_digits(training_labels), &
batch_size=128, &
batch_size=16, &
epochs=1, &
optimizer=sgd(learning_rate=3.) &
optimizer=sgd(learning_rate=0.003) &
)

print '(a,i2,a,f5.2,a)', 'Epoch ', n, ' done, Accuracy: ', accuracy( &
Expand Down
64 changes: 64 additions & 0 deletions example/cnn_mnist_1d.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
program cnn_mnist

use nf, only: network, sgd, &
input, conv2d, maxpool2d, flatten, dense, reshape, reshape_generalized, locally_connected_1d, &
load_mnist, label_digits, softmax, relu

implicit none

type(network) :: net

real, allocatable :: training_images(:,:), training_labels(:)
real, allocatable :: validation_images(:,:), validation_labels(:)
real, allocatable :: testing_images(:,:), testing_labels(:)
integer :: n
integer, parameter :: num_epochs = 10

call load_mnist(training_images, training_labels, &
validation_images, validation_labels, &
testing_images, testing_labels)

net = network([ &
input(784), &
reshape_generalized([28, 28]), &
locally_connected_1d(filters=8, kernel_size=3, activation=relu()), &
dense(10, activation=softmax()) &
])

call net % print_info()

epochs: do n = 1, num_epochs

call net % train( &
training_images, &
label_digits(training_labels), &
batch_size=16, &
epochs=1, &
optimizer=sgd(learning_rate=1) &
)

print '(a,i2,a,f5.2,a)', 'Epoch ', n, ' done, Accuracy: ', accuracy( &
net, validation_images, label_digits(validation_labels)) * 100, ' %'

end do epochs

print '(a,f5.2,a)', 'Testing accuracy: ', &
accuracy(net, testing_images, label_digits(testing_labels)) * 100, '%'

contains

real function accuracy(net, x, y)
type(network), intent(in out) :: net
real, intent(in) :: x(:,:), y(:,:)
integer :: i, good
good = 0
do i = 1, size(x, dim=2)
if (all(maxloc(net % predict(x(:,i))) == maxloc(y(:,i)))) then
good = good + 1
end if
end do
accuracy = real(good) / size(x, dim=2)
end function accuracy

end program cnn_mnist

2 changes: 1 addition & 1 deletion src/nf.f90
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module nf
use nf_datasets_mnist, only: label_digits, load_mnist
use nf_layer, only: layer
use nf_layer_constructors, only: &
conv2d, dense, flatten, input, maxpool2d, reshape
conv2d, dense, flatten, input, maxpool2d, reshape, reshape_generalized, locally_connected_1d
use nf_loss, only: mse, quadratic
use nf_metrics, only: corr, maxabs
use nf_network, only: network
Expand Down
Loading
Loading