Esempio n. 1
0
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])
Esempio n. 2
0
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])
Esempio n. 3
0
def test_FANN_recurrent_gradient_multilayer_multisample():
    rc0 = ForwardAndRecurrentConnection(4, 3)
    rc1 = ForwardAndRecurrentConnection(3, 1)
    nn = FANN([rc0, rc1])
    theta = 2 * np.ones((nn.get_param_dim()))
    grad_c = nn.calculate_gradient(theta, X, T)
    grad_e = approx_fprime(theta, nn.calculate_error, 1e-8, X, T)
    assert_allclose(grad_c, grad_e, rtol=1e-3, atol=1e-5)
Esempio n. 4
0
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)
Esempio n. 5
0
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)
Esempio n. 6
0
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)
Esempio n. 7
0
def test_FANN_recurrent_gradient_single_sample():
    rc = ForwardAndRecurrentConnection(1, 1)
    nn = FANN([rc])
    theta = 2 * np.ones((nn.get_param_dim()))
    for x, t in [[0, 1], [1, 1], [0, 0]]:
        x = np.array([[x]])
        grad_c = nn.calculate_gradient(theta, x, t)
        grad_e = approx_fprime(theta, nn.calculate_error, 1e-8, x, t)
        assert_almost_equal(grad_c, grad_e)
Esempio n. 8
0
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)
Esempio n. 9
0
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)
Esempio n. 10
0
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]])
Esempio n. 11
0
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)
Esempio n. 12
0
def test_ForwardAndRecurrentConnections_param_dim():
    frc = ForwardAndRecurrentConnection(3, 7)
    assert_equal(frc.get_param_dim(), 3*7 + 7*7)
Esempio n. 13
0
def test_ForwardAndRecurrentConnections_dimensions():
    frc = ForwardAndRecurrentConnection(3, 7)
    assert_equal(frc.input_dim, 3)
    assert_equal(frc.output_dim, 7)
Esempio n. 14
0
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]])
Esempio n. 15
0
def test_ForwardAndRecurrentConnections_param_dim():
    frc = ForwardAndRecurrentConnection(3, 7)
    assert_equal(frc.get_param_dim(), 3 * 7 + 7 * 7)
Esempio n. 16
0
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)
Esempio n. 17
0
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)