Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove Sigmoid from function.py [pr] #620

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions tinygrad/function.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,17 +67,6 @@ def forward(self, x:UOp) -> UOp:

def backward(self, grad_output:UOp) -> UOp: return grad_output / (self.ret*2)

# NOTE: the implicit derivative of sigmoid is not stable
# https://towardsdatascience.com/derivative-of-the-sigmoid-function-536880cf918e
# TODO: have the backend automatically find this
class Sigmoid(Function):
def forward(self, x:UOp) -> UOp:
self.ret = (1 + (x * (-1/math.log(2))).exp2()).reciprocal()
return self.ret

def backward(self, grad_output:UOp) -> UOp:
return (self.ret * (1 - self.ret)) * grad_output

class Sign(Function):
# NOTE: the x*0 is to match torch behavior without function.py
def forward(self, x:UOp) -> UOp: return x.ne(0).where((x<0).where(x.const_like(-1), x.const_like(1)), x.const_like(0)) + x*0
Expand Down
5 changes: 4 additions & 1 deletion tinygrad/tensor.py
Original file line number Diff line number Diff line change
Expand Up @@ -2477,6 +2477,7 @@ def exp2(self):
```
"""
return F.Exp.apply(self*math.log(2))

def relu(self):
"""
Applies the Rectified Linear Unit (ReLU) function element-wise.
Expand All @@ -2488,6 +2489,7 @@ def relu(self):
```
"""
return F.Relu.apply(self)

def sigmoid(self):
"""
Applies the Sigmoid function element-wise.
Expand All @@ -2498,7 +2500,8 @@ def sigmoid(self):
print(Tensor([-3., -2., -1., 0., 1., 2., 3.]).sigmoid().numpy())
```
"""
return F.Sigmoid.apply(self.cast(least_upper_float(self.dtype)))
return (1 + (self * (-1/math.log(2))).exp2()).reciprocal()

def hardsigmoid(self, alpha:float=1/6, beta:float=0.5):
"""
Applies the Hardsigmoid function element-wise.
Expand Down
Loading