-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtf_api.py
132 lines (83 loc) · 2.82 KB
/
tf_api.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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import numpy as np
class Graph:
def __init__(self):
self.operations = []
self.placeholders = []
self.variables = []
self.constants = []
def as_default(self):
global _default_graph
_default_graph = self
class Operation:
def __init__(self, input_nodes=None):
self.input_nodes = input_nodes
_default_graph.operations.append(self)
def forward(self):
pass
def backward(self):
pass
class BinaryOperation(Operation):
def __init__(self, a, b):
super().__init__([a, b])
class add(BinaryOperation):
def forward(self, a, b):
return a + b
def backward(self, upstream_grad):
pass
class multiply(BinaryOperation):
def forward(self, a, b):
return a * b
def backward(self, upstream_grad):
pass
class divide(BinaryOperation):
def forward(self, a, b):
return np.true_divide(a, b)
def backward(self, upstream_grad):
pass
class matmul(BinaryOperation):
def forward(self, a, b):
return a.dot(b)
def backward(self, upstream_grad):
pass
class Placeholder:
def __init__(self):
self.value = None
_default_graph.placeholders.append(self)
class Constant:
def __init__(self, value=None):
self.__value = value
_default_graph.placeholders.append(self)
@property
def value(self):
return self.__value
@value.setter
def value(self, value):
raise ValueError("Cannot reassign value.")
class Variable:
def __init__(self, initial_value=None):
self.value = initial_value
_default_graph.variables.append(self)
def topology_sort(operation):
ordering = []
visited_nodes = set()
def recursive_helper(node):
if isinstance(node, Operation):
for input_node in node.input_nodes:
if input_node not in visited_nodes:
recursive_helper(input_node)
visited_nodes.add(node)
ordering.append(node)
recursive_helper(operation)
return ordering
class Session:
def run(self, operation, feed_dict={}):
nodes_sorted = topology_sort(operation)
for node in nodes_sorted:
if type(node) == Placeholder:
node.output = feed_dict[node]
elif type(node) == Variable or type(node) == Constant:
node.output = node.value
else:
inputs = [node.output for node in node.input_nodes]
node.output = node.forward(*inputs)
return operation.output