본문으로 건너뛰기
#contrastive-learning#self-supervised-learning#infonce#simclr#clip

현대 인공지능 VIII — 대조 표현학습: InfoNCE·SimCLR·BYOL·CLIP

양성·음성 쌍, InfoNCE, augmentation, memory bank, SimCLR·BYOL과 이미지-언어 CLIP을 연결한다.

Series map

현대 인공지능 · 8편 학습 지도

기초 수학에서 생성·표현학습까지 이어지는 8편 학습 경로. 빈 번호나 자리 채우기용 글 없이, 실제 확보된 강의자료를 여덟 단계로 연결한다.

8 / 8
  1. 기초 게시됨 AI·ML·DL에서 확률·최적화까지 AI·ML·DL의 관계, 선형대수, 확률변수, 가우시안 모델, WSS, 손실함수와 경사하강법을 연결한다.
  2. 학습 문제 게시됨 머신러닝의 기본 과제와 일반화 분류·회귀·밀도추정에서 과적합, 정규화, 검증, SVM, Bayes 분류와 차원의 저주까지 다룬다.
  3. 신경망 게시됨 퍼셉트론·MLP·CNN과 컨볼루션 선형 판별기에서 다층 퍼셉트론으로 확장하고, 1D·2D 컨볼루션과 CNN의 계산 구조를 정리한다.
  4. 최적화 게시됨 기울기 기반 최적화: GD에서 OGM까지 Lipschitz 연속성, GD·PSD·PGD·FGM과 최적화된 기울기 방법 OGM의 수렴 구조를 비교한다.
  5. 영상 분류 게시됨 이미지 분류의 발전: AlexNet에서 SE Network까지 AlexNet, VGG, GoogLeNet, ResNet, WRN, DenseNet과 채널 재가중 SE Network의 발전을 추적한다.
  6. 픽셀 이해 게시됨 의미론적 영상 분할: FCN·U-Net·DeepLab 픽셀 단위 예측, encoder-decoder, skip connection, dilated convolution과 다중 스케일 분할을 다룬다.
  7. 생성·복원 게시됨 영상 잡음제거·VAE·확산모델 MMSE와 비선형 필터에서 VAE의 ELBO, 재매개변수화, DDPM의 순방향·역방향 확산으로 이어진다.
  8. 표현학습 읽는 중 대조 표현학습: InfoNCE·SimCLR·BYOL·CLIP 양성·음성 쌍, InfoNCE, augmentation, memory bank, SimCLR·BYOL과 이미지-언어 CLIP을 연결한다.
게시된 편만 링크로 연결된다. 후속 PDF의 내용을 담은 글이 공개되면 같은 위치가 자동으로 활성화되며, 빈 게시물은 만들지 않는다.

대조 표현학습은 label을 직접 예측하는 대신 무엇을 같게 보고 무엇을 다르게 볼지를 설계한다. 같은 대상의 두 view는 가깝게, 다른 sample은 멀게 배치하면 embedding 자체가 분류·검색·검출에 재사용되는 특징이 된다. 핵심은 loss 하나가 아니라 positive 정의, augmentation, negative 구성, encoder 갱신 방식의 조합이다.

#8.1 metric learning에서 contrastive learning으로

contrastive loss는 pair의 거리와 같은지 다른지를 함께 본다. triplet loss는 anchor (a), positive (p), negative (n) 사이에 margin (m)을 요구한다.

대조학습의 핵심: positive는 가깝게, negative는 멀게

anchor와 같은 의미의 view에는 높은 similarity를, 다른 sample에는 낮은 similarity를 부여하도록 embedding space를 학습한다.

Triplet loss
Ltriplet=max ⁣(0,d(za,zp)d(za,zn)+m)\mathcal{L}_{\mathrm{triplet}}=\max\!\left(0,d(z_a,z_p)-d(z_a,z_n)+m\right)

쉬운 negative는 loss에 거의 기여하지 않고, 너무 어려운 negative는 label noise나 false negative일 수 있다. lifted structured loss와 N-pair loss는 한 triplet만 보지 않고 batch 안의 여러 비교를 동시에 사용해 이 문제를 완화한다.

#8.2 InfoNCE: 올바른 쌍을 고르는 분류 문제

Contrastive Predictive Coding에서 널리 알려진 InfoNCE는 positive score가 batch 또는 dictionary의 모든 후보 중 가장 크도록 softmax cross entropy를 사용한다.

InfoNCE loss
Li=logexp(sim(zi,zi+)/τ)exp(sim(zi,zi+)/τ)+jiexp(sim(zi,zj)/τ)\mathcal{L}_i=-\log\frac{\exp(\operatorname{sim}(z_i,z_i^+)/\tau)}{\exp(\operatorname{sim}(z_i,z_i^+)/\tau)+\sum_{j\ne i}\exp(\operatorname{sim}(z_i,z_j^-)/\tau)}

