Giter Site home page Giter Site logo

Bidirectional masking? about haste HOT 4 CLOSED

lmnt-com avatar lmnt-com commented on May 23, 2024
Bidirectional masking?

from haste.

Comments (4)

PetrochukM avatar PetrochukM commented on May 23, 2024 1

Sorry and thank you. We stopped using Haste because in our case it was slower than PyTorch and it didn't provide a significant enough performance increase for us to justify the additional overhead.


PyTorch API does support bidirectional RNNs, and we use them.

In order to support reverse_sequence, we used roll and flip. We implemented our own roll function, see here:

def roll(tensor, shift, dim=-1):
    """ Shift a tensor along the specified dimension.
    TODO: Create a `Roll` module so that `indices` are not recomputed each time.
    Args:
        tensor (torch.Tensor [*, dim, *]): The tensor to shift.
        shift (torch.Tensor [*]): The number of elements to shift `dim`. This tensor must have one
            less dimensions than `tensor`.
        dim (int): The dimension to shift.
    Returns:
        tensor (torch.Tensor [*, dim, *]): The tensor that was shifted.
    """
    shift = shift.unsqueeze(dim)
    assert shift.dim() == tensor.dim(
    ), 'The `shift` tensor must be the same size as `tensor` without the `dim` dimension.'
    indices = torch.arange(0, tensor.shape[dim], device=tensor.device)
    dim = tensor.dim() + dim if dim < 0 else dim

    # EXAMPLE:
    # indicies.shape == (3,)
    # tensor.shape == (1, 2, 3, 4, 5)
    # indices_shape == [1, 1, 3, 1, 1]
    indices_shape = [1] * dim + [-1] + [1] * (tensor.dim() - dim - 1)
    indices = indices.view(*tuple(indices_shape)).expand(*tensor.shape)

    indices = (indices - shift) % tensor.shape[dim]
    return torch.gather(tensor, dim, indices)

PyTorch does not provide a lengths parameter; therefore, we needed to implement masking on our own in order to use the PyTorch LSTM. Thanks for providing that mechanism!

We were using LSTMCell with unrolled sequences; therefore, we needed to convert our lengths to binary masks for each call.

from haste.

PetrochukM avatar PetrochukM commented on May 23, 2024

Furthermore, can this be used like an LSTMCell (https://pytorch.org/docs/stable/nn.html#torch.nn.LSTMCell) with the lengths parameter being used more like a binary mask?

from haste.

sharvil avatar sharvil commented on May 23, 2024

The PyTorch API doesn't provide bidirectional RNNs out of the box (the TensorFlow API does). Unfortunately, PyTorch doesn't have a reverse_sequence function so you'll either need to write your own or use the implementation here: pytorch/pytorch#1794.

Suppose you have reverse_sequence. Then the bidirectional RNN would look something like this:

reversed_inputs = reverse_sequence(inputs, lengths)
rnn_fwd = LSTM(...)
rnn_bwd = LSTM(...)

y_fwd, _ = rnn_fwd(inputs, lengths)
y_bwd, _ = rnn_bwd(reversed_inputs, lengths)
y_bwd = reverse_sequence(y_bwd, lengths)
y = concatenate([y_fwd, y_bwd], dim=-1)

I'm not sure what you mean by the lengths parameter being used like a binary mask. AFAICT, the standard PyTorch LSTMCell doesn't accept a lengths parameter. If you have a one-hot vector indicating end-of-sequence, you can just just do argmax(one_hot) + 1 to convert to lengths.

from haste.

sharvil avatar sharvil commented on May 23, 2024

Closing due to inactivity.

from haste.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.