예제 #1
0
def gen_inputs(batch_size, ilen, idim, olen, odim):
    labels, ilens = sequence_utils.gen_random_sequence(batch_size, ilen, idim)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, idim).astype(dtype=np.float32))

    ys, _ = sequence_utils.gen_random_sequence(batch_size, olen, odim)
    return xs, ilens, ys
예제 #2
0
def main():
    import numpy as np
    np.random.seed(43)

    eprojs = 3
    dunits = 4
    att_dim = 5
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10
    dlayers = 2
    odim = 11
    sos = odim - 1
    eos = odim - 1
    aconv_chans = 7
    aconv_filts = 6

    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    hs = []
    for l in ilens:
        hs.append(np.random.rand(l, eprojs).astype(dtype=np.float32))

    ys, ilens = sequence_utils.gen_random_sequence(batch_size, sequence_length,
                                                   odim)

    def gen_test(model_fn, subname=None):
        model = model_fn()
        # Check if our modification is valid.
        expected, _ = model.original(hs, ys)
        actual = model.forward(hs, ys)
        assert np.allclose(expected.array, actual.array)

        testtools.generate_testcase(model_fn, [hs, ys], subname=subname)

    def model_fn():
        # att = AttDot(eprojs, dunits, att_dim)
        # dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att)
        dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att_dim)
        return dec

    gen_test(model_fn)

    testtools.generate_testcase(model_fn, [hs, ys], backprop=True)

    def model_fn():
        dec = Decoder(eprojs, odim, dlayers, dunits, sos, eos, att_dim,
                      aconv_chans, aconv_filts)
        return dec

    gen_test(model_fn, subname='attloc')

    testtools.generate_testcase(model_fn, [hs, ys],
                                subname='attloc',
                                backprop=True)
예제 #3
0
def main():
    import numpy as np
    np.random.seed(314)

    eprojs = 3
    dunits = 4
    att_dim = 5
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10
    aconv_chans = 7
    aconv_filts = 6

    model_fn = lambda: AttLoc(eprojs, dunits, att_dim, aconv_chans, aconv_filts
                              )
    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, eprojs).astype(dtype=np.float32))

    # Check if our modification is valid.
    model = model_fn()
    expected = model.original(xs, None, None)
    model.reset()
    actual = model.forward(xs, None, None)
    for e, a in zip(expected, actual):
        assert np.allclose(e.array, a.array)

    testtools.generate_testcase(model_fn, [xs, None, None])
예제 #4
0
def main():
    import numpy as np
    np.random.seed(314)

    idim = 5
    elayers = 2
    cdim = 3
    hdim = 7
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10

    model = BLSTM(idim, elayers, cdim, hdim, 0)
    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, idim).astype(dtype=np.float32))

    # Check if our modification is valid.
    expected = model.original(xs, ilens)
    actual = model.forward(xs, ilens)
    for e, a in zip(expected[0], actual[0]):
        assert np.allclose(e.array, a.array)
    assert np.allclose(expected[1], actual[1])

    testtools.generate_testcase(model, [xs, ilens])

    testtools.generate_testcase(BLSTMBackprop(idim, elayers, cdim, hdim, 0),
                                [xs, ilens],
                                backprop=True)
def main():
    import numpy as np
    np.random.seed(314)

    eprojs = 3
    dunits = 4
    att_dim = 5
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10

    model_fn = lambda: AttDot(eprojs, dunits, att_dim)
    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, eprojs).astype(np.float32))

    # Check if our modification is valid.
    expected = model_fn().original(xs, None, None)
    actual = model_fn().forward(xs, None, None)
    for e, a in zip(expected, actual):
        assert np.allclose(e.array, a.array)

    testtools.generate_testcase(model_fn, [xs, None, None])

    z = np.random.rand(batch_size, dunits).astype(np.float32)
    testtools.generate_testcase(
        lambda: AttDotBackprop(eprojs, dunits, att_dim), [xs, z, None],
        backprop=True)
예제 #6
0
def main():
    import numpy as np
    np.random.seed(314)

    idim = 5
    elayers = 2
    cdim = 3
    hdim = 7
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10

    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    xs = []
    for l in ilens:
        xs.append(np.random.rand(l, idim).astype(dtype=np.float32))

    testtools.generate_testcase(VGG2L(1), [xs, ilens], subname='VGGL')

    # TODO (hamaji): support lambda
    #testtools.generate_testcase(lambda: VGG2L(1), [xs, ilens], subname='VGGL_lambda')

    testtools.generate_testcase(VGG2LBackprop(1), [xs, ilens],
                                backprop=True,
                                subname='VGGL_backprop')
예제 #7
0
def main():
    np.random.seed(314)

    batch_size = 3
    sequence_length = 4
    num_vocabs = 10
    num_hidden = 5

    model_fn = MyLSTM(num_hidden, batch_size, sequence_length)

    labels, lengths = sequence_utils.gen_random_sequence(
        batch_size, sequence_length, num_vocabs)
    xs = []
    for l in lengths:
        xs.append(np.random.rand(l, num_hidden).astype(dtype=np.float32))

    h = np.zeros((batch_size, num_hidden), dtype=np.float32)
    c = np.zeros((batch_size, num_hidden), dtype=np.float32)
    mask = (np.expand_dims(np.arange(sequence_length), 0) < np.expand_dims(
        lengths, 1)).astype(np.float32)

    args = [xs, h, c, mask]

    testtools.generate_testcase(model_fn, args)
예제 #8
0
    eprojs = 3
    dunits = 4
    att_dim = 5
    batch_size = 3
    sequence_length = 4
    num_vocabs = 10
    dlayers = 2
    odim = 11
    sos = odim - 1
    eos = odim - 1
    aconv_chans = 7
    aconv_filts = 6

    labels, ilens = sequence_utils.gen_random_sequence(batch_size,
                                                       sequence_length,
                                                       num_vocabs)
    hs = []
    for l in ilens:
        hs.append(np.random.rand(l, eprojs).astype(dtype=np.float32))

    ys, ilens = sequence_utils.gen_random_sequence(batch_size, sequence_length,
                                                   odim)

    def gen_test(model_fn, subname=None):
        model = model_fn()
        # Check if our modification is valid.
        expected, _ = model.original(hs, ys)
        actual = model.forward(hs, ys)
        assert np.allclose(expected.array, actual.array)