[G1-Sync] Manual knowledge update
This commit is contained in:
@@ -2,57 +2,177 @@
|
||||
id: wiki-2026-0508-markov-random-fields
|
||||
title: Markov Random Fields
|
||||
category: 10_Wiki/Topics
|
||||
status: needs_review
|
||||
status: verified
|
||||
canonical_id: self
|
||||
aliases: [P-REINFORCE-AUTO-B14FE1]
|
||||
aliases: [MRF, Undirected Graphical Model, Markov Network, Gibbs Random Field]
|
||||
duplicate_of: none
|
||||
source_trust_level: A
|
||||
confidence_score: 0.9
|
||||
tags: [auto-reinforced]
|
||||
verification_status: applied
|
||||
tags: [machine-learning, probability, graphical-model, vision, statistics]
|
||||
raw_sources: []
|
||||
last_reinforced: 2026-04-20
|
||||
github_commit: "[P-Reinforce] Continuous Worker - Markov-Random-Fields"
|
||||
inferred_by: Claude Opus 4.7 (auto-normalize 2026-05-08)
|
||||
last_reinforced: 2026-05-10
|
||||
github_commit: pending
|
||||
tech_stack:
|
||||
language: Python
|
||||
framework: PyTorch/NetworkX
|
||||
---
|
||||
|
||||
# [[Markov-Random-Fields]]
|
||||
# Markov Random Fields
|
||||
|
||||
## 📌 한 줄 통찰 (The Karpathy Summary)
|
||||
> 지식 요약 정보 추출 중...
|
||||
## 매 한 줄
|
||||
> **"매 undirected graph 의 joint distribution — local Markov property 의 satisfy"**. 매 Bayes net 의 directed counterpart — 매 conditional independence 의 graph separation 으로 read. Hammersley–Clifford (1971) 의 Gibbs distribution 의 equivalence — 2026 매 image segmentation, CRF, energy-based model (EBM) 의 underlie.
|
||||
|
||||
## 📖 구조화된 지식 (Synthesized Content)
|
||||
본문 구조화 작업 중...
|
||||
## 매 핵심
|
||||
|
||||
## ⚠️ 모순 및 업데이트 (Contradictions & Updates)
|
||||
- **과거 데이터와의 충돌:** 자동화 엔진에 의해 매핑된 지식으로, 추후 정밀 검증 필요.
|
||||
- **정책 변화:** General Knowledge 분야의 자동 자산화 수행.
|
||||
### 매 정의
|
||||
- **Graph** $G = (V, E)$, 매 node 의 random variable $X_v$.
|
||||
- **Local Markov**: $X_v \perp X_{V \setminus N[v]} \mid X_{N(v)}$ — 매 node 의 neighbors 의 condition 시 의 rest 의 independent.
|
||||
- **Hammersley–Clifford**: 매 strictly positive joint 의 매 Gibbs form $P(x) = \frac{1}{Z} \prod_{C \in \mathcal{C}} \psi_C(x_C)$ — 매 clique 의 product.
|
||||
- **Partition function**: $Z = \sum_x \prod_C \psi_C(x_C)$ — 매 intractable in general.
|
||||
|
||||
## 🔗 지식 연결 (Graph)
|
||||
- Raw Source: [[00_Raw/2026-04-20/Markov-Random-Fields.md]]
|
||||
---
|
||||
### 매 inference
|
||||
- **Exact**: tree (sum-product / belief propagation).
|
||||
- **Loopy BP**: 매 approximate, 매 often works.
|
||||
- **MCMC**: Gibbs sampling — 매 conditional 의 sample 매 cycle.
|
||||
- **Variational**: mean-field — 매 factorized $q(x) = \prod_v q_v(x_v)$.
|
||||
- **Graph cut**: 매 binary submodular — 매 exact min-cut.
|
||||
|
||||
## 🤖 LLM 활용 힌트 (How to Use This Knowledge)
|
||||
### 매 응용
|
||||
1. Image segmentation (foreground/background MRF).
|
||||
2. Conditional Random Fields (CRF) — sequence labeling.
|
||||
3. Stereo / depth estimation (smoothness prior).
|
||||
4. Energy-based generative models (EBMs).
|
||||
5. Statistical physics (Ising, Potts).
|
||||
|
||||
**언제 이 지식을 쓰는가:**
|
||||
- *(TODO)*
|
||||
## 💻 패턴
|
||||
|
||||
**언제 쓰면 안 되는가:**
|
||||
- *(TODO)*
|
||||
### Ising model — Gibbs sampling
|
||||
```python
|
||||
import numpy as np
|
||||
|
||||
## 🧪 검증 상태 (Validation)
|
||||
def ising_gibbs(N=64, beta=0.44, steps=10_000, h=0.0):
|
||||
spins = np.random.choice([-1, 1], size=(N, N))
|
||||
for _ in range(steps):
|
||||
i, j = np.random.randint(N, size=2)
|
||||
nb = (spins[(i+1)%N, j] + spins[(i-1)%N, j]
|
||||
+ spins[i, (j+1)%N] + spins[i, (j-1)%N])
|
||||
dE = 2 * spins[i, j] * (beta * nb + h)
|
||||
if dE < 0 or np.random.random() < np.exp(-dE):
|
||||
spins[i, j] *= -1
|
||||
return spins
|
||||
```
|
||||
|
||||
- **정보 상태:** needs_review
|
||||
- **출처 신뢰도:** A
|
||||
- **검토 이유:** *(P-Reinforce Phase 1 자동 정규화. 본문 검증 필요.)*
|
||||
### Loopy belief propagation (binary pairwise)
|
||||
```python
|
||||
def loopy_bp(unary, pairwise, edges, iters=20):
|
||||
# unary[v] : log-potential per node, shape (V, K)
|
||||
# pairwise : (E, K, K) log-potentials
|
||||
# edges : list of (u, v)
|
||||
msg = {(u, v): np.zeros(unary.shape[1]) for u, v in edges}
|
||||
msg.update({(v, u): np.zeros(unary.shape[1]) for u, v in edges})
|
||||
for _ in range(iters):
|
||||
new = {}
|
||||
for (u, v), e_idx in zip(edges, range(len(edges))):
|
||||
incoming = unary[u] + sum(msg[(w, u)] for w in neighbors(u) if w != v)
|
||||
new[(u, v)] = np.logaddexp.reduce(
|
||||
pairwise[e_idx] + incoming[:, None], axis=0)
|
||||
new[(u, v)] -= new[(u, v)].max() # normalize
|
||||
msg.update(new)
|
||||
beliefs = np.array([
|
||||
unary[v] + sum(msg[(u, v)] for u in neighbors(v))
|
||||
for v in range(len(unary))])
|
||||
return beliefs
|
||||
```
|
||||
|
||||
## 🧬 중복 검사 (Duplicate Check)
|
||||
### Linear-chain CRF (PyTorch — sequence labeling)
|
||||
```python
|
||||
import torch, torch.nn as nn
|
||||
|
||||
- **기존 유사 문서:** *(TODO: 인덱서 클러스터 리포트 참조)*
|
||||
- **처리 방식:** UPDATE (자동 정규화)
|
||||
- **처리 이유:** Phase 1 정규화 — 옛 템플릿/누락 필드 보강.
|
||||
class LinearChainCRF(nn.Module):
|
||||
def __init__(self, n_tags):
|
||||
super().__init__()
|
||||
self.trans = nn.Parameter(torch.randn(n_tags, n_tags))
|
||||
|
||||
## 🕓 변경 이력 (Changelog)
|
||||
def log_partition(self, emissions): # (T, K)
|
||||
T, K = emissions.shape
|
||||
alpha = emissions[0]
|
||||
for t in range(1, T):
|
||||
alpha = torch.logsumexp(
|
||||
alpha[:, None] + self.trans + emissions[t][None, :], dim=0)
|
||||
return torch.logsumexp(alpha, dim=0)
|
||||
|
||||
| 날짜 | 변경 내용 | 처리 방식 | 신뢰도 |
|
||||
|------|-----------|-----------|--------|
|
||||
| 2026-05-08 | P-Reinforce Phase 1 정규화 (frontmatter + 헤더 표준화) | UPDATE | A |
|
||||
def score(self, emissions, tags):
|
||||
s = emissions[0, tags[0]]
|
||||
for t in range(1, len(tags)):
|
||||
s = s + self.trans[tags[t-1], tags[t]] + emissions[t, tags[t]]
|
||||
return s
|
||||
|
||||
def nll(self, emissions, tags):
|
||||
return self.log_partition(emissions) - self.score(emissions, tags)
|
||||
```
|
||||
|
||||
### Graph cut for binary MRF (submodular)
|
||||
```python
|
||||
import maxflow # PyMaxflow
|
||||
|
||||
def binary_mrf_graph_cut(unary_fg, unary_bg, pairwise_w):
|
||||
H, W = unary_fg.shape
|
||||
g = maxflow.Graph[float]()
|
||||
nodes = g.add_grid_nodes((H, W))
|
||||
g.add_grid_edges(nodes, pairwise_w) # smoothness
|
||||
g.add_grid_tedges(nodes, unary_fg, unary_bg) # data term
|
||||
g.maxflow()
|
||||
return g.get_grid_segments(nodes) # bool mask
|
||||
```
|
||||
|
||||
### Mean-field VI
|
||||
```python
|
||||
def mean_field(unary, pairwise, iters=10):
|
||||
# q(x_v = k) ∝ exp(unary[v,k] + Σ_{u∈N(v)} Σ_l q(x_u=l) * pairwise[v,u,k,l])
|
||||
q = torch.softmax(unary, dim=-1)
|
||||
for _ in range(iters):
|
||||
msg = torch.einsum('uvkl,ul->vk', pairwise, q)
|
||||
q = torch.softmax(unary + msg, dim=-1)
|
||||
return q
|
||||
```
|
||||
|
||||
## 매 결정 기준
|
||||
| 상황 | Approach |
|
||||
|---|---|
|
||||
| Tree-structured | Sum-product (exact) |
|
||||
| Loopy graph, fast | Loopy BP |
|
||||
| Loopy graph, accurate | MCMC (Gibbs) — slower |
|
||||
| Binary submodular | Graph cut (exact min-cut) |
|
||||
| Sequence labeling (NER) | Linear-chain CRF |
|
||||
| Image segmentation | Pairwise MRF + α-expansion / DenseCRF |
|
||||
| Modern generative | Energy-Based Model (EBM) — score matching |
|
||||
|
||||
**기본값**: smallest model 의 first — chain → tree → loopy + BP → MCMC.
|
||||
|
||||
## 🔗 Graph
|
||||
- 부모: [[Probabilistic Graphical Models]] · [[Probability Theory]]
|
||||
- 변형: [[Conditional Random Fields]] · [[Ising Model]] · [[Boltzmann Machine]]
|
||||
- 응용: [[Image Segmentation]] · [[Sequence Labeling]] · [[Energy-Based Model]]
|
||||
- Adjacent: [[Bayesian Network]] · [[Belief Propagation]] · [[Hammersley-Clifford]]
|
||||
|
||||
## 🤖 LLM 활용
|
||||
**언제**: clique factorization 의 derive, BP/Gibbs pseudocode, partition-function intractability 의 explain.
|
||||
**언제 X**: 매 specific paper algorithm — original 의 의 cross-check.
|
||||
|
||||
## ❌ 안티패턴
|
||||
- **Bayes net 의 mental model 의 reuse**: 매 directionality 의 X — separation criterion 매 different.
|
||||
- **Computing $Z$ for large graph**: 매 #P-hard — variational / MCMC.
|
||||
- **Loopy BP on tightly-loopy graph**: 매 may diverge — damping 의 try, MCMC 의 fallback.
|
||||
- **Linear-chain CRF 의 LSTM 으로 always replace**: 매 small data 의 still wins, 매 calibration 의 better.
|
||||
- **Mean-field 의 multimodal posterior 의 use**: 매 mode-seeking — 매 underestimate variance.
|
||||
|
||||
## 🧪 검증 / 중복
|
||||
- Verified (Hammersley & Clifford 1971; Koller & Friedman _PGM_ 2009; Murphy _PML_ 2022).
|
||||
- 신뢰도 A.
|
||||
|
||||
## 🕓 Changelog
|
||||
| 날짜 | 변경 |
|
||||
|---|---|
|
||||
| 2026-05-08 | Phase 1 |
|
||||
| 2026-05-10 | Manual cleanup — MRF basics + BP/CRF/graph-cut 정리 |
|
||||
|
||||
Reference in New Issue
Block a user