Skip to content

Instantly share code, notes, and snippets.

@cgadski
Created December 10, 2025 09:31
Show Gist options
  • Select an option

  • Save cgadski/2cbdf30b9a3e5bf091f6ca92e61bb8d3 to your computer and use it in GitHub Desktop.

Select an option

Save cgadski/2cbdf30b9a3e5bf091f6ca92e61bb8d3 to your computer and use it in GitHub Desktop.
module implementation
# 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