Masked Softmax Layers in PyTorch
mcognetta.github.io·13w·
Discuss: Hacker News

Neural classification models typically have a softmax layer as the last step of their model, which turns unnormalized log probabilities (logits) that are produced by the model into a normalized probability distribution over the output classes. Let ℓ\ell be a vector of dimension nn (the number of classes), where ℓi\ell_i represents the logit for class ii. Then, for a given class ii, softmax computes the probability:

pi=softmax(ℓ)i=eℓi∑jneℓj p_i = softmax(\ell)_i = \frac{e^{\ell_i}}{\sum_{j}ne{\ell_j}}

In PyTorch, this is implemented as:

import torch
ell = torch.normal(0., 1., size=(5,)) # -> [1.2355, -0.1710, -0.6606, -0.2050, -1.4690]
torch.softmax(ell, dim = 0)           # -> [0.5886, 0.1442, 0.0884, 0.1394, 0.0394]

However, a side effect of this…

Similar Posts

Loading similar posts...

Keyboard Shortcuts

Navigation
Next / previous item
j/k
Open post
oorEnter
Preview post
v
Post Actions
Love post
a
Like post
l
Dislike post
d
Undo reaction
u
Recommendations
Add interest / feed
Enter
Not interested
x
Go to
Home
gh
Interests
gi
Feeds
gf
Likes
gl
History
gy
Changelog
gc
Settings
gs
Browse
gb
Search
/
General
Show this help
?
Submit feedback
!
Close modal / unfocus
Esc

Press ? anytime to show this help