def test_ForwardAndRecurrentConnections_feed_forward_two_samples_using_carry(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) X = np.array([[1], [1]]) T = np.array([[1], [2]]) assert_equal(frc.forward_pass(theta, X[0]), T[0:1]) assert_equal(frc.forward_pass(theta, X[1], X[0]), T[1:2])
def test_ForwardAndRecurrentConnections_feed_forward_two_samples_using_carry(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) X = np.array([[1],[1]]) T = np.array([[1],[2]]) assert_equal(frc.forward_pass(theta, X[0]), T[0:1]) assert_equal(frc.forward_pass(theta, X[1], X[0]), T[1:2])
def test_ForwardAndRecurrentConnections_backprop_gradient_check(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) X = [[1.], [1.]] Y = [[1.], [2.]] T = np.array([[0.], [0.]]) out_error = [[-1.], [-2.]] error, grad = frc.backprop(theta, X, Y, out_error) f = lambda t : error_function(T - frc.forward_pass(t, X)) assert_almost_equal(approx_fprime(theta, f, 1e-8), grad)
def test_ForwardAndRecurrentConnections_backprop_gradient_check(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) X = [[1.], [1.]] Y = [[1.], [2.]] T = np.array([[0.], [0.]]) out_error = [[-1.], [-2.]] error, grad = frc.backprop(theta, X, Y, out_error) f = lambda t: error_function(T - frc.forward_pass(t, X)) assert_almost_equal(approx_fprime(theta, f, 1e-8), grad)
def test_ForwardAndRecurrentConnections_backprop_random_example_gradient_check(): frc = ForwardAndRecurrentConnection(4, 3) theta = np.random.randn(frc.get_param_dim()) X = np.random.randn(10, 4) Y = frc.forward_pass(theta, X) T = np.zeros((10, 3)) out_error = (T - Y) error, grad_c = frc.backprop(theta, X, Y, out_error) f = lambda t : error_function(T - frc.forward_pass(t, X)) grad_e = approx_fprime(theta, f, 1e-8) assert_allclose(grad_c, grad_e, rtol=1e-3, atol=1e-5)
def test_ForwardAndRecurrentConnections_backprop_random_example_gradient_check( ): frc = ForwardAndRecurrentConnection(4, 3) theta = np.random.randn(frc.get_param_dim()) X = np.random.randn(10, 4) Y = frc.forward_pass(theta, X) T = np.zeros((10, 3)) out_error = (T - Y) error, grad_c = frc.backprop(theta, X, Y, out_error) f = lambda t: error_function(T - frc.forward_pass(t, X)) grad_e = approx_fprime(theta, f, 1e-8) assert_allclose(grad_c, grad_e, rtol=1e-3, atol=1e-5)
def test_ForwardAndRecurrentConnections_backprop_single_sample(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) error, grad = frc.backprop(theta, 1, 1, 1) assert_equal(grad, [-1, 0]) assert_equal(error, 1)
def test_ForwardAndRecurrentConnections_feed_forward_single_sample(): # single sample, the recurrent connection should not jump in frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) assert_equal(frc.forward_pass(theta, 1), 1)
def test_ForwardAndRecurrentConnections_param_dim(): frc = ForwardAndRecurrentConnection(3, 7) assert_equal(frc.get_param_dim(), 3*7 + 7*7)
def test_ForwardAndRecurrentConnections_backprop_two_samples(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) error, grad = frc.backprop(theta, [[1], [1]], [[1], [2]], [[-1], [-2]]) assert_equal(grad, [5, 2]) assert_equal(error, [[-3], [-2]])
def test_ForwardAndRecurrentConnections_param_dim(): frc = ForwardAndRecurrentConnection(3, 7) assert_equal(frc.get_param_dim(), 3 * 7 + 7 * 7)
def test_ForwardAndRecurrentConnections_backprop_single_samples_with_carry(): frc = ForwardAndRecurrentConnection(1, 1) theta = np.ones(frc.get_param_dim()) error, grad = frc.backprop(theta, 1, 1, 1, 1) assert_equal(grad, [-2, -1]) assert_equal(error, 2)