temperature τ는 similarity 분포의 날카로움을 조절한다. 너무 작으면 소수 hard example의 gradient가 과도해지고, 너무 크면 positive와 negative의 구분이 흐려진다. N개의 후보를 쓸 때 InfoNCE는 mutual information의 lower bound와 연결된다.

Mutual-information lower bound
I(X;Y)logNLInfoNCEI(X;Y)\ge \log N-\mathcal{L}_{\mathrm{InfoNCE}}

다만 “모든 다른 이미지가 negative”라는 가정은 같은 class의 서로 다른 샘플을 밀어내는 false-negative 문제를 만든다. debiased objective, hard-negative sampling, supervised positives는 이 가정의 오류를 서로 다른 방식으로 다룬다.

#8.3 augmentation이 곧 학습할 invariance다

같은 이미지에서 random crop, color jitter, grayscale, blur를 적용한 두 view를 positive로 두면 모델은 그 변화에 불변인 특징을 학습한다. 따라서 augmentation은 단순한 regularization이 아니라 semantic equivalence를 정의하는 규칙이다.

  • crop이 지나치게 강하면 서로 다른 object patch가 positive가 될 수 있다.
  • color를 과하게 제거하면 색이 중요한 downstream task에 불리하다.
  • mixup·CutMix는 label interpolation 또는 spatial composition을 추가하지만, instance identity 기반 contrastive pair와 충돌할 수 있다.
  • UDA처럼 prediction consistency를 쓰는 방법은 augmentation 뒤에도 output이 같아야 한다고 요구한다.

#8.4 SimCLR: 단순하지만 batch와 증강에 민감한 기준선

SimCLR은 별도 memory bank 없이 큰 batch의 다른 view를 negative로 사용한다. encoder (f) 뒤의 projection head (g)에서 contrastive loss를 계산하고, downstream task에는 projection 전 representation을 쓴다.

SimCLR: 두 증강, 공유 encoder, projection head

같은 이미지에서 만든 두 view를 positive pair로 두고, batch 안의 다른 view들을 negative로 사용한다.

SimCLR cosine similarity
si,j=zizjzi2zj2s_{i,j}=\frac{z_i^\top z_j}{\lVert z_i\rVert_2\lVert z_j\rVert_2}

핵심 관찰은 세 가지다. 강한 augmentation 조합이 중요하고, nonlinear projection head가 representation 품질을 높이며, 더 큰 batch와 긴 학습이 유리하다. 이 장점은 동시에 계산·memory 비용이라는 약점이 된다.

#8.5 negative 없이 collapse를 피할 수 있는가

모든 입력이 같은 embedding으로 가면 positive similarity는 완벽해 보이지만 아무 정보도 남지 않는다. Barlow Twins와 BYOL은 명시적 negative 없이 이 collapse를 막는 서로 다른 장치를 보여준다.

BYOL과 Barlow Twins: 명시적 negative 없이 collapse 막기

BYOL은 stop-gradient와 momentum target을, Barlow Twins는 cross-correlation의 대각 정렬과 비대각 억제를 사용한다.

Barlow Twins은 두 view embedding의 cross-correlation matrix (C)가 identity에 가까워지게 한다. 대각은 같은 feature의 일치를, 비대각은 feature 사이 redundancy 제거를 담당한다.

Barlow Twins objective
LBT=i(1Cii)2+λijiCij2\mathcal{L}_{\mathrm{BT}}=\sum_i(1-C_{ii})^2+\lambda\sum_i\sum_{j\ne i}C_{ij}^2

BYOL은 online network의 prediction이 momentum target network의 representation을 맞추게 한다. target 쪽에는 stop-gradient를 적용한다.

BYOL regression objective
LBYOL=qθ(zθ)qθ(zθ)2sg ⁣(zξzξ2)22\mathcal{L}_{\mathrm{BYOL}}=\left\lVert\frac{q_\theta(z_\theta)}{\lVert q_\theta(z_\theta)\rVert_2}-\operatorname{sg}\!\left(\frac{z_\xi}{\lVert z_\xi\rVert_2}\right)\right\rVert_2^2

predictor의 비대칭, stop-gradient, target network의 느린 갱신, normalization이 함께 작동한다. “negative가 없으므로 자동으로 안전하다”가 아니라 collapse를 막는 구조적 비대칭이 무엇인지 확인해야 한다.

#8.6 memory bank와 MoCo

large batch 없이 많은 negative를 쓰려면 과거 embedding을 저장할 수 있다. 하지만 encoder가 빠르게 바뀌면 오래된 key와 현재 query가 서로 다른 표현 공간에 놓인다.

