Beispiel #1
0
 def sclstm(input, inputH, inputC, svpair):
     emb = emb_in(input)
     #initial state of lstm, h0, c0, sv0
     initial_state = (inputH, inputC, svpair)
     latent_vector = RecurrenceFrom(lstm_in, go_backwards=False, return_full_state=True)(*(initial_state + (emb,)))
     #output vector with vocab dimension
     output_vector = proj_in(latent_vector.outputs[0])
     output_vector = C.softmax(output_vector)
     return output_vector, latent_vector.outputs[0], latent_vector.outputs[1], latent_vector.outputs[2], svpair
Beispiel #2
0
def test_recurrence():
    inputAxis = Axis('inputAxis')
    stateAxis = Axis('stateAxis')
    InputSequence = SequenceOver[inputAxis]
    StateSequence = SequenceOver[stateAxis]

    # input and expected for both tests below
    x = np.reshape(np.arange(0, 25, dtype=np.float32), (1, 5, 5))
    exp = [[0.239151, 0.239151, 0.239151, 0.239151, 0.239151],
           [0.338713, 0.338713, 0.338713, 0.338713, 0.338713],
           [0.367456, 0.367456, 0.367456, 0.367456, 0.367456],
           [0.375577, 0.375577, 0.375577, 0.375577, 0.375577],
           [0.377891, 0.377891, 0.377891, 0.377891, 0.377891]]

    ####################################################
    # Test 1: Recurrence(): initial state is constant
    ####################################################
    # Note: We cannot use random init of the GRU parameters because random numbers will
    # depend on what previous tests were run. Hence, use a constant (which is not realistic).
    # TODO: Find out how to reset the random generator, then remove the constant init.
    R = Recurrence(GRU(5, init=0.05), go_backwards=False, initial_state=0.1)

    @Function
    @Signature(InputSequence[Tensor[5]])
    def F(x):
        return R(x)

    rt = F(x)
    np.testing.assert_array_almost_equal(
        rt[0], exp, decimal=6, err_msg='Error in Recurrence(GRU()) forward')

    ####################################################
    # Test 2: RecurrenceFrom(): initial state is data input
    ####################################################
    RF = RecurrenceFrom(GRU(5, init=0.05), go_backwards=False)

    @Function
    @Signature(s=StateSequence[Tensor[5]], x=InputSequence[Tensor[5]])
    def FF(s, x):
        return RF(s, x)

    s = np.ones(
        (1, 5, 5)
    ) * 0.1  # we pass the same value as the constant in the previous test to make the result the same
    rt = FF(s, x)
    np.testing.assert_array_almost_equal(
        rt[0],
        exp,
        decimal=6,
        err_msg='Error in RecurrenceFrom(GRU()) forward')