THE CODE READER / PYTHON

example-9bcc1aad30bb.py

Your snippet, in context. Explore the file or visualize its recorded example.

Download raw .py ↓
Complete file
PYTHON / LINE NUMBERS
 1import torch
 2
 3# A toy edge-position distribution, not D-FINE's full decoder.
 4bins = torch.linspace(0.0, 1.0, steps=5)
 5logits = torch.tensor([[-2.0, -0.5, 2.0, 1.0, -2.0]])
 6probabilities = logits.softmax(dim=-1)
 7coordinate = (probabilities * bins).sum(dim=-1)
 8
 9assert coordinate.shape == (1,)
10assert 0.0 <= coordinate.item() <= 1.0
11print(round(coordinate.item(), 3))  # 0.548