How to implement the ReLU function in Numpy

后端 未结 9 1044
野性不改
野性不改 2020-12-02 09:53

I want to make a simple neural network which uses the ReLU function. Can someone give me a clue of how can I implement the function using numpy.

9条回答
  •  执念已碎
    2020-12-02 10:21

    EDIT As jirassimok has mentioned below my function will change the data in place, after that it runs a lot faster in timeit. This causes the good results. It's some kind of cheating. Sorry for your inconvenience.

    I found a faster method for ReLU with numpy. You can use the fancy index feature of numpy as well.

    fancy index:

    20.3 ms ± 272 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)

    >>> x = np.random.random((5,5)) - 0.5 
    >>> x
    array([[-0.21444316, -0.05676216,  0.43956365, -0.30788116, -0.19952038],
           [-0.43062223,  0.12144647, -0.05698369, -0.32187085,  0.24901568],
           [ 0.06785385, -0.43476031, -0.0735933 ,  0.3736868 ,  0.24832288],
           [ 0.47085262, -0.06379623,  0.46904916, -0.29421609, -0.15091168],
           [ 0.08381359, -0.25068492, -0.25733763, -0.1852205 , -0.42816953]])
    >>> x[x<0]=0
    >>> x
    array([[ 0.        ,  0.        ,  0.43956365,  0.        ,  0.        ],
           [ 0.        ,  0.12144647,  0.        ,  0.        ,  0.24901568],
           [ 0.06785385,  0.        ,  0.        ,  0.3736868 ,  0.24832288],
           [ 0.47085262,  0.        ,  0.46904916,  0.        ,  0.        ],
           [ 0.08381359,  0.        ,  0.        ,  0.        ,  0.        ]])
    

    Here is my benchmark:

    import numpy as np
    x = np.random.random((5000, 5000)) - 0.5
    print("max method:")
    %timeit -n10 np.maximum(x, 0)
    print("max inplace method:")
    %timeit -n10 np.maximum(x, 0,x)
    print("multiplication method:")
    %timeit -n10 x * (x > 0)
    print("abs method:")
    %timeit -n10 (abs(x) + x) / 2
    print("fancy index:")
    %timeit -n10 x[x<0] =0
    
    max method:
    241 ms ± 3.53 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    max inplace method:
    38.5 ms ± 4 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    multiplication method:
    162 ms ± 3.1 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    abs method:
    181 ms ± 4.18 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)
    fancy index:
    20.3 ms ± 272 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
    

提交回复
热议问题