← back

Single Neuron with Backpropagation

#25 · Deep Learning · Medium

⊣ Solve on deep-ml.com

Problem

Implement a single neuron with backpropagation. Given inputs, weights, bias, and true labels, perform a forward pass and then update weights and bias using gradient descent.

Solution

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import math

def single_neuron_backprop(features: list[list[float]], labels: list[int],
                            weights: list[float], bias: float,
                            learning_rate: float, epochs: int) -> tuple[list[float], float, list[float]]:
    n = len(features)
    num_features = len(weights)
    w = list(weights)
    b = bias

    for _ in range(epochs):
        # Forward pass
        outputs = []
        for x in features:
            z = sum(x[j] * w[j] for j in range(num_features)) + b
            a = 1 / (1 + math.exp(-z))
            outputs.append(a)

        # Compute gradients
        dw = [0.0] * num_features
        db = 0.0
        for i in range(n):
            error = outputs[i] - labels[i]
            deriv = outputs[i] * (1 - outputs[i])  # sigmoid derivative
            delta = error * deriv
            for j in range(num_features):
                dw[j] += delta * features[i][j]
            db += delta

        # Average gradients
        dw = [d / n for d in dw]
        db /= n

        # Update weights and bias
        for j in range(num_features):
            w[j] -= learning_rate * dw[j]
        b -= learning_rate * db

    # Final MSE
    mse_loss = sum((outputs[i] - labels[i]) ** 2 for i in range(n)) / n
    return [round(wi, 4) for wi in w], round(b, 4), round(mse_loss, 4)

Explanation

  1. Forward pass: Compute z = w*x + b, then apply sigmoid to get the output.
  2. Loss: Mean squared error between predictions and labels.
  3. Backward pass: Compute gradients of MSE w.r.t. weights and bias using the chain rule through the sigmoid.
  4. Update: Adjust weights and bias in the direction that reduces the loss.
  5. Repeat for the specified number of epochs.

Complexity

  • Time: O(epochs n f) where n is samples and f is features
  • Space: O(f) for weights and gradients