def make_activations(inputs, width):
    """Make an Activations object with the right sizing for an input."""
    lh = numpy.ones((inputs.data.shape[0], width), dtype="f")
    po = numpy.ones((len(inputs.lengths), width), dtype="f")
    return Activations(
        RaggedArray(lh, inputs.lengths),
        RaggedArray(po, [1 for _ in inputs.lengths]))
def test_ragged_to_padded(extra_dims, lengths, pad_to, expected_shape):
    arr = RaggedArray(numpy.ones((sum(lengths), ) + extra_dims), lengths)
    if pad_to > 1 and pad_to < max(lengths):
        with pytest.raises(ValueError):
            padded = arr.to_padded(to=pad_to)
    else:
        padded = arr.to_padded(to=pad_to)
        assert padded.shape == expected_shape
def test_ragged_from_truncated(shape, lengths, expected_shape):
    truncated = numpy.ones(shape, dtype="i")
    for i, length in enumerate(lengths):
        truncated[i, length:] = 0
    ragged = RaggedArray.from_truncated(truncated, lengths)
    assert ragged.data.shape == expected_shape
    assert ragged.data.sum() == truncated.sum()
def test_with_length_batching(max_words, lengths, expected_batches, width=12):
    inputs = RaggedArray(numpy.arange(sum(lengths), dtype="i"), lengths)
    # The record_fwd and record_bwd variables will record the calls into the
    # forward and backward passes, respectively. We can use this to check that
    # the batching was done correctly.
    dummy, record_fwd, record_bwd = create_dummy_model(width)
    batched_dummy = with_length_batching(dummy, max_words)
    outputs, backprop = batched_dummy.begin_update(inputs)
    assert len(record_fwd) == len(expected_batches)
    assert [b.lengths for b in record_fwd] == expected_batches
    assert outputs.lh.data.shape == (inputs.data.shape[0], width)
    assert outputs.po.data.shape == (len(inputs.lengths), width)
    none = backprop(outputs)
    assert none is None
    assert len(record_bwd) == len(expected_batches)
def test_wrapper_from_pretrained(name, model, inputs):
    outputs, backprop = model.begin_update(inputs)
    assert outputs.has_lh
    optimizer = Adam(model.ops, 0.001)
    d_outputs = Activations(outputs.lh, RaggedArray.blank())
    backprop(d_outputs, sgd=optimizer)
def inputs(ids):
    return RaggedArray(ids, [len(ids)])