def test_rnn():
    def batch_check(x, modes, params):
        for m, p in zip(modes, params):
            #x.attach_grad()
            #with mx.autograd.record():
            y = npx.rnn(data=x, parameters=p, mode=m, \
                state=np.random.normal(0, 1, (1, 4, 1)), \
                state_size=1, num_layers=1)
            assert y.shape == (INT_OVERFLOW, 4, 1)
            assert type(y).__name__ == 'ndarray'
            #y.backward()
            #assert x.grad.shape == x.shape
            #assert type(x.grad[0]).__name__ == 'ndarray'

    data = np.random.normal(0, 1, (INT_OVERFLOW, 4, 4))
    modes = ['rnn_relu', 'rnn_tanh', 'gru']
    params = [np.random.normal(0, 1, (7,)), \
        np.random.normal(0, 1, (7,)), \
        np.random.normal(0, 1, (21,))]
    batch_check(data, modes, params)
    # check lstm seperately because it has an extra param
    out = npx.rnn(data=data, parameters=np.random.normal(0, 1, (28,)), \
        mode='lstm', \
        state=np.random.normal(0, 1, (1, 4, 1)), \
        state_cell=np.random.normal(0, 1, (1, 4, 1)), \
        state_size=1, num_layers=1)
    assert out.shape == (INT_OVERFLOW, 4, 1)
    assert type(out[0]).__name__ == 'ndarray'
 def batch_check(x, modes, params):
     for m, p in zip(modes, params):
         #x.attach_grad()
         #with mx.autograd.record():
         y = npx.rnn(data=x, parameters=p, mode=m, \
             state=np.random.normal(0, 1, (1, 4, 1)), \
             state_size=1, num_layers=1)
         assert y.shape == (INT_OVERFLOW, 4, 1)
         assert type(y).__name__ == 'ndarray'
 def batch_check(x, modes, params):
     state = np.random.normal(0, 1, (1, BAT, L_STA))
     for m, p in zip(modes, params):
         x.attach_grad()
         with mx.autograd.record():
             y = npx.rnn(data=x, parameters=p, mode=m, \
                 state=state, state_size=L_STA, num_layers=1)
         assert y.shape == (L_SEQ, BAT, L_STA)
         y.backward()
         npx.waitall()
def test_rnn_gru():
    L_SEQ, BAT, L_INP, L_STA = 2**20, 4, 2**10, 2
    data = np.random.uniform(-1, 1, (L_SEQ, BAT, L_INP))
    state = np.random.normal(0, 1, (1, BAT, L_STA))
    params = np.random.normal(0, 1, (6168, ))
    data.attach_grad()
    with mx.autograd.record():
        out = npx.rnn(data=data, parameters=params, mode='gru', \
            state=state, state_size=L_STA, num_layers=1)
    assert out.shape == (L_SEQ, BAT, L_STA)
    out.backward()
    npx.waitall()