Esempio n. 1
0
def q2():
    from code_base.rnn_layers import rnn_step_forward, rnn_step_backward
    import numpy as np

    x_shape = (3, 874)
    Wx_shape = (874, 128)
    h_shape = (3, 128)
    Wh_shape = (128, 128)
    b_shape = (128,)
    x = np.loadtxt('./input_files/x.csv', delimiter=',')
    x = x.reshape(x_shape)
    Wx = np.loadtxt('./input_files/Wx.csv', delimiter=',')
    Wx = Wx.reshape(Wx_shape)
    prev_h = np.loadtxt('./input_files/prev_h.csv', delimiter=',')
    prev_h = prev_h.reshape(h_shape)
    Wh = np.loadtxt('./input_files/Wh.csv', delimiter=',')
    Wh = Wh.reshape(Wh_shape)
    b = np.loadtxt('./input_files/b.csv', delimiter=',')
    out, cache = rnn_step_forward(x, prev_h, Wx, Wh, b)
    dhout = np.loadtxt('./input_files/dho.csv', delimiter=',')
    dx, dh, dWx, dWh, db = rnn_step_backward(dhout, cache)
    np.savetxt('./output_files/rnn_step_backward_out_dx.csv', dx.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_step_backward_out_dh.csv', dh.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_step_backward_out_dwx.csv', dWx.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_step_backward_out_dwh.csv', dWh.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_step_backward_out_db.csv', db.ravel(), delimiter=',')
Esempio n. 2
0
def verify_rnn_step_backward():
    N, D, H = 4, 5, 6
    x = np.random.randn(N, D)
    h = np.random.randn(N, H)
    Wx = np.random.randn(D, H)
    Wh = np.random.randn(H, H)
    b = np.random.randn(H)
    out, cache = rnn_step_forward(x, h, Wx, Wh, b)
    dnext_h = np.random.randn(*out.shape)
    fx = lambda x: rnn_step_forward(x, h, Wx, Wh, b)[0]
    fh = lambda prev_h: rnn_step_forward(x, h, Wx, Wh, b)[0]
    fWx = lambda Wx: rnn_step_forward(x, h, Wx, Wh, b)[0]
    fWh = lambda Wh: rnn_step_forward(x, h, Wx, Wh, b)[0]
    fb = lambda b: rnn_step_forward(x, h, Wx, Wh, b)[0]
    dx_num = eval_numerical_gradient_array(fx, x, dnext_h)
    dprev_h_num = eval_numerical_gradient_array(fh, h, dnext_h)
    dWx_num = eval_numerical_gradient_array(fWx, Wx, dnext_h)
    dWh_num = eval_numerical_gradient_array(fWh, Wh, dnext_h)
    db_num = eval_numerical_gradient_array(fb, b, dnext_h)
    dx, dprev_h, dWx, dWh, db = rnn_step_backward(dnext_h, cache)
    print('dx error: ', rel_error(dx_num, dx))
    print('dprev_h error: ', rel_error(dprev_h_num, dprev_h))
    print('dWx error: ', rel_error(dWx_num, dWx))
    print('dWh error: ', rel_error(dWh_num, dWh))
    print('db error: ', rel_error(db_num, db))
Esempio n. 3
0
def verify_rnn_step_forward():
    from code_base.rnn_layers import rnn_step_forward
    from code_base.layer_utils import rel_error
    import numpy as np

    N, D, H = 3, 10, 4
    x = np.linspace(-0.4, 0.7, num=N * D).reshape(N, D)
    prev_h = np.linspace(-0.2, 0.5, num=N * H).reshape(N, H)
    Wx = np.linspace(-0.1, 0.9, num=D * H).reshape(D, H)
    Wh = np.linspace(-0.3, 0.7, num=H * H).reshape(H, H)
    b = np.linspace(-0.2, 0.4, num=H)
    next_h, _ = rnn_step_forward(x, prev_h, Wx, Wh, b)
    expected_next_h = np.asarray(
        [[-0.58172089, -0.50182032, -0.41232771, -0.31410098],
         [0.66854692, 0.79562378, 0.87755553, 0.92795967],
         [0.97934501, 0.99144213, 0.99646691, 0.99854353]])
    print('next_h error: ', rel_error(expected_next_h, next_h))
Esempio n. 4
0
def q1():
    x_shape = (3, 874)
    Wx_shape = (874, 128)
    h_shape = (3, 128)
    Wh_shape = (128, 128)
    b_shape = (128,)
    x = np.loadtxt('./input_files/x.csv', delimiter=',')
    x = x.reshape(x_shape)
    Wx = np.loadtxt('./input_files/Wx.csv', delimiter=',')
    Wx = Wx.reshape(Wx_shape)
    prev_h = np.loadtxt('./input_files/prev_h.csv', delimiter=',')
    prev_h = prev_h.reshape(h_shape)
    Wh = np.loadtxt('./input_files/Wh.csv', delimiter=',')
    Wh = Wh.reshape(Wh_shape)
    b = np.loadtxt('./input_files/b.csv', delimiter=',')
    out, _ = rnn_step_forward(x, prev_h, Wx, Wh, b)
    np.savetxt('./output_files/rnn_step_forward_out.csv', out.ravel(), delimiter=',')
Esempio n. 5
0
# # RNN Step Forward

# In[1]:

from code_base.rnn_layers import rnn_step_forward
from code_base.layer_utils import rel_error
import numpy as np

N, D, H = 3, 10, 4
x = np.linspace(-0.4, 0.7, num=N * D).reshape(N, D)
prev_h = np.linspace(-0.2, 0.5, num=N * H).reshape(N, H)
Wx = np.linspace(-0.1, 0.9, num=D * H).reshape(D, H)
Wh = np.linspace(-0.3, 0.7, num=H * H).reshape(H, H)
b = np.linspace(-0.2, 0.4, num=H)
next_h, _ = rnn_step_forward(x, prev_h, Wx, Wh, b)
expected_next_h = np.asarray(
    [[-0.58172089, -0.50182032, -0.41232771, -0.31410098],
     [0.66854692, 0.79562378, 0.87755553, 0.92795967],
     [0.97934501, 0.99144213, 0.99646691, 0.99854353]])
print('next_h error: ', rel_error(expected_next_h, next_h))

# In[ ]:

from code_base.rnn_layers import rnn_step_forward
import numpy as np

x_shape = (3, 874)  # N, D
Wx_shape = (874, 128)  # D, H
h_shape = (3, 128)  # N, H
Wh_shape = (128, 128)  # H, H