Beispiel #1
0
def test_FANN_multilayer_with_bias_gradient_multisample():
    fc0 = FullConnectionWithBias(3, 2)
    fc1 = FullConnectionWithBias(2, 1)
    sig0 = SigmoidLayer(2)
    sig1 = SigmoidLayer(1)
    nn = FANN([fc0, sig0, fc1, sig1])
    theta = np.random.randn(nn.get_param_dim())
    grad_c = nn.calculate_gradient(theta, X_nb, T)
    grad_e = approx_fprime(theta, nn.calculate_error, 1e-8, X_nb, T)
    assert_almost_equal(grad_c, grad_e)
Beispiel #2
0
def create_neural_network(in_size, hidden_size, out_size, rnd, logger):
    logger.info(
        "Creating a NN with {} inputs, {} hidden units, and {} output units.".
        format(in_size, hidden_size, out_size))
    c0 = FullConnectionWithBias(in_size, hidden_size)
    s0 = SigmoidLayer(hidden_size)
    c1 = FullConnectionWithBias(hidden_size, out_size)
    s1 = SigmoidLayer(out_size)
    nn = FANN([c0, s0, c1, s1])
    theta = rnd.randn(nn.get_param_dim())
    return nn, theta
Beispiel #3
0
def test_FANN_multilayer_gradient_single_sample():
    fc0 = FullConnection(4, 2)
    fc1 = FullConnection(2, 1)
    sig0 = SigmoidLayer(2)
    sig1 = SigmoidLayer(1)
    nn = FANN([fc0, sig0, fc1, sig1])
    theta = np.random.randn(nn.get_param_dim())
    for x, t in zip(X, T):
        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)
Beispiel #4
0
def test_FANN_converges_on_xor_problem():
    fc0 = FullConnectionWithBias(2, 2)
    fc1 = FullConnectionWithBias(2, 1)
    sig0 = SigmoidLayer(2)
    sig1 = SigmoidLayer(1)
    nn = FANN([fc0, sig0, fc1, sig1])
    xor = load_xor()
    theta = np.random.randn(nn.get_param_dim())
    for i in range(2000):
        g = nn.calculate_gradient(theta, xor.data, xor.target)
        theta -= g * 1
    error = nn.calculate_error(theta, xor.data, xor.target)
    assert_less(error, 0.4)
Beispiel #5
0
def test_FANN_error_single_sample():
    fc = FullConnection(4, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    for x, t, e in zip(X, T, E):
        assert_equal(nn.calculate_error(theta, x, t), 0)
        assert_equal(nn.calculate_error(theta, x, 0), e)
Beispiel #6
0
def test_FANN_with_bias_feed_forward_single_sample():
    fc = FullConnectionWithBias(3, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    for x, t in zip(X_nb, T):
        t = np.atleast_2d(t)
        assert_equal(nn.forward_pass(theta, x), t)
Beispiel #7
0
def test_FANN_gradient_multisample():
    fc = FullConnection(4, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    theta = np.random.randn(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_almost_equal(grad_c, grad_e)
Beispiel #8
0
def test_FANN_with_bias_gradient_single_sample():
    fc = FullConnectionWithBias(3, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    theta = np.random.randn(nn.get_param_dim())
    for x, t in zip(X_nb, T):
        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)
Beispiel #9
0
def test_FANN_converges_on_vote_problem():
    fc = FullConnectionWithBias(9, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    vote = generate_majority_vote()
    theta = np.zeros((10, ))
    for i in range(500):
        g = nn.calculate_gradient(theta, vote.data, vote.target)
        theta -= g * 1
    error = nn.calculate_error(theta, vote.data, vote.target)
    assert_less(error, 0.2)
Beispiel #10
0
def test_FANN_converges_on_and_problem():
    fc = FullConnection(2, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    and_ = load_and()
    theta = np.array([-0.1, 0.1])
    for i in range(100):
        g = nn.calculate_gradient(theta, and_.data, and_.target)
        theta -= g * 1
    error = nn.calculate_error(theta, and_.data, and_.target)
    assert_less(error, 0.2)
Beispiel #11
0
def test_FANN_with_bias_error_multisample():
    fc = FullConnectionWithBias(3, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    assert_equal(nn.calculate_error(theta, X_nb, T), 0.0)
    assert_equal(nn.calculate_error(theta, X_nb, np.zeros_like(T)), np.sum(E))
Beispiel #12
0
def test_FANN_with_bias_feed_forward_multisample():
    fc = FullConnectionWithBias(3, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    assert_equal(nn.forward_pass(theta, X_nb), T)
Beispiel #13
0
def test_FANN_feed_forward_multisample():
    fc = FullConnection(4, 1)
    sig = SigmoidLayer(1)
    nn = FANN([fc, sig])
    assert_equal(nn.forward_pass(theta, X), T)
Beispiel #14
0
def build_nn(hidden_units):
    l0 = FullConnectionWithBias(4, hidden_units)
    s0 = SigmoidLayer(hidden_units)
    l1 = FullConnectionWithBias(hidden_units, 3)
    s1 = SigmoidLayer(3)
    return FANN([l0, s0, l1, s1])