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/inference_code.py
Views: 4819
1
def inference_model(LSTM_cell, densor, n_x = 90, n_a = 64, Ty = 100):
2
"""
3
Uses the trained "LSTM_cell" and "densor" from model() to generate a sequence of values.
4
5
Arguments:
6
LSTM_cell -- the trained "LSTM_cell" from model(), Keras layer object
7
densor -- the trained "densor" from model(), Keras layer object
8
n_x -- number of unique values
9
n_a -- number of units in the LSTM_cell
10
Ty -- number of time steps to generate
11
12
Returns:
13
inference_model -- Keras model instance
14
"""
15
16
# Define the input of your model with a shape
17
x0 = Input(shape=(1, n_x))
18
19
# Define s0, initial hidden state for the decoder LSTM
20
a0 = Input(shape=(n_a,), name='a0')
21
c0 = Input(shape=(n_a,), name='c0')
22
a = a0
23
c = c0
24
x = x0
25
26
### START CODE HERE ###
27
# Step 1: Create an empty list of "outputs" to later store your predicted values (≈1 line)
28
outputs = []
29
30
# Step 2: Loop over Ty and generate a value at every time step
31
for t in range(Ty):
32
33
# Step 2.A: Perform one step of LSTM_cell (≈1 line)
34
a, _, c = LSTM_cell(x, initial_state=[a, c])
35
36
# Step 2.B: Apply Dense layer to the hidden state output of the LSTM_cell (≈1 line)
37
out = densor(a)
38
39
# Step 2.C: Append the prediction "out" to "outputs" (≈1 line)
40
outputs.append(out)
41
42
# Step 2.D: Set the prediction "out" to be the next input "x". You will need to use RepeatVector(1). (≈1 line)
43
x = RepeatVector(1)(out)
44
45
# Step 3: Create model instance with the correct "inputs" and "outputs" (≈1 line)
46
inference_model = Model(inputs=[x0, a0, c0], outputs=outputs)
47
### END CODE HERE ###
48
49
return inference_model
50
51
52
inference_model = inference_model(LSTM_cell, densor)
53
54
55
x1 = np.zeros((1, 1, 90))
56
x1[:,:,35] = 1
57
a1 = np.zeros((1, n_a))
58
c1 = np.zeros((1, n_a))
59
predicting = inference_model.predict([x1, a1, c1])
60
61
62
indices = np.argmax(predicting, axis = -1)
63
results = to_categorical(indices, num_classes=90)
64