Skip to content

Instantly share code, notes, and snippets.

@cjams
Created December 31, 2025 19:46
Show Gist options
  • Select an option

  • Save cjams/e02cd8bf2aa86e56a7028c3811047b55 to your computer and use it in GitHub Desktop.

Select an option

Save cjams/e02cd8bf2aa86e56a7028c3811047b55 to your computer and use it in GitHub Desktop.
Bengio sampling
story = ‘’
ctx = [0] * ctx_window # start with context full of “special” characters
while True:
x = torch.tensor([ctx], device=device)
for layer in model:
x = layer(x)
counts = x.exp()
probs = counts / counts.sum(dim=1, keepdim=True)
i = torch.multinomial(probs, num_samples=1, replacement=True, generator=g).item()
s = itos[i]
story += s
ctx = ctx[1:] + [i]
if i == 0:
break
print(f”Story time: {story}”)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment