Skip to content

torch.distributions.uniform.Uniform.cdf() can return negative values or values above one #7532

@andreh7

Description

@andreh7

Issue description

torch.distributions.uniform.Uniform.cdf() can return negative values or values above one because it does not do a range check like log_prob() in the same class does.

Relevant code in cdf():

def cdf(self, value):
if self._validate_args:
self._validate_sample(value)
result = (value - self.low) / (self.high - self.low)
return result

Range checking in log_prob():

def log_prob(self, value):
if self._validate_args:
self._validate_sample(value)
lb = value.ge(self.low).type_as(self.low)
ub = value.lt(self.high).type_as(self.low)
return torch.log(lb.mul(ub)) - torch.log(self.high - self.low)

I can provide a pull request based on the range checking as done in the log_prob() method of the same class (although log_prob() throws an exception with scalar arguments because of these range checks while cdf() accepts scalar arguments).

Code example

import torch
from torch.distributions.uniform import Uniform
uniform = Uniform(1,2)
print uniform.cdf(0), uniform.cdf(5)

will give

tensor(-1.) tensor(4.)

System Info

Collecting environment information...
PyTorch version: 0.4.0
Is debug build: No
CUDA used to build PyTorch: Could not collect

OS: Mac OSX 10.13.4
GCC version: Could not collect
CMake version: version 3.11.0

Python version: 2.7
Is CUDA available: No
CUDA runtime version: 9.1.128
GPU models and configuration: Could not collect
Nvidia driver version: Could not collect
cuDNN version: Could not collect

Versions of relevant libraries:
[pip] numpy (1.14.2)
[pip] root-numpy (4.4.0)
[pip] torch (0.4.0)
[pip] torchvision (0.2.1)
[conda] Could not collect

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