Importance Sampling - Tinker Documentation

Importance Sampling

For RL, we implement a common variant of the policy gradient objective, used in practical settings where the learner policy ppp may differ from the sampling policy qqq, which is common due to, e.g., non-determinism. The issue is that if these policies differ, then the objective:

[ L(\theta)=\mathbb{E}{x\sim p{\theta}}\bigl[A(x)\bigr] ]

is not computed in an unbiased way due to x∼qx \sim q (sampler) not exactly matching the desired x∼pθx \sim p_{\theta} (learner). To correct the bias, we use a modified "importance sampling" objective:

[ L_{\text{IS}}(\theta)=\mathbb{E}{x\sim q}\Bigl[\frac{p{\theta}(x)}{q(x)}A(x)\Bigr] ]

which yields the correct expected reward. In the formula above:

This is implemented as:

# Compute probability ratio
prob_ratio = torch.exp(target_logprobs - sampling_logprobs)
# Compute importance-weighted loss
loss = -(prob_ratio * advantages).sum()

Input tensors:

Output tensors:

Output diagnostics: