Files
2nd/10_Wiki/Topic_Programming/From_Topics_Root/Encoder-Decoder-Inconsistency.md
T
Antigravity Agent 9148c358d0 docs(10_Wiki): 위키 전체 재구성 — Topic_* 폴더를 4개 카테고리로 통합 + 대규모 중복 제거
Topic_Agent/Topic_Blog/Topics/Topics_Biz/Topics_Meeting/Topics_Rag의 마크다운 지식 문서를
Topic_General/Topic_Programming/Topic_Graphic/Topic_Business 4개 카테고리로 재분류.

- 중복 제거: frontmatter의 status:duplicate/merged + duplicate_of/redirect_to 필드로
  자기 자신을 중복으로 선언한 리다이렉트 stub 1032개 제거, 완전 동일 내용 파일 472개 제거,
  동일 파일명·다른 내용 충돌 시 더 큰(완전한) 버전만 유지(162개 제거) — 총 1639개 중복 제거.
- 분류: 폴더 단위로 명확한 항목(AI_and_ML/Coding/Architecture 등 → Programming,
  Comfyui/Visual_Effects → Graphic, Topics_Biz/Topics_Meeting/사업 등 → Business,
  Poetic_Blog_Writing/창의성/Game_Design 등 → General)은 폴더 우선순위로,
  나머지 혼재 폴더(Topic_Agent/Topic_Blog/Topics 루트/Thinking & Reasoning/Other/UI_UX_Assets)는
  title/tags 키워드 스코어링으로 파일 단위 분류(불명확한 경우 General로 폴백).
  원본 폴더명은 "From_*" 서브폴더로 보존해 추적 가능성 유지.
- 최종 배치: Programming 2784 / General 1608 / Graphic 285 / Business 249 = 4926개 문서.
- 에이전트 운영 상태(.astra/.agent/.obsidian/sessions/memory/_company/docs/lessons/_shared/src)는
  지식 콘텐츠가 아니므로 재분류 대상에서 제외하고 원위치 유지.
- Topics/Topic_email(상위 보호 폴더 Topic_email과 파일명 100% 중복) 삭제 — 보호 폴더 자체는 미변경.
- 완전히 비게 된 Topic_Agent/Topic_Blog/Topics_Biz/Topics_Rag 폴더 제거.
2026-07-05 00:33:48 +09:00

4.6 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-encoder-decoder-inconsistency Encoder Decoder Inconsistency 10_Wiki/Topics verified self
encoder-decoder-mismatch
seq2seq-inconsistency
none A 0.9 applied
nlp
transformer
training
decoding
2026-05-10 pending
language framework
python pytorch

Encoder Decoder Inconsistency

매 한 줄

"매 encoder가 본 분포 ≠ decoder가 생성하는 분포". Seq2seq training 시 encoder는 ground-truth context를 보지만 decoder는 inference에서 자기 prediction을 다시 입력으로 받기 때문에 train/inference 간 distribution shift가 발생한다. 매 exposure bias 의 근본 원인.

매 핵심

매 정의

  • Train: decoder input = teacher-forced ground truth.
  • Inference: decoder input = previously generated token.
  • Gap: 매 error compounds along sequence — early mistake → later tokens conditioned on out-of-distribution prefix.

매 표현

  • Exposure bias (Ranzato 2016).
  • Schedule sampling 의 motivation.
  • Hallucination 의 한 원인 (특히 long-form generation).

매 응용

  1. NMT (Neural Machine Translation) — 매 long sentence translation degradation.
  2. Summarization — repetition / drift.
  3. Speech recognition — RNN-T vs CTC trade-off.
  4. Code generation — 매 long completion 의 syntax break.

💻 패턴

Scheduled Sampling

import torch
import torch.nn.functional as F

def scheduled_sampling_step(decoder, prev_token, hidden, gt_token, p_use_gt: float):
    """p_use_gt 의 확률로 ground-truth, 아니면 model prediction 의 사용."""
    if torch.rand(1).item() < p_use_gt:
        input_tok = gt_token
    else:
        with torch.no_grad():
            logits, _ = decoder(prev_token, hidden)
            input_tok = logits.argmax(dim=-1)
    out_logits, hidden = decoder(input_tok, hidden)
    return out_logits, hidden

Minimum Risk Training

def mrt_loss(model, src, refs, n_samples=8):
    """매 sequence-level loss 의 — 매 sampled hypotheses 에 대해 risk minimize."""
    hyps = [model.sample(src) for _ in range(n_samples)]
    risks = torch.tensor([1 - bleu(h, refs) for h in hyps])
    log_probs = torch.stack([model.log_prob(h, src) for h in hyps])
    weights = F.softmax(log_probs, dim=0)
    return (weights * risks).sum()

Self-distillation Fix

def self_distill(student, teacher, src, T=2.0):
    """매 teacher 가 자기 생성한 sequence 의 사용 — 매 train/inference gap 축소."""
    with torch.no_grad():
        gen = teacher.generate(src, do_sample=True, top_p=0.9)
        teacher_logits = teacher(src, gen).logits
    student_logits = student(src, gen).logits
    return F.kl_div(
        F.log_softmax(student_logits / T, dim=-1),
        F.softmax(teacher_logits / T, dim=-1),
        reduction="batchmean",
    ) * T * T

Beam Search with Length Penalty

def length_penalty(score, length, alpha=0.7):
    """GNMT length penalty — 매 short hypothesis 의 bias 보정."""
    return score / ((5 + length) ** alpha / (5 + 1) ** alpha)

Contrastive Decoding

def contrastive_decode(big, small, prompt, alpha=0.5):
    """매 large model logit  small model logit — 매 expert/amateur gap 의 강조."""
    big_logits = big(prompt).logits[:, -1]
    small_logits = small(prompt).logits[:, -1]
    return big_logits - alpha * small_logits

매 결정 기준

상황 Approach
Short sequence (<32) Teacher forcing 충분
Long sequence Scheduled sampling / MRT
Production NMT Beam + length penalty + coverage
LLM long-form Contrastive decoding / self-distillation

기본값: teacher forcing + 1k step warmup 이후 scheduled sampling.

🔗 Graph

🤖 LLM 활용

언제: long-form generation 의 quality issue 분석 시. Train/eval BLEU gap 의 진단. 언제 X: 매 short classification — 매 inconsistency 의 무관.

안티패턴

  • Pure teacher forcing forever: 매 inference distribution 의 미본 채 deploy.
  • Greedy decoding only: 매 early mistake 의 lock-in.
  • No length normalization: beam 의 short hypothesis bias.

🧪 검증 / 중복

  • Verified (Ranzato et al. 2016, Bengio et al. 2015 scheduled sampling).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — encoder/decoder distribution shift + scheduled sampling/MRT/contrastive decoding