Jommarn's picture
v5: memory at layers 10+18, normal-chat preserving training (checkpoint, code, logs)
6118a66 verified
Raw History Blame Contribute Delete
1.22 kB
from __future__ import annotations
import math
import random
from .schema import ForgettingParams, RecallLabel
def compute_stability(
memory_tendency: float,
importance: float,
arousal: float,
n_rehearsals: int,
params: ForgettingParams,
) -> float:
"""
Ebbinghaus-inspired stability formula (exp_stability_v1):
S = S0 * tendency * (1 + a*importance) * (1 + b*arousal) * (1 + c)^n_rehearsals
"""
s = (
params.S0_days
* memory_tendency
* (1.0 + params.a_importance * importance)
* (1.0 + params.b_arousal * arousal)
* ((1.0 + params.c_rehearsal) ** n_rehearsals)
)
return round(s, 4)
def compute_p_recall(days_since_last_rehearsal: float, stability: float) -> float:
"""Exponential forgetting curve: p_recall = exp(-days / S)."""
if stability <= 0:
return 0.0
p = math.exp(-max(days_since_last_rehearsal, 0.0) / stability)
return round(p, 6)
def sample_recall_label(
p_recall: float,
partial_band: float,
rng: random.Random,
) -> RecallLabel:
u = rng.random()
if u < p_recall:
return "full"
if u < p_recall + partial_band:
return "partial"
return "forgotten"