Buckets:
| #!/usr/bin/env python3 | |
| """Fix the encoder/decoder dimension issues in cell 2""" | |
| import json | |
| # Read notebook | |
| with open("notebooks/CA8.ipynb", "r") as f: | |
| notebook = json.load(f) | |
| # The problematic code is in cell index 4 (cell 2) | |
| cell = notebook["cells"][4] | |
| # Get current code | |
| code_lines = cell["source"] | |
| code = "".join(code_lines) | |
| # Find and replace the problematic encoder forward method | |
| old_encoder_forward = """ def forward(self, x, lengths=None): | |
| # x: (batch_size, seq_len) | |
| embedded = self.embedding(x) | |
| if lengths is not None: | |
| # Pack padded sequence for efficiency | |
| packed = nn.utils.rnn.pack_padded_sequence(embedded, lengths, batch_first=True, enforce_sorted=False) | |
| output, (hidden, cell) = self.lstm(packed) | |
| output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True) | |
| else: | |
| output, (hidden, cell) = self.lstm(embedded) | |
| # Combine forward and backward hidden states | |
| # hidden: (num_layers * 2, batch_size, hidden_dim) | |
| hidden = hidden.view(self.num_layers, 2, -1, self.hidden_dim) | |
| hidden = torch.cat([hidden[:, 0, :, :], hidden[:, 1, :, :]], dim=2) # Concatenate forward and backward | |
| return output, hidden, cell""" | |
| new_encoder_forward = """ def forward(self, x, lengths=None): | |
| # x: (batch_size, seq_len) | |
| embedded = self.embedding(x) | |
| if lengths is not None: | |
| # Pack padded sequence for efficiency | |
| packed = nn.utils.rnn.pack_padded_sequence(embedded, lengths, batch_first=True, enforce_sorted=False) | |
| output, (hidden, cell) = self.lstm(packed) | |
| output, _ = nn.utils.rnn.pad_packed_sequence(output, batch_first=True) | |
| else: | |
| output, (hidden, cell) = self.lstm(embedded) | |
| # Combine forward and backward hidden states | |
| # hidden: (num_layers * 2, batch_size, hidden_dim) | |
| # Reshape to separate directions: (num_layers, 2, batch_size, hidden_dim) | |
| batch_size = hidden.size(1) | |
| hidden = hidden.view(self.num_layers, 2, batch_size, self.hidden_dim) | |
| # Concatenate forward [0] and backward [1] on the last dimension | |
| hidden = torch.cat([hidden[:, 0, :, :], hidden[:, 1, :, :]], dim=2) # (num_layers, batch_size, hidden_dim * 2) | |
| # Similarly for cell state | |
| cell = cell.view(self.num_layers, 2, batch_size, self.hidden_dim) | |
| cell = torch.cat([cell[:, 0, :, :], cell[:, 1, :, :]], dim=2) | |
| return output, hidden, cell""" | |
| code = code.replace(old_encoder_forward, new_encoder_forward) | |
| # Update the cell | |
| cell["source"] = code.split("\n") | |
| # Add newlines back | |
| cell["source"] = [ | |
| line + "\n" if i < len(cell["source"]) - 1 else line | |
| for i, line in enumerate(cell["source"]) | |
| ] | |
| # Save notebook | |
| with open("notebooks/CA8.ipynb", "w") as f: | |
| json.dump(notebook, f, indent=1) | |
| print("✓ Fixed encoder forward method") | |
| print("✓ Notebook updated") | |
Xet Storage Details
- Size:
- 3.03 kB
- Xet hash:
- d97c5bb837de6d26f8b2ae1df904310e885fb51bba604a4133c7fe393e02f16d
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.