CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
amanchadha

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: amanchadha/coursera-deep-learning-specialization
Path: blob/master/C5 - Sequence Models/Week 1/Jazz improvisation with LSTM/generateTestCases.py
Views: 4819
1
# New Generate Test Cases
2
from .solutions import *
3
import numpy as np
4
np.random.seed(3)
5
6
import math
7
8
from tensorflow.keras.optimizers import Adam
9
from tensorflow.keras.layers import Dense, LSTM, Reshape
10
11
# import copy
12
# from keras.callbacks import History
13
# import tensorflow as tf
14
15
16
#from grader_support import stdout_redirector
17
#from grader_support import util
18
19
os.environ['TF_CPP_MIN_LOG_LEVEL']='3'
20
21
# with suppress_stdout_stderr():
22
n_a = 64
23
n_values = 90
24
LSTM_cell = LSTM(n_a, return_state=True) # Used in Step 2.C
25
densor = Dense(n_values, activation='softmax') # Used in Step 2.D
26
x_initializer = np.zeros((1, 1, 90))
27
a_initializer = np.zeros((1, n_a))
28
c_initializer = np.zeros((1, n_a))
29
reshapor = Reshape((1, n_values))
30
31
# ================================================================================================
32
# generating the test cases for dj model
33
'''
34
def djmodel_gen():
35
m = 60
36
a0 = np.zeros((m, n_a))
37
c0 = np.zeros((m, n_a))
38
djmodelx = djmodel(Tx = 30 , LSTM_cell=LSTM_cell, densor=densor, reshapor=reshapor)
39
opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)
40
djmodelx.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
41
cp = djmodelx.count_params()
42
ml = len(djmodelx.layers)
43
print(cp, ml)
44
return (cp, ml)
45
46
# ================================================================================================
47
'''
48
# GENERATING TEST CASES FOR THE MUSIC INFERENCE MODEL
49
'''
50
def music_inference_model_gen():
51
im = music_inference_model(LSTM_cell, densor, Ty=10)
52
opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)
53
im.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
54
cp1 = im.count_params()
55
ml1 = len(im.layers)
56
m_out1 = np.asarray((cp1, ml1))
57
print(m_out1)
58
return m_out1
59
'''
60
# ================================================================================================
61
62
# generating the test cases for predicted_and_sample
63
64
inference_model = music_inference_model(LSTM_cell, densor, 13)
65
results, indices = predict_and_sample(inference_model, x_initializer, a_initializer, c_initializer)
66
67
def generateTestCases():
68
testCases = {
69
'djmodel': {
70
'partId': 'iz6sX',
71
'testCases': [
72
{
73
'testInput': (30, LSTM_cell, densor, reshapor),
74
'testOutput': (45530, 36)
75
}
76
]
77
},
78
'music_inference_model': {
79
'partId': 'MtuL2',
80
'testCases': [
81
{
82
'testInput': (LSTM_cell, densor, 10),
83
'testOutput': (45530, 32)
84
}
85
]
86
},
87
'predict_and_sample': {
88
'partId': 'tkaiA',
89
'testCases': [
90
{
91
'testInput': (inference_model, x_initializer, a_initializer, c_initializer),
92
'testOutput': (results, indices)
93
}
94
]
95
},
96
}
97
return testCases
98
99
100