Skip to content

Commit 4fad6a3

Browse files
committed
pt l1p2 student
1 parent e87feae commit 4fad6a3

1 file changed

Lines changed: 25 additions & 46 deletions

File tree

lab1/PT_Part2_Music_Generation.ipynb

Lines changed: 25 additions & 46 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/PT_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/PT_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/PT_Part2_Music_Generation_Solution.ipynb\">\n",
15+
" <td align=\"center\"><a target=\"_blank\" href=\"https://github.com/aamini/introtodeeplearning/blob/master/lab1/PT_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",
@@ -61,7 +61,7 @@
6161
"First, let's download the course repository, install dependencies, and import the relevant packages we'll need for this lab.\n",
6262
"\n",
6363
"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`."
64+
") (you can use your Google or Github account). You will need to generate a new 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`."
6565
]
6666
},
6767
{
@@ -269,11 +269,7 @@
269269
" the number of characters in the input string\n",
270270
"'''\n",
271271
"def vectorize_string(string):\n",
272-
" vectorized_output = np.array([char2idx[char] for char in string])\n",
273-
" return vectorized_output\n",
274-
"\n",
275-
"# def vectorize_string(string):\n",
276-
" # TODO\n",
272+
" '''TODO'''\n",
277273
"\n",
278274
"vectorized_songs = vectorize_string(songs_joined)"
279275
]
@@ -332,10 +328,10 @@
332328
" idx = np.random.choice(n - seq_length, batch_size)\n",
333329
"\n",
334330
" '''TODO: construct a list of input sequences for the training batch'''\n",
335-
" input_batch = [vectorized_songs[i: i + seq_length] for i in idx]\n",
331+
" input_batch = # TODO\n",
336332
"\n",
337333
" '''TODO: construct a list of output sequences for the training batch'''\n",
338-
" output_batch = [vectorized_songs[i + 1: i + seq_length + 1] for i in idx]\n",
334+
" output_batch = # TODO\n",
339335
"\n",
340336
" # Convert the input and output batches to tensors\n",
341337
" x_batch = torch.tensor(input_batch, dtype=torch.long)\n",
@@ -454,16 +450,13 @@
454450
" # of a fixed embedding size\n",
455451
" self.embedding = nn.Embedding(vocab_size, embedding_dim)\n",
456452
"\n",
457-
" # Layer 2: LSTM with hidden_size `hidden_size`. note: number of layers defaults to 1.\n",
458-
" # TODO: Use the nn.LSTM() module from pytorch.\n",
459-
" self.lstm = nn.LSTM(embedding_dim, hidden_size, batch_first=True)\n",
460-
" # self.lstm = nn.LSTM('''TODO''')\n",
453+
" '''TODO: Layer 2: LSTM with hidden_size `hidden_size`. note: number of layers defaults to 1.\n",
454+
" Use the nn.LSTM() module from pytorch.'''\n",
455+
" self.lstm = nn.LSTM('''TODO''') # TODO\n",
461456
"\n",
462-
" # Layer 3: Linear (fully-connected) layer that transforms the LSTM output\n",
463-
" # into the vocabulary size.\n",
464-
" # TODO: Add the Linear layer.\n",
465-
" self.fc = nn.Linear(hidden_size, vocab_size)\n",
466-
" # self.fc = nn.Linear('''TODO''')\n",
457+
" '''TODO: Layer 3: Linear (fully-connected) layer that transforms the LSTM output\n",
458+
" # into the vocabulary size.'''\n",
459+
" self.fc = nn.Linear('''TODO''') # TODO\n",
467460
"\n",
468461
" def init_hidden(self, batch_size, device):\n",
469462
" # Initialize hidden state and cell state with zeros\n",
@@ -650,12 +643,10 @@
650643
" batched_labels = labels.view(-1)\n",
651644
"\n",
652645
" ''' TODO: Batch the logits so that the shape of the logits should be (B * L, V) '''\n",
653-
" batched_logits = logits.view(-1, logits.size(-1))\n",
654-
" # batched_logits = \"\"\" TODO \"\"\" # TODO\n",
646+
" batched_logits = \"\"\" TODO \"\"\" # TODO\n",
655647
"\n",
656648
" '''TODO: Compute the cross-entropy loss using the batched next characters and predictions'''\n",
657-
" loss = cross_entropy(batched_logits, batched_labels)\n",
658-
" # loss = \"\"\" TODO \"\"\" # TODO\n",
649+
" loss = \"\"\" TODO \"\"\" # TODO\n",
659650
" return loss"
660651
]
661652
},
@@ -668,8 +659,7 @@
668659
"\n",
669660
"'''TODO: compute the loss using the true next characters from the example batch\n",
670661
" and the predictions from the untrained model several cells above'''\n",
671-
"example_batch_loss = compute_loss(y, pred)\n",
672-
"# example_batch_loss = compute_loss('''TODO''', '''TODO''') # TODO\n",
662+
"example_batch_loss = compute_loss('''TODO''', '''TODO''') # TODO\n",
673663
"\n",
674664
"print(f\"Prediction shape: {pred.shape} # (batch_size, sequence_length, vocab_size)\")\n",
675665
"print(f\"scalar_loss: {example_batch_loss.mean().item()}\")"
@@ -779,8 +769,7 @@
779769
"\n",
780770
"'''TODO: instantiate a new LSTMModel model for training using the hyperparameters\n",
781771
" created above.'''\n",
782-
"model = LSTMModel(vocab_size, params[\"embedding_dim\"], params[\"hidden_size\"])\n",
783-
"# model = LSTMModel('''TODO: arguments''')\n",
772+
"model = LSTMModel('''TODO: arguments''')\n",
784773
"\n",
785774
"# Move the model to the GPU\n",
786775
"model.to(device)\n",
@@ -789,8 +778,7 @@
789778
" Checkout the PyTorch website for a list of supported optimizers.\n",
790779
" https://pytorch.org/docs/stable/optim.html\n",
791780
" Try using the Adam optimizer to start.'''\n",
792-
"optimizer = torch.optim.Adam(model.parameters(), lr=params[\"learning_rate\"])\n",
793-
"# optimizer = # TODO\n",
781+
"optimizer = # TODO\n",
794782
"\n",
795783
"def train_step(x, y):\n",
796784
" # Set the model's mode to train\n",
@@ -801,22 +789,19 @@
801789
"\n",
802790
" # Forward pass\n",
803791
" '''TODO: feed the current input into the model and generate predictions'''\n",
804-
" y_hat = model(x) # TODO\n",
805-
" # y_hat = model('''TODO''')\n",
792+
" y_hat = model('''TODO''')\n",
806793
"\n",
807794
" # Compute the loss\n",
808795
" '''TODO: compute the loss!'''\n",
809-
" loss = compute_loss(y, y_hat) # TODO\n",
810-
" # loss = compute_loss('''TODO''', '''TODO''')\n",
796+
" loss = compute_loss('''TODO''', '''TODO''')\n",
811797
"\n",
812798
" # Backward pass\n",
813799
" '''TODO: complete the gradient computation and update step.\n",
814800
" Remember that in PyTorch there are two steps to the training loop:\n",
815801
" 1. Backpropagate the loss\n",
816802
" 2. Update the model parameters using the optimizer\n",
817803
" '''\n",
818-
" loss.backward() # TODO\n",
819-
" optimizer.step() # TODO\n",
804+
" '''TODO'''\n",
820805
"\n",
821806
" return loss\n",
822807
"\n",
@@ -909,8 +894,7 @@
909894
" # Evaluation step (generating ABC text using the learned RNN model)\n",
910895
"\n",
911896
" '''TODO: convert the start string to numbers (vectorize)'''\n",
912-
" input_idx = [char2idx[s] for s in start_string] # TODO\n",
913-
" # input_idx = ['''TODO''']\n",
897+
" input_idx = ['''TODO'''] # TODO\n",
914898
" input_idx = torch.tensor([input_idx], dtype=torch.long).to(device)\n",
915899
"\n",
916900
" # Initialize the hidden state\n",
@@ -922,21 +906,17 @@
922906
"\n",
923907
" for i in tqdm(range(generation_length)):\n",
924908
" '''TODO: evaluate the inputs and generate the next character predictions'''\n",
925-
" predictions, state = model(input_idx, state, return_state=True)\n",
926-
" # predictions, hidden_state = model('''TODO''', '''TODO''', return_state=True)\n",
909+
" predictions, hidden_state = model('''TODO''', '''TODO''', return_state=True) # TODO\n",
927910
"\n",
928911
" # Remove the batch dimension\n",
929912
" predictions = predictions.squeeze(0)\n",
930913
"\n",
931914
" '''TODO: use a multinomial distribution to sample over the probabilities'''\n",
932-
" input_idx = torch.multinomial(torch.softmax(predictions, dim=-1), num_samples=1)\n",
933-
" # input_idx = torch.multinomial('''TODO''', dim=-1), num_samples=1)\n",
915+
" input_idx = torch.multinomial('''TODO''', dim=-1), num_samples=1) # TODO\n",
934916
"\n",
935917
" '''TODO: add the predicted character to the generated text!'''\n",
936918
" # Hint: consider what format the prediction is in vs. the output\n",
937-
" text_generated.append(idx2char[input_idx].item()) # TODO\n",
938-
" # text_generated.append('''TODO''')\n",
939-
"\n",
919+
" text_generated.append('''TODO''') # TODO\n",
940920
"\n",
941921
" return (start_string + ''.join(text_generated))"
942922
]
@@ -951,8 +931,7 @@
951931
"source": [
952932
"'''TODO: Use the model and the function defined above to generate ABC format text of length 1000!\n",
953933
" As you may notice, ABC files start with \"X\" - this may be a good start string.'''\n",
954-
"generated_text = generate_text(model, start_string=\"X\", generation_length=1000) # TODO\n",
955-
"# generated_text = generate_text('''TODO''', '''TODO''', '''TODO''')"
934+
"generated_text = generate_text('''TODO''', '''TODO''', '''TODO''') # TODO"
956935
]
957936
},
958937
{

0 commit comments

Comments
 (0)