-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClassOfTensorflow.py
More file actions
322 lines (256 loc) · 10.5 KB
/
ClassOfTensorflow.py
File metadata and controls
322 lines (256 loc) · 10.5 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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
import numpy as np
import tensorflow as tf
import time
import sys
num_stddev = 0.09
class Timer(object):
def __init__(self, name):
self.Time_AllStart = time.time() * 1000
self.Time_End = 0
self.name = name
print("\n~ # %s 程式開始\n" % self.name)
return
def operation_time(self, deltaTime):
mHour = 0
mMin = 0
mS = 0
mMs = 0
if deltaTime > 3600000:
mHour = int(deltaTime / 3600000)
deltaTime = deltaTime % 3600000
if deltaTime > 60000:
mMin = int(deltaTime / 60000)
deltaTime = deltaTime % 60000
if deltaTime > 1000:
mS = int(deltaTime / 1000)
mMs = deltaTime % 1000
return [mHour, mMin, mS, mMs]
def now(self, remind=""):
self.Time_End = time.time() * 1000
deltaTime = float(self.Time_End - self.Time_AllStart)
timeList = self.operation_time(deltaTime)
if timeList[0] > 0:
print('\n~ # %s 已過時間:%d h, %d min, %d s, %d ms' % (
remind, timeList[0], timeList[1], timeList[2], timeList[3]))
elif timeList[1] > 0:
print('\n~ # %s 已過時間:%d h, %d min, %d s, %d ms' % (
remind, timeList[0], timeList[1], timeList[2], timeList[3]))
elif timeList[2] > 0:
print('\n~ # %s 已過時間:%d h, %d min, %d s, %d ms' % (
remind, timeList[0], timeList[1], timeList[2], timeList[3]))
else:
print('\n~ # %s 已過時間:%d h, %d min, %d s, %d ms' % (
remind, timeList[0], timeList[1], timeList[2], timeList[3]))
return
def end(self):
self.Time_End = time.time() * 1000
deltaTime = float(self.Time_End - self.Time_AllStart)
timeList = self.operation_time(deltaTime)
if timeList[0] > 0:
print('\n~ # %s 程式結束,時間共:%d h, %d min, %d s, %d ms' % (
self.name, timeList[0], timeList[1], timeList[2], timeList[3]))
elif timeList[1] > 0:
print('\n~ # %s 程式結束,時間共:%d h, %d min, %d s, %d ms' % (
self.name, timeList[0], timeList[1], timeList[2], timeList[3]))
elif timeList[2] > 0:
print('\n~ # %s 程式結束,時間共:%d h, %d min, %d s, %d ms' % (
self.name, timeList[0], timeList[1], timeList[2], timeList[3]))
else:
print('\n~ # %s 程式結束,時間共:%d h, %d min, %d s, %d ms' % (
self.name, timeList[0], timeList[1], timeList[2], timeList[3]))
return
class Constant(object):
def __init__(self, is1D):
self.size_image = 28
self.size_batch = 64
self.num_labels = 10
self.num_steps = 70001
self._keep_prob = [None, 0.7, 0.9, 0.9]
self.loss_beta_w = 0.0002
self.loss_beta_b = 0.00005
if type(is1D) == bool:
if is1D:
self.size_input = self.size_image * self.size_image
else:
print("'is1D' is not bool")
sys.exit()
return
def keep_prob(self, index):
return self._keep_prob[index - 1]
def w_var(shape):
with tf.name_scope('W'):
initial = tf.truncated_normal(shape, stddev=num_stddev)
return tf.Variable(initial)
def b_var(shape):
with tf.name_scope('B'):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv_2d(x, W):
with tf.name_scope('conv2d'):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
with tf.name_scope('maxPool'):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def W_conv_X_plus_b(inX, w, b, isRelu):
if type(isRelu) == bool:
# print("'isNeedRelu' is bool")
if isRelu:
with tf.name_scope('WcX_b_Relu'):
return tf.nn.relu(conv_2d(inX, w) + b)
elif not isRelu:
with tf.name_scope('WcX_b'):
return conv_2d(inX, w) + b
else:
print("'isNeedRelu' is not bool")
sys.exit()
def Wx_plus_b(inX, w, b, isRelu):
if type(isRelu) == bool:
# print("'isNeedRelu' is bool")
if isRelu:
with tf.name_scope('WX_b_Relu'):
return tf.nn.relu(tf.matmul(inX, w) + b)
elif not isRelu:
with tf.name_scope('WX_b'):
return tf.matmul(inX, w) + b
else:
print("'isNeedRelu' is not bool")
sys.exit()
'''def Layer(layer_num, input_tensor, input_dim, output_dim, isNeedRelu, isNeedConv, keep_prob):
def w_var(shape):
with tf.name_scope('W'):
initial = tf.truncated_normal(shape, stddev=0.1)
return tf.Variable(initial)
def b_var(shape):
with tf.name_scope('B'):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv_2d(x, W):
with tf.name_scope('conv2d'):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(x):
with tf.name_scope('maxPool'):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def W_conv_X_plus_b(inX, w, b, isRelu):
if type(isRelu) == bool:
# print("'isNeedRelu' is bool")
if isRelu:
with tf.name_scope('WcX_b_Relu'):
return tf.nn.relu(conv_2d(inX, w) + b)
elif not isRelu:
with tf.name_scope('WcX_b'):
return conv_2d(inX, w) + b
else:
print("'isNeedRelu' is not bool")
return None
def Wx_plus_b(inX, w, b, isRelu):
if type(isRelu) == bool:
# print("'isNeedRelu' is bool")
if isRelu:
with tf.name_scope('WX_b_Relu'):
return tf.nn.relu(tf.matmul(inX, w) + b)
elif not isRelu:
with tf.name_scope('WX_b'):
return tf.matmul(inX, w) + b
else:
print("'isNeedRelu' is not bool")
return None
def nn_layer(layerNum, inputT, inputS, outputS, isRelu, isConv, mKeep_prob):
layerName = "Layer%d" % layerNum
print(layerName)
if type(isRelu) == bool and type(isConv) == bool:
with tf.name_scope(layerName):
w = w_var([inputS, outputS])
b = b_var([outputS])
if mKeep_prob is not None:
if isNeedConv:
logits = W_conv_X_plus_b(inputT, w, b, isRelu)
else:
logits = Wx_plus_b(inputT, w, b, isRelu)
with tf.name_scope('dropout'):
logits_final = tf.nn.dropout(logits, mKeep_prob)
else:
if isNeedConv:
logits_final = W_conv_X_plus_b(inputT, w, b, isRelu)
else:
logits_final = Wx_plus_b(inputT, w, b, isRelu)
return logits_final
else:
return None
return nn_layer(layer_num, input_tensor, input_dim, output_dim, isNeedRelu, isNeedConv, keep_prob)'''
class Layer(object):
def __init__(self, layer_num, input_tensor, input_dim, output_dim, isNeedRelu, isNeedConv, keep_prob=None):
if type(layer_num) == int:
if layer_num > 0:
self.layerName = "Layer%02d" % layer_num
else:
print("'layer_num' need > 0")
sys.exit()
else:
print("'layer_num' is not int")
sys.exit()
self.w = None
self.b = None
self.layer = None
if type(isNeedRelu) == bool and type(isNeedConv) == bool:
if keep_prob is None \
or type(keep_prob) == float \
or type(keep_prob) == int:
with tf.name_scope(self.layerName):
self.w = self.w_var([input_dim, output_dim])
self.b = self.b_var([output_dim])
self.layer = self.nn_layer(layer_num, input_tensor,
input_dim, output_dim,
isNeedRelu, isNeedConv,
keep_prob)
else:
print("'keep_prob' is not None or int or float")
sys.exit()
else:
if type(isNeedRelu) != bool:
print("'isNeedRelu' is not bool")
if type(isNeedConv) != bool:
print("'isNeedConv' is not bool")
sys.exit()
return
def w_var(self, shape):
with tf.name_scope('W'):
initial = tf.truncated_normal(shape, stddev=num_stddev)
return tf.Variable(initial)
def b_var(self, shape):
with tf.name_scope('B'):
initial = tf.constant(0.1, shape=shape)
return tf.Variable(initial)
def conv_2d(self, x, W):
with tf.name_scope('conv2d'):
return tf.nn.conv2d(x, W, strides=[1, 1, 1, 1], padding='SAME')
def max_pool_2x2(self, x):
with tf.name_scope('maxPool'):
return tf.nn.max_pool(x, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding='SAME')
def W_conv_X_plus_b(self, inX, w, b, isRelu):
if isRelu:
with tf.name_scope('WcX_b_Relu'):
return tf.nn.relu(self.conv_2d(inX, w) + b)
elif not isRelu:
with tf.name_scope('WcX_b'):
return self.conv_2d(inX, w) + b
def Wx_plus_b(self, inX, w, b, isRelu):
if isRelu:
with tf.name_scope('WX_b_Relu'):
return tf.nn.relu(tf.matmul(inX, w) + b)
elif not isRelu:
with tf.name_scope('WX_b'):
return tf.matmul(inX, w) + b
def nn_layer(self, layerNum, inputT, inputS, outputS, isRelu, isConv, mKeep_prob):
if mKeep_prob is not None:
if isConv:
logits = self.W_conv_X_plus_b(inputT, self.w, self.b, isRelu)
else:
logits = self.Wx_plus_b(inputT, self.w, self.b, isRelu)
with tf.name_scope('dropout'):
logits_final = tf.nn.dropout(logits, mKeep_prob)
else:
if isConv:
logits_final = self.W_conv_X_plus_b(inputT, self.w, self.b, isRelu)
else:
logits_final = self.Wx_plus_b(inputT, self.w, self.b, isRelu)
return logits_final