Skip to content

Does def safe_divide really work? #67

@686llhhhhhh

Description

@686llhhhhhh
def safe_divide(a, b):
    den = b.clamp(min=1e-9) + b.clamp(max=1e-9)  # ???
    den = den + den.eq(0).type(den.type()) * 1e-9
    return a / den * b.ne(0).type(b.type())

and

b = torch.randn((1, 10))
print((b.clamp(min=1e-9) + b.clamp(max=1e-9)) == (b + 1e-9))
# output = tensor([[True, True, True, True, True, True, True, True, True, True]])

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions