-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathoperations.py
54 lines (37 loc) · 1.05 KB
/
operations.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
"""
Naive operations with forwards and backwards numpy functions
"""
import numpy as np
class Operation(object):
def forwards(self, *args):
yield NotImplementedError('Abstract class')
def backwards(self, *args):
yield NotImplementedError('Abstract class')
class Sum(Operation):
# noinspection PyAttributeOutsideInit
def forwards(self, x):
self.x = x
return np.sum(x)
def backwards(self):
return np.ones_like(self.x)
class Dot(Operation):
# noinspection PyAttributeOutsideInit
def forwards(self, x, a):
self.a = a
return np.dot(a, x)
def backwards(self):
return self.a
class Reciprocal(Operation):
# noinspection PyAttributeOutsideInit
def forwards(self, x):
self.x = x
return 1 / x
def backwards(self):
return - 1 / np.square(self.x)
class Exp(Operation):
# noinspection PyAttributeOutsideInit
def forwards(self, x):
self.x = x
return np.exp(x)
def backwards(self):
return np.exp(self.x)