tensornn.nn

This file contains the neural network class.

Classes

NeuralNetwork

Create your neural network with this class.

class tensornn.nn.NeuralNetwork(layers: Iterable[Layer] = ())

Bases: TensorNNObject

Create your neural network with this class.

add(layers: Layer | Iterable[Layer]) None

Add another layer(s) to the network. This is the same as initializing the network with this layer included.

Parameters:

layer – the layer to be added

backward(loss_deriv: Tensor) None

Find out how much each weight/bias contributes to the loss and gets stored in each layer. Not meant for external use.

Parameters:

loss_deriv – the derivative of the loss wrt the output of the last layer

Returns:

nothing

forward(inputs: Tensor) Tensor

Propagate the inputs through the network and return the last layer’s output.

Parameters:

inputs – inputs to the network

Returns:

the output of the last layer in the network

get_loss(inputs: Tensor, desired_outputs: Tensor) Tensor

Calculate the loss for the given data.

Parameters:
  • inputs – input to the network

  • desired_outputs – desired output of the network for the given inputs

Returns:

the loss of the network for the given parameters

get_minibatches(inputs: Tensor, desired_outputs: Tensor, batch_size: int, shuffle: bool = False) List[Tuple[Tensor, Tensor]]

Get the minibatches of the inputs and desired outputs. This is used to split the inputs and desired outputs into smaller batches for training.

Parameters:
  • inputs – the inputs to the network

  • desired_outputs – the desired outputs of the network

  • batch_size – the size of each batch

  • shuffle – whether to shuffle the inputs and desired outputs before splitting into batches

Returns:

a list of tuples containing the inputs and desired outputs for each batch

classmethod load(path: str) NeuralNetwork

Load a network from a file using the pickle module.

Parameters:

path – path to load the network from

Returns:

the loaded network

predict(inputs: Tensor) int

Get the prediction of the neural network. This will return the index of the most highly activated neuron. This method should only be used on a trained network, because otherwise it will produce useless random values.

Parameters:

inputs – inputs to the network

Returns:

an array which contains the value of each neuron in the last layer

register(loss: Loss, optimizer: Optimizer) None

Register the neural network. This method initializes the network with loss and optimizer and also finishes up any last touches to its layers.

Parameters:
  • loss – type of loss this network uses to calculate loss

  • optimizer – type of optimizer this network uses

Raises:

InitializationError – num_inputs not specified to first layer

reset_gradients() None

Reset the gradients of all the layers in the network.

save(path: str) None

Save the network to a file using the pickle module.

Parameters:

path – path to save the network to

classmethod simple(sizes: Sequence[int], learning_rate: float = 0.001)

Create a NeuralNetwork from the number of neurons per layer. First layer will be considered the input layer. All layers will be the Dense layer with the ReLU activation. The last layer will be Dense with the Softmax activation. The network will also be registered with MSE loss and the SGD optimizer.

Parameters:
  • sizes – list of numbers of neurons per layer

  • learning_rate – the learning rate of the network

train(inputs: Tensor, desired_outputs: Tensor, epochs: int = 5, batch_size: int = 32, learning_rate: float | None = None, **kwargs) None

Train the neural network. What training essentially does is adjust the weights and biases of the neural network for the inputs to match the desired outputs as close as possible.

Parameters:
  • inputs – training data which is inputted to the network

  • desired_outputs – these values is what you want the network to output for respective inputs

  • epochs – how many iterations will your network will run to learn

  • verbose – the level of verbosity of the program (1-3), defaults to 1

Raises:
update_log(epoch) None

Update the log file with the current weights, biases, and gradients of the network.