def test_rnn_step_backward(self): print('Checking RNN Step backward') np.random.seed(231) 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) def fx(x): return rnn_step_forward(x, h, Wx, Wh, b)[0] def fh(prev_h): return rnn_step_forward(x, h, Wx, Wh, b)[0] def fWx(Wx): return rnn_step_forward(x, h, Wx, Wh, b)[0] def fWh(Wh): return rnn_step_forward(x, h, Wx, Wh, b)[0] def fb(b): return rnn_step_forward(x, h, Wx, Wh, b)[0] dx_num = self.eval_numerical_gradient_array(fx, x, dnext_h) dprev_h_num = self.eval_numerical_gradient_array(fh, h, dnext_h) dWx_num = self.eval_numerical_gradient_array(fWx, Wx, dnext_h) dWh_num = self.eval_numerical_gradient_array(fWh, Wh, dnext_h) db_num = self.eval_numerical_gradient_array(fb, b, dnext_h) dx, dprev_h, dWx, dWh, db = rnn_step_backward(dnext_h, cache) self.assertTrue( self.rel_error(dx_num, dx) < 1e-5, "dx elative error should be less than 1e-5") self.assertTrue( self.rel_error(dprev_h_num, dprev_h) < 1e-5, "dprev_h relative error should be less than 1e-5") self.assertTrue( self.rel_error(dWx_num, dWx) < 1e-5, "dWx relative error should be less than 1e-5") self.assertTrue( self.rel_error(dWh_num, dWh) < 1e-5, "dWh relative error should be less than 1e-5") self.assertTrue( self.rel_error(db_num, db) < 1e-5, "db relative error should be less than 1e-5")
def test_rnn_step_forward(self): N, D, H = 3, 10, 4 print('Checking RNN Step forward') 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]]) self.assertTrue( self.rel_error(expected_next_h, next_h) < 1e-5, "Relative error should be less than 1e-5")
def fb(b): return rnn_step_forward(x, h, Wx, Wh, b)[0]
def fWh(Wh): return rnn_step_forward(x, h, Wx, Wh, b)[0]
def fWx(Wx): return rnn_step_forward(x, h, Wx, Wh, b)[0]
def fh(prev_h): return rnn_step_forward(x, h, Wx, Wh, b)[0]