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/inference_code.py
Views: 4819
def inference_model(LSTM_cell, densor, n_x = 90, n_a = 64, Ty = 100):1"""2Uses the trained "LSTM_cell" and "densor" from model() to generate a sequence of values.34Arguments:5LSTM_cell -- the trained "LSTM_cell" from model(), Keras layer object6densor -- the trained "densor" from model(), Keras layer object7n_x -- number of unique values8n_a -- number of units in the LSTM_cell9Ty -- number of time steps to generate1011Returns:12inference_model -- Keras model instance13"""1415# Define the input of your model with a shape16x0 = Input(shape=(1, n_x))1718# Define s0, initial hidden state for the decoder LSTM19a0 = Input(shape=(n_a,), name='a0')20c0 = Input(shape=(n_a,), name='c0')21a = a022c = c023x = x02425### START CODE HERE ###26# Step 1: Create an empty list of "outputs" to later store your predicted values (≈1 line)27outputs = []2829# Step 2: Loop over Ty and generate a value at every time step30for t in range(Ty):3132# Step 2.A: Perform one step of LSTM_cell (≈1 line)33a, _, c = LSTM_cell(x, initial_state=[a, c])3435# Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)36out = densor(a)3738# Step 2.C: Append the prediction "out" to "outputs" (≈1 line)39outputs.append(out)4041# Step 2.D: Set the prediction "out" to be the next input "x". You will need to use RepeatVector(1). (≈1 line)42x = RepeatVector(1)(out)4344# Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line)45inference_model = Model(inputs=[x0, a0, c0], outputs=outputs)46### END CODE HERE ###4748return inference_model495051inference_model = inference_model(LSTM_cell, densor)525354x1 = np.zeros((1, 1, 90))55x1[:,:,35] = 156a1 = np.zeros((1, n_a))57c1 = np.zeros((1, n_a))58predicting = inference_model.predict([x1, a1, c1])596061indices = np.argmax(predicting, axis = -1)62results = to_categorical(indices, num_classes=90)6364