|
10 | 10 | " <td align=\"center\"><a target=\"_blank\" href=\"http://introtodeeplearning.com\">\n", |
11 | 11 | " <img src=\"https://i.ibb.co/Jr88sn2/mit.png\" style=\"padding-bottom:5px;\" />\n", |
12 | 12 | " 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", |
14 | 14 | " <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", |
16 | 16 | " <img src=\"https://i.ibb.co/xfJbPmL/github.png\" height=\"70px\" style=\"padding-bottom:5px;\" />View Source on GitHub</a></td>\n", |
17 | 17 | "</table>\n", |
18 | 18 | "\n", |
|
266 | 266 | " the number of characters in the input string\n", |
267 | 267 | "'''\n", |
268 | 268 | "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", |
274 | 270 | "\n", |
275 | 271 | "vectorized_songs = vectorize_string(songs_joined)" |
276 | 272 | ] |
|
329 | 325 | " idx = np.random.choice(n-seq_length, batch_size)\n", |
330 | 326 | "\n", |
331 | 327 | " '''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", |
334 | 330 | " '''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", |
337 | 332 | "\n", |
338 | 333 | " # x_batch, y_batch provide the true inputs and targets for network training\n", |
339 | 334 | " x_batch = np.reshape(input_batch, [batch_size, seq_length])\n", |
|
464 | 459 | "\n", |
465 | 460 | " # Layer 2: LSTM with `rnn_units` number of units.\n", |
466 | 461 | " # 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", |
469 | 463 | "\n", |
470 | 464 | " # Layer 3: Dense (fully-connected) layer that transforms the LSTM output\n", |
471 | 465 | " # into the vocabulary size.\n", |
472 | 466 | " # 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", |
475 | 468 | " ])\n", |
476 | 469 | "\n", |
477 | 470 | " return model\n", |
|
620 | 613 | "'''TODO: define the loss function to compute and return the loss between\n", |
621 | 614 | " the true labels and predictions (logits). Set the argument from_logits=True.'''\n", |
622 | 615 | "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", |
625 | 617 | " return loss\n", |
626 | 618 | "\n", |
627 | 619 | "'''TODO: compute the loss using the true next characters from the example batch\n", |
628 | 620 | " 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", |
631 | 622 | "\n", |
632 | 623 | "print(\"Prediction shape: \", pred.shape, \" # (batch_size, sequence_length, vocab_size)\")\n", |
633 | 624 | "print(\"scalar_loss: \", example_batch_loss.numpy().mean())" |
|
732 | 723 | "\n", |
733 | 724 | "'''TODO: instantiate a new model for training using the `build_model`\n", |
734 | 725 | " 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", |
737 | 727 | "\n", |
738 | 728 | "'''TODO: instantiate an optimizer with its learning rate.\n", |
739 | 729 | " Checkout the tensorflow website for a list of supported optimizers.\n", |
740 | 730 | " https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/\n", |
741 | 731 | " 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", |
744 | 733 | "\n", |
745 | 734 | "@tf.function\n", |
746 | 735 | "def train_step(x, y):\n", |
747 | 736 | " # Use tf.GradientTape()\n", |
748 | 737 | " with tf.GradientTape() as tape:\n", |
749 | 738 | "\n", |
750 | 739 | " '''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", |
753 | 741 | "\n", |
754 | 742 | " '''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", |
757 | 744 | "\n", |
758 | 745 | " # Now, compute the gradients\n", |
759 | 746 | " '''TODO: complete the function call for gradient computation.\n", |
760 | 747 | " Remember that we want the gradient of the loss with respect all\n", |
761 | 748 | " of the model parameters.\n", |
762 | 749 | " HINT: use `model.trainable_variables` to get a list of all model\n", |
763 | 750 | " 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", |
766 | 752 | "\n", |
767 | 753 | " # Apply the gradients to the optimizer so it can update the model accordingly\n", |
768 | 754 | " optimizer.apply_gradients(zip(grads, model.trainable_variables))\n", |
|
836 | 822 | "outputs": [], |
837 | 823 | "source": [ |
838 | 824 | "'''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", |
841 | 826 | "\n", |
842 | 827 | "# Restore the model weights for the last checkpoint after training\n", |
843 | 828 | "model.build(tf.TensorShape([1, None]))\n", |
|
892 | 877 | " # Evaluation step (generating ABC text using the learned RNN model)\n", |
893 | 878 | "\n", |
894 | 879 | " '''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", |
897 | 881 | " input_eval = tf.expand_dims(input_eval, 0)\n", |
898 | 882 | "\n", |
899 | 883 | " # Empty string to store our results\n", |
|
905 | 889 | "\n", |
906 | 890 | " for i in tqdm(range(generation_length)):\n", |
907 | 891 | " '''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", |
910 | 893 | "\n", |
911 | 894 | " # Remove the batch dimension\n", |
912 | 895 | " predictions = tf.squeeze(predictions, 0)\n", |
913 | 896 | "\n", |
914 | 897 | " '''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", |
917 | 899 | "\n", |
918 | 900 | " # Pass the prediction along with the previous hidden state\n", |
919 | 901 | " # as the next inputs to the model\n", |
920 | 902 | " input_eval = tf.expand_dims([predicted_id], 0)\n", |
921 | 903 | "\n", |
922 | 904 | " '''TODO: add the predicted character to the generated text!'''\n", |
923 | 905 | " # 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", |
926 | 907 | "\n", |
927 | 908 | " return (start_string + ''.join(text_generated))" |
928 | 909 | ] |
|
937 | 918 | "source": [ |
938 | 919 | "'''TODO: Use the model and the function defined above to generate ABC format text of length 1000!\n", |
939 | 920 | " 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)" |
942 | 922 | ] |
943 | 923 | }, |
944 | 924 | { |
|
0 commit comments