Skip to content

Commit d5668c5

Browse files
committed
student version l1p2
1 parent 06406be commit d5668c5

1 file changed

Lines changed: 21 additions & 41 deletions

File tree

lab1/TF_Part2_Music_Generation.ipynb

Lines changed: 21 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
" <td align=\"center\"><a target=\"_blank\" href=\"http://introtodeeplearning.com\">\n",
1111
" <img src=\"https://i.ibb.co/Jr88sn2/mit.png\" style=\"padding-bottom:5px;\" />\n",
1212
" Visit MIT Deep Learning</a></td>\n",
13-
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/master/lab1/solutions/TF_Part2_Music_Generation_Solution.ipynb\">\n",
13+
" <td align=\"center\"><a target=\"_blank\" href=\"https://colab.research.google.com/github/aamini/introtodeeplearning/blob/master/lab1/TF_Part2_Music_Generation.ipynb\">\n",
1414
" <img src=\"https://i.ibb.co/2P3SLwK/colab.png\" style=\"padding-bottom:5px;\" />Run in Google Colab</a></td>\n",
15-
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab1/solutions/TF_Part2_Music_Generation_Solution.ipynb\">\n",
15+
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab1/TF_Part2_Music_Generation.ipynb\">\n",
1616
" <img src=\"https://i.ibb.co/xfJbPmL/github.png\" height=\"70px\" style=\"padding-bottom:5px;\" />View Source on GitHub</a></td>\n",
1717
"</table>\n",
1818
"\n",
@@ -266,11 +266,7 @@
266266
" the number of characters in the input string\n",
267267
"'''\n",
268268
"def vectorize_string(string):\n",
269-
" vectorized_output = np.array([char2idx[char] for char in string])\n",
270-
" return vectorized_output\n",
271-
"\n",
272-
"# def vectorize_string(string):\n",
273-
" # TODO\n",
269+
" '''TODO'''\n",
274270
"\n",
275271
"vectorized_songs = vectorize_string(songs_joined)"
276272
]
@@ -329,11 +325,10 @@
329325
" idx = np.random.choice(n-seq_length, batch_size)\n",
330326
"\n",
331327
" '''TODO: construct a list of input sequences for the training batch'''\n",
332-
" input_batch = [vectorized_songs[i : i+seq_length] for i in idx]\n",
333-
" # input_batch = # TODO\n",
328+
" input_batch = # TODO\n",
329+
"\n",
334330
" '''TODO: construct a list of output sequences for the training batch'''\n",
335-
" output_batch = [vectorized_songs[i+1 : i+seq_length+1] for i in idx]\n",
336-
" # output_batch = # TODO\n",
331+
" output_batch = # TODO\n",
337332
"\n",
338333
" # x_batch, y_batch provide the true inputs and targets for network training\n",
339334
" x_batch = np.reshape(input_batch, [batch_size, seq_length])\n",
@@ -464,14 +459,12 @@
464459
"\n",
465460
" # Layer 2: LSTM with `rnn_units` number of units.\n",
466461
" # TODO: Call the LSTM function defined above to add this layer.\n",
467-
" LSTM(rnn_units),\n",
468-
" # LSTM('''TODO'''),\n",
462+
" LSTM('''TODO'''),\n",
469463
"\n",
470464
" # Layer 3: Dense (fully-connected) layer that transforms the LSTM output\n",
471465
" # into the vocabulary size.\n",
472466
" # TODO: Add the Dense layer.\n",
473-
" tf.keras.layers.Dense(vocab_size)\n",
474-
" # '''TODO: DENSE LAYER HERE'''\n",
467+
" '''TODO: DENSE LAYER HERE'''\n",
475468
" ])\n",
476469
"\n",
477470
" return model\n",
@@ -620,14 +613,12 @@
620613
"'''TODO: define the loss function to compute and return the loss between\n",
621614
" the true labels and predictions (logits). Set the argument from_logits=True.'''\n",
622615
"def compute_loss(labels, logits):\n",
623-
" loss = tf.keras.losses.sparse_categorical_crossentropy(labels, logits, from_logits=True)\n",
624-
" # loss = tf.keras.losses.sparse_categorical_crossentropy('''TODO''', '''TODO''', from_logits=True) # TODO\n",
616+
" loss = tf.keras.losses.sparse_categorical_crossentropy('''TODO''', '''TODO''', from_logits=True) # TODO\n",
625617
" return loss\n",
626618
"\n",
627619
"'''TODO: compute the loss using the true next characters from the example batch\n",
628620
" and the predictions from the untrained model several cells above'''\n",
629-
"example_batch_loss = compute_loss(y, pred)\n",
630-
"# example_batch_loss = compute_loss('''TODO''', '''TODO''') # TODO\n",
621+
"example_batch_loss = compute_loss('''TODO''', '''TODO''') # TODO\n",
631622
"\n",
632623
"print(\"Prediction shape: \", pred.shape, \" # (batch_size, sequence_length, vocab_size)\")\n",
633624
"print(\"scalar_loss: \", example_batch_loss.numpy().mean())"
@@ -732,37 +723,32 @@
732723
"\n",
733724
"'''TODO: instantiate a new model for training using the `build_model`\n",
734725
" function and the hyperparameters created above.'''\n",
735-
"model = build_model(vocab_size, params[\"embedding_dim\"], params[\"rnn_units\"], params[\"batch_size\"])\n",
736-
"# model = build_model('''TODO: arguments''')\n",
726+
"model = build_model('''TODO: arguments''')\n",
737727
"\n",
738728
"'''TODO: instantiate an optimizer with its learning rate.\n",
739729
" Checkout the tensorflow website for a list of supported optimizers.\n",
740730
" https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/\n",
741731
" Try using the Adam optimizer to start.'''\n",
742-
"optimizer = tf.keras.optimizers.Adam(params[\"learning_rate\"])\n",
743-
"# optimizer = # TODO\n",
732+
"optimizer = # TODO\n",
744733
"\n",
745734
"@tf.function\n",
746735
"def train_step(x, y):\n",
747736
" # Use tf.GradientTape()\n",
748737
" with tf.GradientTape() as tape:\n",
749738
"\n",
750739
" '''TODO: feed the current input into the model and generate predictions'''\n",
751-
" y_hat = model(x) # TODO\n",
752-
" # y_hat = model('''TODO''')\n",
740+
" y_hat = model('''TODO''')\n",
753741
"\n",
754742
" '''TODO: compute the loss!'''\n",
755-
" loss = compute_loss(y, y_hat) # TODO\n",
756-
" # loss = compute_loss('''TODO''', '''TODO''')\n",
743+
" loss = compute_loss('''TODO''', '''TODO''')\n",
757744
"\n",
758745
" # Now, compute the gradients\n",
759746
" '''TODO: complete the function call for gradient computation.\n",
760747
" Remember that we want the gradient of the loss with respect all\n",
761748
" of the model parameters.\n",
762749
" HINT: use `model.trainable_variables` to get a list of all model\n",
763750
" parameters.'''\n",
764-
" grads = tape.gradient(loss, model.trainable_variables) # TODO\n",
765-
" # grads = tape.gradient('''TODO''', '''TODO''')\n",
751+
" grads = tape.gradient('''TODO''', '''TODO''')\n",
766752
"\n",
767753
" # Apply the gradients to the optimizer so it can update the model accordingly\n",
768754
" optimizer.apply_gradients(zip(grads, model.trainable_variables))\n",
@@ -836,8 +822,7 @@
836822
"outputs": [],
837823
"source": [
838824
"'''TODO: Rebuild the model using a batch_size=1'''\n",
839-
"model = build_model(vocab_size, params[\"embedding_dim\"], params[\"rnn_units\"], batch_size=1) # TODO\n",
840-
"# model = build_model('''TODO''', '''TODO''', '''TODO''', batch_size=1)\n",
825+
"model = build_model('''TODO''', '''TODO''', '''TODO''', batch_size=1)\n",
841826
"\n",
842827
"# Restore the model weights for the last checkpoint after training\n",
843828
"model.build(tf.TensorShape([1, None]))\n",
@@ -892,8 +877,7 @@
892877
" # Evaluation step (generating ABC text using the learned RNN model)\n",
893878
"\n",
894879
" '''TODO: convert the start string to numbers (vectorize)'''\n",
895-
" input_eval = [char2idx[s] for s in start_string] # TODO\n",
896-
" # input_eval = ['''TODO''']\n",
880+
" input_eval = ['''TODO''']\n",
897881
" input_eval = tf.expand_dims(input_eval, 0)\n",
898882
"\n",
899883
" # Empty string to store our results\n",
@@ -905,24 +889,21 @@
905889
"\n",
906890
" for i in tqdm(range(generation_length)):\n",
907891
" '''TODO: evaluate the inputs and generate the next character predictions'''\n",
908-
" predictions = model(input_eval)\n",
909-
" # predictions = model('''TODO''')\n",
892+
" predictions = model('''TODO''')\n",
910893
"\n",
911894
" # Remove the batch dimension\n",
912895
" predictions = tf.squeeze(predictions, 0)\n",
913896
"\n",
914897
" '''TODO: use a multinomial distribution to sample'''\n",
915-
" predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()\n",
916-
" # predicted_id = tf.random.categorical('''TODO''', num_samples=1)[-1,0].numpy()\n",
898+
" predicted_id = tf.random.categorical('''TODO''', num_samples=1)[-1,0].numpy()\n",
917899
"\n",
918900
" # Pass the prediction along with the previous hidden state\n",
919901
" # as the next inputs to the model\n",
920902
" input_eval = tf.expand_dims([predicted_id], 0)\n",
921903
"\n",
922904
" '''TODO: add the predicted character to the generated text!'''\n",
923905
" # Hint: consider what format the prediction is in vs. the output\n",
924-
" text_generated.append(idx2char[predicted_id]) # TODO\n",
925-
" # text_generated.append('''TODO''')\n",
906+
" text_generated.append('''TODO''')\n",
926907
"\n",
927908
" return (start_string + ''.join(text_generated))"
928909
]
@@ -937,8 +918,7 @@
937918
"source": [
938919
"'''TODO: Use the model and the function defined above to generate ABC format text of length 1000!\n",
939920
" As you may notice, ABC files start with \"X\" - this may be a good start string.'''\n",
940-
"generated_text = generate_text(model, start_string=\"X\", generation_length=1000) # TODO\n",
941-
"# generated_text = generate_text('''TODO''', start_string=\"X\", generation_length=1000)"
921+
"generated_text = generate_text('''TODO''', start_string=\"X\", generation_length=1000)"
942922
]
943923
},
944924
{

0 commit comments

Comments
 (0)