جارٍ تحميل الدرس...
في نافذة الطباعة اختر «حفظ كملف PDF» كوجهة.
الشبكات العصبية - الدرس 2
البنى الأساسية للبيانات، العمليات الحسابية، الرسوم البيانية الحسابية، والاشتقاق التلقائي
eager execution) مفعل افتراضياtf.keras هي الواجهة عالية المستوى الموصى بهاCPU وGPU وTPU والأجهزة المحمولة؛ وتدعم التدريب الموزعالمعالجة المسبقة
التدريب
التقييم
النشر
import tensorflow as tf
print("TensorFlow Version:", tf.__version__)
print("Is GPU available?:", tf.config.list_physical_devices('GPU'))tf.Tensortf.Variabletf.TensorArrayRNNs)tf.RaggedTensortf.SparseTensortf.Tensor: الرتب والثباتscalar) موتر من الرتبة 0 (بلا محاور)؛ والمتجه (vector) من الرتبة 1 (محور واحد)؛ والمصفوفة (matrix) من الرتبة 2 (محوران)rank_0_tensor = tf.constant(4)
# tf.Tensor(4, shape=(), dtype=int32)
rank_1_tensor = tf.constant([2.0, 3.0, 4.0])
# tf.Tensor([2. 3. 4.], shape=(3,), dtype=float32)
rank_2_tensor = tf.constant([[1, 2], [3, 4], [5, 6]], dtype=tf.float16)
# shape=(3, 2), dtype=float16NumPy عبر np.array(tensor) أو tensor.numpy().shape (الحجم لكل محور) و.dtype (نوع العنصر)NumPytensor = tf.constant([[1, 2], [3, 4]], dtype=tf.float32)
print("Shape:", tensor.shape) # (2, 2)
print("Data Type:", tensor.dtype) # float32
tensor[0, 0] # عنصر واحد -> 1.0
tensor[:, 0] # كل الصفوف، العمود 0 -> [1. 3.]tf.Variable، RaggedTensor، SparseTensortf.Variablevar = tf.Variable([[5.0, 6.0], [7.0, 8.0]])
var.assign([[1, 2], [3, 4]])
var.assign_add([[1, 1], [1, 1]])
# Incremented Value: [[2. 3.] [4. 5.]]tf.RaggedTensortf.SparseTensortf.sparse.to_dense()TensorFlow أدوات رياضية وجبر خطي وأرقام عشوائية لبناء النماذجx = tf.constant([2.0, 4.0, 6.0])
y = tf.constant([1.0, 2.0, 3.0])
tf.add(x, y) # [3. 6. 9.]
tf.subtract(x, y) # [1. 2. 3.]
tf.multiply(x, y) # [2. 8. 18.]
tf.divide(x, y) # [2. 2. 2.]tf.matmul لضرب المصفوفات, العمود الفقري لطبقات الشبكة العصبيةtf.linalg.det وtf.linalg.inv وtf.linalg.eig وtf.linalg.svdA = tf.constant([[2.0, 3.0], [1.0, 4.0]])
B = tf.constant([[1.0, 2.0], [3.0, 4.0]])
tf.linalg.matmul(A, B)
# [[11. 16.]
# [13. 18.]]reduce_meantf.random.normal وtf.random.uniform وtf.random.shuffle وtf.random.set_seed لضمان قابلية التكرارtf.reduce_mean يحسب متوسط العناصر، اختياريا على طول محور axis محددtf.random.normal([3, 3], mean=0.0, stddev=1.0)
tf.random.uniform([2, 2], minval=0, maxval=10)
tf.random.set_seed(42)
x = tf.constant([[1, 2, 3], [4, 5, 6]], dtype=tf.float32)
tf.reduce_mean(x) # 3.5 (كل العناصر)
tf.reduce_mean(x, axis=0) # [2.5 3.5 4.5] (لكل عمود)
tf.reduce_mean(x, axis=1) # [2. 5.] (لكل صف)tf.functioneager)TensorFlow 2.x فوريا افتراضيا@tf.functionstatic graph)@tf.function
def computation_graph(x1, x2):
add_op = x1 + x2
mul_op = x1 * x2
return add_op - mul_op
x1 = tf.constant(7, dtype=tf.float32)
x2 = tf.constant(3, dtype=tf.float32)
computation_graph(x1, x2) # -11tf.GradientTapeTensorFlow التدرجات عبر الاشتقاق التلقائي العكسي (reverse-mode autodiff)tf.GradientTape()، ثم استدع .gradient()x = tf.Variable(2.0)
with tf.GradientTape() as tape:
y = x ** 3 # y = x^3
dy_dx = tape.gradient(y, x)
# dy/dx = 3*x^2 = 12.0GradientTape مع متغيرات متعددةw = tf.Variable(1.0)
b = tf.Variable(2.0)
x_vals = tf.constant([1.0, 2.0, 3.0])
with tf.GradientTape() as tape:
y_vals = w * x_vals + b
loss = tf.reduce_mean((y_vals - tf.constant([2.0, 4.0, 6.0]))**2)
gradients = tape.gradient(loss, [w, b])
# dLoss/dw: -1.333, dLoss/db: 0.0tf.Tensor ثابت (immutable)؛ والـtf.Variable قابل للتغيير ويستخدم للمعاملات القابلة للتدريب.shape و.dtypeTensorFlow واجهات رياضية وجبر خطي (tf.matmul) وأرقام عشوائية مبنية لتعلم الآلة@tf.function يترجمها إلى رسم بياني ثابت أسرعtf.GradientTape() العمليات وتحسب التدرجات تلقائيا عبر .gradient()GradientTape هو الآلية التي تشغل التدريب في كل شبكة عصبيةMahmoud Abas|مقدمة في TensorFlow 2.x