בינה מלאכותית RB108-5T : תרגיל בית
תרגיל בית 1: תרגול רשת נוירונים : הרצה COLAB

- נתון הקוד הבא לרשת נוירונים – נתח בעזרת בינה מלאכותית מה עושה הקוד בכל שורה
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
import numpy as np import tensorflow as tf import matplotlib.pyplot as plt # 1) seed seed = 0 np.random.seed(seed) tf.random.set_seed(seed) # 2) synthetic 3-class data (2D blobs) def make_3_blobs(n_per_class=600, std=1.0): centers = np.array([[0, 0], [4, 0], [2, 3.5]], dtype=np.float32) X_list, y_list = [], [] for c in range(3): Xc = centers[c] + std * np.random.randn(n_per_class, 2).astype(np.float32) yc = np.full((n_per_class,), c, dtype=np.int32) X_list.append(Xc); y_list.append(yc) X = np.vstack(X_list) y = np.concatenate(y_list) return X, y X, y = make_3_blobs() # shuffle idx = np.random.permutation(len(X)) X, y = X[idx], y[idx] # 3) split split = int(0.8 * len(X)) X_train, y_train = X[:split], y[:split] X_val, y_val = X[split:], y[split:] # optional plot plt.figure() plt.scatter(X_train[:,0], X_train[:,1], c=y_train, s=8) plt.title("Train data (3 classes)") plt.xlabel("x1"); plt.ylabel("x2") plt.show() # 4) model (logits output) model = tf.keras.Sequential([ tf.keras.layers.Input(shape=(2,)), tf.keras.layers.Dense(64, activation="relu"), tf.keras.layers.Dropout(0.1), tf.keras.layers.Dense(64, activation="relu"), tf.keras.layers.Dropout(0.1), tf.keras.layers.Dense(3) # logits (no softmax) ]) # 5) compile model.compile( optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3), loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True), metrics=[tf.keras.metrics.SparseCategoricalAccuracy()] ) # 6) fit history = model.fit( X_train, y_train, validation_data=(X_val, y_val), epochs=80, batch_size=128, verbose=1 ) # 7) evaluate val_loss, val_acc = model.evaluate(X_val, y_val, verbose=0) print("VAL loss:", val_loss, "VAL acc:", val_acc) # 8) plots plt.figure() plt.plot(history.history["loss"]) plt.plot(history.history["val_loss"]) plt.title("Loss") plt.xlabel("epoch"); plt.ylabel("loss") plt.legend(["train", "val"]) plt.show() plt.figure() plt.plot(history.history["sparse_categorical_accuracy"]) plt.plot(history.history["val_sparse_categorical_accuracy"]) plt.title("Accuracy") plt.xlabel("epoch"); plt.ylabel("acc") plt.legend(["train", "val"]) plt.show() # predictions logits = model(X_val[:10]) pred = tf.argmax(logits, axis=1).numpy() print("pred:", pred) print("true:", y_val[:10]) |
2. מה תפקיד ה SEED
3. מה עושות השורות הבאות למה הם נחוצות בכלל ? :
3.1 מה תפקיד ה STD שנה אותו ל 5 וראה שינוי אל תשכחו להחזיר אותו ל 1
|
1 |
def make_3_blobs(n_per_class=600, std=1.0): |
3.2 שים לב הפונקציה מחזירה 2 ערכים איזה טיפוס הם ?
|
1 |
return X, y |
|
1 2 3 4 |
# 3) split split = int(0.8 * len(X)) X_train, y_train = X[:split], y[:split] X_val, y_val = X[split:], y[split:] |
4. מה תפקיד ה VAL , ERROR LOST

5.שנה התכונה של קוד הבינה מלאכותית של הרשת
שינוי 1 :
|
1 2 3 4 5 6 7 |
# 4) model (logits output) model = tf.keras.Sequential([ tf.keras.layers.Input(shape=(2,)), tf.keras.layers.Dense(6, activation="relu"), tf.keras.layers.Dense(6, activation="relu"), tf.keras.layers.Dense(3) # logits (no softmax) ]) |
שינוי 2 :
|
1 2 3 4 5 6 7 8 |
# 6) fit history = model.fit( X_train, y_train, validation_data=(X_val, y_val), epochs=24, batch_size=6, verbose=1 ) |