-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReLU.py
More file actions
32 lines (24 loc) · 780 Bytes
/
Copy pathReLU.py
File metadata and controls
32 lines (24 loc) · 780 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import numpy as np
def relu_forward(x):
"""Computes the forward pass for a layer of rectified linear units (ReLUs).
Input:
- x: Inputs, of any shape
Returns a tuple of:
- out: Output, of the same shape as x
- cache: x
"""
out = np.maximum(0,x)
cache = x
return out, cache
def relu_backward(dout, cache):
"""Computes the backward pass for a layer of rectified linear units (ReLUs).
Input:
- dout: Upstream derivatives, of any shape
- cache: Input x, of same shape as dout
Returns:
- dx: Gradient with respect to x
"""
dx = None
x = cache
dx = dout * (x > 0) # pass the upstream gradient through unchanged where x > 0, and zero it out where x ≤ 0.
return dx