rational.numpy

class rational.numpy.Rational(approx_func='leaky_relu', degrees=(5, 4), version='A')[source]

Rational activation function based on numpy

Parameters
  • approx_func (str) –

    The name of the approximated function for initialisation. The different initialable 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)

  • version (str) –

    Version of Rational to use. Rational(x) = P(x)/Q(x)

    A: Q(x) = 1 + |b_1.x| + |b_2.x| + … + |b_n.x|

    B: Q(x) = 1 + |b_1.x + b_2.x + … + b_n.x|

    C: Q(x) = 0.1 + |b_1.x + b_2.x + … + b_n.x|

    D: like B with noise

    Default A

Returns

Rational module

Return type

Module

fit(function, x_range=array([- 3.0, - 2.9, - 2.8, - 2.7, - 2.6, - 2.5, - 2.4, - 2.3, - 2.2, - 2.1, - 2.0, - 1.9, - 1.8, - 1.7, - 1.6, - 1.5, - 1.4, - 1.3, - 1.2, - 1.1, - 1.0, - 0.9, - 0.8, - 0.7, - 0.6, - 0.5, - 0.4, - 0.3, - 0.2, - 0.1, 2.66453526e-15, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 1.7, 1.8, 1.9, 2.0, 2.1, 2.2, 2.3, 2.4, 2.5, 2.6, 2.7, 2.8, 2.9]))[source]

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 True

  • 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

show(input_range=None, display=True, distribution=None)[source]

Show the function using matplotlib.

Parameters
  • input_range (range) –

    The range to print the function on.

    Default None

  • display (bool) –

    If True, displays the graph. Otherwise, returns it.

    Default True

torch(cuda=None, trainable=True, train_numerator=True, train_denominator=True)[source]

Returns a torch version of this activation function.

Parameters
  • cuda (bool) –

    Use GPU CUDA version. If None, use cuda if available on the machine

    Default None

  • trainable (bool) –

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

    Default True

Returns

Rational torch function

Return type

function