rational.keras

class rational.keras.Rational(*args, **kwargs)[source]

Rational Activation Functions, inheriting from tensorflow.keras.layers.Layer.

Parameters
  • approx_func (str) –

    The name of the approximated function for initialisation. The different functions are available in rational.rationals_config.json.

    Default: leaky_relu

  • degrees (tuple of int) –

    The degrees of the numerator (P) and denominator (Q).

    Default: (5, 4)

  • cuda (bool) – whether to execute on cuda device. NOTE: THIS PARAMETER IS CURRENTLY NOT CONSIDERED. CUDA GPUS ARE USED WHEN IT IS POSSIBLE

  • version (str) –

    Version of Rational to use. Rational(x) = P(x)/Q(x), where P(x) = (a_0 + a_1 * x + a_2 * x^2 + … + a_n * x^n) and

    A: Q(x) = (1 + |b_0 * x| + | b_1 * x^2| + … + | b_m * x^{m+1}|)

    B: Q(x) = (1 + |b_0 * x + b_1 * x^2 + … + b_m * x^{m + 1}|)

    C: Q(x) = (0.1 + |b_0 + b_1 * x + b_2 * x^2 + … + b_m * x^m|)

    D: like B with noised coefficients b_i

    Default: A

  • trainable (bool) –

    Whether the weights are trainable, i.e, if they are updated during backward pass.

    Default: True

Returns

Rational layer

Return type

Layer

best_fit(functions_list, x=None, show=False)

Compute the distance between the rational and the functions in functions_list, and return the one with the minimal the distance.

Parameters
  • functions_list (list of callable) – The function you want to fit to rational.

  • x (array) –

    The range on which the curves of the functions are fitted together.

    Default None

  • show (bool) –

    If True, plots the final fitted function and rational (using matplotlib).

    Default False

Returns

((a, b, c, d), dist) with:

a, b, c, d: the parameters to adjust the function (vertical and horizontal scales and bias)

dist: The final distance between the rational function and the fitted one

Return type

tuple

build(input_shape)[source]

Inherited from tensorflow.keras.layers.Layer

This method can be used to create weights that depend on the shape(s) of the input(s), using add_weight(). __call__() will automatically build the layer (if it has not been built yet) by calling build().

Parameters

input_shape (TensorShape) – one or a list of instances of TensorShape if the layer expects a list of inputs (one instance per input).

call(inputs, **kwargs)[source]

Inherited from tensorflow.keras.layers.Layer

Called in __call__ after making sure build() has been called. call() performs the logic of applying the layer to the input tensors (which should be passed in as argument). Two reserved keyword arguments you can optionally use in call() are:

  • training (boolean, whether the call is in inference mode or training mode)

  • mask (boolean tensor encoding masked timesteps in the input, used in RNN layers)

Parameters

inputs – Input tensorflow tensor

Returns

output tensor, with the Rational Activation Function applied to it

capture(name='snapshot_0', x=None, fitted_function=True, other_func=None, returns=False)

Captures a snapshot of the rational functions and related in the snapshot_list variable (or returns it if returns=True).

