-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmini_tf.py
More file actions
211 lines (144 loc) · 5.02 KB
/
mini_tf.py
File metadata and controls
211 lines (144 loc) · 5.02 KB
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import random
import numpy as np
MAX_SEED = 2 ** 32
float32 = 'float32'
class Graph:
def __init__(self):
self.reset()
def set_env(self, new_env):
self.env = new_env
def set_seed(self):
self.seed = random.randrange(MAX_SEED)
def add_variable(self, variable):
self.variables.append(variable)
def reset(self):
self.env = {}
self.set_seed()
self.variables = []
def as_default(self):
return GraphContext(self)
class GraphContext:
def __init__(self, graph):
self.graph = graph
def __enter__(self):
global _default_graph
self.prev_graph = get_default_graph()
_default_graph = self.graph
def __exit__(self, type, value, traceback):
global _default_graph
_default_graph = self.prev_graph
_default_graph = Graph()
def get_default_graph():
return _default_graph
class Op:
def __init__(self, name, shape, dtype):
self.name = name
if isinstance(shape, int):
shape = (shape,)
self.shape = tuple(shape)
self.dtype = dtype
def __add__(self, other):
if isinstance(other, Op):
return AddOp(self, other)
return AddOp(self, ConstantOp(other))
def __repr__(self):
return 'Tensor("{}", shape={}, dtype={})'.format(
self.name, self.shape, self.dtype)
class ConstantOp(Op):
def __init__(self, val, shape=None, dtype=None):
self.val = np.array(val)
super().__init__('Const', self.val.shape, self.val.dtype)
def evaluate(self):
return self.val
def constant(val, shape=(), dtype=float32):
return ConstantOp(val, shape, dtype)
class RandomUniformOp(Op):
def __init__(self, shape=None, dtype=None):
super().__init__('random_uniform', shape, dtype)
def evaluate(self):
np.random.seed(get_default_graph().seed)
return np.random.uniform(0, 1, self.shape)
def random_uniform(shape=(), dtype=float32):
return RandomUniformOp(shape, dtype)
class AddOp(Op):
def __init__(self, val1, val2):
super().__init__('add', val1.shape, val1.dtype)
self.val1 = val1
self.val2 = val2
def evaluate(self):
return self.val1.evaluate() + self.val2.evaluate()
class PlaceholderOp(Op):
def __init__(self, shape=None, dtype=None):
super().__init__('Placeholder', shape, dtype)
def evaluate(self):
return np.array(get_default_graph().env[self], dtype=self.dtype)
def placeholder(dtype, shape=()):
return PlaceholderOp(shape, dtype)
class Initializer():
def __init__(self, init_fn):
self.init_fn = init_fn
def set_var(self, variable):
self.variable = variable
def evaluate(self):
self.variable.assign(self.init_fn(self.variable))
def random_uniform_initializer():
return Initializer(
lambda v: RandomUniformOp(shape=v.shape).evaluate())
class VariableOp(Op):
def __init__(self, name, shape=None, dtype=None, initializer=None):
super().__init__(name, shape, dtype)
get_default_graph().add_variable(self)
self.initializer = initializer
if callable(self.initializer):
self.initializer = self.initializer()
self.initializer.set_var(self)
def assign(self, value):
self.value = value
def evaluate(self):
return self.value
def get_variable(
name, shape=(), dtype=float32,
initializer=random_uniform_initializer):
return VariableOp(name, shape, dtype, initializer)
def global_variables_initializer():
return [v.initializer for v in get_default_graph().variables]
class layers:
class Layer(Op):
def __init__(self, name, shape=None, dtype=None):
super().__init__(name, shape, dtype)
class Dense(Layer):
def __init__(self, units=1):
self.units = units
self.shape = (self.units,)
super().__init__('dense', shape=self.shape)
def __call__(self, inputs):
self.inputs = inputs
self.dtype = inputs.dtype
self.weights = get_variable(
'dense_weights', shape=(inputs.shape[-1], self.units))
self.biases = get_variable('dense_biases', shape=(self.units,))
return self
def evaluate(self):
W = self.weights.evaluate()
x = self.inputs.evaluate()
b = self.biases.evaluate()
return np.dot(x, W) + b
def evaluate(graph):
if isinstance(graph, tuple):
return tuple(evaluate(g) for g in graph)
if isinstance(graph, list):
return [evaluate(g) for g in graph]
return graph.evaluate()
class Session:
def __init__(self):
self.graph = get_default_graph()
def __enter__(self):
return self
def __exit__(self, type, value, traceback):
self.close()
def run(self, graph, feed_dict={}):
self.graph.reset()
self.graph.set_env(feed_dict)
return evaluate(graph)
def close(self):
self.graph.reset()