Example #1
0
    def get_final_states(self) -> RnnStateStorage:
        return self._states


class _MisoSeq2SeqWrapper(_Seq2SeqWrapper):
    """
    It inherits ``_Seq2SeqWrapper'' from AllenNLP such that we have more flexibility
    of defining our own seq2seq encoders.
    """
    def __init__(self, *args, **kwargs) -> None:
        super().__init__(*args, **kwargs)

    def __call__(self, **kwargs) -> PytorchSeq2SeqWrapper:
        return self.from_params(Params(kwargs))

    # Logic requires custom from_params
    def from_params(self, params: Params) -> _PytorchSeq2SeqWrapper:
        if not params.pop_bool('batch_first', True):
            raise ConfigurationError(
                "Our encoder semantics assumes batch is always first!")
        if self._module_class in self.PYTORCH_MODELS:
            params['batch_first'] = True
        stateful = params.pop_bool('stateful', False)
        module = self._module_class(**params.as_dict(infer_type_and_cast=True))
        return _PytorchSeq2SeqWrapper(module, stateful=stateful)


# pylint: disable=protected-access
Seq2SeqEncoder.register("miso_stacked_bilstm")(
    _MisoSeq2SeqWrapper(MisoStackedBidirectionalLstm))
Example #2
0
        if weight_dropout > 0.0:
            module = weight_drop_factory(self._module_class)(
                module_args=params.as_dict(infer_type_and_cast=True),
                weights=all_recurrent_weights,
                wdrop=weight_dropout,
                variational=variational,
            )
        else:
            module = self._module_class(**params.as_dict(
                infer_type_and_cast=True))

        return PytorchSeq2SeqWrapper(module, stateful=stateful)


Seq2SeqEncoder.register("weightdrop_gru")(_WeightDropSeq2SeqWrapper(
    torch.nn.GRU))
Seq2SeqEncoder.register("weightdrop_lstm")(_WeightDropSeq2SeqWrapper(
    torch.nn.LSTM))

if __name__ == '__main__':
    import torch

    # Input is (seq, batch, input)
    x = torch.autograd.Variable(torch.randn(2, 1, 10)).cuda()
    h0 = None

    ###

    print('Testing WeightDrop')
    print('=-=-=-=-=-=-=-=-=-=')
Example #3
0
from typing import Optional, Tuple
import torch
from torch.nn.utils.rnn import PackedSequence, pack_padded_sequence, pad_packed_sequence

from allennlp.modules.stacked_bidirectional_lstm import StackedBidirectionalLstm
from allennlp.common.checks import ConfigurationError
from allennlp.modules.seq2seq_encoders import PytorchSeq2SeqWrapper, Seq2SeqEncoder


Seq2SeqEncoder.register("miso_stacked_bilstm")
class MisoStackedBidirectionalLstmSeq2SeqEncoder(PytorchSeq2SeqWrapper):
    """
    Registered as a `Seq2SeqEncoder` with name "stacked_bidirectional_lstm".
    """

    def __init__(
        self,
        input_size: int,
        hidden_size: int,
        num_layers: int,
        recurrent_dropout_probability: float = 0.0,
        layer_dropout_probability: float = 0.0,
        use_highway: bool = True,
        stateful: bool = False,
    ) -> None:
        module = MisoStackedBidirectionalLstm(
            input_size=input_size,
            hidden_size=hidden_size,
            num_layers=num_layers,
            recurrent_dropout_probability=recurrent_dropout_probability,
            layer_dropout_probability=layer_dropout_probability,