LECTURE 05 | المحاضرة 05

Artificial Neural Networks

الشبكات العصبية الاصطناعية
Neurons, Activation Functions, Loss, Gradients, and Backpropagation
العصبونات ودوال التنشيط ودالة الخطأ والتدرجات والانتشار الخلفي

🎯 Learning Objectives

  • Understand the mathematical model of an artificial neuron.
  • Explain weights, bias, weighted sum, and activation functions.
  • Understand the Sigmoid activation function.
  • Derive the derivative of the Sigmoid function.
  • Understand forward propagation.
  • Define and interpret a loss function.
  • Understand gradient descent.
  • Use the chain rule to derive backpropagation equations.
  • Apply neural networks to power-system problems.

🎯 أهداف التعلم

  • فهم النموذج الرياضي للعصبون الاصطناعي.
  • فهم الأوزان والانزياح والمجموع الموزون ودوال التنشيط.
  • فهم دالة Sigmoid.
  • اشتقاق مشتق دالة Sigmoid خطوة بخطوة.
  • فهم الانتشار الأمامي.
  • تعريف دالة الخطأ وفهم معناها.
  • فهم خوارزمية الانحدار المتدرج.
  • استخدام قاعدة السلسلة لاشتقاق معادلات الانتشار الخلفي.
  • تطبيق الشبكات العصبية على مسائل نظم الطاقة الكهربائية.

1. From Biological Neuron to Artificial Neuron

Artificial neural networks are inspired by the idea of biological neurons: receive signals, combine them, and generate an output.

Inputs
Weighted Sum
Activation
Output
An artificial neuron is fundamentally a mathematical function.

1. من العصبون الحيوي إلى العصبون الاصطناعي

تستلهم الشبكات العصبية الاصطناعية فكرتها بصورة مبسطة من العصبونات الحيوية: استقبال إشارات، دمجها، ثم إنتاج خرج.

العصبون الاصطناعي في جوهره عبارة عن دالة رياضية.

2. Mathematical Model of a Neuron

Suppose the neuron receives n inputs:

x₁, x₂, ..., xₙ

Each input has a corresponding weight:

w₁, w₂, ..., wₙ

The neuron first calculates the weighted sum:

z = w₁x₁ + w₂x₂ + ... + wₙxₙ + b

or more compactly:

z = Σ wᵢxᵢ + b

The output is:

y = φ(z)

2. النموذج الرياضي للعصبون

لنفترض أن العصبون يستقبل n من المدخلات:

x₁, x₂, ..., xₙ

ولكل مدخل وزن:

w₁, w₂, ..., wₙ

يقوم العصبون أولًا بحساب المجموع الموزون:

z = Σ wᵢxᵢ + b

ثم يتم تمرير z عبر دالة التنشيط:

y = φ(z)

3. Why Do We Need Weights?

The weights determine how strongly each input influences the output.

z = w₁x₁ + w₂x₂

If:

|w₁| > |w₂|

then input x₁ has a stronger influence than x₂.

3. لماذا نحتاج إلى الأوزان؟

تحدد الأوزان مقدار تأثير كل مدخل على خرج العصبون.

z = w₁x₁ + w₂x₂

فإذا كان:

|w₁| > |w₂|

فإن تأثير x₁ على الخرج أكبر من تأثير x₂.

4. What Does the Bias Do?

The bias shifts the activation function and allows the neuron to produce useful outputs even when all inputs are zero.

z = Σ wᵢxᵢ + b
Weights control the slope and importance of inputs. Bias controls the horizontal shift of the neuron response.

4. ما وظيفة الانزياح Bias؟

يسمح الانزياح بتحريك استجابة العصبون ويساعده على إعطاء خرج مناسب حتى عندما تكون بعض المدخلات مساوية للصفر.

الأوزان تحدد تأثير المدخلات، بينما يساعد الانزياح على تغيير موضع دالة التنشيط.

5. Structure of a Simple Neural Network

x₁ x₂ h₁ h₂ y Input Layer Hidden Layer Output Layer

This is a 2–2–1 neural network:

2 Inputs → 2 Hidden Neurons → 1 Output

5. بنية شبكة عصبية بسيطة

يوضح الشكل شبكة عصبية من النوع:

2 → 2 → 1

أي مدخلان، عصبونان في الطبقة المخفية، وخرج واحد.

