Created
December 10, 2025 09:31
-
-
Save cgadski/2cbdf30b9a3e5bf091f6ca92e61bb8d3 to your computer and use it in GitHub Desktop.
module implementation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # forward(x) -> y | |
| # backward(dy, x) -> dx | |
| class Sigmoid(dl.Module): | |
| """ | |
| Will take x, return an array with the same shape and values | |
| 1 / (1 + e^(-x)) | |
| """ | |
| def forward(self, x): | |
| # takes x, returns something | |
| self.y = 1 / (1 + np.exp(-x)) | |
| return self.y | |
| def backward(self, dy): | |
| # I want to return dL/dx, where I am given dy = dL/dy, | |
| # and y = self.forward(x) | |
| # | |
| # I want to compute | |
| # dy / dx | |
| # where | |
| # y = 1 / (1 + exp(-x)) | |
| # because then | |
| # dL/dx = dy/dx dL/dy. | |
| return self.y * (1 - self.y) # this works because of a trick! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment