Implement Engram context-aware gating, a mechanism inspired by memory engrams in neuroscience. The gate modulates a hidden state based on contextual input, selectively amplifying or suppressing features depending on the context signal.
import numpy as np
def sigmoid(x: np.ndarray) -> np.ndarray:
return 1.0 / (1.0 + np.exp(-np.clip(x, -500, 500)))
def engram_gating(
hidden: np.ndarray,
context: np.ndarray,
W_gate: np.ndarray,
b_gate: np.ndarray,
W_transform: np.ndarray,
b_transform: np.ndarray
) -> np.ndarray:
# Concatenate hidden and context
combined = np.concatenate([hidden, context], axis=-1)
# Compute gate values (0 to 1) via sigmoid
gate = sigmoid(combined @ W_gate + b_gate)
# Compute transformed context
transformed = np.tanh(context @ W_transform + b_transform)
# Gated output: blend hidden state with transformed context
output = gate * hidden + (1 - gate) * transformed
return output