Skip to content

[Feature request] unique operation #2031

@brenoarosa

Description

@brenoarosa

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.data

I 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions