-
Notifications
You must be signed in to change notification settings - Fork 26.3k
Closed
Description
Hi,
I'm missing an operation like tensorflow's unique.
I'm trying to build minibatch weights for very skewed datasets.
My current code looks like the following snippet.
def get_loss_weights(target, num_classes=2, eps=1e-08):
"""
Gets loss weights by counting target classes examples
To help with skewed datasets we need to weight the gradient.
Each class need to have the same influence in gradient.
So, each example will have weight equal to 1/num_classes * 1/class_count
Usage:
>>> tensor = Variable(torch.FloatTensor([1, 0, 0, 0]))
>>> get_loss_weights(tensor)
0.5000
0.1667
0.1667
0.1667
[torch.FloatTensor of size 4]
"""
weights = Variable(torch.zeros(target.data.size()))
for cls in range(num_classes):
cls_members = (target.data == cls).float()
weights += Variable(1 / (torch.sum(cls_members) + eps) * cls_members)
weights /= num_classes
return weights.dataI would like to build a more robust approach using unique operation.
I'm happy to hear different approaches or optimizations.
Thanks for the package!
Edit:
For anyone interested, the code to compute samples weights as sent as PR #2549
chenzhekl, bishesh, vadimkantorov, d1ff, ai-bites and 13 moremgarbade
Metadata
Metadata
Assignees
Labels
No labels