Skip to content

Instantly share code, notes, and snippets.

@tsuchm
Created June 9, 2020 11:22
Show Gist options
  • Save tsuchm/8b1817f1331a3e2d07b666877bb6b8e5 to your computer and use it in GitHub Desktop.
Save tsuchm/8b1817f1331a3e2d07b666877bb6b8e5 to your computer and use it in GitHub Desktop.
import torch
import torch.nn as nn
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
n_vocab = 3
n_layers = 1
n_embed = 5
def padding_test(x_padding):
embedding = nn.Embedding(n_vocab, n_embed, padding_idx=x_padding)
inputs = [[1,1,2,1,1,2,1],[1,1,2]]
inputs = nn.utils.rnn.pad_sequence([torch.tensor(e, device=device) for e in inputs],
batch_first=True,
padding_value=x_padding)
print(inputs)
embedded = embedding(inputs)
print(embedded)
# When using 0 for padding, PyTorch works well.
# However, this means that we cannot use 0 as the index of the word.
padding_test(0)
# When using -1 for padding, PyTorch raises "index out of range" error.
padding_test(-1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment