def _sort_batch(self, obs):
    ' Extract instructions from a list of observations and sort by descending\n            sequence length (to enable PyTorch packing). '
    seq_tensor = np.array([ob['instr_encoding'] for ob in obs])
    seq_lengths = np.argmax((seq_tensor == padding_idx), axis=1)
    seq_lengths[(seq_lengths == 0)] = seq_tensor.shape[1]
    seq_tensor = torch.from_numpy(seq_tensor)
    seq_lengths = torch.from_numpy(seq_lengths)
    (seq_lengths, perm_idx) = seq_lengths.sort(0, True)
    sorted_tensor = seq_tensor[perm_idx]
    mask = (sorted_tensor == padding_idx)[:, :seq_lengths[0]]
    return (Variable(sorted_tensor, requires_grad=False).long().cuda(), mask.byte().cuda(), list(seq_lengths), list(perm_idx))
Extract instructions from a list of observations and sort by descending
sequence length (to enable PyTorch packing).
def _sort_batch(self, obs):
    ' Extract instructions from a list of observations and sort by descending\n            sequence length (to enable PyTorch packing). '
    seq_tensor = np.array([ob['instr_encoding'] for ob in obs])
    seq_lengths = np.argmax((seq_tensor == padding_idx), axis=1)
    seq_lengths[(seq_lengths == 0)] = seq_tensor.shape[1]
    seq_tensor = torch.from_numpy(seq_tensor)
    seq_lengths = torch.from_numpy(seq_lengths)
    (seq_lengths, perm_idx) = seq_lengths.sort(0, True)
    sorted_tensor = seq_tensor[perm_idx]
    mask = (sorted_tensor == padding_idx)[:, :seq_lengths[0]]
    return (Variable(sorted_tensor, requires_grad=False).long().cuda(), mask.byte().cuda(), list(seq_lengths), list(perm_idx))