Neural Network

A neural network is a set of connected units (neurons) that learn from data. A multilayer feed-forward network has an input layer, one or more hidden layers and an output layer, and each connection has a weight. Each unit computes the weighted sum of its inputs plus a bias, and passes it through an activation function, usually the sigmoid f(x) = 1 / (1 + e^(-x)), which gives a value between 0 and 1.

Back Propagation Algorithm

Back propagation is the learning method. It repeats these steps for every training tuple:

  1. Initialise the weights and biases with small random numbers.
  2. Forward pass: compute the output of each unit, layer by layer.
  3. Error at the output unit: δ = O(1 - O)(T - O), where O is the output and T the target.
  4. Error of a hidden unit: δj = Oj(1 - Oj) Σ δk wjk, passed back from the next layer.
  5. Update: each weight changes by Δw = l x δ x (input), where l is the learning rate, and each bias by Δb = l x δ.

It stops when the error is small enough or after a set number of passes (epochs).

Features

Networks learn complex, non-linear patterns, tolerate noisy data and classify unseen data well. Their drawbacks are long training time, the need to choose the network layout and parameters, and poor interpretability (they behave as a black box). Rule extraction and network pruning help.

Definition: Back propagation. A learning method for neural networks that passes the output error backward through the layers and adjusts the weights to reduce that error.

Table 3.4: Layers of a neural network

Layer Role Number of units
Input Receives attribute values One per attribute
Hidden Learns patterns Chosen by the designer
Output Gives the class One per class or one for two classes

Example 3.4: One weight update of a neuron

Problem: A neuron has inputs x1 = 1 and x2 = 0, weights 0.5 and -0.4, bias 0.1, a sigmoid output and target T = 1. With learning rate 0.9, find the output, the error, and the new weights and bias.

Solution:

  1. Net input = 0.5 x 1 + (-0.4) x 0 + 0.1 = 0.6, so O = 1 / (1 + e^-0.6) = 0.646.
  2. Error δ = 0.646 x (1 - 0.646) x (1 - 0.646) = 0.0811.
  3. New w1 = 0.5 + 0.9 x 0.0811 x 1 = 0.573; w2 stays -0.4 because x2 = 0.
  4. New bias = 0.1 + 0.9 x 0.0811 = 0.173.
  5. New net = 0.573 + 0.173 = 0.746, so the new output is 0.678.

Result: The output moves from 0.646 to 0.678, closer to the target 1.

from math import exp
def sig(v):
    return 1 / (1 + exp(-v))
x, w, b, T, lr = [1, 0], [0.5, -0.4], 0.1, 1, 0.9
for step in range(3):
    o = sig(sum(wi * xi for wi, xi in zip(w, x)) + b)
    d = o * (1 - o) * (T - o)
    print("step", step, "output", round(o, 3), "error", round(d, 4))
    w = [wi + lr * d * xi for wi, xi in zip(w, x)]
    b = b + lr * d
print("weights", [round(v, 3) for v in w], "bias", round(b, 3))

Output

step 0 output 0.646 error 0.0811
step 1 output 0.678 error 0.0702
step 2 output 0.705 error 0.0613
weights [0.691, -0.4] bias 0.291

Common mistakes

  • Using a learning rate that is too large, so the weights oscillate.
  • Starting with all weights equal to zero, so that units learn the same thing.
  • Training for too many epochs, which overfits.
  • Forgetting to normalise inputs to a small range.

Key takeaways

  • Network: input, hidden and output layers with weights and sigmoid units.
  • Output error δ = O(1 - O)(T - O).
  • Weight change Δw = learning rate x δ x input.
  • Back propagation passes the error backward to adjust weights.

Practice questions

  1. Which activation function is commonly used in back propagation?
  2. What does the learning rate control?
  3. Why are neural networks called black boxes?

Answers to the practice questions

  1. The sigmoid function.
  2. The size of each weight change.
  3. Their learned weights are hard to interpret.

Sources

  • Official BCA syllabus Data Warehousing and Mining, course code 25BCA406DS03, Unit III