CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
y33-j3T

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.

GitHub Repository: y33-j3T/Coursera-Deep-Learning
Path: blob/master/Custom Models, Layers, and Loss Functions with TensorFlow/Week 1 - Functional APIs/utils.py
Views: 13371
1
import numpy as np
2
import tensorflow as tf
3
from tensorflow.keras.models import Model
4
from tensorflow.keras.layers import Dense, Input
5
from sklearn.model_selection import train_test_split
6
7
def test_loop(test_cases):
8
9
success = 0
10
fails = 0
11
12
for test_case in test_cases:
13
try:
14
assert test_case["result"] == test_case["expected"]
15
success += 1
16
17
except:
18
fails += 1
19
print(f'{test_case["name"]}: {test_case["error_message"]}\nExpected: {test_case["expected"]}\nResult: {test_case["result"]}\nPlease open utils.py if you want to see the unit test here.\n')
20
21
if fails == 0:
22
print("\033[92m All public tests passed")
23
24
else:
25
print('\033[92m', success," Tests passed")
26
print('\033[91m', fails, " Tests failed")
27
raise Exception(test_case["error_message"])
28
29
def test_white_df(white_df):
30
31
test_cases = [
32
{
33
"name": "type_check",
34
"result": type(white_df.is_red[0]),
35
"expected": np.int64,
36
"error_message": f'white_df.is_red has an incorrect type.'
37
},
38
{
39
"name": "output_check",
40
"result": white_df.is_red[0],
41
"expected": 0,
42
"error_message": "white_df.is_red is not set correctly"
43
},
44
{
45
"name": "len_check",
46
"result": len(white_df),
47
"expected": 3961,
48
"error_message": "Number of rows is incorrect. Please drop duplicates."
49
}
50
]
51
52
test_loop(test_cases)
53
54
def test_red_df(red_df):
55
56
test_cases = [
57
{
58
"name": "type_check",
59
"result": type(red_df.is_red[0]),
60
"expected": np.int64,
61
"error_message": f'red_df.is_red has an incorrect type.'
62
},
63
{
64
"name": "output_check",
65
"result": red_df.is_red[0],
66
"expected": 1,
67
"error_message": "red_df.is_red is not set correctly"
68
},
69
{
70
"name": "len_check",
71
"result": len(red_df),
72
"expected": 1359,
73
"error_message": "Number of rows is incorrect. Please drop duplicates."
74
}
75
]
76
77
test_loop(test_cases)
78
79
def test_df_drop(df):
80
81
test_cases = [
82
{
83
"name": "df.alcohol[0]_check",
84
"result": df.alcohol[0],
85
"expected": 9.4,
86
"error_message": f'Value is not as expected. Please check quality interval.'
87
},
88
{
89
"name": "df.alcohol[100]_check",
90
"result": df.alcohol[100],
91
"expected": 10.9,
92
"error_message": f'Value is not as expected. Please check quality interval.'
93
}
94
]
95
96
test_loop(test_cases)
97
98
def test_data_sizes(train_size, test_size, val_size):
99
100
test_cases = [
101
{
102
"name": "train_test_size_check",
103
"result": train_size > test_size,
104
"expected": True,
105
"error_message": f'train.size is too small. Please check implementation.'
106
},
107
{
108
"name": "train_val_size_check",
109
"result": train_size > val_size,
110
"expected": True,
111
"error_message": f'train.size is too small. Please check implementation.'
112
},
113
{
114
"name": "test_val_size_check",
115
"result": test_size > val_size,
116
"expected": True,
117
"error_message": f'test.size is too small. Please check implementation.'
118
}
119
]
120
121
test_loop(test_cases)
122
123
def test_format_output(df, train_Y, val_Y, test_Y):
124
125
train, test = train_test_split(df, test_size=0.2, random_state=1)
126
train, val = train_test_split(train, test_size=0.2, random_state=1)
127
128
test_cases = [
129
{
130
"name": "train_Y[0]_check",
131
"result": np.all(train_Y[0] == np.array(train.quality)),
132
"expected": True,
133
"error_message": f'train_Y[0] is not equal to train.quality. Please check implementation.'
134
},
135
{
136
"name": "train_Y[1]_check",
137
"result": np.all(train_Y[1] == np.array(train.is_red)),
138
"expected": True,
139
"error_message": f'train_Y[1] is not equal to train.is_red. Please check implementation.'
140
},
141
{
142
"name": "val_Y[0]_check",
143
"result": np.all(val_Y[0] == np.array(val.quality)),
144
"expected": True,
145
"error_message": f'train_Y[0] is not equal to val.quality. Please check implementation.'
146
},
147
{
148
"name": "val_Y[1]_check",
149
"result": np.all(val_Y[1] == np.array(val.is_red)),
150
"expected": True,
151
"error_message": f'train_Y[1] is not equal to val.is_red. Please check implementation.'
152
},
153
{
154
"name": "test_Y[0]_check",
155
"result": np.all(test_Y[0] == np.array(test.quality)),
156
"expected": True,
157
"error_message": f'test_Y[0] is not equal to test.quality. Please check implementation.'
158
},
159
{
160
"name": "test_Y[1]_check",
161
"result": np.all(test_Y[1] == np.array(test.is_red)),
162
"expected": True,
163
"error_message": f'test_Y[1] is not equal to test.is_red. Please check implementation.'
164
}
165
]
166
167
test_loop(test_cases)
168
169
def test_norm(norm_train_X, norm_val_X, norm_test_X, train, val, test):
170
171
from pandas.core.frame import DataFrame
172
173
test_cases = [
174
{
175
"name": "norm_train_X_type_check",
176
"result": type(norm_train_X),
177
"expected": DataFrame,
178
"error_message": f'norm_train_X has an incorrect type.'
179
},
180
{
181
"name": "norm_val_X_type_check",
182
"result": type(norm_val_X),
183
"expected": DataFrame,
184
"error_message": f'norm_val_X has an incorrect type.'
185
},
186
{
187
"name": "norm_test_X_type_check",
188
"result": type(norm_test_X),
189
"expected": DataFrame,
190
"error_message": f'norm_test_X has an incorrect type.'
191
},
192
{
193
"name": "norm_train_X_length_check",
194
"result": len(norm_train_X),
195
"expected": len(train),
196
"error_message": f'norm_train_X has an incorrect length.'
197
},
198
{
199
"name": "norm_val_X_length_check",
200
"result": len(norm_val_X),
201
"expected": len(val),
202
"error_message": f'norm_val_X has an incorrect length.'
203
},
204
{
205
"name": "norm_test_X_length_check",
206
"result": len(norm_test_X),
207
"expected": len(test),
208
"error_message": f'norm_test_X has an incorrect length.'
209
},
210
]
211
212
test_loop(test_cases)
213
214
def test_base_model(base_model):
215
216
test_inputs = tf.keras.layers.Input(shape=(11,))
217
test_output = base_model(test_inputs)
218
test_model = Model(inputs=test_inputs, outputs=test_output)
219
220
test_cases = [
221
{
222
"name": "return_type_check",
223
"result": type(test_output),
224
"expected": tf.Tensor,
225
"error_message": 'Return type is incorrect. Please check implementation.'
226
},
227
{
228
"name": "return_shape_check",
229
"result": str(test_output.shape),
230
"expected": '(None, 128)',
231
"error_message": 'Return shape is incorrect. Please check implementation.'
232
},
233
{
234
"name": "tensor_dtype_check",
235
"result": str(test_output.dtype),
236
"expected": "<dtype: 'float32'>",
237
"error_message": 'model dtype is incorrect. Please check implementation.'
238
},
239
{
240
"name": "base_model_num_layers_check",
241
"result": len(test_model.layers),
242
"expected": 3,
243
"error_message": 'There are too many layers. Please check implementation.'
244
},
245
{
246
"name": "base_model_layer1_check",
247
"result": type(test_model.layers[-2]),
248
"expected": Dense,
249
"error_message": 'First layer type is incorrect. Please check implementation.'
250
},
251
{
252
"name": "base_model_layer2_check",
253
"result": type(test_model.layers[-1]),
254
"expected": Dense,
255
"error_message": 'Output layer type is incorrect. Please check implementation.'
256
},
257
]
258
259
test_loop(test_cases)
260
261
def test_final_model(final_model):
262
263
test_inputs = tf.keras.layers.Input(shape=(11,))
264
test_output = final_model(test_inputs)
265
266
test_cases = [
267
{
268
"name": "return_type_check",
269
"result": type(test_output),
270
"expected": tf.keras.Model,
271
"error_message": 'Return type is incorrect. Please check implementation.'
272
},
273
{
274
"name": "layer_3_activation_check",
275
"result": test_output.layers[4].activation,
276
"expected": tf.keras.activations.sigmoid,
277
"error_message": 'wine_quality layer has an incorrect activation. Please check implementation.'
278
},
279
]
280
281
test_loop(test_cases)
282
283
def test_model_compile(model):
284
285
from tensorflow.python.keras.metrics import MeanMetricWrapper
286
287
test_cases = [
288
{
289
"name": "metrics_0_check",
290
"result": type(model.metrics[0]),
291
"expected": tf.keras.metrics.RootMeanSquaredError,
292
"error_message": 'wine quality metrics is incorrect. Please check implementation.'
293
},
294
{
295
"name": "metrics_1_check",
296
"result": (model.metrics[1].name == 'wine_type_accuracy') or
297
(model.metrics[1].name == 'wine_type_binary_accuracy'),
298
"expected": True,
299
"error_message": f'wine type metrics: {model.metrics[1].name} is incorrect. Please check implementation.'
300
},
301
{
302
"name": "wine_type_loss_check",
303
"result": (model.loss['wine_type'] == 'binary_crossentropy') or
304
(model.loss['wine_type'].name == 'binary_crossentropy') or
305
(str(model.loss['wine_type']).split()[1] == 'binary_crossentropy'),
306
"expected": True,
307
"error_message": f'wine type loss: {model.loss["wine_type"]} is incorrect. Please check implementation.'
308
},
309
{
310
"name": "wine_quality_loss_check",
311
"result": (model.loss['wine_quality'] in ['mse', 'mean_squared_error']) or
312
(str(model.loss['wine_quality']).split()[1] == 'mean_squared_error') or
313
(model.loss['wine_quality'].name == 'mean_squared_error'),
314
"expected": True,
315
"error_message": f'wine quality loss: {model.loss["wine_type"]} is incorrect. Please check implementation.'
316
},
317
]
318
319
test_loop(test_cases)
320
321
def test_history(history):
322
323
vars_history = vars(history)
324
325
test_cases = [
326
{
327
"name": "type_check",
328
"result": type(history),
329
"expected": tf.keras.callbacks.History,
330
"error_message": 'history type is incorrect. Please check model.fit().'
331
},
332
{
333
"name": "params_samples_check",
334
"result": vars_history['params']['samples'],
335
"expected": 3155,
336
"error_message": 'Training samples is incorrect. Please check arguments to model.fit().'
337
},
338
{
339
"name": "params_do_validation_check",
340
"result": vars_history['params']['do_validation'],
341
"expected": True,
342
"error_message": 'No validation data is present. Please check arguments to model.fit().'
343
},
344
]
345
346
test_loop(test_cases)
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383