Typing Error: numba.cores.errors.TypingError: np.random.binomial <unknown function>(array(float64, 2d, C), float64)

Hello,
I’m trying to use Numba to speedup my code but i get this following error:

numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
No implementation of function Function(np.random.binomial) found for signature:
 
 >>> <unknown function>(array(float64, 2d, C), float64)
 
There are 2 candidate implementations:
   - Of which 2 did not match due to:
   Overload in function 'Numpy_negative_binomial.generic': File: numba\core\typing\randomdecl.py: Line 171.
     With argument(s): '(array(float64, 2d, C), float64)':
    No match for registered cases:
     * (int64, float64) -> int64

During: resolving callee type: Function(np.random.binomial)

def train(X, y, classes, words, hidden_neurons=10, alpha=1, epochs=50000, dropout=False, dropout_percent=0.5):
    <source elided>
            negative_dropout = 1 - dropout_percent
            binomial = np.random.binomial(arr, negative_dropout)
            ^

This is the code snippet providing the error:

for j in iter(range(epochs + 1)):

        # Feed forward through layers 0, 1, and 2
        layer_0 = X.astype(np.float64)
        layer_1 = sigmoid(np.dot(layer_0, synapse_0))

        if dropout:
            arr = np.ones((len(X), hidden_neurons))
            negative_dropout = 1 - dropout_percent
            binomial = np.random.binomial(arr, negative_dropout)
            layer_1 *= binomial[0] * (1.0 / negative_dropout)

        layer_2 = sigmoid(np.dot(layer_1, synapse_1))

        [...]

How do I solve this problem?

@LordMaddhi you should provide a testable example not just a code snippet. The supported numpy feature list is here: https://numba.pydata.org/numba-doc/dev/reference/numpysupported.html

Thank you for your answer. I cannot give a testable example cause this would be the whole project… I already checked the feature list and the functions I use are listed there.

Hi,

I think the error message gives a good hint at what is going wrong here:
The dtype of your array arr seems to be float64, but binomial needs the first argument to be of type int (this is even true for numpy itself, not just numba).

For numba it seems to me like binomial is only implemented for scalar n. So you may have to create a simple wrapper to pass arrays (loops are cheap in numba, so that is no problem).
Or you use the vectorize decorator for even more simplicity

from numba import vectorize
import numpy as np

@vectorize
def binom(n, p):
    return np.random.binomial(n, p)
1 Like

Thanks, that worked!