Components

Here is a list of classes in the package.

Overview:

class neural_network.Edge(left_neuron: Neuron, right_neuron: Neuron)[source]

Class to represent an Edge joining two Nodes of a network

__init__(left_neuron: Neuron, right_neuron: Neuron)[source]

Constructor method

Parameters:
  • left_neuron (Neuron) – The left Neuron of the edge

  • right_neuron (Neuron) – The right Neuron of the edge

get_delta() float[source]

Getter method

Returns:

The delta

Return type:

float

get_id() Tuple[int, ...][source]

Getter method

Returns:

The id

Return type:

Tuple[int, …]

get_left_neuron() Neuron[source]

Getter method

Returns:

The left Neuron

Return type:

Neuron

get_right_neuron() Neuron[source]

Getter method

Returns:

The right Neuron

Return type:

float

get_velocity() float[source]

Getter method

Returns:

The velocity

Return type:

float

get_weight() float[source]

Getter method

Returns:

The weight

Return type:

float

set_delta(delta: float)[source]

Setter method

Parameters:

delta (float) – The new delta

set_velocity(velocity: float)[source]

Setter method

Parameters:

velocity (float) – The new velocity

set_weight(weight: float)[source]

Setter method

Parameters:

weight (float) – The new weight

class neural_network.Layer(_id: int, num_neurons: int)[source]

Class to represent one Layer of a network

__init__(_id: int, num_neurons: int)[source]

Constructor method

Parameters:
  • _id (int) – The id of the Layer

  • num_neurons (int) – The number of Neurons in the Layer

get_id() int[source]

Getter method

Returns:

The id

Return type:

int

get_neurons() List[Neuron][source]

Getter method

Returns:

The Neurons in the Layer

Return type:

List[Neuron]

class neural_network.Network(num_features: int, num_hidden_layers: int, neuron_counts: List[int], num_classes: int = 2, regression: bool = False, leak: float = 0.01, learning_rate: float = 0.01, adaptive: bool = False, gamma: float = 0.9, he_weights: bool = False)[source]

Class to represent the whole Network

__init__(num_features: int, num_hidden_layers: int, neuron_counts: List[int], num_classes: int = 2, regression: bool = False, leak: float = 0.01, learning_rate: float = 0.01, adaptive: bool = False, gamma: float = 0.9, he_weights: bool = False)[source]

Constructor method

Parameters:
  • num_features (int) – The number of features for the network

  • num_hidden_layers (int) – The total number of hidden Layers in the Network

  • neuron_counts (List[int]) – A list of numbers of Neurons for each hidden Layer

  • num_classes (int) – The number of classes for the classification task

  • regression (bool) – Whether we are performing regression or not (if False we are performing classification)

  • leak (float) – The leak rate of LeakyReLU

  • learning_rate (float) – The learning rate of the network

  • adaptive (bool) – Whether we wish to have an adaptive learning rate or not

  • gamma (float) – The adaptive learning rate parameter

  • he_weights (bool) – Whether we wish to initialise the weights according to He or not

_activate_output_layer(z_list: List[float]) List[float][source]

Activates the values from the output_layer using the Softmax activation function.

Returns:

The List of softmax probabilities

Return type:

List[float]

_calculate_pre_activated_value(left_layer: Layer, right_neuron: Neuron) float[source]

Given a left_layer and a right_neuron, this calculates the activation function and value from the left_layer and propagates this value to the right_neuron

Parameters:
  • left_layer (Layer) – The current left_layer in forward propagation

  • right_neuron (Neuron) – The current right_neuron in forward propagation

back_propagate_bias(neuron: Neuron)[source]

Uses the bias gradients of all datapoints (for this specific neuron) to perform gradient descent and calculate a new bias for this neuron.

neuronNeuron

The Neuron whose bias we are interested in updating

back_propagate_weight(edge: Edge)[source]

Uses the loss gradients of all datapoints (for this specific edge) to perform gradient descent and calculate a new weight for this edge.

edgeEdge

The Edge whose weight we are interested in updating

forward_pass_one_datapoint(x: array) List[float][source]

Performs a forward pass for one datapoint, excluding the ground truth value. This method returns the predicted value in the final neuron in the output layer.

Parameters:

x (np.array) – The input value, with all features

Returns:

The softmax probabilities of each class (for classification) or the predicted regression value (for regression)

Return type:

List[float]

get_edges() List[List[List[Edge]]][source]

Getter method for edges

Returns:

A list of Edges

Return type:

List[List[List[Edge]]]

get_layers() List[Layer][source]

Getter method for layers

Returns:

A list of Layers

Return type:

List[Layer]

get_neuron_counts() List[int][source]

Getter method for neuron_counts.

Returns:

A list of numbers of neurons per layer

Return type:

List[int]

is_regressor() bool[source]

Getter method for regression.

Returns:

Whether we do regression or classification

Return type:

bool

store_gradient_of_loss(edge: Edge, target: float, first: bool)[source]

Calculates the gradient of the loss function with respect to one weight (assigned to the edge) based on the values at edges of future layers. One part of the back propagation process.

edgeEdge

The Edge containing the weight we are interested in

targetint

The target value for the final output node for this specific datapoint

firstbool

Determines whether we find the bias gradient or not

visualise_network(title: str = '')[source]

Uses the networkx package to visualise the network.

Parameters:

title (str [Optional, Default='']) – An optional title for the network.

class neural_network.Neuron(layer_id: int, row_id: int)[source]

Class to represent a single Neuron in a neural network

__init__(layer_id: int, row_id: int)[source]

Constructor method

Parameters:
  • layer_id (int) – The layer of the network

  • row_id (int) – The row in the layer

get_bias() float[source]

Getter method

Returns:

The bias

Return type:

float

get_id() Tuple[int, ...][source]

Getter method

Returns:

The id

Return type:

Tuple[int, …]

get_value() float[source]

Getter method

Returns:

The value

Return type:

float

set_bias(bias: float)[source]

Setter method

Parameters:

bias (float) – The new bias

set_value(value: float)[source]

Setter method

Parameters:

value (float) – The new value