Skip to content

Commit

Permalink
Added sigmoid activation, support for disabling grad
Browse files Browse the repository at this point in the history
  • Loading branch information
Paul Ilioaica committed Sep 19, 2024
1 parent 12c40bc commit 2d2b9d3
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/activation_functions/relu.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ def forward(self, input):

def _build_backward_function(self, input, out):
def _backward():
input.grad += (out.value > 0) * out.grad
input.grad += (out.value > 0) * out.grad if out.requires_grad else 0
return _backward
14 changes: 14 additions & 0 deletions src/activation_functions/sigmoid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from activation_functions.activation import ActivationFunction
import math

class SigmoidActivation(ActivationFunction):
def _simgoid(self, x):
return 1 / (1 + math.exp(-x))

def forward(self, input):
return self._simgoid(input.value)

def _build_backward_function(self, input, out):
def _backward():
input.grad += out.grad * out.value * (1 - out.value) if out.requires_grad else 0
return _backward
20 changes: 10 additions & 10 deletions src/ops/basic_ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ def forward(self, a, b):

def _build_backward_function(self, a, b, out):
def _backward():
a.grad += out.grad
b.grad += out.grad
a.grad += out.grad if a.requires_grad else 0
b.grad += out.grad if b.requires_grad else 0
return _backward


Expand All @@ -19,8 +19,8 @@ def forward(self, a, b):

def _build_backward_function(self, a, b, out):
def _backward():
a.grad += b.value * out.grad
b.grad += a.value * out.grad
a.grad += b.value * out.grad if a.requires_grad else 0
b.grad += a.value * out.grad if b.requires_grad else 0
return _backward


Expand All @@ -30,8 +30,8 @@ def forward(self, a, b):

def _build_backward_function(self, a, b, out):
def _backward():
a.grad += out.grad
b.grad -= out.grad
a.grad += out.grad if a.requires_grad else 0
b.grad -= out.grad if b.requires_grad else 0
return _backward

class PowOperation(Operation):
Expand All @@ -40,8 +40,8 @@ def forward(self, a, b):

def _build_backward_function(self, a, b, out):
def _backward():
a.grad += b.value * (a.value ** (b.value - 1)) * out.grad
b.grad += (a.value ** b.value) * math.log(a.value) * out.grad
a.grad += b.value * (a.value ** (b.value - 1)) * out.grad if a.requires_grad else 0
b.grad += (a.value ** b.value) * math.log(a.value) * out.grad if b.requires_grad else 0
return _backward

class DivOperation(Operation):
Expand All @@ -50,7 +50,7 @@ def forward(self, a, b):

def _build_backward_function(self, a, b, out):
def _backward():
a.grad += (1/b.value) * out.grad
b.grad -= (a.value / (b.value **2)) * out.grad
a.grad += (1/b.value) * out.grad if a.requires_grad else 0
b.grad -= (a.value / (b.value **2)) * out.grad if b.requires_grad else 0
return _backward

3 changes: 2 additions & 1 deletion src/variable/variable.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from ops.basic_ops import *

class Variable:
def __init__(self, value, _children=(), _op=None, _name=None) -> None:
def __init__(self, value, _children=(), _op=None, _name=None, requires_grad=True) -> None:
self.value = value
self.grad = 0
self._op = _op
self._prev = set(_children)
self._backward = lambda: None
self._name = _name
self.requires_grad = requires_grad

def __add__(self, other):
return self._apply(AddOperation(), other)
Expand Down

0 comments on commit 2d2b9d3

Please sign in to comment.