Rajesh Valluri
Rajesh Valluri

Reputation: 21

How do I run expit function on an array on a GPU?

I am trying to benchmark a simple neural network based handwritten digit recognition application. It currently uses Numpy for matrices, and scipy's expit function for activation. As good as this was ( pretty basic network really), I wanted to run this whole thing on GPU, and hence decided to use Cupy library.

Unfortunately, I am unable to get the expit function to run on GPU. I keep getting an error message stating "not implemented".

self.activation_function = lambda x: scipy.special.expit(x)

error message

TypeError: operand type(s) all returned NotImplemented from array_ufunc(<ufunc 'expit'>, 'call', array([[ 0.96079161], [ 1.37400426], [-0.46329254]])): 'ndarray'

Upvotes: 0

Views: 260

Answers (1)

Honghao Zhang
Honghao Zhang

Reputation: 11

I've just met the exactly same problem today, you could try defining functions with CuPy's User-Defined Kernels.

For the sigmoid function:

import cupy as cp

expit = cp.ElementwiseKernel(
        'float64 x',
        'float64 y',
        'y = 1 / (1 + exp(-x))',
        'expit')

>>> x = cp.arange(10, dtype=np.float64).reshape(2, 5)
>>> expit(x)
array([[0.5       , 0.73105858, 0.88079708, 0.95257413, 0.98201379],
       [0.99330715, 0.99752738, 0.99908895, 0.99966465, 0.99987661]])

>>> scipy.special.expit(x.get())
array([[0.5       , 0.7310586 , 0.880797  , 0.95257413, 0.98201376],
       [0.9933072 , 0.9975274 , 0.999089  , 0.99966466, 0.9998766 ]],
      dtype=float32)

Upvotes: 1

Related Questions