Functions

Here is a list of classes in the package.

Overview:

class neural_network.AbstractFunction[source]

Class to represent an abstract function

__call__(x: float | List[float], w: List[float] | None = None) float[source]

Calling of the function

Parameters:
  • x (float | List[float]) – The input value

  • w (List[float] [Optional, Default = None]) – The weights

Returns:

The output value

Return type:

float

__init__()[source]

Constructor method

gradient(x: float | List[float], w: List[float] | None = None) float | List[float][source]

The gradient of the function

Parameters:
  • x (float | List[float]) – The input value

  • w (List[float]) – The weights

Returns:

The gradient of the function

Return type:

float | List[float]

class neural_network.CrossEntropyLoss[source]

Class to represent the cross entropy loss function for classification networks.

__call__(y_hat: List[float], y: int) float[source]

The loss function.

Parameters:
  • y_hat (List[float]) – Output vector from softmax layer

  • y (int) – Target class (in {0, 1, …})

Returns:

Cross entropy loss value

Return type:

float

__init__()[source]

Constructor method

class neural_network.MSELoss[source]

Class to represent the mean squared error loss for regressional neural networks.

__call__(y_hat: float, y: float) float[source]

The loss function.

Parameters:
  • y_hat (float) – Output value from neuron in output layer

  • y (float) – Ground truth value

Returns:

Squared difference of the two values

Return type:

float

__init__()[source]

Constructor method

gradient(y_hat: float, y: float) float[source]

The gradient of the loss function.

Parameters:
  • y_hat (float) – Output value from neuron in output layer

  • y (float) – Ground truth value

Returns:

Difference of the two values multiplied by 2

Return type:

float

class neural_network.ReLU(leak: float = 0.0)[source]

Class to represent the ReLU function

__call__(x: float, w: List[float] | None = None) float[source]

Implementation of ReLU

Parameters:
  • x (float) – Input to function

  • w (List[float]) – Weights (not used here)

Returns:

Output to function

Return type:

float

__init__(leak: float = 0.0)[source]

Constructor method

Parameters:

leak (float) – The parameter to be used if this is a LeakyReLU

gradient(x: float, w: List[float] | None = None) float[source]

Gradient of ReLU

Parameters:
  • x (float) – Input to function

  • w (List[float]) – Not used for this class

Returns:

Gradient of ReLU

Return type:

float

class neural_network.Sigmoid[source]

Class to represent the Sigmoid function

__call__(x: float, w: List[float] | None = None) float[source]

Implementation of Sigmoid

Parameters:
  • x (float) – Input to function

  • w (List[float]) – Weights (not used here)

Returns:

Output to function

Return type:

float

__init__()[source]

Constructor method

gradient(x: float, w: List[float] | None = None) float[source]

Gradient of Sigmoid

Parameters:
  • x (float) – Input to function

  • w (List[float]) – Not used for this class

Returns:

Gradient of Sigmoid

Return type:

float

class neural_network.Softmax[source]

Class to represent the softmax function

__call__(z_k: float) float[source]

The loss function. Note we multiply top and bottom by _max_z to avoid any overflow error.

Parameters:

z_k (float) – The value of an output node

Returns:

The softmax output

Return type:

float

__init__()[source]

Constructor method

normalise(z: List[float])[source]

Calculates the normalisation constant for the softmax function. We wish to avoid any overflow errors, so we multiply the normalisation constant by \(e^{-m}\). We account for this when finding the Softmax value later.

Parameters:

z (List[float]) – The vector of values from the output layer of the main network