בינה מאלכותית : קורס תרגול רשתוות נירונים – למתחלים
ANN SINGLE INPUT SINGLE OUT PUT
pip install tensorflow או pip install tensorflow!
-
Imports TensorFlow and NumPy libraries.
-
Creates a simple dataset where
y = 2 * x
. -
Builds a neural network with:
-
1 input layer
-
3 hidden layers (ReLU activation)
-
1 linear output layer for regression
-
-
Compiles the model using Adam optimizer and mean squared error loss.
-
Trains the model for 200 epochs with batch size = 2.
-
Predicts outputs for test inputs
[6], [7], [8]
. -
Prints predictions for each test input.
Let me know if you want this written as comments directly inside the code.
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 |
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # Create dataset: X → y = 2 * X X = np.array([[1], [2], [3], [4], [5]], dtype=np.float32) y = np.array([[2], [4], [6], [8], [10]], dtype=np.float32) # Build the model model = tf.keras.Sequential() model.add(tf.keras.layers.InputLayer(shape=(1,))) # Input layer model.add(tf.keras.layers.Dense(8, activation='relu')) # Hidden layer 1 model.add(tf.keras.layers.Dense(16, activation='relu')) # Hidden layer 2 model.add(tf.keras.layers.Dense(8, activation='relu')) # Hidden layer 3 model.add(tf.keras.layers.Dense(1)) # Output layer (linear for regression) # Compile the model optimizer = tf.keras.optimizers.Adam(learning_rate=0.0001) model.compile(optimizer=optimizer, loss='mse') # Train the model and store the training history history = model.fit(X, y, epochs=200, batch_size=2, verbose=1) # Show progress during training # Predict values for new inputs test = np.array([[6], [7], [8]], dtype=np.float32) predictions = model.predict(test) # Print predictions for i, val in enumerate(test): print(f"Input: {val[0]} → Prediction: {predictions[i][0]}") # Plot training loss plt.plot(history.history['loss']) plt.title('Training Loss Over Epochs') plt.xlabel('Epoch') plt.ylabel('Loss (MSE)') plt.grid(True) plt.show() |
מה ניתן לעשות בשביל בשביל לשפר את הדיוק ?
הרבה מאוד דברים וניראה במספר שלבים
- הוספת עוד נתונים למשל במקום 5 להוסיף 25
- יותר epocs
- learning_rate=0.001 למשל להגדיל את הקצב או להקטין למשל learning_rate=0.01
1.נוסיף נתונים :
2.נשנה את קצב הלימוד :
נשמה לערך לימוד מהיר יותר קצב לימוד מהיר יותר
הרץ את התוכנה והראה ערכים
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 |
import tensorflow as tf import numpy as np import matplotlib.pyplot as plt # Create dataset: X → y = 2 * X X = np.array([ [-3.5], [-3.0], [-2.5], [-2.0], [-1.5], [-1.0], [-0.5], [ 0.0], [ 0.5], [ 1.0], [ 1.5], [ 2.0], [ 2.5], [ 3.0], [ 3.5], [ 4.0], [ 4.5], [ 5.0], [ 5.5], [ 6.0], [ 6.5], [ 7.0], [ 7.5], [ 8.0], [ 8.5] ], dtype=np.float32) y = np.array([ [-7.0], [-6.0], [-5.0], [-4.0], [-3.0], [-2.0], [-1.0], [ 0.0], [ 1.0], [ 2.0], [ 3.0], [ 4.0], [ 5.0], [ 6.0], [ 7.0], [ 8.0], [ 9.0], [10.0], [11.0], [12.0], [13.0], [14.0], [15.0], [16.0], [17.0] ], dtype=np.float32) # Build the model model = tf.keras.Sequential() model.add(tf.keras.layers.InputLayer(shape=(1,))) # Input layer model.add(tf.keras.layers.Dense(8, activation='relu')) # Hidden layer 1 model.add(tf.keras.layers.Dense(16, activation='relu')) # Hidden layer 2 model.add(tf.keras.layers.Dense(8, activation='relu')) # Hidden layer 3 model.add(tf.keras.layers.Dense(1)) # Output layer (linear for regression) # Compile the model optimizer = tf.keras.optimizers.Adam(learning_rate=0.001) #more fast model.compile(optimizer=optimizer, loss='mse') # Train the model and store the training history history = model.fit(X, y, epochs=200, batch_size=2, verbose=1) # Show progress during training # Predict values for new inputs test = np.array([[6], [7], [8]], dtype=np.float32) predictions = model.predict(test) # Print predictions for i, val in enumerate(test): print(f"Input: {val[0]} → Prediction: {predictions[i][0]}") # Plot training loss plt.plot(history.history['loss']) plt.title('Training Loss Over Epochs') plt.xlabel('Epoch') plt.ylabel('Loss (MSE)') plt.grid(True) plt.show() |
לומד או זוכר ?
(האם הבינה מלאכותית – המודל זוכרת או לומדת ) ?
Overfitting זה כשהמודל זוכר אבל לא לומד
דוגמה פשוטה:
אם סטודנט שינן בעל־פה את התשובות למבחן אבל לא הבין את החומר,
אז אם ישנו טיפה את הניסוח של השאלה — הוא ייפול.
זה בדיוק מה שקורה למודל ב־Overfitting
Model is memorizing, not learning” means:
The model is learning to copy exact patterns from the training data instead of learning general rules that work on new data.
מה ההבדל בין שינון לבין למידה אמיתית?
📌 שינון (memorizing):
המודל "זוכר בעל־פה" את הנתונים שעליהם התאמן, מבלי להבין את הדפוס הכללי לא מוצא קשר א חוקיות .
📌 למידה (learning):
המודל מבין את הקשר בין הקלט לפלט, ולכן מצליח גם על דוגמאות חדשות