def as_tensor(self,
padding_lengths: Dict[str, int],
cuda_device: int = -1,
for_training: bool = True) -> Dict[str, torch.Tensor]:
tensors = {}
desired_num_tokens = padding_lengths['num_tokens']
for indexer_name, indexer in self._token_indexers.items():
padded_array = indexer.pad_token_sequence(self._indexed_tokens[indexer_name],
desired_num_tokens, padding_lengths)
# We use the key of the indexer to recognise what the tensor corresponds to within the
# field (i.e. the result of word indexing, or the result of character indexing, for
# example).
# TODO(mattg): we might someday have a TokenIndexer that needs to use something other
# than a LongTensor here, and it's not clear how to signal that. Maybe we'll need to
# add a class method to TokenIndexer to tell us the type? But we can worry about that
# when there's a compelling use case for it.
tensor = Variable(torch.LongTensor(padded_array), volatile=not for_training)
tensors[indexer_name] = tensor if cuda_device == -1 else tensor.cuda(cuda_device)
return tensors
评论列表
文章目录