Commit a49e768a authored by Elif Ceylan's avatar Elif Ceylan
Browse files

GROUPWORK_minor corrections for tanh

parent 2689acf9
......@@ -169,11 +169,11 @@ class Tanh(Module):
def forward(self, input):
self.x = input
return torch.tanh(self.x)
# return (self.x.exp() - self.x.mul(-1).exp()) / (self.x.exp() + self.x.mul(-1).exp())
self.x = torch.tanh(self.x) # (self.x.exp() - self.x.mul(-1).exp()) / (self.x.exp() + self.x.mul(-1).exp())
return self.x
def backward(self, gradwrtoutput):
# gradient of tanh = ((1 - tanh^2(x)) * gradwrtoutput)
# gradient of tanh = 1 - tanh^2(x)
return (1 - self.forward(self.x).pow(2)) * gradwrtoutput
def param(self):
......@@ -226,7 +226,7 @@ class BCE(Module):
def forward(self, y, t):
# BCE = - sum(y * log(t) + (1 - y) * log(1 - t))
# clamp log in order to avoid inf values (just as PyTorch implements BCE, but with min=-100)
return -1 * torch.sum(y*torch.clamp(torch.log(t.float()), min=-1) + (1-y)*torch.clamp(torch.log(1-t.float()), min=-1))
return -torch.sum(y*torch.clamp(torch.log(t.float()), min=-1) + (1-y)*torch.clamp(torch.log(1-t.float()), min=-1))
def backward(self, y, t):
# gradient of BCE = mean(y - t)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment