Add tanh method to tensor
This commit is contained in:
parent
f182af0cdd
commit
f8101400c2
|
@ -92,6 +92,18 @@ class Tensor:
|
|||
tensor._back = back
|
||||
return tensor
|
||||
|
||||
def tanh(self):
|
||||
tensor = Tensor(np.tanh(self.value))
|
||||
tensor._save(self)
|
||||
|
||||
def back(upstream):
|
||||
# dtanh(x)/dx = 1 - tanh2(x)
|
||||
a, = tensor._parents
|
||||
return [1 - np.dot(np.tanh(a.value) ** 2, upstream)]
|
||||
|
||||
tensor._back = back
|
||||
return tensor
|
||||
|
||||
# TODO Compute gradients only for tensors that need it.
|
||||
def _backprop(self, upstream):
|
||||
# Backprop through the tensor iff it has any parents.
|
||||
|
|
Loading…
Reference in a new issue