We’ll understand how neural networks work while implementing one from scratch in Python.
1. Building Blocks: Neurons
Neuron Concept: A neuron is like a tiny decision-making unit in a neural network. It takes inputs, performs some calculations, and produces an output. The key components of a neuron are:
Inputs: These are values coming into the neuron.
Weights: Each input is multiplied by a weight to give it importance.
Bias: An additional value that's added to the sum of weighted inputs.
Activation Function: This function processes the weighted sum and produces the neuron's output.
Math Behind a Neuron: Let's break down what happens inside a neuron:
Each input is multiplied by its corresponding weight.
These weighted inputs are summed up along with the bias.
The sum goes through an activation function to produce the output.
A commonly used activation function is the sigmoid function, which maps the output to a value between 0 and 1.
The activation function is used to turn an unbounded input into an output that has a nice, predictable form. A commonly used activation function is the sigmoid function:
2. Coding a Neuron
Now, let's implement a neuron in Python using NumPy:
import numpy as np
def sigmoid(x):
# Sigmoid activation function: f(x) = 1 / (1 + e^(-x))
return 1 / (1 + np.exp(-x))
class Neuron:
def __init__(self, weights, bias):
self.weights = weights
self.bias = bias
def feedforward(self, inputs):
# Weight inputs, add bias, then use the activation function
total = np.dot(self.weights, inputs) + self.bias
return sigmoid(total)
# Example usage:
weights = np.array([0, 1]) # w1 = 0, w2 = 1
bias = 4 # b = 4
n = Neuron(weights, bias)
x = np.array([2, 3]) # x1 = 2, x2 = 3
output = n.feedforward(x) # The neuron's output for input [2, 3]
print(output) # Output: 0.9990889488055994
In this code, we create a Neuron class that takes weights and a bias as inputs and has a feedforward method that calculates the neuron's output.
3. Combining Neurons into a Neural Network
Now that we understand how a single neuron works, let's build a simple neural network by combining multiple neurons together. A neural network is just a collection of interconnected neurons. Each neuron in a layer takes inputs from the previous layer and produces outputs for the next layer.
Coding a Neural Network: Feedforward
Let’s implement feedforward for our neural network. Here’s the image of the network again for reference:
4. Training a Neural Network
To train a neural network, we need to adjust its weights and biases to make it perform better on a given task. This is done through a process called optimization. One common optimization algorithm is stochastic gradient descent (SGD).
Loss Function: Before training, we need a way to measure how well the network is doing. This is done using a loss function. One commonly used loss function is mean squared error (MSE), which quantifies the difference between predicted and actual values.
Gradient Descent: Gradient descent is the process of iteratively adjusting weights and biases to minimize the loss. It involves computing the gradients (partial derivatives) of the loss with respect to each weight and bias and then updating them in the direction that reduces the loss.
This is a high-level overview of building and training neural networks. As students become more comfortable with these concepts, you can introduce more complex network architectures and optimization techniques.