MoCo: momentum encoder와 queue로 큰 dictionary 유지

query encoder는 gradient로, key encoder는 이동평균으로 갱신한다. 이전 batch의 key를 queue에 보관해 많은 negative를 일관되게 쓴다.

MoCo는 key encoder를 query encoder의 exponential moving average로 갱신해 dictionary의 일관성을 높인다.

Momentum encoder update
ξmξ+(1m)θ\xi\leftarrow m\xi+(1-m)\theta

queue는 batch size와 dictionary size를 분리한다. instance discrimination의 memory bank보다 key가 더 최근이고, SimCLR보다 작은 batch에서도 큰 negative set을 유지할 수 있다.

#8.7 feature clustering: DeepCluster와 SwAV

DeepCluster는 feature를 clustering해 얻은 pseudo-label로 encoder를 다시 학습하는 과정을 반복한다. collapse를 막기 위해 빈 cluster와 한 cluster 쏠림을 관리해야 한다.

SwAV는 서로 다른 view의 cluster assignment를 교환해 예측한다. 모든 pairwise negative를 직접 비교하는 대신 prototype assignment를 일치시킨다.

SwAV swapped prediction
LSwAV=(zt,qs)+(zs,qt)\mathcal{L}_{\mathrm{SwAV}}=\ell(z_t,q_s)+\ell(z_s,q_t)

이 계열은 instance를 무조건 서로 밀어내기보다 dataset에 반복되는 prototype을 표현에 담는다.

#8.8 label이 일부 있다면: supervised contrastive learning

supervised contrastive learning(SupCon)은 anchor와 같은 class의 모든 sample을 positive set (P(i))로 사용한다. 한 개의 정답 logit만 키우는 cross entropy와 달리 class 내부를 모으고 class 사이를 분리하는 representation objective다.

Supervised contrastive loss
Lisup=1P(i)pP(i)logexp(zizp/τ)aiexp(ziza/τ)\mathcal{L}_i^{\mathrm{sup}}=-\frac{1}{|P(i)|}\sum_{p\in P(i)}\log\frac{\exp(z_i^\top z_p/\tau)}{\sum_{a\ne i}\exp(z_i^\top z_a/\tau)}

SupCon 원 논문은 여러 positive를 안정적으로 활용하는 형태를 제시한다. 하지만 class 안의 multi-modal structure까지 한 점으로 압축하면 fine-grained variation을 잃을 수 있으므로 downstream 목적과 맞춰야 한다.

#8.9 CLIP: positive를 image–text pair로 확장

CLIP은 image encoder와 text encoder를 함께 학습한다. batch의 N개 image와 N개 caption으로 N × N similarity matrix를 만들고, 맞는 쌍이 있는 대각을 양방향 cross entropy로 키운다.

CLIP: image–text 쌍을 하나의 대조 공간에 정렬

batch의 N개 image와 N개 text 사이 N×N similarity matrix를 만들고, 올바른 대각 쌍이 커지도록 양방향 cross entropy를 학습한다.

CLIP image-to-text loss
LIT=1Ni=1Nlogexp(viti/τ)j=1Nexp(vitj/τ)\mathcal{L}_{I\to T}=-\frac{1}{N}\sum_{i=1}^{N}\log\frac{\exp(v_i^\top t_i/\tau)}{\sum_{j=1}^{N}\exp(v_i^\top t_j/\tau)}

학습 뒤에는 class 이름을 prompt로 바꾸어 text embedding을 만들고 image와 가장 가까운 prompt를 고르는 zero-shot classifier로 쓸 수 있다. 자연어가 label space를 유연하게 만들지만, web-scale image–text data의 bias와 중복, caption quality도 표현에 그대로 들어온다. zero-shot 성능은 prompt wording과 dataset shift에도 민감하다.

#8.10 방법을 고르는 기준

상황우선 볼 방법주의점
큰 batch와 강한 augmentation을 쓸 수 있음SimCLRfalse negative, batch 비용
작은 batch에서 큰 dictionary가 필요함MoComomentum·queue staleness
명시적 negative를 피하고 싶음BYOL·Barlow Twinscollapse 방지 장치
prototype 구조를 학습하고 싶음DeepCluster·SwAVcluster imbalance
label 일부를 representation에 반영SupConclass 내부 다양성
image–text zero-shot transferCLIPdata bias, prompt sensitivity

대조학습을 비교할 때 loss 이름만 보면 안 된다. positive를 어떻게 만들었는지, negative 또는 collapse 방지 신호가 무엇인지, dictionary가 얼마나 크고 일관적인지, downstream에서 어느 representation을 쓰는지를 함께 봐야 한다.

Connect