(lstm, wemb_l, l, return_hidden=False, hc0=None, last_only=False)
[batch_size, max token length, dim_emb]
[batch_size, max token length, dim_emb]
[ batch_size max token length dim_emb ]
def encode(lstm, wemb_l, l, return_hidden=False, hc0=None, last_only=False):
    """ [batch_size, max token length, dim_emb]
    """
    bS, mL, eS = wemb_l.shape


    # sort before packking
    l = array(l)
    perm_idx = argsort(-l)
    perm_idx_inv = generate_perm_inv(perm_idx)

    # pack sequence

    packed_wemb_l = nn.utils.rnn.pack_padded_sequence(wemb_l[perm_idx, :, :],
                                                      l[perm_idx],
                                                      batch_first=True)

    # Time to encode
    if hc0 is not None:
        hc0 = (hc0[0][:, perm_idx], hc0[1][:, perm_idx])

    # ipdb.set_trace()
    packed_wemb_l = packed_wemb_l.float() # I don't know why..
    packed_wenc, hc_out = lstm(packed_wemb_l, hc0)
    hout, cout = hc_out

    # unpack
    wenc, _l = nn.utils.rnn.pad_packed_sequence(packed_wenc, batch_first=True)

    if last_only:
        # Take only final outputs for each columns.
        wenc = wenc[tuple(range(bS)), l[perm_idx] - 1]  # [batch_size, dim_emb]
        wenc.unsqueeze_(1)  # [batch_size, 1, dim_emb]

    wenc = wenc[perm_idx_inv]



    if return_hidden:
        # hout.shape = [number_of_directoin * num_of_layer, seq_len(=batch size), dim * number_of_direction ] w/ batch_first.. w/o batch_first? I need to see.
        hout = hout[:, perm_idx_inv].to(device)
        cout = cout[:, perm_idx_inv].to(device)  # Is this correct operation?

        return wenc, hout, cout
    else:
        return wenc
def encode ( lstm , wemb_l , l , return_hidden = False , hc0 = None , last_only = False ) : bS , mL , eS = wemb_l . shape # sort before packking l = array ( l ) perm_idx = argsort ( - l ) perm_idx_inv = generate_perm_inv ( perm_idx ) # pack sequence packed_wemb_l = nn . utils . rnn . pack_padded_sequence ( wemb_l [ perm_idx , : , : ] , l [ perm_idx ] , batch_first = True ) # Time to encode if hc0 is not None : hc0 = ( hc0 [ 0 ] [ : , perm_idx ] , hc0 [ 1 ] [ : , perm_idx ] ) # ipdb.set_trace() packed_wemb_l = packed_wemb_l . float ( ) # I don't know why.. packed_wenc , hc_out = lstm ( packed_wemb_l , hc0 ) hout , cout = hc_out # unpack wenc , _l = nn . utils . rnn . pad_packed_sequence ( packed_wenc , batch_first = True ) if last_only : # Take only final outputs for each columns. wenc = wenc [ tuple ( range ( bS ) ) , l [ perm_idx ] - 1 ] # [batch_size, dim_emb] wenc . unsqueeze_ ( 1 ) # [batch_size, 1, dim_emb] wenc = wenc [ perm_idx_inv ] if return_hidden : # hout.shape = [number_of_directoin * num_of_layer, seq_len(=batch size), dim * number_of_direction ] w/ batch_first.. w/o batch_first? I need to see. hout = hout [ : , perm_idx_inv ] . to ( device ) cout = cout [ : , perm_idx_inv ] . to ( device ) # Is this correct operation? return wenc , hout , cout else : return wenc