Query the Network

Learn about the query() function, which is used to train the neural network, and its simple matrix form implementation.

The query() function

Before we start working on the code that trains the neural network by fleshing out the currently empty train() function, we’ll work on the simpler query() function. This will give us more time to build confidence and get some practice at using both Python and these weight matrices inside the neural network object.

The query() function takes the input to a neural network and returns the network’s output. That’s simple enough, but to do that, we need to pass the input signals from the input layer of nodes, through the hidden layer and out of the final output layer. We’ll also need to remember that we use the link weights to moderate the signals as they feed into any given hidden or output node. Moreover, we also use the sigmoid activation function to squish the signal coming out of those nodes.

If we had lots of nodes, we’d have the time-intensive task of writing out the Python code for each of those nodes, doing the weight moderating, summing signals, and applying the activation function. And the more nodes we have, the more code we need.

Simple implementation

Luckily, we don’t have to do that because we worked out how to write all these instructions in a simple, concise matrix form. The following equation shows how the matrix of weights for the link between the input and hidden layers can be combined with the matrix of inputs to give the signals into the hidden layer nodes:

Xhidden=Winput_hiddenI X_\text{hidden} = W_\text{input\_hidden} \cdot I

Not only is it was easier for us to write down, but programming languages like Python can also understand matrices and do all the real work quite efficiently because they recognize the similarities between all the underlying calculations.

The following code applies the NumPy library’s dot product function for matrices to the link weights Winput_hiddenW_\text{input\_hidden} and the inputs II:

Get hands-on with 1200+ tech skills courses.