Skip to content

Instantly share code, notes, and snippets.

View haozheji's full-sized avatar
:shipit:

Haozhe Ji haozheji

:shipit:
View GitHub Profile
@haozheji
haozheji / pad_packed_demo.py
Created December 4, 2019 11:06 — forked from HarshTrivedi/pad_packed_demo.py
Minimal tutorial on packing (pack_padded_sequence) and unpacking (pad_packed_sequence) sequences in pytorch.
import torch
from torch import LongTensor
from torch.nn import Embedding, LSTM
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
## We want to run LSTM on a batch of 3 character sequences ['long_str', 'tiny', 'medium']
#
# Step 1: Construct Vocabulary
# Step 2: Load indexed data (list of instances, where each instance is list of character indices)