A numpy vectorized function doesn't return the expected result

Question:

First I define a relu function and vectorize it.

Then I feed an arbitrary list into this relu function, but it returns the wrong result, because the value of relu(1.5) should be 1.5.

The code is as follows:

import numpy as np

def relu(x):
    return x if x > 0 else 0

relu = np.vectorize(relu)

print(relu([-3,-1.5,0,1.5,3]))
# result: array([0, 0, 0, 1, 3])

Could you please explain to me why this happens?

Asked By: Yuex

||

Answers:

Because vectorize assumes the type from the first element unless output type is specified. Use

relu = np.vectorize(relu,otypes=[float])
Answered By: user19077881

I thought I should add that np.vectorize is not true vectorization, its basically a for loop.

The vectorize function is provided primarily for convenience, not for
performance. The implementation is essentially a for loop.

You should use this instead:

def relu(x):
    return np.maximum(x, 0)

Timings:

Your method:
%%timeit
relu([-3,-1.5,0,1.5,3])
27.1 µs ± 317 ns per loop (mean ± std. dev. of 7 runs, 10000 loops each)

This method:
%%timeit
relu([-3,-1.5,0,1.5,3])
3.19 µs ± 14.7 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
Answered By: Tom McLean
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.