|
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/2024/lab1/solutions/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/solutions/Part2_Music_Generation_Solution.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/2024/lab1/solutions/Part2_Music_Generation_Solution.ipynb\">\n", |
| 15 | + " <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab1/solutions/Part2_Music_Generation_Solution.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",
|
|
58 | 58 | },
|
59 | 59 | "source": [
|
60 | 60 | "## 2.1 Dependencies\n",
|
61 |
| - "First, let's download the course repository, install dependencies, and import the relevant packages we'll need for this lab. Remember that we'll be using Comet to track training runs -- sign up for a Comet account [at this link](https://www.comet.com/signup?utm_source=mit_dl&utm_medium=partner&utm_content=github), and use your API key linked with your account." |
| 61 | + "First, let's download the course repository, install dependencies, and import the relevant packages we'll need for this lab.\n", |
| 62 | + "\n", |
| 63 | + "We will be using [Comet ML](https://www.comet.com/docs/v2/) to track our model development and training runs. First, sign up for a Comet account [at this link](https://www.comet.com/signup?utm_source=mit_dl&utm_medium=partner&utm_content=github\n", |
| 64 | + ") (you can use your Google or Github account). This will generate a personal API Key, which you can find either in the first 'Get Started with Comet' page, under your account settings, or by pressing the '?' in the top right corner and then 'Quickstart Guide'. Enter this API key as the global variable `COMET_API_KEY`." |
62 | 65 | ]
|
63 | 66 | },
|
64 | 67 | {
|
|
69 | 72 | },
|
70 | 73 | "outputs": [],
|
71 | 74 | "source": [
|
72 |
| - "# import CometML -- see instructions in Lab 1, Part 1 for this\n", |
73 |
| - "# if prompted for an API key, supply your Comet API key\n", |
74 |
| - "%pip install comet_ml\n", |
| 75 | + "!pip install comet_ml > /dev/null 2>&1\n", |
75 | 76 | "import comet_ml\n",
|
76 |
| - "comet_ml.init(project_name=\"6.s191_lab1_part2\")\n", |
77 |
| - "comet_experiment = comet_ml.Experiment()\n", |
| 77 | + "COMET_API_KEY = \"SlXYfzYjZ9qAqoLNOGHTF6qLU\" # TODO: ENTER YOUR API KEY HERE!!\n", |
78 | 78 | "\n",
|
79 | 79 | "# Import Tensorflow 2.0\n",
|
80 |
| - "# %tensorflow_version 2.x\n", |
81 |
| - "!pip install tensorflow\n", |
82 | 80 | "import tensorflow as tf\n",
|
83 | 81 | "\n",
|
84 | 82 | "# Download and import the MIT Introduction to Deep Learning package\n",
|
85 |
| - "!pip install mitdeeplearning\n", |
| 83 | + "!pip install mitdeeplearning --quiet\n", |
86 | 84 | "import mitdeeplearning as mdl\n",
|
87 | 85 | "\n",
|
88 | 86 | "# Import all remaining packages\n",
|
|
92 | 90 | "import functools\n",
|
93 | 91 | "from IPython import display as ipythondisplay\n",
|
94 | 92 | "from tqdm import tqdm\n",
|
| 93 | + "from scipy.io.wavfile import write\n", |
95 | 94 | "!apt-get install abcmidi timidity > /dev/null 2>&1\n",
|
96 | 95 | "\n",
|
97 | 96 | "\n",
|
98 | 97 | "# Check that we are using a GPU, if not switch runtimes\n",
|
99 | 98 | "# using Runtime > Change Runtime Type > GPU\n",
|
100 | 99 | "assert len(tf.config.list_physical_devices('GPU')) > 0\n",
|
101 |
| - "\n", |
102 |
| - "from scipy.io.wavfile import write" |
| 100 | + "assert COMET_API_KEY != \"\", \"Please insert your Comet API Key\"" |
103 | 101 | ]
|
104 | 102 | },
|
105 | 103 | {
|
|
652 | 650 | "source": [
|
653 | 651 | "### Hyperparameter setting and optimization ###\n",
|
654 | 652 | "\n",
|
655 |
| - "# Optimization parameters:\n", |
656 |
| - "num_training_iterations = 2000 # Increase this to train longer\n", |
657 |
| - "batch_size = 4 # Experiment between 1 and 64\n", |
658 |
| - "seq_length = 100 # Experiment between 50 and 500\n", |
659 |
| - "learning_rate = 5e-3 # Experiment between 1e-5 and 1e-1\n", |
| 653 | + "vocab_size = len(vocab)\n", |
660 | 654 | "\n",
|
661 | 655 | "# Model parameters:\n",
|
662 |
| - "vocab_size = len(vocab)\n", |
663 |
| - "embedding_dim = 256\n", |
664 |
| - "rnn_units = 1024 # Experiment between 1 and 2048\n", |
| 656 | + "params = dict(\n", |
| 657 | + " num_training_iterations = 3000, # Increase this to train longer\n", |
| 658 | + " batch_size = 8, # Experiment between 1 and 64\n", |
| 659 | + " seq_length = 100, # Experiment between 50 and 500\n", |
| 660 | + " learning_rate = 5e-3, # Experiment between 1e-5 and 1e-1\n", |
| 661 | + " embedding_dim = 256,\n", |
| 662 | + " rnn_units = 1024, # Experiment between 1 and 2048\n", |
| 663 | + ")\n", |
665 | 664 | "\n",
|
666 | 665 | "# Checkpoint location:\n",
|
667 | 666 | "checkpoint_dir = './training_checkpoints'\n",
|
668 | 667 | "checkpoint_prefix = os.path.join(checkpoint_dir, \"my_ckpt\")"
|
669 | 668 | ]
|
670 | 669 | },
|
| 670 | + { |
| 671 | + "cell_type": "markdown", |
| 672 | + "source": [ |
| 673 | + "Having defined our hyperparameters we can set up for experiment tracking with Comet. [`Experiment`](https://www.comet.com/docs/v2/api-and-sdk/python-sdk/reference/Experiment/) are the core objects in Comet and will allow us to track training and model development. Here we have written a short function to create a new comet experiment. Note that in this setup, when hyperparameters change, you can run the `create_experiment()` function to initiate a new experiment. All experiments defined with the same `project_name` will live under that project in your Comet interface.\n", |
| 674 | + "\n" |
| 675 | + ], |
| 676 | + "metadata": { |
| 677 | + "id": "AyLzIPeAIqfg" |
| 678 | + } |
| 679 | + }, |
| 680 | + { |
| 681 | + "cell_type": "code", |
| 682 | + "source": [ |
| 683 | + "### Create a Comet experiment to track our training run ###\n", |
| 684 | + "\n", |
| 685 | + "def create_experiment():\n", |
| 686 | + " # end any prior experiments\n", |
| 687 | + " if 'experiment' in locals():\n", |
| 688 | + " experiment.end()\n", |
| 689 | + "\n", |
| 690 | + " # initiate the comet experiment for tracking\n", |
| 691 | + " experiment = comet_ml.Experiment(\n", |
| 692 | + " api_key=COMET_API_KEY,\n", |
| 693 | + " project_name=\"6S191_Lab1_Part2\")\n", |
| 694 | + " # log our hyperparameters, defined above, to the experiment\n", |
| 695 | + " for param, value in params.items():\n", |
| 696 | + " experiment.log_parameter(param, value)\n", |
| 697 | + " experiment.flush()\n", |
| 698 | + "\n", |
| 699 | + " return experiment" |
| 700 | + ], |
| 701 | + "metadata": { |
| 702 | + "id": "MBsN1vvxInmN" |
| 703 | + }, |
| 704 | + "execution_count": null, |
| 705 | + "outputs": [] |
| 706 | + }, |
671 | 707 | {
|
672 | 708 | "cell_type": "markdown",
|
673 | 709 | "metadata": {
|
|
693 | 729 | "\n",
|
694 | 730 | "'''TODO: instantiate a new model for training using the `build_model`\n",
|
695 | 731 | " function and the hyperparameters created above.'''\n",
|
696 |
| - "model = build_model(vocab_size, embedding_dim, rnn_units, batch_size)\n", |
| 732 | + "model = build_model(vocab_size, params[\"embedding_dim\"], params[\"rnn_units\"], params[\"batch_size\"])\n", |
697 | 733 | "# model = build_model('''TODO: arguments''')\n",
|
698 | 734 | "\n",
|
699 | 735 | "'''TODO: instantiate an optimizer with its learning rate.\n",
|
700 | 736 | " Checkout the tensorflow website for a list of supported optimizers.\n",
|
701 | 737 | " https://www.tensorflow.org/api_docs/python/tf/keras/optimizers/\n",
|
702 | 738 | " Try using the Adam optimizer to start.'''\n",
|
703 |
| - "optimizer = tf.keras.optimizers.Adam(learning_rate)\n", |
| 739 | + "optimizer = tf.keras.optimizers.Adam(params[\"learning_rate\"])\n", |
704 | 740 | "# optimizer = # TODO\n",
|
705 | 741 | "\n",
|
706 | 742 | "@tf.function\n",
|
|
735 | 771 | "\n",
|
736 | 772 | "history = []\n",
|
737 | 773 | "plotter = mdl.util.PeriodicPlotter(sec=2, xlabel='Iterations', ylabel='Loss')\n",
|
738 |
| - "if hasattr(tqdm, '_instances'): tqdm._instances.clear() # clear if it exists\n", |
| 774 | + "experiment = create_experiment()\n", |
739 | 775 | "\n",
|
740 |
| - "for iter in tqdm(range(num_training_iterations)):\n", |
| 776 | + "if hasattr(tqdm, '_instances'): tqdm._instances.clear() # clear if it exists\n", |
| 777 | + "for iter in tqdm(range(params[\"num_training_iterations\"])):\n", |
741 | 778 | "\n",
|
742 | 779 | " # Grab a batch and propagate it through the network\n",
|
743 |
| - " x_batch, y_batch = get_batch(vectorized_songs, seq_length, batch_size)\n", |
| 780 | + " x_batch, y_batch = get_batch(vectorized_songs, params[\"seq_length\"], params[\"batch_size\"])\n", |
744 | 781 | " loss = train_step(x_batch, y_batch)\n",
|
745 | 782 | "\n",
|
746 | 783 | " # log the loss to the Comet interface! we will be able to track it there.\n",
|
747 |
| - " comet_experiment.log_metric(\"loss\", loss.numpy().mean(), step=iter)\n", |
748 |
| - " # Update the progress bar\n", |
| 784 | + " experiment.log_metric(\"loss\", loss.numpy().mean(), step=iter)\n", |
| 785 | + " # Update the progress bar and also visualize within notebook\n", |
749 | 786 | " history.append(loss.numpy().mean())\n",
|
750 | 787 | " plotter.plot(history)\n",
|
751 | 788 | "\n",
|
|
754 | 791 | " model.save_weights(checkpoint_prefix)\n",
|
755 | 792 | "\n",
|
756 | 793 | "# Save the trained model and the weights\n",
|
757 |
| - "model.save_weights(checkpoint_prefix)\n" |
| 794 | + "model.save_weights(checkpoint_prefix)\n", |
| 795 | + "experiment.flush()\n" |
758 | 796 | ]
|
759 | 797 | },
|
760 | 798 | {
|
|
778 | 816 | "id": "JIPcXllKjkdr"
|
779 | 817 | },
|
780 | 818 | "source": [
|
| 819 | + "\n", |
781 | 820 | "### Restore the latest checkpoint\n",
|
782 | 821 | "\n",
|
783 | 822 | "To keep this inference step simple, we will use a batch size of 1. Because of how the RNN state is passed from timestep to timestep, the model will only be able to accept a fixed batch size once it is built.\n",
|
|
794 | 833 | "outputs": [],
|
795 | 834 | "source": [
|
796 | 835 | "'''TODO: Rebuild the model using a batch_size=1'''\n",
|
797 |
| - "model = build_model(vocab_size, embedding_dim, rnn_units, batch_size=1) # TODO\n", |
| 836 | + "model = build_model(vocab_size, params[\"embedding_dim\"], params[\"rnn_units\"], batch_size=1) # TODO\n", |
798 | 837 | "# model = build_model('''TODO''', '''TODO''', '''TODO''', batch_size=1)\n",
|
799 | 838 | "\n",
|
800 | 839 | "# Restore the model weights for the last checkpoint after training\n",
|
|
909 | 948 | "\n",
|
910 | 949 | "We can now call a function to convert the ABC format text to an audio file, and then play that back to check out our generated music! Try training longer if the resulting song is not long enough, or re-generating the song!\n",
|
911 | 950 | "\n",
|
912 |
| - "We will save the song to Comet -- you will be able to find your songs under the `Audio` and `Assets & Artificats` pages in your Comet interface! Note the [`log_asset()`](https://www.comet.com/docs/v2/api-and-sdk/python-sdk/reference/Experiment/#experimentlog_asset) documentation, where you will see how to specify file names and other parameters for saving your assets." |
| 951 | + "We will save the song to Comet -- you will be able to find your songs under the `Audio` and `Assets & Artificats` pages in your Comet interface for the project. Note the [`log_asset()`](https://www.comet.com/docs/v2/api-and-sdk/python-sdk/reference/Experiment/#experimentlog_asset) documentation, where you will see how to specify file names and other parameters for saving your assets." |
913 | 952 | ]
|
914 | 953 | },
|
915 | 954 | {
|
|
937 | 976 | " wav_file_path = f\"output_{i}.wav\"\n",
|
938 | 977 | " write(wav_file_path, 88200, numeric_data)\n",
|
939 | 978 | "\n",
|
940 |
| - " # save your song to the Comet interface -- you will be able to access there\n", |
941 |
| - " comet_experiment.log_asset(wav_file_path)\n", |
942 |
| - "\n", |
943 |
| - "comet_experiment.end()" |
| 979 | + " # save your song to the Comet interface -- you can access it there\n", |
| 980 | + " experiment.log_asset(wav_file_path)" |
944 | 981 | ]
|
945 | 982 | },
|
| 983 | + { |
| 984 | + "cell_type": "code", |
| 985 | + "source": [ |
| 986 | + "# when done, end the comet experiment\n", |
| 987 | + "experiment.end()" |
| 988 | + ], |
| 989 | + "metadata": { |
| 990 | + "id": "4353qSV76gnJ" |
| 991 | + }, |
| 992 | + "execution_count": null, |
| 993 | + "outputs": [] |
| 994 | + }, |
946 | 995 | {
|
947 | 996 | "cell_type": "markdown",
|
948 | 997 | "metadata": {
|
|
974 | 1023 | "\n",
|
975 | 1024 | "\n"
|
976 | 1025 | ]
|
977 |
| - }, |
978 |
| - { |
979 |
| - "cell_type": "code", |
980 |
| - "source": [], |
981 |
| - "metadata": { |
982 |
| - "id": "kpRRYfYUwdAf" |
983 |
| - }, |
984 |
| - "execution_count": null, |
985 |
| - "outputs": [] |
986 | 1026 | }
|
987 | 1027 | ],
|
988 | 1028 | "metadata": {
|
|
0 commit comments