ReLU derivative with NumPy

懵懂的女人 提交于 2019-12-06 00:18:29

Your first mistake is in assuming python passes objects by value... it doesn't - it's pass by assignment (similar to passing by reference, if you're familiar with this concept). However, only mutable objects, as the name suggests, can be modified in-place. This includes, among other things, numpy arrays.

You shouldn't have d_relu modify z inplace, because that's what it's doing right now, through the z[...] = ... syntax. Try instead building a mask using broadcasted comparison and returning that instead.

def d_relu(z):
    return (z > 0).astype(int)

This returns a fresh array instead of modifying z in-place, and your code prints

y = [5 1 0 0]
z = [1 1 0 0]

If you're building a layered architecture, you can leverage the use of a computed mask during the forward pass stage:

class relu:
    def __init__(self):
        self.mask = None

    def forward(self, x):
        self.mask = x > 0
        return x * self.mask

    def backward(self, x):
        return self.mask

Where the derivative is simply 1 if the input during feedforward if > 0, else 0.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!