جارٍ تحميل الدرس...
في نافذة الطباعة اختر «حفظ كملف PDF» كوجهة.
الشبكات العصبية - الدرس 4
استخدام خلية sigmoid لتنفيذ البوابات المنطقية الأساسية، وتعلمها، وتجاوز حدودها
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import itertools as itσ بين 0 و1: القيم السالبة لـx تدفعها نحو 0، والقيم الموجبة تدفعها نحو 1، وσ(0) = 0.5def sigmoid(x):
"""Sigmoid function"""
return 1.0 / (1.0 + np.exp(-x))
xs = np.linspace(-10, 10, num=100, dtype=np.float32)
plt.plot(xs, sigmoid(xs))
plt.plot(0, .5, 'ro')0 أو 1 وفق قاعدة ثابتةz، ثم تضغطها عبر دالة sigmoid0، يجب أن يكون z سالبا جدا (b سالب)؛ وعندما يكون أي مدخل 1، يجب أن يميل z نحو موجب جدا| x1 | x2 | OR |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 1 |
w1 = 20، w2 = 20، b = -10logic_gate تبني خلية sigmoid بمدخلين؛ وtest_gate تطبع جدول حقيقتهاdef logic_gate(w1, w2, b, x1, x2):
return sigmoid(w1 * x1 + w2 * x2 + b)
def test_gate(w1, w2, b):
for x1 in range(2):
for x2 in range(2):
print("{}, {}: {}".format(x1, x2, np.round(logic_gate(w1, w2, b, x1, x2))))
or_gate = test_gate(20, 20, -10)
# 0, 0: 0.0
# 0, 1: 1.0
# 1, 0: 1.0
# 1, 1: 1.0تمرين
1 فقط عندما يكون كلا المدخلين 11 فقط عندما يكون كلا المدخلين 0 (عكس OR)1 إلا حين يكون كلا المدخلين 1 (عكس AND)# Fill in w1, w2, b so the truth table matches:
# and_gate = test_gate(...)
# nor_gate = test_gate(...)
# nand_gate = test_gate(...)(w1, w2, b) لخلية sigmoid نفسهاtf.Module على جدول الحقيقة كبياناتclass LogicGate(tf.Module):
def __init__(self):
super().__init__()
self.built = False
def __call__(self, x, train=True):
if not self.built:
input_dim = x.shape[-1]
self.w = tf.Variable(tf.random.normal([input_dim, 1]))
self.b = tf.Variable(tf.zeros([1]))
self.built = True
z = tf.add(tf.matmul(x, self.w), self.b)
return tf.sigmoid(z)def compute_loss(y_pred, y_true):
return tf.reduce_mean(tf.square(y_pred - y_true))
def train_model(model, x_train, y_train, learning_rate=0.5, epochs=5000):
for epoch in range(epochs):
with tf.GradientTape() as tape:
y_pred = model(x_train)
loss = compute_loss(y_pred, y_train)
grads = tape.gradient(loss, model.variables)
for g, v in zip(grads, model.variables):
v.assign_sub(learning_rate * g)
def compute_accuracy(model, x, y_true):
y_pred = tf.round(model(x, train=False))
correct = tf.equal(y_pred, y_true)
return tf.reduce_mean(tf.cast(correct, tf.float32)).numpy()(x1, x2) -> yand_table = np.array([[0, 0, 0], [1, 0, 0], [0, 1, 0], [1, 1, 1]], dtype=np.float32)
x_train = and_table[:, :2]
y_train = and_table[:, 2:]
model = LogicGate()
train_model(model, x_train, y_train)Epoch 0, Loss: 0.3312, Accuracy: 0.5000
Epoch 1000, Loss: 0.0126, Accuracy: 1.0000
Epoch 4000, Loss: 0.0027, Accuracy: 1.0000
Learned weight for w1: 5.733
Learned weight for w2: 5.733
Learned bias: -8.692حدود الخلية العصبية الواحدة
1 فقط عندما يختلف مدخلاها، وهي أساس الحساب (أنصاف الجامعات)| x1 | x2 | XOR |
|---|---|---|
| 0 | 0 | 0 |
| 0 | 1 | 1 |
| 1 | 0 | 1 |
| 1 | 1 | 0 |
XOR(A, B) = OR(A, B) AND NAND(A, B)المدخلات A، B
OR
بوابة 1
NAND
بوابة 2
AND
بوابة 3
XOR
المخرج
# def xor_gate(a, b):
# c = or_gate(a, b)
# d = nand_gate(a, b)
# return and_gate(c, d)σ(w1x1 + w2x2 + b)، وهي اللبنة الأساسية لسلوك شبيه بالبوابات المنطقيةtf.GradientTapeXOR = OR AND NANDMahmoud Abas|الإعداد