pyro icon indicating copy to clipboard operation
pyro copied to clipboard

[Feature Request] Better Errors when passing numpy array instead of torch.tensor

Open nipunbatra opened this issue 3 years ago • 0 comments

I was trying to write a small example for GP classification.

import pyro
import pyro.contrib.gp as gp
import numpy as np
import torch

X = torch.randn(200, 2)
Y = torch.logical_xor(X[:, 0] > 0, X[:, 1] > 0).double()

kernel = gp.kernels.RBF(input_dim=2)
likelihood = gp.likelihoods.Binary()
model = gp.models.VariationalGP(X, Y, kernel, likelihood=likelihood, whiten=True)

The above code works. However, as expected if instead of X = torch.tensor, I passed a numpy.arrayI got an error which wasn't very obvious.

rng = np.random.RandomState(0)

X = rng.randn(200, 2)

likelihood = gp.likelihoods.Binary()
model = gp.models.VariationalGP(X, Y, kernel, likelihood=likelihood, whiten=True)

Error Trace


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [5], in <module>
      3 X = rng.randn(200, 2)
      5 likelihood = gp.likelihoods.Binary()
----> 6 model = gp.models.VariationalGP(X, Y, kernel, likelihood=likelihood, whiten=True)

File ~/miniforge3/lib/python3.9/site-packages/pyro/contrib/gp/models/vgp.py:74, in VariationalGP.__init__(self, X, y, kernel, likelihood, mean_function, latent_shape, whiten, jitter)
     63 def __init__(
     64     self,
     65     X,
   (...)
     72     jitter=1e-6,
     73 ):
---> 74     super().__init__(X, y, kernel, mean_function, jitter)
     76     self.likelihood = likelihood
     78     y_batch_shape = self.y.shape[:-1] if self.y is not None else torch.Size([])

File ~/miniforge3/lib/python3.9/site-packages/pyro/contrib/gp/models/model.py:93, in GPModel.__init__(self, X, y, kernel, mean_function, jitter)
     91 def __init__(self, X, y, kernel, mean_function=None, jitter=1e-6):
     92     super().__init__()
---> 93     self.set_data(X, y)
     94     self.kernel = kernel
     95     self.mean_function = (
     96         mean_function if mean_function is not None else _zero_mean_function
     97     )

File ~/miniforge3/lib/python3.9/site-packages/pyro/contrib/gp/models/model.py:189, in GPModel.set_data(self, X, y)
    135 def set_data(self, X, y=None):
    136     """
    137     Sets data for Gaussian Process models.
    138 
   (...)
    187         number of data points.
    188     """
--> 189     if y is not None and X.size(0) != y.size(-1):
    190         raise ValueError(
    191             "Expected the number of input data points equal to the "
    192             "number of output data points, but got {} and {}.".format(
    193                 X.size(0), y.size(-1)
    194             )
    195         )
    196     self.X = X

TypeError: 'int' object is not callable

I think something like type(X) should be torch.tensor would perhaps be more useful?

nipunbatra avatar Feb 28 '22 09:02 nipunbatra