FNN 1.0.0
Toolbox to use NNs in Fortran.
Loading...
Searching...
No Matches
Classes | Functions | Variables
keras_to_fnn Namespace Reference

Interface module between keras and FNN. More...

Classes

class  UnsupportedLayerException
 Exception class for unsupported layers. More...
 
class  UnsupportedModelException
 Exception class for unsupported models. More...
 

Functions

def keras_to_txt (filename_out, model, **kwargs)
 Transforms a keras model into a txt model file which can be read by FNN. More...
 
def keras_file_to_txt (filename_out, filename_in, **kwargs)
 Transforms a keras model file into a txt model file which can be read by FNN. More...
 

Variables

string STR_FORMAT = '{}'.format
 Format function for strings. More...
 
string INT_FORMAT = '{:>7d}'.format
 Format function for integers. More...
 
string FLOAT_FORMAT = '{:0.7e}'.format
 Format function for real numbers. More...
 

Detailed Description

Interface module between keras and FNN.

This package provides the keras_file_to_txt and keras_to_txt functions to convert a keras model into a txt file which can be read by FNN.

Programmatic usage

Use the keras_file_to_txt function as follows to convert a .h5 file into a .txt file:

>>> keras_file_to_txt('model_out.txt', 'model_in.h5')

Use the keras_to_txt function as follows to convert a keras model into a .txt file:

>>> keras_to_txt('model_out.txt', keras_model)

Command-line usage

Use the following command to convert a .h5 file into a .txt file:

>>> python keras_to_fnn.py model_out.txt model_in.h5

Function Documentation

◆ keras_file_to_txt()

def keras_to_fnn.keras_file_to_txt (   filename_out,
  filename_in,
**  kwargs 
)

Transforms a keras model file into a txt model file which can be read by FNN.

This function is actually a wrapper around keras_to_txt. It accepts the same kwargs, with the addition of custom_objects.

Note

This function uses tf.keras.models.load_model to read filename_in.

Accepted kwargs

  • [in] custom_objects : list of str
    • List of custom objects to ignore while reading keras file. All other kwargs are forwarded to keras_to_txt.
      Parameters
      [in]filename_outThe name of the txt file to write.
      [in]filename_inThe keras model file.
      [in]kwargsKey-word arguments.
      Accepted kwargs
  • [in] add_norm_in : bool
    • Whether to add a normalisation layer for the input.
  • [in] norm_alpha_in : np.ndarray
    • Value of alpha (1d array) for the input normalisation layer, if any.
  • [in] norm_beta_in : np.ndarray
    • Value of beta (1d array) for the input normalisation layer, if any.
  • [in] add_norm_out : bool
    • Whether to add a normalisation layer for the output.
  • [in] norm_alpha_out : np.ndarray
    • Value of alpha (1d array) for the output normalisation layer, if any.

◆ keras_to_txt()

def keras_to_fnn.keras_to_txt (   filename_out,
  model,
**  kwargs 
)

Transforms a keras model into a txt model file which can be read by FNN.

Accepted kwargs

  • [in] add_norm_in : bool
    • Whether to add a normalisation layer for the input.
  • [in] norm_alpha_in : np.ndarray
    • Value of alpha (1d array) for the input normalisation layer, if any.
  • [in] norm_beta_in : np.ndarray
    • Value of beta (1d array) for the input normalisation layer, if any.
  • [in] add_norm_out : bool
    • Whether to add a normalisation layer for the output.
  • [in] norm_alpha_out : np.ndarray
    • Value of alpha (1d array) for the output normalisation layer, if any.
  • [in] norm_beta_out : np.ndarray
    • Value of beta (1d array) for the output normalisation layer, if any.
  • [in] dropout_rates : None or float or list of float
    • Whether to add dropout after each internal layer, with the given rate.
  • [in] ignore_dropout : bool
    • Whether to ignore the keras Dropout layers.
      Parameters
      [in]filename_outThe name of the txt file to write.
      [in]modelThe keras model.
      [in]kwargsKey-word arguments.

Variable Documentation

◆ FLOAT_FORMAT

string keras_to_fnn.FLOAT_FORMAT = '{:0.7e}'.format

Format function for real numbers.

◆ INT_FORMAT

string keras_to_fnn.INT_FORMAT = '{:>7d}'.format

Format function for integers.

◆ STR_FORMAT

string keras_to_fnn.STR_FORMAT = '{}'.format

Format function for strings.