Esempio n. 1
0
def q4():
    from code_base.rnn_layers import rnn_forward, rnn_backward
    import numpy as np

    x_all_shape = (3, 5, 874)
    Wx_shape = (874, 128)
    h_shape = (3, 128)
    Wh_shape = (128, 128)
    b_shape = (128,)
    dh_all_shape = (3, 5, 128)
    x_all = np.loadtxt('./input_files/x_all.csv', delimiter=',')
    x_all = x_all.reshape(x_all_shape)
    Wx = np.loadtxt('./input_files/Wx.csv', delimiter=',')
    Wx = Wx.reshape(Wx_shape)
    h0 = np.loadtxt('./input_files/prev_h.csv', delimiter=',')
    h0 = h0.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_forward(x_all, h0, Wx, Wh, b)
    dhout = np.loadtxt('./input_files/dho_all.csv', delimiter=',')
    dhout = dhout.reshape(dh_all_shape)
    dx_all, dh0, dWx, dWh, db = rnn_backward(dhout, cache)
    np.savetxt('./output_files/rnn_backward_out_dx.csv', dx_all.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_backward_out_dh0.csv', dh0.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_backward_out_dwx.csv', dWx.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_backward_out_dwh.csv', dWh.ravel(), delimiter=',')
    np.savetxt('./output_files/rnn_backward_out_db.csv', db.ravel(), delimiter=',')
Esempio n. 2
0
def verify_rnn_backward():
    from code_base.rnn_layers import rnn_forward, rnn_backward
    import numpy as np

    N, D, T, H = 2, 3, 10, 5
    x = np.random.randn(N, T, D)
    h0 = 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_forward(x, h0, Wx, Wh, b)
    dout = np.random.randn(*out.shape)
    dx, dh0, dWx, dWh, db = rnn_backward(dout, cache)
    fx = lambda x: rnn_forward(x, h0, Wx, Wh, b)[0]
    fh0 = lambda h0: rnn_forward(x, h0, Wx, Wh, b)[0]
    fWx = lambda Wx: rnn_forward(x, h0, Wx, Wh, b)[0]
    fWh = lambda Wh: rnn_forward(x, h0, Wx, Wh, b)[0]
    fb = lambda b: rnn_forward(x, h0, Wx, Wh, b)[0]
    dx_num = eval_numerical_gradient_array(fx, x, dout)
    dh0_num = eval_numerical_gradient_array(fh0, h0, dout)
    dWx_num = eval_numerical_gradient_array(fWx, Wx, dout)
    dWh_num = eval_numerical_gradient_array(fWh, Wh, dout)
    db_num = eval_numerical_gradient_array(fb, b, dout)
    print('dx error: ', rel_error(dx_num, dx))
    print('dh0 error: ', rel_error(dh0_num, dh0))
    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 q3():
    from code_base.rnn_layers import rnn_forward
    import numpy as np

    x_all_shape = (3, 5, 874)
    Wx_shape = (874, 128)
    h_shape = (3, 128)
    Wh_shape = (128, 128)
    b_shape = (128,)
    x_all = np.loadtxt('./input_files/x_all.csv', delimiter=',')
    x_all = x_all.reshape(x_all_shape)
    Wx = np.loadtxt('./input_files/Wx.csv', delimiter=',')
    Wx = Wx.reshape(Wx_shape)
    h0 = np.loadtxt('./input_files/prev_h.csv', delimiter=',')
    h0 = h0.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_forward(x_all, h0, Wx, Wh, b)
    np.savetxt('./output_files/rnn_forward_out.csv', out.ravel(), delimiter=',')
Esempio n. 4
0
def verify_rnn_forward():
    from code_base.rnn_layers import rnn_forward
    import numpy as np

    N, T, D, H = 2, 3, 4, 5
    x = np.linspace(-0.1, 0.3, num=N * T * D).reshape(N, T, D)
    h0 = np.linspace(-0.3, 0.1, num=N * H).reshape(N, H)
    Wx = np.linspace(-0.2, 0.4, num=D * H).reshape(D, H)
    Wh = np.linspace(-0.4, 0.1, num=H * H).reshape(H, H)
    b = np.linspace(-0.7, 0.1, num=H)
    h, _ = rnn_forward(x, h0, Wx, Wh, b)
    expected_h = np.asarray(
        [[
            [-0.42070749, -0.27279261, -0.11074945, 0.05740409, 0.22236251],
            [-0.39525808, -0.22554661, -0.0409454, 0.14649412, 0.32397316],
            [-0.42305111, -0.24223728, -0.04287027, 0.15997045, 0.35014525],
        ],
         [[-0.55857474, -0.39065825, -0.19198182, 0.02378408, 0.23735671],
          [-0.27150199, -0.07088804, 0.13562939, 0.33099728, 0.50158768],
          [-0.51014825, -0.30524429, -0.06755202, 0.17806392, 0.40333043]]])
    print('h error: ', rel_error(expected_h, h))
Esempio n. 5
0
# # RNN Forward

# In[ ]:

from code_base.rnn_layers import rnn_forward
from code_base.layer_utils import *
import numpy as np

N, T, D, H = 2, 3, 4, 5
x = np.linspace(-0.1, 0.3, num=N * T * D).reshape(N, T, D)
h0 = np.linspace(-0.3, 0.1, num=N * H).reshape(N, H)
Wx = np.linspace(-0.2, 0.4, num=D * H).reshape(D, H)
Wh = np.linspace(-0.4, 0.1, num=H * H).reshape(H, H)
b = np.linspace(-0.7, 0.1, num=H)
h, _ = rnn_forward(x, h0, Wx, Wh, b)
expected_h = np.asarray(
    [[
        [-0.42070749, -0.27279261, -0.11074945, 0.05740409, 0.22236251],
        [-0.39525808, -0.22554661, -0.0409454, 0.14649412, 0.32397316],
        [-0.42305111, -0.24223728, -0.04287027, 0.15997045, 0.35014525],
    ],
     [[-0.55857474, -0.39065825, -0.19198182, 0.02378408, 0.23735671],
      [-0.27150199, -0.07088804, 0.13562939, 0.33099728, 0.50158768],
      [-0.51014825, -0.30524429, -0.06755202, 0.17806392, 0.40333043]]])
print('h error: ', rel_error(expected_h, h))

# In[ ]:

from code_base.rnn_layers import rnn_forward
import numpy as np