ENع
→Neurons
الدرس 12 · التعلّم العميق

Neurons and activations

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة10 خطوات5 أسئلةIntermediate
عن هذا الدرس

A neural network is built from one small part repeated many times: the neuron. It multiplies each input by a weight, adds a bias, and bends the result with an activation function such as ReLU. Follow numbers through a two-neuron network that computes XOR, then remove the activation and watch it fail.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 12 · تفاعلي
01 / 10

Numbers in: the XOR task

A neural network works only with numbers. Ours gets two inputs, x1 and x2, each 0 or 1, and must output XOR (exclusive or): 1 when exactly one input is 1, else 0. First input: x = (1, 0).

XOR ("exclusive or")
  x1 x2   XOR
   0  0    0
   0  1    1
   1  0    1
   1  1    0
first input: x1 = 1, x2 = 0
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Numbers in: the XOR task

    A neural network works only with numbers. Ours gets two inputs, x1 and x2, each 0 or 1, and must output XOR (exclusive or): 1 when exactly one input is 1, else 0. First input: x = (1, 0).

    XOR ("exclusive or")
      x1 x2   XOR
       0  0    0
       0  1    1
       1  0    1
       1  1    0
    first input: x1 = 1, x2 = 0
  2. A neuron: a weighted sum

    A neuron multiplies each input by a weight, adds the results and adds a bias b: z = w1·x1 + w2·x2 + b. Neuron h1 has weights 1 and 1 and bias 0, so z = 1·1 + 1·0 + 0 = 1.

    z = w1*x1 + w2*x2 + b
    h1: w1 = 1, w2 = 1, b = 0
        z = 1*1 + 1*0 + 0 = 1
    a weight says how much an input counts (and its sign);
    the bias shifts the sum up or down
  3. Activation: ReLU bends it

    Then an activation function bends the sum. The usual one is ReLU, the rectified linear unit: a = max(0, z). It passes positive numbers through and turns negative ones into 0. Here z = 1, so h1 outputs a = 1.

    a = max(0, z)          ReLU
      z = -3  ->  a = 0
      z =  0  ->  a = 0
      z =  2  ->  a = 2
    h1: z = 1  ->  a = 1
  4. A bias, and a layer

    Neuron h2 reads the same inputs with the same weights, but its bias is −1: z = 1 + 0 − 1 = 0, and ReLU gives 0. Neurons side by side, reading the same inputs, form a layer: here h = (1, 0).

    h2: w1 = 1, w2 = 1, b = -1
        z = 1*1 + 1*0 - 1 = 0
        a = max(0, 0) = 0
    hidden layer: h1 and h2 read the same x
        h = (1, 0)
  5. The output neuron

    The output neuron weighs the layer's numbers: y = 1·h1 − 2·h2 + 0 = 1 − 0 = 1. It has no ReLU, so it may pass on any number. Exactly one input was 1, so XOR says 1: correct.

    y = 1*h1 - 2*h2 + 0      (no activation)
      = 1*1 - 2*0 + 0 = 1
    XOR(1, 0) = 1   correct
  6. Both inputs on

    Now x = (1, 1). h1: z = 1 + 1 + 0 = 2. h2: z = 1 + 1 − 1 = 1. ReLU passes both unchanged, so y = 2 − 2·1 = 0. XOR of 1 and 1 is 0: correct.

    x = (1, 1)
    h1: z = 1 + 1 + 0 = 2    a = 2
    h2: z = 1 + 1 - 1 = 1    a = 1
    y  = 1*2 - 2*1 = 0
    XOR(1, 1) = 0   correct
  7. ReLU clips a negative

    Now x = (0, 0). h2 gets z = 0 + 0 − 1 = −1, and ReLU clips it to 0, so h = (0, 0) and y = 0: correct. (0, 1) works like (1, 0): all four XOR cases are right.

    x = (0, 0)
    h1: z = 0 + 0 + 0 = 0     a = 0
    h2: z = 0 + 0 - 1 = -1    a = max(0, -1) = 0
    y  = 1*0 - 2*0 = 0     correct
    all four:  00->0  01->1  10->1  11->0
  8. Without the bend: wrong

    Take the bend away and h2 passes −1 straight on: y = 0 − 2·(−1) = 2, wrong. Without activations, a sum of weighted sums is just one weighted sum, a straight line, and no straight line separates XOR's 1s from its 0s.

    no activation: a = z
    h2: a = -1
    y  = 1*0 - 2*(-1) = 2     wrong (XOR says 0)
    without activations the whole network is
      y = -x1 - x2 + 2     one weighted sum: a straight line
  9. Other activations

    Sigmoid squashes any z into 0 to 1; tanh into −1 to 1. Hidden layers today mostly use ReLU or relatives such as GELU. ReLU is cheap, and its slope is 1 for every positive z, so learning signals do not fade.

    ReLU     max(0, z)          0 to infinity
    sigmoid  1 / (1 + e^-z)     0 to 1   (sigmoid(0) = 0.5)
    tanh     tanh(z)            -1 to 1  (tanh(0) = 0)
    slope:   ReLU 1 for z > 0;  sigmoid at most 0.25
  10. Where the weights come from

    We copied these weights from the XOR example in the Deep Learning textbook. A real network starts from random weights and learns them: gradient descent nudges every weight and bias to shrink the loss, and backpropagation works out each one's slope.

    this network: 9 numbers
      hidden  w = 1, 1 and 1, 1    b = 0, -1
      output  w = 1, -2            b = 0
    training: start random, then repeat
      backpropagation -> a slope for every weight
      gradient descent -> w = w - lr * slope

اختبار يتاح بعد الخطوة الأخيرة.