15. Running a Neural Network with Python
By Bernd Klein. Last modified: 19 Apr 2024.
A Neural Network Class
We learned in the previous chapter of our tutorial on neural networks the most important facts about weights. We saw how they are used and how we can implement them in Python. We saw that the multiplication of the weights with the input values can be accomplished with arrays from Numpy by applying matrix multiplication.
However, what we hadn't done was to test them in a real neural network environment. We have to create this environment first. We will now create a class in Python, implementing a neural network. We will proceed in small steps so that everything is easy to understand.
The most essential methods our class needs are:
__init__to initialize a class, i.e. we will set the number of neurons for every layer and initialize the weight matrices.run: A method which is applied to a sample, which which we want to classify. It applies this sample to the neural network. We could say, we 'run' the network to 'predict' the result. This method is in other implementations often known aspredict.train: This method gets a sample and the corresponding target value as an input. With this input it can adjust the weight values if necessary. This means the network learns from an input. Seen from the user point of view, we 'train' the network. Insklearnfor example, this method is calledfit
We will postpone the definition of the train and run method until later. The weight matrices should be initialized inside of the __init__ method. We do this indirectly. We define a method create_weight_matrices and call it in __init__. In this way, the init method remains clear.
We will also postpone adding bias nodes to the layers.
The following Python code contains an implementation of a neural network class applying the knowledge we worked out in the previous chapter:
import numpy as np
from scipy.stats import truncnorm
def truncated_normal(mean=0, sd=1, low=0, upp=10):
return truncnorm(
(low - mean) / sd, (upp - mean) / sd, loc=mean,