6. Activation Functions

Without an activation function, a neural network would behave mainly like a linear mathematical model.

Sigmoid
σ(z)=1/(1+e⁻ᶻ)
Tanh
tanh(z)
ReLU
ReLU(z)=max(0,z)

6. دوال التنشيط

تسمح دوال التنشيط للشبكة بتمثيل علاقات غير خطية ومعقدة.

من أشهرها Sigmoid وTanh وReLU.

7. The Sigmoid Function

σ(z)=1/(1+e⁻ᶻ)

The output always lies between 0 and 1:

0 < σ(z) < 1

This is useful when the output can be interpreted as a probability or normalized response.

7. دالة Sigmoid

σ(z)=1/(1+e⁻ᶻ)

تكون قيمة الخرج دائمًا بين الصفر والواحد:

0 < σ(z) < 1

8. Derivative of the Sigmoid Function

Start with:

σ(z) = (1+e⁻ᶻ)⁻¹

Differentiate using the chain rule:

dσ/dz = -(1+e⁻ᶻ)⁻² × (-e⁻ᶻ)

Therefore:

dσ/dz = e⁻ᶻ / (1+e⁻ᶻ)²

Now note:

σ(z)=1/(1+e⁻ᶻ)

and:

1-σ(z)=e⁻ᶻ/(1+e⁻ᶻ)

Multiplying both expressions:

σ(z)[1-σ(z)] = e⁻ᶻ/(1+e⁻ᶻ)²

Hence:

σ'(z)=σ(z)[1-σ(z)]
This compact derivative is one reason Sigmoid is convenient for learning derivations.

8. اشتقاق دالة Sigmoid

نبدأ من:

σ(z)=(1+e⁻ᶻ)⁻¹

باستخدام قاعدة السلسلة نحصل على:

σ'(z)=e⁻ᶻ/(1+e⁻ᶻ)²

وبإعادة ترتيبها باستخدام قيمة σ نفسها:

σ'(z)=σ(z)[1-σ(z)]
أي أننا لا نحتاج في التطبيق إلى إعادة حساب المشتقة من البداية؛ يمكن حسابها مباشرة باستخدام قيمة خرج Sigmoid.

9. Forward Propagation

Forward propagation means calculating the network output from the input toward the output layer.

Hidden neuron 1

z₁ = w₁x₁ + w₂x₂ + b₁
h₁ = σ(z₁)

Hidden neuron 2

z₂ = w₃x₁ + w₄x₂ + b₂
h₂ = σ(z₂)

Output neuron

z₃ = v₁h₁ + v₂h₂ + b₃
y = σ(z₃)

9. الانتشار الأمامي

يعني الانتشار الأمامي حساب خرج الشبكة بدءًا من المدخلات، مرورًا بالطبقات المخفية، وصولًا إلى الخرج النهائي.

Input → Hidden Layer → Output

10. Loss Function

The network prediction y must be compared with the desired target t.

A simple squared-error loss is:

E = 1/2 (y-t)²

The factor 1/2 is used because it simplifies the derivative.

10. دالة الخطأ

نقارن خرج الشبكة y مع القيمة المطلوبة t.

E = 1/2 (y-t)²

نستخدم العامل 1/2 لأنه يجعل الاشتقاق أبسط.

11. Derivative of the Loss

Start from:

E = 1/2 (y-t)²

Differentiate with respect to y:

∂E/∂y = y-t
If y is larger than t, the derivative is positive. If y is smaller than t, the derivative is negative.

11. مشتقة دالة الخطأ

نبدأ من:

E = 1/2 (y-t)²

وبالاشتقاق بالنسبة إلى y:

∂E/∂y = y-t

هذه القيمة تخبرنا باتجاه الخطأ.

12. Why Do We Need Backpropagation?

We know the final error, but we need to know:

How much did each weight contribute to the error?

Backpropagation answers this question using derivatives and the chain rule.

12. لماذا نحتاج إلى الانتشار الخلفي؟

نعرف الخطأ النهائي، لكننا نحتاج إلى معرفة مقدار مساهمة كل وزن في هذا الخطأ.

الانتشار الخلفي يستخدم المشتقات وقاعدة السلسلة لحساب تأثير كل وزن.

13. The Chain Rule

If:

E → y → z → w

then:

