Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/C5 - Sequence Models/Week 1/Jazz improvisation with LSTM/generateTestCases.py
Views: 4819
# New Generate Test Cases1from .solutions import *2import numpy as np3np.random.seed(3)45import math67from tensorflow.keras.optimizers import Adam8from tensorflow.keras.layers import Dense, LSTM, Reshape910# import copy11# from keras.callbacks import History12# import tensorflow as tf131415#from grader_support import stdout_redirector16#from grader_support import util1718os.environ['TF_CPP_MIN_LOG_LEVEL']='3'1920# with suppress_stdout_stderr():21n_a = 6422n_values = 9023LSTM_cell = LSTM(n_a, return_state=True) # Used in Step 2.C24densor = Dense(n_values, activation='softmax') # Used in Step 2.D25x_initializer = np.zeros((1, 1, 90))26a_initializer = np.zeros((1, n_a))27c_initializer = np.zeros((1, n_a))28reshapor = Reshape((1, n_values))2930# ================================================================================================31# generating the test cases for dj model32'''33def djmodel_gen():34m = 6035a0 = np.zeros((m, n_a))36c0 = np.zeros((m, n_a))37djmodelx = djmodel(Tx = 30 , LSTM_cell=LSTM_cell, densor=densor, reshapor=reshapor)38opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)39djmodelx.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])40cp = djmodelx.count_params()41ml = len(djmodelx.layers)42print(cp, ml)43return (cp, ml)4445# ================================================================================================46'''47# GENERATING TEST CASES FOR THE MUSIC INFERENCE MODEL48'''49def music_inference_model_gen():50im = music_inference_model(LSTM_cell, densor, Ty=10)51opt = Adam(lr=0.01, beta_1=0.9, beta_2=0.999, decay=0.01)52im.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])53cp1 = im.count_params()54ml1 = len(im.layers)55m_out1 = np.asarray((cp1, ml1))56print(m_out1)57return m_out158'''59# ================================================================================================6061# generating the test cases for predicted_and_sample6263inference_model = music_inference_model(LSTM_cell, densor, 13)64results, indices = predict_and_sample(inference_model, x_initializer, a_initializer, c_initializer)6566def generateTestCases():67testCases = {68'djmodel': {69'partId': 'iz6sX',70'testCases': [71{72'testInput': (30, LSTM_cell, densor, reshapor),73'testOutput': (45530, 36)74}75]76},77'music_inference_model': {78'partId': 'MtuL2',79'testCases': [80{81'testInput': (LSTM_cell, densor, 10),82'testOutput': (45530, 32)83}84]85},86'predict_and_sample': {87'partId': 'tkaiA',88'testCases': [89{90'testInput': (inference_model, x_initializer, a_initializer, c_initializer),91'testOutput': (results, indices)92}93]94},95}96return testCases979899100