Exemple #1
0
def test_transformer_decoder_block3():
    """ Typical use case: encoder feed into decoder with multi layer """
    a = C.sequence.input_variable(10)
    b = C.sequence.input_variable(10)

    encoder_block = TransformerEncoderBlock(num_heads=2, model_dim=10, intermediate_dim=30, dropout_rate=0.1)
    decoder_block1 = TransformerDecoderBlock(num_heads=2, model_dim=10, intermediate_dim=30, dropout_rate=0.1,
                                             obey_sequence_order=True, max_seq_len=100)
    decoder_block2 = TransformerDecoderBlock(num_heads=2, model_dim=10, intermediate_dim=30, dropout_rate=0.1,
                                             obey_sequence_order=True, max_seq_len=100)

    encoded = encoder_block(a)
    decoded = decoder_block1(encoded, b)
    decoded = decoder_block2(encoded, decoded)

    assert decoded.shape == (10, )

    n = [np.random.random((2, 10)).astype(np.float32),
         np.random.random((4, 10)).astype(np.float32),
         np.random.random((6, 10)).astype(np.float32)]
    m = [np.random.random((2, 10)).astype(np.float32),
         np.random.random((4, 10)).astype(np.float32),
         np.random.random((6, 10)).astype(np.float32)]

    results = decoded.eval({a: n, b: m})
Exemple #2
0
def test_transformer_decoder_block2():
    """ default settings: encoder block output feed into decoder block """
    a = C.sequence.input_variable(10)
    b = C.sequence.input_variable(10)

    encoder_block = TransformerEncoderBlock(num_heads=2,
                                            model_dim=10,
                                            intermediate_dim=30,
                                            dropout_rate=0.1)
    decoder_block = TransformerDecoderBlock(num_heads=2,
                                            model_dim=10,
                                            intermediate_dim=30,
                                            dropout_rate=0.1,
                                            obey_sequence_order=True,
                                            max_seq_len=100)

    encoded = encoder_block(a)
    decoded = decoder_block(encoded, b)

    assert decoded.shape == (10, )

    n = [
        np.random.random((2, 10)).astype(np.float32),
        np.random.random((4, 10)).astype(np.float32),
        np.random.random((6, 10)).astype(np.float32)
    ]
    m = [
        np.random.random((2, 10)).astype(np.float32),
        np.random.random((4, 10)).astype(np.float32),
        np.random.random((6, 10)).astype(np.float32)
    ]

    results = decoded.eval({a: n, b: m})