1

I'm trying to build my own distance function but I need it quite efficient so I hoped I could use numba. The problem is that it returns an error when I try to insert more than 1 input value. This code doesn't work and I cannot understand why:

import numba
import numpy as np
@numba.njit(parallel=True)
def distance(a,b):
   dist = np.zeros((len(a),len(b))
   for i in numba.prange(len(a)):
      for j in numba.prange(len(b)):
         dist[i,j] = np.linalg.norm(a[i]-b[j])
   return dist

The error I obtain is:

---------------------------------------------------------------------------
TypingError                               Traceback (most recent call last)
<ipython-input-18-9be257aa275d> in <module>
----> 1 dist = distance(a,b)

~/miniconda3/lib/python3.7/site-packages/numba/dispatcher.py in _compile_for_args(self, *args, **kws)
    374                 e.patch_message(msg)
    375 
--> 376             error_rewrite(e, 'typing')
    377         except errors.UnsupportedError as e:
    378             # Something unsupported is present in the user code, add help info

~/miniconda3/lib/python3.7/site-packages/numba/dispatcher.py in error_rewrite(e, issue_type)
    341                 raise e
    342             else:
--> 343                 reraise(type(e), e, None)
    344 
    345         argtypes = []

~/miniconda3/lib/python3.7/site-packages/numba/six.py in reraise(tp, value, tb)
    656             value = tp()
    657         if value.__traceback__ is not tb:
--> 658             raise value.with_traceback(tb)
    659         raise value
    660 

TypingError: Failed in nopython mode pipeline (step: nopython frontend)
Invalid use of Function(<function norm at 0x7f21b053add0>) with argument(s) of type(s): (array(int64, 1d, C))
 * parameterized
In definition 0:
    TypingError: np.linalg.norm() only supported on float and complex arrays.
    raised from /home/plongo/miniconda3/lib/python3.7/site-packages/numba/targets/linalg.py:847
In definition 1:
    TypingError: np.linalg.norm() only supported on float and complex arrays.
    raised from /home/plongo/miniconda3/lib/python3.7/site-packages/numba/targets/linalg.py:847
This error is usually caused by passing an argument of a type that is unsupported by the named function.
[1] During: resolving callee type: Function(<function norm at 0x7f21b053add0>)
[2] During: typing of call at <ipython-input-16-e3299481baaf> (6)


File "<ipython-input-16-e3299481baaf>", line 6:
def distance(a,b):
    <source elided>
        for j in numba.prange(len(b)):
            dist[i,j] = np.linalg.norm(a[i]-b[j])
            ^

This is not usually a problem with Numba itself but instead often caused by
the use of unsupported features or an issue in resolving types.

To see Python/NumPy features supported by the latest release of Numba visit:
http://numba.pydata.org/numba-doc/latest/reference/pysupported.html
and
http://numba.pydata.org/numba-doc/latest/reference/numpysupported.html

For more information about typing errors and how to debug them visit:
http://numba.pydata.org/numba-doc/latest/user/troubleshoot.html#my-code-doesn-t-compile

If you think your code should work with Numba, please report the error message
and traceback, along with a minimal reproducer at:
https://github.com/numba/numba/issues/new

I totally cannot understand what is wrong... even better would be to use CUDA but I get the same problem.. I think I missed some concept.

Thank you

talonmies
  • 70,661
  • 34
  • 192
  • 269
p. l.
  • 55
  • 4
  • 3
    `TypingError: np.linalg.norm() only supported on float and complex arrays.` what are the dtypes of `a` and `b`? Should you convert them to `float`? – Quang Hoang Nov 26 '19 at 15:48
  • Wow! Many thanks! They were just integers (built using np.arange(...)). I got scared by that large error! – p. l. Nov 26 '19 at 15:51
  • Write simple loops instead things like np.linalg.norm. Example of a custom distance function in Numba https://stackoverflow.com/a/58752553/4045774 – max9111 Nov 26 '19 at 15:55
  • [`scipy.spatial.distance.cdist`](https://docs.scipy.org/doc/scipy/reference/generated/scipy.spatial.distance.cdist.html) does exactly that. – jdehesa Nov 26 '19 at 16:13

0 Answers0