本文整理汇总了Python中torch.nn.utils.rnn.pad_sequence方法的典型用法代码示例。如果您正苦于以下问题:Python rnn.pad_sequence方法的具体用法?Python rnn.pad_sequence怎么用?Python rnn.pad_sequence使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类torch.nn.utils.rnn
的用法示例。
在下文中一共展示了rnn.pad_sequence方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: collect_text_batch
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def collect_text_batch(batch, mode):
'''Collects a batch of text, should be list of list of int token
e.g. [txt1 <list>,txt2 <list>,...] '''
# Bucketed batch should be [[txt1, txt2,...]]
if type(batch[0][0]) is list:
batch = batch[0]
# Half batch size if input to long
if len(batch[0]) > HALF_BATCHSIZE_TEXT_LEN and mode == 'train':
batch = batch[:len(batch)//2]
# Read batch
text = [torch.LongTensor(b) for b in batch]
# Zero-padding
text = pad_sequence(text, batch_first=True)
return text
示例2: __call__
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def __call__(self, batch):
sorted_batch = sorted(batch, key=lambda x: x[0].shape[0], reverse=True)
seqs = [x[0] for x in sorted_batch]
seqs_padded = pad_sequence(seqs, batch_first=True, padding_value=self.seq_pad_value)
x_lengths = torch.LongTensor([len(x) for x in seqs])
labels = list(map(lambda x: x[1], sorted_batch))
labels_padded = pad_sequence(labels, batch_first=True, padding_value=self.label_pad_value)
y_lengths = torch.LongTensor([len(x) for x in labels])
labels2 = list(map(lambda x: x[2], sorted_batch))
labels2_padded = pad_sequence(labels2, batch_first=True, padding_value=self.label2_pad_value)
y2_lengths = torch.LongTensor([len(x) for x in labels2])
return seqs_padded, labels_padded, labels2_padded, \
x_lengths, y_lengths, y2_lengths
示例3: __call__
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def __call__(self, batch):
sorted_batch = sorted(batch, key=lambda x: x[0].shape[0], reverse=True)
seqs = [x[0] for x in sorted_batch]
seqs_padded = pad_sequence(seqs, batch_first=True, padding_value=self.seq_pad_value)
x_lengths = torch.LongTensor([len(x) for x in seqs])
labels = list(map(lambda x: x[1], sorted_batch))
labels_padded = pad_sequence(labels, batch_first=True, padding_value=self.label_pad_value)
y_lengths = torch.LongTensor([len(x) for x in labels])
labels2 = list(map(lambda x: x[2], sorted_batch))
labels2_padded = pad_sequence(labels2, batch_first=True, padding_value=self.label2_pad_value)
y2_lengths = torch.LongTensor([len(x) for x in labels2])
labels3 = list(map(lambda x: x[3], sorted_batch))
labels3_padded = pad_sequence(labels3, batch_first=True, padding_value=self.label3_pad_value)
y3_lengths = torch.LongTensor([len(x) for x in labels3])
labels4 = list(map(lambda x: x[4], sorted_batch))
labels4_padded = pad_sequence(labels4, batch_first=True, padding_value=self.label4_pad_value)
y4_lengths = torch.LongTensor([len(x) for x in labels4])
return seqs_padded, labels_padded, labels2_padded, labels3_padded, labels4_padded,\
x_lengths, y_lengths, y2_lengths, y3_lengths, y4_lengths
示例4: transform
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def transform(self, sents):
"""Converts lists of tokens into a Tensor of embedding indices.
Args:
sents: A list of lists of tokens (representing sentences)
NOTE: These sentences should already be marked using the
mark_entities() helper.
Returns:
X: A Tensor of shape (num_items, max_seq_len)
"""
def convert(tokens):
return torch.tensor([self.vocab.stoi[t] for t in tokens], dtype=torch.long)
if self.vocab is None:
raise Exception(
"Must run .fit() for .fit_transform() before " "calling .transform()."
)
seqs = sorted([convert(s) for s in sents], key=lambda x: -len(x))
X = torch.LongTensor(pad_sequence(seqs, batch_first=True))
return X
示例5: __iter__
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def __iter__(self):
src_list = list()
tgt_list = list()
# sampler is RandomSampler
for i in self.sampler:
self.count += 1
src, tgt = self.sampler.data_source[i]
src_list.append(src)
tgt_list.append(tgt)
if self.count % self.batch_size == 0:
assert len(src_list) == self.batch_size
src = rnn.pad_sequence(src_list, batch_first=True, padding_value=self.pad_id)
tgt = rnn.pad_sequence(tgt_list, batch_first=True, padding_value=self.pad_id)
src_list.clear()
tgt_list.clear()
yield src, tgt
示例6: test_viterbi_two_lengths
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def test_viterbi_two_lengths(operator):
states1, emissions1, theta1 = make_data(10)
states2, emissions2, theta2 = make_data(5)
lengths = torch.LongTensor([10, 5])
theta1 = torch.from_numpy(theta1)
theta2 = torch.from_numpy(theta2)
theta1.requires_grad_()
theta2.requires_grad_()
W = pad_sequence([theta1, theta2])
viterbi = Viterbi(operator)
v = viterbi(W, lengths=lengths)
s = v.sum()
s.backward()
decoded1 = torch.argmax(theta1.grad.sum(dim=2), dim=1).numpy()
decoded2 = torch.argmax(theta2.grad.sum(dim=2), dim=1).numpy()
assert np.all(decoded1 == states1)
assert np.all(decoded2 == states2)
示例7: test_grad_hessian_viterbi_two_samples
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def test_grad_hessian_viterbi_two_samples(operator):
states1, emissions1, theta1 = make_data(10)
states2, emissions2, theta2 = make_data(5)
lengths = torch.LongTensor([10, 5])
theta1 = torch.from_numpy(theta1)
theta2 = torch.from_numpy(theta2)
theta1.requires_grad_()
theta2.requires_grad_()
viterbi = Viterbi(operator)
def func(theta1_, theta2_):
W = pad_sequence([theta1_, theta2_])
return viterbi(W, lengths)
gradcheck(func, (theta1, theta2))
gradgradcheck(func, (theta1, theta2))
示例8: list2padseq
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def list2padseq(ls, longtensor, padding_value=0):
assert len(ls[0].size()) == 2, "need to rewrite sorted_mask.unsqueeze(-1).view(-1) cuz it's more than 1 dim"
order, sorted_ls = zip(*sorted(enumerate(ls), key=lambda x: -len(x[1])))
rev_order, _ = zip(*sorted(enumerate(order), key=lambda x: x[1]))
rev_order = torch.tensor(rev_order, dtype=longtensor.dtype, device=longtensor.device)
# mask of sorted_ls: one dim less than ls
sorted_mask = [torch.ones_like(i[..., 0].squeeze(-1)).view(-1) for i in sorted_ls]
padded = pad_sequence(sorted_ls, batch_first=True, padding_value=padding_value)
padded = padded[rev_order]
sorted_mask = pad_sequence(sorted_mask, batch_first=True)
padded_mask = sorted_mask[rev_order]
return padded, padded_mask.float()
示例9: collate
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def collate(features):
input_ids = pad_sequence([torch.tensor(f.input_ids, dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
position_ids = pad_sequence([torch.tensor(f.position_ids,
dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
token_type_ids = pad_sequence([torch.tensor(f.token_type_ids,
dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
labels = pad_sequence([torch.tensor(f.lm_labels, dtype=torch.long)
for f in features],
batch_first=True, padding_value=-1)
return (input_ids, position_ids, token_type_ids, labels)
示例10: _batch_feature
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def _batch_feature(self, features):
input_ids = pad_sequence([torch.tensor(f.choices_features['input_ids'],
dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
position_ids = pad_sequence(
[torch.tensor(f.choices_features['position_ids'], dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
token_type_ids = pad_sequence(
[torch.tensor(f.choices_features['token_type_ids'],
dtype=torch.long)
for f in features],
batch_first=True, padding_value=0)
labels = pad_sequence([torch.tensor(f.lm_labels, dtype=torch.long)
for f in features],
batch_first=True, padding_value=-1)
context_len = torch.tensor([f.context_len for f in features],
dtype=torch.long)
response_len = torch.tensor([f.response_len for f in features],
dtype=torch.long)
return (input_ids, position_ids, token_type_ids, labels,
context_len, response_len)
示例11: collate_audio
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def collate_audio(batch):
batch = sorted(batch, key=lambda b: b[0].shape[0], reverse=True)
n = len(batch)
xs = []
ys = []
xn = torch.empty(n, dtype=torch.int)
yn = torch.empty(n, dtype=torch.int)
for i, (x, y) in enumerate(batch):
xs.append(x)
ys.append(y)
xn[i] = len(x)
yn[i] = len(y)
# N x 1 x D x T
xs = pad_sequence(xs, batch_first=True)
xs = xs.unsqueeze(dim=1).transpose(2, 3)
# N x S
ys = pad_sequence(ys, batch_first=True)
return xs, ys, xn, yn
示例12: custom_collate_fn
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def custom_collate_fn(batch):
r"""
Custom collate function ordering the element in a batch by descending length
:param batch: batch from pytorch dataloader
:return: the ordered batch
"""
x_adj, x_coord, y_adj, y_coord, img, seq_len, ids = zip(*batch)
x_adj = pad_sequence(x_adj, batch_first=True, padding_value=0)
x_coord = pad_sequence(x_coord, batch_first=True, padding_value=0)
y_adj = pad_sequence(y_adj, batch_first=True, padding_value=0)
y_coord = pad_sequence(y_coord, batch_first=True, padding_value=0)
img, seq_len = torch.stack(img), torch.stack(seq_len)
seq_len, perm_index = seq_len.sort(0, descending=True)
x_adj = x_adj[perm_index]
x_coord = x_coord[perm_index]
y_adj = y_adj[perm_index]
y_coord = y_coord[perm_index]
img = img[perm_index]
ids = [ids[perm_index[i]] for i in range(perm_index.shape[0])]
return x_adj, x_coord, y_adj, y_coord, img, seq_len, ids
示例13: custom_collate_fn_with_coordinates
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def custom_collate_fn_with_coordinates(batch):
r"""
Custom collate function ordering the element in a batch by descending length
:param batch: batch from pytorch dataloader
:return: the ordered batch
"""
x_adj, x_coord, y_adj, y_coord, img, seq_len, ids, original_xy = zip(*batch)
x_adj = pad_sequence(x_adj, batch_first=True, padding_value=0)
x_coord = pad_sequence(x_coord, batch_first=True, padding_value=0)
y_adj = pad_sequence(y_adj, batch_first=True, padding_value=0)
y_coord = pad_sequence(y_coord, batch_first=True, padding_value=0)
img, seq_len = torch.stack(img), torch.stack(seq_len)
seq_len, perm_index = seq_len.sort(0, descending=True)
x_adj = x_adj[perm_index]
x_coord = x_coord[perm_index]
y_adj = y_adj[perm_index]
y_coord = y_coord[perm_index]
img = img[perm_index]
original_xy = original_xy[perm_index]
ids = [ids[perm_index[i]] for i in range(perm_index.shape[0])]
return x_adj, x_coord, y_adj, y_coord, img, seq_len, ids, original_xy
示例14: mse_loss_for_variable_length_data
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def mse_loss_for_variable_length_data():
def loss_function(ipt, target, n_frames_list):
"""Calculate the MSE loss for variable length dataset.
"""
E = 1e-7
with torch.no_grad():
masks = []
for n_frames in n_frames_list:
masks.append(torch.ones((n_frames, target.size(2)), dtype=torch.float32)) # the shape is (T, F)
binary_mask = pad_sequence(masks, batch_first=True).cuda()
masked_ipt = ipt * binary_mask
masked_target = target * binary_mask
return ((masked_ipt - masked_target) ** 2).sum() / (binary_mask.sum() + E)
return loss_function
示例15: batchify
# 需要导入模块: from torch.nn.utils import rnn [as 别名]
# 或者: from torch.nn.utils.rnn import pad_sequence [as 别名]
def batchify(
data: List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]
) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
"""custom collate_fn for DataLoader
Args:
data (List[Tuple[torch.Tensor, torch.Tensor, torch.Tensor]]): list of tuples of torch.Tensors
Returns:
qpair (Tuple[torch.Tensor, torch.Tensor, torch.Tensor]): tuple of torch.Tensors
"""
data = list(zip(*data))
queries_a, queries_b, is_duplicates = data
queries_a = pad_sequence(queries_a, batch_first=True, padding_value=1)
queries_b = pad_sequence(queries_b, batch_first=True, padding_value=1)
is_duplicates = torch.stack(is_duplicates, 0)
return queries_a, queries_b, is_duplicates