From f182af0cdd7e6f0c7be7e897c45ec5bab168bebe Mon Sep 17 00:00:00 2001 From: aodhneine Date: Sun, 15 Nov 2020 21:52:34 +0000 Subject: [PATCH] Add element-wise division of tensors --- pygrad/tensor.py | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/pygrad/tensor.py b/pygrad/tensor.py index 21097b7..0c0e9e9 100644 --- a/pygrad/tensor.py +++ b/pygrad/tensor.py @@ -48,6 +48,17 @@ class Tensor: tensor._back = back return tensor + def div(self, other): + tensor = Tensor(self.value / other.value) + tensor._save(self, other) + + def back(upstream): + a, b = tensor._parents + return 1 / np.dot(b.value, upstream), -a.value / np.dot(b.value ** 2, upstream) + + tensor._back = back + return tensor + def expt(self, exponent): tensor = Tensor(self.value ** exponent) tensor._save(self)