Neural network
In this part, we will study the principles of neural networks and write a neural network of 2 layers.
Neural networks are made up of interconnected neurons.
I wrote more about neurons in the previous part.
Neural networks are divided into layers:
Input layer
Hidden layers
Output layer
Hidden layers are layers between input and output, the number of hidden layers can be any.
We will write a neural network of 2 layers, input and output layers.
First, let's look at the principles of neural networks.
As I said, neural networks are divided into layers. Each layer contains a number of neurons. The outputs of all neurons in a layer are sent to the inputs of all neurons in the next layer.
A neural network diagram of 3 layers with 2 neurons at the input, 3 hidden, 1 output will look like this
This connection between layers is called feedforward.
As a result, we got 3 layers and 6 neurons.
For large projects this is not much, but since we are just learning, we will write a neural network of 2 layers with 2 input neurons and 1 output.
Scheme for our neural network
Let's create file NeuronNet.py
Let's connect the neuron class that we wrote in the last part:
from Neuron import *
Let's describe the NeuronNet class and the constructor for it in the file:
class NeuronNet:
def __init__(self):
self.n = []
for i in range(3):
self.n.append(Neuron(2))
An array of objects of the Neuron class of 3 neurons in size is created in the class constructor. We pass the number 2 to the neuron in the parameters, since there will be 2 inputs for all neurons.
, 3 1 2 , 1 2 :
def activate(self, inputs):
return self.n[2].activate(np.array([self.n[0].activate(inputs), self.n[1].activate(inputs)]))
NeuronNet. NeuronNet.py.
NeuronNet.py:
from Neuron import *
class NeuronNet:
def __init__(self):
self.n = []
for i in range(3):
self.n.append(Neuron(2))
def activate(self, inputs):
return self.n[2].activate(np.array([self.n[0].activate(inputs), self.n[1].activate(inputs)]))
main.py, .
main.py numpy NeuronNet.py:
import numpy as np
from NeuronNet import *
:
net = NeuronNet()
:
x = np.array([1, 2])
print(net.activate(x))
:
import numpy as np
from NeuronNet import *
net = NeuronNet()
x = np.array([1, 2])
print(net.activate(x))
. :
python main.py
. .
Let's summarize.
Today we:
Learned the principles of neural networks
Learned the principles of neuron communication in neural networks
We wrote the NeuronNet class in the python programming language
We launched our first neural network
In the next part, we will implement training our neural network.
If you have any questions after reading the article, ask them in the comments.