Ejemplo n.º 1
0
def test_rnn(
    rnn_type="LSTM",
    input_size=500,
    hidden_size=50,
    sequence_length=10,
    batch_size=8,
    num_layers=2,
    bidirectional=True,
    lr=0.001,
):

    rnn_model = getattr(nn, rnn_type)(input_size,
                                      hidden_size,
                                      num_layers,
                                      bidirectional=bidirectional)

    num_directions = 2 if bidirectional else 1

    model = PyTorchWrapperRNN(rnn_model)
    sgd = SGD(model.ops, lr)

    # Inputs and expected ouput
    X, Y = generate_data(sequence_length, batch_size, input_size, hidden_size,
                         num_directions)

    # Test output shape
    check_Y_shape(model, X, Y, sequence_length, batch_size, hidden_size,
                  num_directions)

    # Test initializing hidden
    initial_hidden = init_hidden(rnn_model, rnn_type, batch_size,
                                 num_layers * num_directions, hidden_size)
    # Test if sum of rnn output converges to with initial hidden
    check_learns_zero_output_rnn(model, sgd, X, Y, initial_hidden)
Ejemplo n.º 2
0
def PyTorchBiLSTM(nO, nI, depth, dropout=0.2):
    import torch.nn
    from thinc.api import with_square_sequences
    from thinc.extra.wrappers import PyTorchWrapperRNN

    if depth == 0:
        return layerize(noop())
    model = torch.nn.LSTM(nI, nO // 2, depth, bidirectional=True, dropout=dropout)
    return with_square_sequences(PyTorchWrapperRNN(model))
Ejemplo n.º 3
0
def PyTorchBiLSTM(nO, nI, depth, dropout=0.2):
    if depth == 0:
        return layerize(noop())
    model = torch.nn.LSTM(nI,
                          nO // 2,
                          depth,
                          bidirectional=True,
                          dropout=dropout)
    return with_square_sequences(PyTorchWrapperRNN(model))
Ejemplo n.º 4
0
def TorchBiLSTMEncoder(config):
    import torch.nn
    from thinc.extra.wrappers import PyTorchWrapperRNN

    width = config["width"]
    depth = config["depth"]
    if depth == 0:
        return layerize(noop())
    return with_square_sequences(
        PyTorchWrapperRNN(torch.nn.LSTM(width, width // 2, depth, bidirectional=True))
    )
Ejemplo n.º 5
0
def PyTorchBiLSTM(nO, nI, depth):
    model = torch.nn.LSTM(nI, nO // 2, depth, bidirectional=True)
    return with_square_sequences(PyTorchWrapperRNN(model))