geometric-kernels / GeometricKernels

Geometric kernels on manifolds, meshes and graphs
https://geometric-kernels.github.io/
Apache License 2.0
214 stars 18 forks source link

NotFoundLookupError: `cast(tensorflow.python.framework.dtypes.DType, numpy.ndarray)` could not be resolved. #132

Closed MBulli closed 5 months ago

MBulli commented 5 months ago

With the new version from yesterday, my code throws the following exception in MaternKarhunenLoeveKernel._spectrum():

NotFoundLookupError: `cast(tensorflow.python.framework.dtypes.DType, numpy.ndarray)` could not be resolved.

I'm using the code from the mesh example. With the old version (do not know the version) my code still worked. So I'm assuming that there might be a bug in the new framework code?

Stack trace, or error message

---------------------------------------------------------------------------
NotFoundLookupError                       Traceback (most recent call last)
Cell In[18], line 27
     20 # kernel.nu.assign(np.array(np.inf))
     21 # kernel.nu.assign(3/2.0)
     22 # kernel.lengthscale.assign(0.5)
     24 model = gpflow.models.GPR(
     25     (X, Y), kernel, mean_function=DefaultFloatZeroMeanFunction(), noise_variance=1.1e-6
     26 )
---> 27 print("LML", model.log_marginal_likelihood().numpy())
     29 X_test = np.arange(mesh.num_vertices).reshape(-1, 1)
     31 mean_prediction, variance_prediction = model.predict_f(X_test)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/check_shapes/integration/tf.py:76, in install_tf_integration.<locals>.TfWrapperPostProcessor.on_wrap.<locals>.wrapped_method(self, *args, **kwargs)
     75 def wrapped_method(self: Any, *args: Any, **kwargs: Any) -> Any:
---> 76     return wrapped_function(self, *args, **kwargs)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/check_shapes/decorator.py:192, in check_shapes.<locals>._check_shapes.<locals>.wrapped_function(*args, **kwargs)
    190 with set_shape_checker(checker):
    191     try:
--> 192         result = func(*args, **kwargs)
    193     except Exception:
    194         drop_stack_frame()

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/gpflow/models/gpr.py:100, in GPR_deprecated.log_marginal_likelihood(self)
     92 r"""
     93 Computes the log marginal likelihood.
     94 
   (...)
     97 
     98 """
     99 X, Y = self.data
--> 100 K = self.kernel(X)
    101 ks = add_likelihood_noise_cov(K, self.likelihood, X)
    102 L = tf.linalg.cholesky(ks)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/check_shapes/integration/tf.py:76, in install_tf_integration.<locals>.TfWrapperPostProcessor.on_wrap.<locals>.wrapped_method(self, *args, **kwargs)
     75 def wrapped_method(self: Any, *args: Any, **kwargs: Any) -> Any:
---> 76     return wrapped_function(self, *args, **kwargs)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/check_shapes/decorator.py:192, in check_shapes.<locals>._check_shapes.<locals>.wrapped_function(*args, **kwargs)
    190 with set_shape_checker(checker):
    191     try:
--> 192         result = func(*args, **kwargs)
    193     except Exception:
    194         drop_stack_frame()

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/gpflow/kernels/base.py:214, in Kernel.__call__(self, X, X2, full_cov, presliced)
    211     return self.K_diag(X)
    213 else:
--> 214     return self.K(X, X2)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/geometric_kernels/frontends/gpflow.py:121, in GPflowGeometricKernel.K(self, X, X2)
    119 variance = tf.convert_to_tensor(self.variance)
    120 params = dict(lengthscale=lengthscale, nu=nu)
--> 121 return variance * self.base_kernel.K(params, X, X2)

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/geometric_kernels/kernels/karhunen_loeve.py:199, in MaternKarhunenLoeveKernel.K(self, params, X, X2, **kwargs)
    196 assert "nu" in params
    197 assert params["nu"].shape == (1,)
--> 199 weights = B.cast(B.dtype(params["nu"]), self.eigenvalues(params))  # [L, 1]
    200 Phi = self.eigenfunctions
    201 K = Phi.weighted_outerproduct(weights, X, X2, **params)  # [N, N2]

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/geometric_kernels/kernels/karhunen_loeve.py:171, in MaternKarhunenLoeveKernel.eigenvalues(self, params, normalize)
    168 assert "nu" in params
    169 assert params["nu"].shape == (1,)
--> 171 spectral_values = self._spectrum(
    172     self.eigenvalues_laplacian,
    173     nu=params["nu"],
    174     lengthscale=params["lengthscale"],
    175 )
    176 normalize = normalize or (normalize is None and self.normalize)
    177 if normalize:

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/geometric_kernels/kernels/karhunen_loeve.py:117, in MaternKarhunenLoeveKernel._spectrum(self, s, nu, lengthscale)
    114 assert nu.shape == (1,)
    116 # Note: 1.0 in safe_nu can be replaced by any finite positive value
--> 117 safe_nu = B.where(nu == np.inf, B.cast(B.dtype(lengthscale), np.r_[1.0]), nu)
    119 # for nu == np.inf
    120 spectral_values_nu_infinite = B.exp(
    121     -(lengthscale**2) / 2.0 * B.cast(B.dtype(lengthscale), s)
    122 )

    [... skipping hidden 1 frame]

File ~/Downloads/geoflow/.venv/lib64/python3.11/site-packages/lab/util.py:214, in abstract.<locals>.decorator.<locals>.wrapper(*args, **kw_args)
    212 if types_before == types_after:
    213     signature = plum.signature.Signature(*types_after)
--> 214     raise plum.NotFoundLookupError(f.__name__, signature, [])
    216 # Retry call.
    217 return getattr(B, f.__name__)(*args, **kw_args)

NotFoundLookupError: `cast(tensorflow.python.framework.dtypes.DType, numpy.ndarray)` could not be resolved.

Expected behavior

No error at all.

System information

vabor112 commented 5 months ago

Hey @MBulli, thanks for reporting this! I think I know what the problem is. I will look into it later today or tomorrow and get back to you.

vabor112 commented 5 months ago

Could you please post the output of

pip show backends
MBulli commented 5 months ago

Sure!

Name: backends
Version: 1.6.3
Summary: A generic interface for linear algebra backends
Home-page: https://github.com/wesselb/lab
Author: Wessel Bruinsma
Author-email: wessel.p.bruinsma@gmail.com
License: MIT
Location: /home/markus/Downloads/geoflow/.venv/lib64/python3.11/site-packages
Requires: fdm, numpy, opt-einsum, plum-dispatch, scipy
Required-by: geometric_kernels
MBulli commented 5 months ago

btw is B.dtype(lengthscale) correct in the faulty line? Shouldn't it be B.dtype(nu)?

stoprightthere commented 5 months ago

Hi @MBulli, could you please provide a minimal reproducible example? It would help us debug the problem.

vabor112 commented 5 months ago

btw is B.dtype(lengthscale) correct in the faulty line? Shouldn't it be B.dtype(nu)?

Yes, we assume that params["nu"] and params["lengthscale"] dtypes are the same but also that lengthscale's dtype takes precedence if they are not.

I'm having trouble reproducing this, could you please post the full source code that leads to it?

BTW, did you do import geometric_kernels.tensorflow right after import geometric_kernels?

MBulli commented 5 months ago

BTW, did you do import geometric_kernels.tensorflow right after import geometric_kernels?

This fixed the problem! I was still using the imports from the old example notebooks. My bad, should have checked if the example code changed with the latest release, sorry. Thank you!