Using a trained neural network model in a fortran code

Here is the code of simple.f90. I do not get any output when executing it.
Since I am using ifort 2021, this may indeed be the problem. Would you
have an idea on how to fix this ?

program simple
  use nf, only: dense, input, network
  implicit none
  type(network) :: net
  real, allocatable :: x(:), y(:)
  integer, parameter :: num_iterations = 500
  integer :: n

  print '("Simple")'
  print '(60("="))'

  net = network([ &
    input(3), &
    dense(5), &
    dense(2) &
  ])

  call net % print_info()

  x = [0.2, 0.4, 0.6]
  y = [0.123456, 0.246802]

  do n = 0, num_iterations

    call net % forward(x)
    call net % backward(y)
    call net % update(1.)

    if (mod(n, 50) == 0) &
      print '(i4,2(3x,f8.6))', n, net % predict(x)

  end do

end program simple

I tried to compile simple.f90 but was stopped at line 2 because my system doesn’t have nf.mod and I don’t know how to get it (from a module nf.f90 somewhere?)

I can’t seem to reproduce this behavior. Here’s my compiler:

$ ifort --version
ifort (IFORT) 2021.9.0 20230302
Copyright (C) 1985-2023 Intel Corporation.  All rights reserved.

and my output of simple:

Simple
============================================================
Layer: input
------------------------------------------------------------
Output shape: 3
Parameters: 0
 
Layer: dense
------------------------------------------------------------
Input shape: 3
Output shape: 5
Parameters: 20
Activation: sigmoid
 
Layer: dense
------------------------------------------------------------
Input shape: 5
Output shape: 2
Parameters: 12
Activation: sigmoid
 
   0   0.483744   0.423037
  50   0.137223   0.247378
 100   0.125899   0.246760
 150   0.123943   0.246793
 200   0.123555   0.246800
 250   0.123476   0.246802
 300   0.123460   0.246802
 350   0.123457   0.246802
 400   0.123456   0.246802
 450   0.123456   0.246802
 500   0.123456   0.246802

It is unusual for a program that prints to stdout to not produce any output or error message. What happens if you compile and run this program:

print *, "It works"
end

@Harper, yes, the nf module is provided by compiling neural-fortran.

Hey everyone,

short update on the topic, so neural-fortran works really well for me. But now I am trying to use it on a supercalculator that has a newer version of HDF5 (hdf5/1.14.1-2/oneapi-2023.1.0-mpi-cxx-fortran). I don’t manage to compile neural fortran due to the error :

cnn_from_keras                         failed.
[ 11%] Compiling...
ld: cannot find -lhdf5hl_fortran

when I look at the libraries of HDF5, we have a libhdf5_hl_fortran.a, suggesting we would need to replace the -lhdf5hl_fortran by -lhdf5_hl_fortran in the compilation line of fpm. Yet I do not know how to change that in fpm. Would anyone have an idea?

Thank you very much in advance!

Have you tried editing as required line 10

link = ["hdf5", "hdf5_fortran"]

of fpm.toml?

Yes, I have tried using the lines :

link = ["hdf5", "hdf5_fortran"]

and

link = ["hdf5", "hdf5_fortran", "hdf5_hl_fortran"]

But both options give the same error.