∂E/∂w = ∂E/∂y × ∂y/∂z × ∂z/∂w
Backpropagation is essentially repeated application of the chain rule.

13. قاعدة السلسلة

إذا كان الوزن w يؤثر على z، وz تؤثر على y، وy تؤثر على الخطأ E، فإن تأثير w على E يحسب بضرب المشتقات الجزئية على طول المسار.

∂E/∂w = ∂E/∂y × ∂y/∂z × ∂z/∂w

14. Output-Layer Error Signal

For the output neuron:

y = σ(z₃)

We already know:

∂E/∂y = y-t

and:

∂y/∂z₃ = y(1-y)

Therefore:

δ₃ = ∂E/∂z₃ = (y-t)y(1-y)
This is the origin of the MATLAB expression:

dO = (y - t) * y * (1 - y)

14. إشارة الخطأ في طبقة الخرج

لدينا:

y = σ(z₃)

وباستخدام قاعدة السلسلة:

δ₃ = (y-t)y(1-y)

ولهذا تظهر في كود MATLAB العلاقة:

dO = (y - t) * y * (1 - y)

15. Gradient of an Output Weight

For weight v₁:

z₃ = v₁h₁ + v₂h₂ + b₃

Therefore:

∂z₃/∂v₁ = h₁

Thus:

∂E/∂v₁ = δ₃h₁

Similarly:

∂E/∂v₂ = δ₃h₂

and for the bias:

∂E/∂b₃ = δ₃

15. تدرج وزن في طبقة الخرج

لأن:

z₃ = v₁h₁ + v₂h₂ + b₃

فإن:

∂z₃/∂v₁ = h₁

وبالتالي:

∂E/∂v₁ = δ₃h₁

16. Hidden-Layer Error Signal

Hidden neuron h₁ influences the output through weight v₁.

Using the chain rule:

δ₁ = δ₃ v₁ h₁(1-h₁)

Similarly:

δ₂ = δ₃ v₂ h₂(1-h₂)
The output error is propagated backward through the connection weights.

16. إشارة الخطأ في الطبقة المخفية

يصل تأثير الخطأ من طبقة الخرج إلى الطبقة المخفية عبر الأوزان.

δ₁ = δ₃ v₁ h₁(1-h₁)
δ₂ = δ₃ v₂ h₂(1-h₂)

17. Gradient of Input-to-Hidden Weights

If:

z₁ = w₁x + b₁

then:

∂z₁/∂w₁ = x

Therefore:

∂E/∂w₁ = δ₁x

and:

∂E/∂b₁ = δ₁

17. تدرجات الأوزان بين الدخل والطبقة المخفية

لأن:

z₁ = w₁x + b₁

فإن:

∂E/∂w₁ = δ₁x
∂E/∂b₁ = δ₁

18. Gradient Descent

After calculating the gradients, we update every parameter:

w(new) = w(old) - η ∂E/∂w

where η is the learning rate.

We move in the opposite direction of the gradient because the gradient points toward increasing error.

18. الانحدار المتدرج

بعد حساب المشتقات نقوم بتحديث الأوزان:

w(new) = w(old) - η ∂E/∂w

حيث η هو معدل التعلم.

نطرح التدرج لأننا نريد التحرك باتجاه انخفاض الخطأ وليس زيادته.

19. Complete Training Cycle

Input
Forward Propagation
Prediction
Loss
Backpropagation
Update Weights
Repeat until the error becomes sufficiently small.

19. دورة التدريب الكاملة

المدخلات ← الانتشار الأمامي ← التنبؤ ← الخطأ ← الانتشار الخلفي ← تحديث الأوزان ← التكرار

20. Numerical Example

Assume:

x = 1
w = 0.5
b = 0

Then:

z = wx+b = 0.5

Sigmoid output:

y = σ(0.5) ≈ 0.622

Suppose the target is:

t = 1

Loss:

E = 1/2(0.622-1)²
E ≈ 0.0714

20. مثال عددي

إذا كان:

x=1, w=0.5, b=0

فإن:

z=0.5
y≈0.622

وإذا كانت القيمة المطلوبة t=1 فإن الخطأ:

E≈0.0714

21. MATLAB Connection

Forward propagation:

z1 = W.w1*x + W.b1
h1 = sigma(z1)

Output-layer error:

dO = (y - t) * y * (1 - y)