Parameters
  • name (str) –

    Name of the snapshot.

    Default "snapshot_0"

  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_funcs (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

  • returns (bool) –

    If True, returns the snapshot. Otherwise, saves it in self.snapshot_list

    Default False

classmethod capture_all(name='snapshot_0', x=None, fitted_function=True, other_func=None, returns=False)

Captures a snapshot of every instanciated rational functions and related in the snapshot_list variable (or returns a list of them if returns=True).

Parameters
  • name (str) –

    Name of the snapshot.

    Default "snapshot_0"

  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_funcs (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

  • returns (bool) –

    If True, returns the snapshot. Otherwise, saves it in self.snapshot_list

    Default False

export_evolution_graph(path='rational_evolution.gif', animated=True, other_func=None)

Creates and saves an animated graph of the function evolution based on the successive snapshots saved in snapshot_list.

Parameters
  • path (str) –

    Complete path with name of the figure.

    Default "rational_evolution.gif"

  • animated (bool) –

    Complete path with name of the figure.

    Default True

  • other_func (callable) –

    another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

    Default None

classmethod export_evolution_graphs(path='rationals_evolution.gif', together=True, layout='auto', animated=True, other_func=None)

Creates and saves an animated graph of the function evolution based on the successive snapshots saved in snapshot_list for each instanciated rational function.

Parameters
  • path (str) –

    Complete path with name of the figure.

    Default "rationals_evolution.gif"

  • together (bool) –

    If True, the graphs of every functions are stored in different files.

    Default True

  • layout (tuple or 'auto') –

    Grid layout of the figure. If “auto”, one is generated. (see layout).

    Default "auto"

  • animated (bool) –

    If True, creates an animated gif, else, different files are created.

    Default True

  • other_func (callable) –

    another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

    Default None

export_graph(path='rational_function.svg', snap_number=- 1, other_func=None)

Saves one graph of the function based on the last snapshot (by default, and if available).

Parameters
  • path (str) –

    Complete path with name of the figure.

    Default "rational_functions.svg"

  • together (bool) –

    If True, the graphs of every functions are stored in different files.

    Default True

  • layout (tuple or 'auto') – Grid layout of the figure. If “auto”, one is generated. (see layout). Default auto

  • snap_number (int) –

    The snap to take in snapshot_list for each function.

    Default -1 (last)

  • other_func (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value. Default None

classmethod export_graphs(path='rational_functions.svg', together=True, layout='auto', snap_number=- 1, other_func=None)

Saves one or more graph(s) of the function based on the last snapshot (by default, and if available) for each instanciated rational function.

Parameters
  • path (str) –

    Complete path with name of the figure.

    Default "rational_functions.svg"

  • together (bool) –

    If True, the graphs of every functions are stored in different files.

    Default True

  • layout (tuple or 'auto') – Grid layout of the figure. If “auto”, one is generated. (see layout). Default "auto"

  • snap_number (int) –

    The snap to take in snapshot_list for each function.

    Default -1 (last)

  • other_func (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value. Default None

fit(function, x=None, show=False)

Compute the parameters a, b, c, and d to have the neurally equivalent function of the provided one as close as possible to this rational function.

Parameters
  • function (callable) – The function you want to fit to rational.

  • x (array) –

    The range on which the curves of the functions are fitted together.

    Default None

  • show (bool) –

    If True, plots the final fitted function and rational (using matplotlib).

    Default False

Returns

((a, b, c, d), dist) with:

a, b, c, d: the parameters to adjust the function (vertical and horizontal scales and bias)

dist: The final distance between the rational function and the fitted one

Return type

tuple

numpy()[source]

Returns a numpy version of this activation function.

show(x=None, fitted_function=True, other_func=None, display=True, tolerance=0.001, title=None, axis=None)

Shows a graph of the function (or returns it if returns=True).

Parameters
  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_funcs (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

  • display (bool) –

    If True, displays the plot. Otherwise, returns the figure.

    Default False

  • tolerance (float) –

    If the input histogram is used, it will be pruned.

    Every bin containg less than tolerance of the total input is pruned out. (Reduces noise). Default 0.001

  • title (str) – If not None, a title for the figure Default None

  • axis (matplotlib.pyplot.axis) – axis to be plotted on. If None, creates one automatically. Default None

classmethod show_all(x=None, fitted_function=True, other_func=None, display=True, tolerance=0.001, title=None, axes=None, layout='auto')

Shows a graph of the all instanciated rational functions (or returns it if returns=True).

Parameters
  • x (range) –

    The range to print the function on.

    Default None

  • fitted_function (bool) –

    If True, displays the best fitted function if searched. Otherwise, returns it.

    Default True

  • other_funcs (callable) – another function to be plotted or a list of other callable functions or a dictionary with the function name as key and the callable as value.

  • display (bool) –

    If True, displays the plot. Otherwise, returns the figure.

    Default False

  • tolerance (float) –

    If the input histogram is used, it will be pruned.

    Every bin containg less than tolerance of the total input is pruned out. (Reduces noise). Default 0.001

  • title (str) – If not None, a title for the figure Default None

  • axes (matplotlib.pyplot.axis) –

    On ax or a list of axes to be plotted on.

    If None, creates them automatically (see layout).

    Default None

  • layout (tuple or 'auto') –

    Grid layout of the figure. If “auto”, one is generated.

    Default "auto"