Everything boils down to matrix multiplication.
class Neuron:
def __init__(self, num_inputs):
# Initialize weights randomly, bias to 0
self.weights = np.random.randn(num_inputs)
self.bias = 0
def forward(self, inputs):
# Dot product of inputs and weights, plus bias
z = np.dot(inputs, self.weights) + self.bias
return sigmoid(z)Squishes values between 0 and 1. Great for probabilities.
If positive, keep it. If negative, zero it. Solves vanishing gradients.
import numpy as np
def sigmoid(x):
# Smooth S-curve (0 to 1)
return 1 / (1 + np.exp(-x))
def relu(x):
# The most popular activation function
# Returns x if x > 0, else 0
return np.maximum(0, x)We update weights in the opposite direction of the gradient to minimize loss.
w_new = w_old - (learning_rate * gradient)
# The Training Step
learning_rate = 0.01
for epoch in range(1000):
# 1. Forward Pass
predictions = network.forward(X)
# 2. Calculate Loss
loss = mse_loss(predictions, y)
# 3. Backward Pass (Calculate gradients)
gradients = network.backward(loss)
# 4. Update Weights
network.weights -= learning_rate * gradients