def select_last(x, lengths):
    '\n    Adaptively select the hidden state at the end of sentences\n    '
    batch_size = x.size(0)
    seq_length = x.size(1)
    mask = x.data.new().resize_as_(x.data).fill_(0)
    for i in range(batch_size):
        mask[i][(lengths[i] - 1)].fill_(1)
    mask = Variable(mask)
    x = x.mul(mask)
    x = x.sum(1).view(batch_size, x.size(2), x.size(3))
    return x
Adaptively select the hidden state at the end of sentences
def select_last(x, lengths):
    '\n    \n    '
    batch_size = x.size(0)
    seq_length = x.size(1)
    mask = x.data.new().resize_as_(x.data).fill_(0)
    for i in range(batch_size):
        mask[i][(lengths[i] - 1)].fill_(1)
    mask = Variable(mask)
    x = x.mul(mask)
    x = x.sum(1).view(batch_size, x.size(2), x.size(3))
    return x