[Week 2] A History of Machine Learning: The Emergence of Machines That Learn Their Own Features π
English translation
Welcome to Week 2!
Last time, we learned the basic types of Machine Learning and 'linear models'.
Today, we will go one step further: > "Why has Deep Learning become mainstream in modern Machine Learning?"
Let us travel back in time to find the answer to this question.
Today's goal is to understand the key ideas that overcame the limitations of each era,
The goal is to gain a natural sense of why Deep Learning is needed.
β°οΈ Module 1: 'Limitations' of Traditional Machine Learning
- Scenario: Imagine building a model to distinguish the digits '7' and '1'.
- Question: To help the model distinguish '7' from '1', > what should we tell it?
- Traditional Approach: Data Features are defined by humans and supplied to the model.
- "The digit '7' has a horizontal stroke."
- "The digit '1' has a long vertical line."
- "How densely are pixels concentrated in the image center?"
- Limitations
- Expertise required: Creating good features is difficult without domain expertise.
- Enormous effort: As data grows more complex, manually accounting for every feature becomes almost impossible.
- (e.g., distinguishing dog and cat photos)
- Performance limits: Model performance is difficult to generalize across all problems.
Then,
Could machines also learn useful features on their own from data?
This question is the starting point of Deep Learning.
# Import libraries
import numpy as np
import matplotlib.pyplot as plt
from sklearn.datasets import load_digits
# Hide unnecessary warnings
import warnings
warnings.filterwarnings(action='ignore')
# Load the digit images built into scikit-learn (0β9)
digits = load_digits()
# Filter for only the digits '1' and '7'
X = digits.data[(digits.target == 1) | (digits.target == 7)]
y = digits.target[(digits.target == 1) | (digits.target == 7)]
y[y == 1] = 0 # Assign digit 1 to class 0
y[y == 7] = 1 # Assign digit 7 to class 1
# Feature Engineering: A human defines two features directly.
# Feature 1: Sum of all image pixel values (brightness)
feature1 = X.sum(axis=1)
# Feature 2: Ratio of pixel sums in the top and bottom halves of the image
feature2 = X[:, :32].sum(axis=1) / (X[:, 32:].sum(axis=1) + 1e-6)
# Visualize the data using 2 human-designed features
plt.figure(figsize=(8, 6))
plt.scatter(feature1[y==0], feature2[y==0], label='Digit 1')
plt.scatter(feature1[y==1], feature2[y==1], label='Digit 7')
plt.title("Data Distribution By Heuristics")
plt.xlabel("Feature 1")
plt.ylabel("Feature 2")
plt.legend()
plt.show()
print("λ νΉμ§μΌλ‘ μ΄λμ λ ꡬλΆμ΄ λλ κ² κ°μ§λ§, κ²ΉμΉλ λΆλΆμ΄ λ§μ 보μ
λλ€.")<Figure size 800x600 with 1 Axes>
λ νΉμ§μΌλ‘ μ΄λμ λ ꡬλΆμ΄ λλ κ² κ°μ§λ§, κ²ΉμΉλ λΆλΆμ΄ λ§μ 보μ λλ€.
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LogisticRegression
# Human-designed feature data
X_features = np.c_[feature1, feature2]
# Split into training/test data
(X_train, X_test,
y_train, y_test) = train_test_split(X_features, y,
test_size=0.3,
random_state=42)
# Train a Logistic Regression model
model = LogisticRegression()
model.fit(X_train, y_train)
# Check the results
accuracy = model.score(X_test, y_test)
print(f"μ¬λμ΄ λ§λ νΉμ§μ μ¬μ©ν λͺ¨λΈμ μ νλ: {accuracy * 100:.2f}%")
print("\nλμμ§ μμ μ±λ₯μ΄μ§λ§, λ μ’μ νΉμ§μ μ°ΎμΌλ©΄ μ±λ₯μ΄ μΌλ§λ λ μ€λ₯ΌκΉμ?")
print("\nμ΄μ²λΌ λͺ¨λΈμ μ±λ₯μ΄ 'μ¬λμ μ§κ΄(Heuristics)'μ μμ‘΄νλ κ²μ΄,")
print(f"κ·Έλ¦¬κ³ κ·Έ νκ³κ° λλ ·ν κ²μ΄, μ ν΅μ μΈ λ¨Έμ λ¬λμ νκ³μμ΅λλ€.")μ¬λμ΄ λ§λ νΉμ§μ μ¬μ©ν λͺ¨λΈμ μ νλ: 58.72% λμμ§ μμ μ±λ₯μ΄μ§λ§, λ μ’μ νΉμ§μ μ°ΎμΌλ©΄ μ±λ₯μ΄ μΌλ§λ λ μ€λ₯ΌκΉμ? μ΄μ²λΌ λͺ¨λΈμ μ±λ₯μ΄ 'μ¬λμ μ§κ΄(Heuristics)'μ μμ‘΄νλ κ²μ΄, κ·Έλ¦¬κ³ κ·Έ νκ³κ° λλ ·ν κ²μ΄, μ ν΅μ μΈ λ¨Έμ λ¬λμ νκ³μμ΅λλ€.
βοΈ Module 2: The Birth of Artificial Neural Networks and the First 'AI Winter'
- Perceptron (1957)
- The first artificial Neural Network model, inspired by neurons in the brain.
- It received multiple inputs and produced one output, with a structure nearly identical to the linear model from Week 1.
- Great Expectations: It inspired excitement: "Machines can now think like brains!"
- Critical limitation: The perceptron was shown to be unable to solve problems that are not linearly separable.
- XOR problem: A classic nonlinear problem in which a single straight line can never separate the two groups, as shown below.
- The First AI Winter: The period when support and interest in artificial Neural Network research declined sharply after this limitation became known.
# Generate XOR problem data
X_xor = np.array([[0, 0], [0, 1], [1, 0], [1, 1]])
y_xor = np.array([0, 1, 1, 0]) # Output 1 for different inputs and 0 for identical inputs
# Visualize the XOR data
plt.figure(figsize=(8, 6))
plt.scatter(X_xor[:, 0], X_xor[:, 1], c=y_xor, cmap='coolwarm', s=200)
plt.title("XOR Problem: Can we find the line for classification?")
plt.xlabel("Input 1")
plt.ylabel("Input 2")
plt.grid(True)
plt.show()
print("μ΄λ€ λ°©ν₯μΌλ‘ μ§μ μ κ·Έμ΄λ, λΉ¨κ° μ κ³Ό νλ μ μ μλ²½νκ² λλ μ μμ΅λλ€.")<Figure size 800x600 with 1 Axes>
μ΄λ€ λ°©ν₯μΌλ‘ μ§μ μ κ·Έμ΄λ, λΉ¨κ° μ κ³Ό νλ μ μ μλ²½νκ² λλ μ μμ΅λλ€.
# Attempt to solve XOR using the linear model (Logistic Regression) from Week 1
xor_model = LogisticRegression()
xor_model.fit(X_xor, y_xor)
# Check the results
accuracy = xor_model.score(X_xor, y_xor)
print(f"μ ν λͺ¨λΈμ XOR λ¬Έμ μ νλ: {accuracy * 100:.2f}%")
print("\nλͺ¨λΈμ΄ 'μ°κΈ°'μ λ€λ¦μλ μ±λ₯μ 보μ¬μ€λλ€. μ΄λ μ ν λͺ¨λΈμ λͺ
λ°±ν νκ³μ
λλ€.")μ ν λͺ¨λΈμ XOR λ¬Έμ μ νλ: 50.00% λͺ¨λΈμ΄ 'μ°κΈ°'μ λ€λ¦μλ μ±λ₯μ 보μ¬μ€λλ€. μ΄λ μ ν λͺ¨λΈμ λͺ λ°±ν νκ³μ λλ€.
π₯ Module 3: Ideas That Thawed the AI Winter
The AI winter lasted a long time, but a few researchers kept seeking solutions.
- Solution: "Stacking multiple layers of perceptrons can solve nonlinear problems!"
- Multilayer Perceptron (MLP)
- This is a basic structure of modern Deep Learning.
- It combines multiple 'lines' to form 'curves' or 'regions'.
- A New Problem: Multilayer models were too complex to train using existing methods.
- Breakthrough: Backpropagation (1986)
- Propagate the error between the model's final prediction and the true answer > from the Back toward the front (Propagate),
- sequentially adjusting each neuron's connection strengths (weights).
It finally became possible to train Deep Neural Networks.
from sklearn.neural_network import MLPClassifier
# Create an MLP model (2 hidden layers, with 4-4 neurons)
mlp_model = MLPClassifier(hidden_layer_sizes=(4,4,),
activation='relu', solver='adam',
random_state=42, max_iter=1000)
# Train the MLP on the XOR data
mlp_model.fit(X_xor, y_xor)
# Check the results
accuracy = mlp_model.score(X_xor, y_xor)
print(f"MLP λͺ¨λΈμ XOR λ¬Έμ μ νλ: {accuracy * 100:.2f}%")
print("\nμ¬λ¬ κ°μ μ μ μ‘°ν©νλ MLPλ λΉμ νμ μΈ XOR λ¬Έμ λ₯Ό μλ²½νκ² ν΄κ²°νμ΅λλ€!")MLP λͺ¨λΈμ XOR λ¬Έμ μ νλ: 100.00% μ¬λ¬ κ°μ μ μ μ‘°ν©νλ MLPλ λΉμ νμ μΈ XOR λ¬Έμ λ₯Ό μλ²½νκ² ν΄κ²°νμ΅λλ€!
# Visualize the decision boundary learned by the MLP
plt.figure(figsize=(8, 6))
xx, yy = np.meshgrid(np.linspace(-0.5, 1.5, 100),
np.linspace(-0.5, 1.5, 100))
Z = mlp_model.predict(np.c_[xx.ravel(), yy.ravel()])
Z = Z.reshape(xx.shape)
plt.contourf(xx, yy, Z, cmap='coolwarm', alpha=0.8) # Show the decision boundary with background colors
plt.scatter(X_xor[:, 0], X_xor[:, 1],
c=y_xor, cmap='coolwarm',
s=200, edgecolors='k')
plt.title("Classification by MLP")
plt.show()
print("MLPλ κ·Έλ¦Όμ²λΌ μ§μ μ΄ μλ, 볡μ‘ν ννμ κ²½κ³μ μ λ§λ€μ΄λΌ μ μμ΅λλ€.")<Figure size 800x600 with 1 Axes>
MLPλ κ·Έλ¦Όμ²λΌ μ§μ μ΄ μλ, 볡μ‘ν ννμ κ²½κ³μ μ λ§λ€μ΄λΌ μ μμ΅λλ€.
π₯ Module 4: The Big Bang of Deep Learning
Despite the invention of Backpropagation, > Deep Learning still could not solve real-world problems,
It did not become mainstream.
At the time, nobody knew why, but
Two main reasons were discovered later.
- Slow computers (CPU)
- Insufficient training data
The decisive event that brought all this to light occurred in 2012.
- The ImageNet competition
- Classifying more than one million images into 1000 categories,
- it was the largest and most challenging image recognition competition of its time.
- The arrival of AlexNet (2012, Professor Geoffrey Hinton's team)
- A model built from Convolutional Neural Networks (CNNs).
- GPU parallel processing dramatically accelerated training.
- It won by a large margin, astonishing the world.
- A Paradigm Shift
- This event shifted the Machine Learning research paradigm: > from human-designed Feature Engineering to Deep Learning, where models learn features themselves.
- This complete shift marked the full beginning of the Deep Learning era.
# Solve Module 1's digit '1' versus '7' problem using Deep Learning (MLP).
# This time, do not use the human-designed features (feature1, feature2).
# Use all 64 raw image pixel values (X) directly.
# The problem is simple enough that a CNN is unnecessary.
# Split into training/test data
(X_train_orig,
X_test_orig,
y_train_orig,
y_test_orig) = train_test_split(X, y, test_size=0.3, random_state=42)
# Create a deeper MLP model
deep_model = MLPClassifier(hidden_layer_sizes=(100, 50),
random_state=42, max_iter=500)
# Train directly on raw pixel data
deep_model.fit(X_train_orig, y_train_orig)
# Check the results
accuracy = deep_model.score(X_test_orig, y_test_orig)
print("λͺ¨λ 1μμ μ¬μ©νλ λ°μ΄ν°μ λμΌν λ°μ΄ν°μ
λλ€.")
print(f"λͺ¨λ 1μμ μ¬λμ΄ λ§λ νΉμ§ κΈ°λ° λͺ¨λΈμ μ νλ: 58.72%")
print(f"λ₯λ¬λ λͺ¨λΈ(MLP)μ μ νλ: {accuracy * 100:.2f}%")
print("\nβ¨ κ²°κ³Ό: λ₯λ¬λ λͺ¨λΈμ 64κ° ν½μ
μ 볡μ‘ν κ΄κ³ μμμ 'μ€μ€λ‘' μ΅μ μ νΉμ§μ νμ΅νμ¬,")
print(f"μ¬λμ΄ λ§λ νΉμ§λ³΄λ€ λ λμ μ±λ₯μ 보μ¬μ€λλ€!")λͺ¨λ 1μμ μ¬μ©νλ λ°μ΄ν°μ λμΌν λ°μ΄ν°μ λλ€. λͺ¨λ 1μμ μ¬λμ΄ λ§λ νΉμ§ κΈ°λ° λͺ¨λΈμ μ νλ: 58.72% λ₯λ¬λ λͺ¨λΈ(MLP)μ μ νλ: 100.00% β¨ κ²°κ³Ό: λ₯λ¬λ λͺ¨λΈμ 64κ° ν½μ μ 볡μ‘ν κ΄κ³ μμμ 'μ€μ€λ‘' μ΅μ μ νΉμ§μ νμ΅νμ¬, μ¬λμ΄ λ§λ νΉμ§λ³΄λ€ λ λμ μ±λ₯μ 보μ¬μ€λλ€!