Files
2nd/10_Wiki/Topic_Programming/AI_and_ML/Reward-Shaping-in-RL.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

6.5 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-reward-shaping-in-rl Reward Shaping in RL 10_Wiki/Topics verified self
Reward Shaping
Shaped Reward
Dense Reward Design
none A 0.95 applied
reinforcement-learning
reward-design
RLHF
GRPO
sparse-reward
2026-05-10 pending
language framework
Python PyTorch/Gymnasium/TRL

Reward Shaping in RL

매 한 줄

"매 sparse reward → dense intermediate signal — without changing optimal policy.". Ng, Harada, Russell 1999 ("Policy Invariance Under Reward Transformations") 의 prove 의 매 potential-based shaping F(s,s') = γΦ(s') Φ(s) 가 optimal policy 의 preserve, 매 modern RLHF/GRPO/RLVR 의 reward design 의 foundation 의.

매 핵심

매 핵심 theorem (Ng et al. 1999)

  • Shaped reward: r'(s, a, s') = r(s, a, s') + F(s, s').
  • F(s, s') = γ·Φ(s') Φ(s) (potential-based) → policy invariance guaranteed.
  • 의 X 가 well-defined Φ — 매 arbitrary bonus 의 optimal policy 의 distort 의 가능.

매 shaping types

  • Potential-based (theory-safe): heuristic value Φ(s).
  • Curiosity / intrinsic motivation: ICM, RND — exploration bonus.
  • Demonstrations (LfD): shaped reward from expert similarity.
  • Curriculum: progressively harder targets.
  • RLHF reward model: human-trained dense reward.
  • RLVR (verifiable): rule-based pass/fail (math, code) — sparse but exact.
  • GRPO advantages (DeepSeek 2024-25): group-relative normalization replaces critic.

매 응용

  1. Sparse-reward locomotion / manipulation.
  2. Game RL (StarCraft II, Atari hard-exploration).
  3. RLHF for LLM alignment.
  4. RLVR/GRPO for math/code (DeepSeek-R1, o1).
  5. Robotics imitation + RL hybrid.

💻 패턴

Potential-Based Shaping (Ng 1999)

def potential(state) -> float:
    """매 heuristic 의 — e.g. 의 distance-to-goal."""
    return -goal_distance(state)

def shaped_reward(r, s, s_next, gamma=0.99):
    return r + gamma * potential(s_next) - potential(s)

Curiosity-Driven (RND)

import torch
import torch.nn as nn

class RND(nn.Module):
    def __init__(self, obs_dim, feat_dim=128):
        super().__init__()
        self.target = nn.Sequential(nn.Linear(obs_dim, 256), nn.ReLU(),
                                    nn.Linear(256, feat_dim))
        for p in self.target.parameters(): p.requires_grad_(False)
        self.predictor = nn.Sequential(nn.Linear(obs_dim, 256), nn.ReLU(),
                                       nn.Linear(256, feat_dim))

    def intrinsic(self, obs):
        return ((self.predictor(obs) - self.target(obs)) ** 2).mean(-1)

Curriculum Reward

def curriculum_target(episode_idx, easy_target, hard_target, ramp_episodes=10000):
    t = min(episode_idx / ramp_episodes, 1.0)
    return easy_target + t * (hard_target - easy_target)

RLHF Reward Model

import torch.nn as nn
from transformers import AutoModel

class RewardModel(nn.Module):
    def __init__(self, base="meta-llama/Llama-3-8b"):
        super().__init__()
        self.backbone = AutoModel.from_pretrained(base)
        self.head = nn.Linear(self.backbone.config.hidden_size, 1)

    def forward(self, input_ids, attn):
        out = self.backbone(input_ids, attn).last_hidden_state
        last = out[:, -1]
        return self.head(last).squeeze(-1)

# Bradley-Terry pairwise loss
def bt_loss(r_chosen, r_rejected):
    return -torch.nn.functional.logsigmoid(r_chosen - r_rejected).mean()

RLVR — Verifiable Rule Reward

def rlvr_reward(generated: str, gold: str, task: str) -> float:
    if task == "math":
        return 1.0 if extract_answer(generated) == gold else 0.0
    elif task == "code":
        return float(run_unit_tests(generated))
    elif task == "format":
        return 1.0 if has_required_tags(generated) else 0.0

GRPO Advantage (DeepSeek 2024)

import numpy as np

def grpo_advantages(group_rewards: np.ndarray) -> np.ndarray:
    """매 group-relative normalization — critic 의 X."""
    mean = group_rewards.mean()
    std = group_rewards.std() + 1e-8
    return (group_rewards - mean) / std

# Usage: sample G=8 outputs per prompt, compute rewards, normalize within group

Combined Shaping

def combined_reward(r_env, s, s_next, model, obs, gamma=0.99,
                    pot_w=1.0, cur_w=0.1):
    pot = gamma * potential(s_next) - potential(s)
    cur = model.intrinsic(obs).item()
    return r_env + pot_w * pot + cur_w * cur

Reward Hacking Detector

def detect_hacking(rewards, true_returns, window=100):
    """매 reward 의 up 의 X 의 true return 의 stagnant → hacking."""
    if len(rewards) < window: return False
    rew_trend = np.polyfit(range(window), rewards[-window:], 1)[0]
    ret_trend = np.polyfit(range(window), true_returns[-window:], 1)[0]
    return rew_trend > 0.01 and ret_trend < 0

매 결정 기준

상황 Approach
Sparse reward, known heuristic Potential-based shaping
Hard exploration RND / ICM curiosity
Have expert demos LfD-shaped reward + BC pretrain
LLM alignment, subjective RLHF reward model
LLM math/code RLVR (rule-based) + GRPO
Robotic manipulation Combined: potential + curiosity + demo

기본값: Potential-based primary; RLVR + GRPO 의 LLM verifiable tasks 의; RLHF 의 subjective tasks 의.

🔗 Graph

🤖 LLM 활용

언제: reward model training (RLHF), reward function code generation, reward hacking analysis from logs. 언제 X: LLM 의 reward function 의 propose 의 hacking 의 prone 의 — verify 의 with controlled rollouts.

안티패턴

  • Non-potential bonus: arbitrary +10 의 sub-goal 의 reach → optimal policy 의 distort.
  • Reward hacking ignored: cumulative reward up 의 task fail 의 monitor 의 X.
  • Over-shaping: dense bonus 의 overwhelm sparse signal → agent 의 task 의 ignore.
  • Static curriculum: agent 의 surpass 의 still serving easy targets.
  • No baseline check: shaping with vs without 의 ablation 의 X — actual gain unknown.

🧪 검증 / 중복

  • Verified (Ng/Harada/Russell 1999 ICML; DeepSeek-R1 paper 2025; Sutton & Barto Ch 17).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — potential-based + RND + RLHF + GRPO + RLVR