지식 재사용: 전이 학습의 아이디어
Il 전이 학습 이는 머신러닝의 가장 혁신적인 기술 중 하나입니다. 현대. 아이디어는 간단하지만 강력합니다. 새로운 문제가 있을 때마다 처음부터 모델을 훈련시키는 대신, 대규모 데이터세트에 대해 이미 훈련된 모델에서 시작합니다( 소스 도메인) 그리고 그렇습니다 귀하의 특정 문제에 적합합니다( 대상 도메인). 이것은 작동합니다. 초기 레이어에서 학습된 기능(이미지의 가장자리, 질감, 모양, 텍스트의 언어 구조) 그것들은 보편적이고 양도 가능합니다.
전이 학습은 딥 러닝을 민주화했습니다. 더 이상 GPU 클러스터와 수백만 개가 필요하지 않습니다. 이미지 분류기를 훈련하기 위한 이미지. 사전 훈련된 모델을 사용할 수 있습니다. ResNet(120만 개의 ImageNet 이미지로 훈련됨)과 같이 몇 가지 이미지만으로 문제에 맞게 조정할 수 있습니다. 수백 개의 이미지와 훈련이 단 몇 분만에 완료됩니다.
이 기사에서 배울 내용
- 전이 학습이 작동하는 시기와 이유
- 특징 추출과 미세 조정
- 레이어 동결 전략
- 이미지 및 텍스트에 대해 사전 학습된 모델
- 소규모 데이터 세트를 위한 데이터 증대
- Python을 사용한 실제 구현
특징 추출: 백본 동결
가장 간단한 전이 학습 전략은 다음과 같습니다. 특징 추출: 사용된다 고정된 특징 추출기로 사전 훈련된 모델, 마지막 레이어(분류 헤드)가 제거됩니다. 대상 도메인에 대해 훈련된 새로운 분류기가 추가됩니다. 백본 가중치는 그대로 유지됩니다. congelati: 훈련 중에는 업데이트되지 않습니다. 이는 다음과 같은 경우에 이상적입니다. 대상 데이터 세트가 작고 소스 도메인이 대상과 유사합니다.
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LogisticRegression
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_digits
import numpy as np
# Simulazione di transfer learning con scikit-learn
# Il concetto: usare feature apprese da un modello pre-addestrato
# come input per un classificatore leggero
# Dataset: riconoscimento cifre (8x8 pixel)
digits = load_digits()
X, y = digits.data, digits.target
# Scenario 1: Training diretto (no transfer)
direct_pipeline = Pipeline([
('scaler', StandardScaler()),
('clf', LogisticRegression(max_iter=5000, random_state=42))
])
scores_direct = cross_val_score(direct_pipeline, X, y, cv=5, scoring='accuracy')
print(f"Diretto (tutte le feature): {scores_direct.mean():.3f}")
# Scenario 2: Simulazione feature extraction
# Pre-trained model ha gia' estratto feature ad alto livello
# Usiamo PCA come proxy per le "feature apprese"
from sklearn.decomposition import PCA
# Feature extractor pre-addestrato (congela i pesi)
feature_extractor = PCA(n_components=20, random_state=42)
X_features = feature_extractor.fit_transform(X)
# Nuovo classificatore sulle feature estratte
transfer_pipeline = Pipeline([
('scaler', StandardScaler()),
('clf', LogisticRegression(max_iter=5000, random_state=42))
])
scores_transfer = cross_val_score(transfer_pipeline, X_features, y, cv=5, scoring='accuracy')
print(f"Feature extraction (20 comp): {scores_transfer.mean():.3f}")
# Scenario 3: Con dataset ridotto (simula pochi dati nel target domain)
small_idx = np.random.choice(len(X), size=200, replace=False)
X_small, y_small = X[small_idx], y[small_idx]
X_small_features = feature_extractor.transform(X_small)
scores_small_direct = cross_val_score(direct_pipeline, X_small, y_small, cv=5)
scores_small_transfer = cross_val_score(transfer_pipeline, X_small_features, y_small, cv=5)
print(f"\nCon solo 200 campioni:")
print(f" Diretto: {scores_small_direct.mean():.3f}")
print(f" Feature extraction: {scores_small_transfer.mean():.3f}")
미세 조정: 모델 조정
Il 미세 조정 특징 추출 그 이상: 분류 헤드를 교체한 후, 응 그들은 해동된다 백본의 일부 레이어를 학습하고 매우 낮은 학습률로 재교육합니다. 이를 통해 모델은 중간 기능을 대상 도메인에 적용할 수도 있습니다. 일반적인 규칙은 다음과 같습니다. 대상 도메인이 소스와 다를수록 고정 해제할 가치가 있는 레이어가 더 많아집니다.
전략 레이어 동결 포함: 레이어의 마지막 블록만 해동합니다(보수적). 끝부터 시작까지 점진적으로 해동하고(점진적으로 동결 해제) 각 학습률에 대해 서로 다른 학습률을 사용합니다. 다른 레이어(깊은 레이어의 학습률이 더 낮은 차별적 학습률)
from sklearn.ensemble import GradientBoostingClassifier
from sklearn.model_selection import cross_val_score
from sklearn.datasets import load_digits
import numpy as np
digits = load_digits()
X, y = digits.data, digits.target
# Simulazione di fine-tuning:
# Step 1: Pre-training su un sottoinsieme (source domain)
# Step 2: Fine-tuning su un altro sottoinsieme (target domain)
# Source domain: cifre 0-4
source_mask = y <= 4
X_source, y_source = X[source_mask], y[source_mask]
# Target domain: cifre 5-9 (con pochi dati)
target_mask = y > 4
X_target, y_target = X[target_mask], y[target_mask] - 5
# Solo 50 campioni nel target domain
np.random.seed(42)
small_idx = np.random.choice(len(X_target), size=50, replace=False)
X_target_small = X_target[small_idx]
y_target_small = y_target[small_idx]
# Modello 1: Training from scratch sul target piccolo
from_scratch = GradientBoostingClassifier(
n_estimators=100, random_state=42
)
scores_scratch = cross_val_score(
from_scratch, X_target_small, y_target_small, cv=5
)
# Modello 2: Pre-trained sul source, fine-tuned sul target
pretrained = GradientBoostingClassifier(
n_estimators=100, random_state=42,
warm_start=True # permette di continuare il training
)
# Pre-training sul source
pretrained.fit(X_source, y_source)
# Fine-tuning: aggiungiamo altri estimators sul target
pretrained.n_estimators = 150 # aggiungi 50 estimators
pretrained.fit(X_target_small, y_target_small)
# Data Augmentation: aggiungi rumore per ampliare il dataset
noise_factor = 0.3
X_augmented = np.vstack([
X_target_small,
X_target_small + np.random.normal(0, noise_factor, X_target_small.shape),
X_target_small + np.random.normal(0, noise_factor, X_target_small.shape)
])
y_augmented = np.concatenate([y_target_small] * 3)
aug_model = GradientBoostingClassifier(n_estimators=100, random_state=42)
scores_aug = cross_val_score(aug_model, X_augmented, y_augmented, cv=5)
print("Risultati con 50 campioni target:")
print(f" From scratch: {scores_scratch.mean():.3f}")
print(f" Data augmentation: {scores_aug.mean():.3f}")
생태계의 사전 훈련된 모델
그들을 위해 이미지, 가장 많이 사용되는 모델은 ImageNet(120만 개의 이미지, 1000개의 클래스)에서 교육됩니다. 레스넷 (잔여 아키텍처), EfficientNet (효율성을 위해 최적화), VGG (간단하지만 효과적입니다). 에 대한 텍스트, 사전 훈련된 변환기 모델 지배하다: 버트 (구글, 양방향), GPT (OpenAI, 생성), 로베르타 (BERT 최적화).
포옹하는 얼굴 수천 개의 사전 훈련된 모델을 위한 플랫폼입니다.
텍스트 분류, 번역, 질문 답변, 텍스트 생성 등을 위한 템플릿
라이브러리를 통해 몇 줄의 코드로 모두 접근 가능 transformers.
전이 학습이 가치 있는 경우
전이 학습은 다음과 같은 경우에 올바른 선택입니다. 작은 (샘플 수가 수천 개 미만) 소스 도메인은 다음과 같습니다. 비슷한 대상으로 (이미지 의료 영상 분류를 위해 자연스럽게) 또는 원할 때마다 시간을 절약하다 및 자원 계산적. 다음과 같은 경우에는 편리하지 않습니다. 소스 도메인과 대상 도메인이 매우 다르거나(텍스트와 이미지) 대상 데이터 세트가 이미 크거나 소스 기능이 대상에 유용하지 않습니다.
훈련 비용: BERT를 처음부터 사전 훈련하는 데 16개의 TPU에서 4일이 소요됩니다(예상 비용 $10,000~$50,000). 특정 작업에 대한 BERT를 미세 조정하는 데는 단일 GPU에서 몇 분이 걸립니다. 이 차이 비용은 현대 딥 러닝에서 전이 학습이 표준 선택인 이유를 설명합니다.
핵심 사항
- 전이 학습은 새로운 문제에 대해 대규모 데이터 세트에서 사전 훈련된 모델을 재사용합니다.
- 특징 추출: 백본을 동결하고 새로운 분류 헤드만 학습시킵니다.
- 미세 조정: 학습률이 낮은 레이어를 점진적으로 고정 해제합니다.
- 원본과 대상 도메인이 유사하고 대상 데이터 세트가 작을 때 가장 잘 작동합니다.
- 이미지의 경우 ResNet/EfficientNet, 텍스트의 경우 BERT/GPT가 표준 모델입니다.
- 데이터 증강은 데이터가 부족한 경우 전이 학습을 보완합니다.