Output-weight gradients:

dV1 = dO*h1
dV2 = dO*h2

Hidden-layer error:

d1 = dO*W.v1*h1*(1-h1)

Weight gradient:

dW1 = d1*x

21. الربط مع MATLAB

الآن يمكن فهم كل سطر في كود الانتشار الخلفي بصورة رياضية.

كل قيمة d تمثل مشتقة جزئية أو إشارة خطأ مستخدمة لتحديد اتجاه تحديث الوزن.

22. Power-System Application: Load Forecasting

A neural network can receive several power-system variables:

x = [Temperature, Hour, Previous Load, Day Type]

and predict:

y = Future Electrical Load
Weather + Historical Data
Neural Network
Load Forecast

22. تطبيق في نظم الطاقة: التنبؤ بالحمل

يمكن للشبكة العصبية استقبال معلومات مثل درجة الحرارة والوقت والحمل السابق ونوع اليوم.

ثم تقوم بإنتاج تنبؤ للحمل الكهربائي المستقبلي.

23. Other Power-System Applications

PV Forecasting

Predict photovoltaic power generation.

Wind Forecasting

Predict wind-power generation.

Fault Classification

Identify and classify network faults.

Voltage Estimation

Estimate voltage states using measurements.

Battery Management

Estimate SOC or predict optimal operation.

Smart Grid Monitoring

Detect abnormal conditions from large datasets.

23. تطبيقات أخرى في نظم الطاقة

  • التنبؤ بالطاقة الشمسية.
  • التنبؤ بطاقة الرياح.
  • تصنيف الأعطال.
  • تقدير الجهد والحالة التشغيلية.
  • إدارة أنظمة تخزين الطاقة.
  • مراقبة الشبكات الذكية.

24. Summary

  • A neuron calculates a weighted sum and applies an activation function.
  • The fundamental neuron equation is z = Σwᵢxᵢ + b.
  • Sigmoid maps values into the interval (0,1).
  • Its derivative is σ'(z)=σ(z)[1-σ(z)].
  • Forward propagation calculates network predictions.
  • The loss function measures prediction error.
  • Backpropagation calculates gradients using the chain rule.
  • Gradient descent updates the weights to reduce error.
  • Neural networks have many applications in modern power systems.

24. الخلاصة

  • يحسب العصبون مجموعًا موزونًا ثم يطبق دالة تنشيط.
  • المعادلة الأساسية للعصبون هي z = Σwᵢxᵢ + b.
  • تحول Sigmoid القيم إلى المجال بين الصفر والواحد.
  • مشتقتها هي σ'(z)=σ(z)[1-σ(z)].
  • يحسب الانتشار الأمامي خرج الشبكة.
  • تقيس دالة الخطأ الفرق بين التنبؤ والقيمة المطلوبة.
  • يحسب الانتشار الخلفي التدرجات باستخدام قاعدة السلسلة.
  • يحدث الانحدار المتدرج الأوزان لتقليل الخطأ.
  • للشبكات العصبية تطبيقات واسعة في نظم الطاقة الكهربائية.

📝 Review Questions

  1. What is the mathematical equation of an artificial neuron?
  2. What is the role of weights?
  3. What is the role of the bias?
  4. Why are activation functions required?
  5. Derive the derivative of the Sigmoid function.
  6. What is forward propagation?
  7. Why do we need a loss function?
  8. Explain the chain rule in backpropagation.
  9. Derive dO = (y-t)y(1-y).
  10. Why do we subtract the gradient during training?
  11. Name four neural-network applications in electrical power systems.

📝 أسئلة المراجعة

  1. ما المعادلة الرياضية للعصبون الاصطناعي؟
  2. ما وظيفة الأوزان؟
  3. ما وظيفة الانزياح؟
  4. لماذا نحتاج إلى دوال التنشيط؟
  5. اشتق مشتقة دالة Sigmoid.
  6. ما المقصود بالانتشار الأمامي؟
  7. لماذا نحتاج إلى دالة خطأ؟
  8. اشرح قاعدة السلسلة في الانتشار الخلفي.
  9. اشتق العلاقة dO = (y-t)y(1-y).
  10. لماذا نطرح التدرج أثناء التدريب؟
  11. اذكر أربعة تطبيقات للشبكات العصبية في نظم الطاقة الكهربائية.