@@ -10,12 +10,7 @@ Read the paper [here](https://arxiv.org/abs/1902.06714).
1010 - [ Building with fpm] ( https://github.com/modern-fortran/neural-fortran#building-with-fpm )
1111 - [ Building with CMake] ( https://github.com/modern-fortran/neural-fortran#building-with-cmake )
1212* [ Examples] ( https://github.com/modern-fortran/neural-fortran#examples )
13- - [ Creating a network] ( https://github.com/modern-fortran/neural-fortran#creating-a-network )
14- - [ Training the network] ( https://github.com/modern-fortran/neural-fortran#training-the-network )
15- - [ Saving and loading from file] ( https://github.com/modern-fortran/neural-fortran#saving-and-loading-from-file )
16- - [ MNIST training example] ( https://github.com/modern-fortran/neural-fortran#mnist-training-example )
1713* [ API documentation] ( https://github.com/modern-fortran/neural-fortran#api-documentation )
18- * [ Contributing] ( https://github.com/modern-fortran/neural-fortran#contributing )
1914* [ Contributors] ( https://github.com/modern-fortran/neural-fortran#contributors )
2015* [ Related projects] ( https://github.com/modern-fortran/neural-fortran#related-projects )
2116
@@ -25,7 +20,6 @@ Read the paper [here](https://arxiv.org/abs/1902.06714).
2520* Backprop with Mean Square Error cost function
2621* Data-based parallelism
2722* Several activation functions
28- * Support for 32, 64, and 128-bit floating point numbers
2923
3024## Getting started
3125
@@ -44,7 +38,7 @@ Dependencies:
4438
4539Compilers tested include:
4640
47- * gfortran-10.3 .0
41+ * gfortran-9.4 .0
4842* ifort-2021.4
4943* ifx-2021.4
5044
@@ -53,7 +47,7 @@ Compilers tested include:
5347#### Building in serial mode
5448
5549```
56- fpm build --flag "-cpp -O3 -ffast-math -fcoarray=single"
50+ fpm build
5751```
5852
5953#### Building in parallel mode
@@ -64,13 +58,13 @@ Once installed, use the compiler wrappers `caf` and `cafrun` to build and execut
6458in parallel, respectively:
6559
6660```
67- fpm build --compiler caf --flag "-cpp -O3 -ffast-math"
61+ fpm build --compiler caf
6862```
6963
7064#### Testing with fpm
7165
7266```
73- fpm test --flag "-cpp -O3 -ffast-math -fcoarray=single"
67+ fpm test
7468```
7569
7670For the time being, you need to specify the same compiler flags to ` fpm test `
@@ -103,7 +97,7 @@ in parallel, respectively:
10397```
10498FC=caf cmake ..
10599make
106- cafrun -n 4 bin/example_mnist # run MNIST example on 4 cores
100+ cafrun -n 4 bin/mnist # run MNIST example on 4 cores
107101```
108102
109103#### Building with a different compiler
@@ -128,22 +122,6 @@ where the value of `-DBLAS` should point to the desired BLAS implementation,
128122which has to be available in the linking path.
129123This option is currently available only with gfortran.
130124
131- #### Building in double or quad precision
132-
133- By default, neural-fortran is built in single precision mode
134- (32-bit floating point numbers). Alternatively, you can configure to build
135- in 64 or 128-bit floating point mode:
136-
137- ```
138- cmake .. -DREAL=64
139- ```
140-
141- or
142-
143- ```
144- cmake .. -DREAL=128
145- ```
146-
147125#### Building in debug mode
148126
149127To build with debugging flags enabled, type:
@@ -164,202 +142,12 @@ to run the tests.
164142
165143## Examples
166144
167- ### Creating a network
168-
169- Creating a network with 3 layers,
170- one input, one hidden, and one output layer,
171- with 3, 5, and 2 neurons each:
172-
173- ``` fortran
174- use mod_network, only: network_type
175- type(network_type) :: net
176- net = network_type([3, 5, 2])
177- ```
178-
179- ### Setting the activation function
180-
181- By default, the network will be initialized with the sigmoid activation
182- function for all layers. You can specify a different activation function:
183-
184- ``` fortran
185- net = network_type([3, 5, 2], activation='tanh')
186- ```
187-
188- or set it after the fact:
189-
190- ``` fortran
191- net = network_type([3, 5, 2])
192- call net % set_activation('tanh')
193- ```
194-
195- It's possible to set different activation functions for each layer.
196- For example, this snippet will create a network with a Gaussian
197- activation functions for all layers except the output layer,
198- and a RELU function for the output layer:
199-
200- ``` fortran
201- net = network_type([3, 5, 2], activation='gaussian')
202- call net % layers(3) % set_activation('relu')
203- ```
204-
205- Available activation function options are: ` gaussian ` , ` relu ` , ` sigmoid ` ,
206- ` step ` , and ` tanh ` .
207- See [ mod_activation.f90] ( https://github.com/modern-fortran/neural-fortran/blob/master/src/lib/mod_activation.f90 )
208- for specifics.
209-
210- ### Training the network
211-
212- To train the network, pass the training input and output data sample,
213- and a learning rate, to ` net % train() ` :
214-
215- ``` fortran
216- program example_simple
217- use mod_network, only: network_type
218- implicit none
219- type(network_type) :: net
220- real, allocatable :: input(:), output(:)
221- integer :: i
222- net = network_type([3, 5, 2])
223- input = [0.2, 0.4, 0.6]
224- output = [0.123456, 0.246802]
225- do i = 1, 500
226- call net % train(input, output, eta=1.0)
227- print *, 'Iteration: ', i, 'Output:', net % output(input)
228- end do
229- end program example_simple
230- ```
231-
232- The size of ` input ` and ` output ` arrays must match the sizes of the
233- input and output layers, respectively. The learning rate ` eta ` determines
234- how quickly are weights and biases updated.
235-
236- The output is:
237-
238- ```
239- Iteration: 1 Output: 0.470592350 0.764851630
240- Iteration: 2 Output: 0.409876496 0.713752568
241- Iteration: 3 Output: 0.362703383 0.654729187
242- ...
243- Iteration: 500 Output: 0.123456128 0.246801868
244- ```
245-
246- The initial values will vary between runs because we initialize weights
247- and biases randomly.
248-
249- ### Saving and loading from file
250-
251- To save a network to a file, do:
252-
253- ``` fortran
254- call net % save('my_net.txt')
255- ```
256-
257- Loading from file works the same way:
258-
259- ``` fortran
260- call net % load('my_net.txt')
261- ```
262-
263- ### Synchronizing networks in parallel mode
145+ The easiest way to get a sense of how to use neural-fortran is to look at
146+ examples, in increasing level of complexity:
264147
265- When running in parallel mode, you may need to synchronize the weights
266- and biases between images. You can do it like this:
267-
268- ``` fortran
269- call net % sync(1)
270- ```
271-
272- The argument to ` net % sync() ` refers to the source image from which to
273- broadcast. It can be any positive number not greater than ` num_images() ` .
274-
275- ### MNIST training example
276-
277- Here's the complete program:
278-
279- ``` fortran
280- program example_mnist
281-
282- ! A training example with the MNIST dataset.
283- ! Uses stochastic gradient descent and mini-batch size of 100.
284- ! Can be run in serial or parallel mode without modifications.
285-
286- use mod_kinds, only: ik, rk
287- use mod_mnist, only: label_digits, load_mnist
288- use mod_network, only: network_type
289-
290- implicit none
291-
292- real(rk), allocatable :: tr_images(:,:), tr_labels(:)
293- real(rk), allocatable :: te_images(:,:), te_labels(:)
294- real(rk), allocatable :: input(:,:), output(:,:)
295-
296- type(network_type) :: net
297-
298- integer(ik) :: i, n, num_epochs
299- integer(ik) :: batch_size, batch_start, batch_end
300- real(rk) :: pos
301-
302- call load_mnist(tr_images, tr_labels, te_images, te_labels)
303-
304- net = network_type([784, 30, 10])
305-
306- batch_size = 100
307- num_epochs = 10
308-
309- if (this_image() == 1) print '(a,f5.2,a)', 'Initial accuracy: ', &
310- net % accuracy(te_images, label_digits(te_labels)) * 100, ' %'
311-
312- epochs: do n = 1, num_epochs
313- batches: do i = 1, size(tr_labels) / batch_size
314-
315- ! pull a random mini-batch from the dataset
316- call random_number(pos)
317- batch_start = int(pos * (size(tr_labels) - batch_size + 1))
318- batch_end = batch_start + batch_size - 1
319-
320- ! prepare mini-batch
321- input = tr_images(:,batch_start:batch_end)
322- output = label_digits(tr_labels(batch_start:batch_end))
323-
324- ! train the network on the mini-batch
325- call net % train(input, output, eta=3._rk)
326-
327- end do batches
328-
329- if (this_image() == 1) print '(a,i2,a,f5.2,a)', 'Epoch ', n, ' done, Accuracy: ', &
330- net % accuracy(te_images, label_digits(te_labels)) * 100, ' %'
331-
332- end do epochs
333-
334- end program example_mnist
335- ```
336-
337- The MNIST data will be automatically downloaded at the first attempt at loading it
338- with the ` load_mnist ` subroutine.
339-
340- Running the program will report the accuracy after each epoch:
341-
342- ```
343- $ ./example_mnist
344- Initial accuracy: 10.32 %
345- Epoch 1 done, Accuracy: 91.06 %
346- Epoch 2 done, Accuracy: 92.35 %
347- Epoch 3 done, Accuracy: 93.32 %
348- Epoch 4 done, Accuracy: 93.62 %
349- Epoch 5 done, Accuracy: 93.97 %
350- Epoch 6 done, Accuracy: 94.16 %
351- Epoch 7 done, Accuracy: 94.42 %
352- Epoch 8 done, Accuracy: 94.55 %
353- Epoch 9 done, Accuracy: 94.67 %
354- Epoch 10 done, Accuracy: 94.81 %
355- ```
356-
357- You can also run this example without any modifications in parallel,
358- for example on 16 cores using [ OpenCoarrays] ( https://github.com/sourceryinstitute/OpenCoarrays ) :
359-
360- ```
361- $ cafrun -n 16 ./example_mnist
362- ```
148+ 1 . [ simple] ( example/simple.f90 ) : Approximating a simple, constant data relationship
149+ 2 . [ sine] ( example/sine.f90 ) : Approximating a sine function
150+ 3 . [ mnist] ( example/mnist.f90 ) : Hand-written digit recognition using the MNIST dataset
363151
364152## API documentation
365153
@@ -373,23 +161,11 @@ ford ford.md
373161from the neural-fortran top-level directory to generate the API documentation in doc/html.
374162Point your browser to doc/html/index.html to read it.
375163
376- ## Contributing
377-
378- neural-fortran is currently a proof-of-concept with potential for
379- use in production. Contributions are welcome, especially for:
380-
381- * Expanding the network class to other network infrastructures
382- * Adding other cost functions such as cross-entropy.
383- * Model-based (` matmul ` ) parallelism
384- * Adding more examples
385- * Others?
386-
387- You can start at the list of open [ issues] ( https://github.com/modern-fortran/neural-fortran/issues ) .
388-
389164## Contributors
390165
391166Thanks to all open-source contributors to neural-fortran:
392167
168+ * [ @awvwgk ] ( https://github.com/awvwgk )
393169* [ @ivan-pi ] ( https://github.com/ivan-pi )
394170* [ @jvdp1 ] ( https://github.com/jvdp1 )
395171* [ @milancurcic ] ( https://github.com/milancurcic )
0 commit comments