def test_FANN_recurrent_gradient_multisample(): rc = ForwardAndRecurrentConnection(4,1) nn = FANN([rc]) 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)
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)
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)
def test_FANN_recurrent_gradient_multisample(): rc = ForwardAndRecurrentConnection(4, 1) nn = FANN([rc]) 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)
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)
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)
def test_FANN_with_bias_gradient_multisample(): fc = FullConnectionWithBias(3, 1) sig = SigmoidLayer(1) nn = FANN([fc, sig]) 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)
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)
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
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)
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)
def test_FANN_gradient_single_sample(): fc = FullConnection(4, 1) sig = SigmoidLayer(1) nn = FANN([fc, sig]) 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)
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)
def test_FANN_multilayer_gradient_multisample(): fc0 = FullConnectionWithBias(4, 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, T) grad_e = approx_fprime(theta, nn.calculate_error, 1e-8, X, T) assert_almost_equal(grad_c, grad_e)
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)
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)
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)
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)
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)
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
def test_FANN_with_bias_multilayer_gradient_single_sample(): 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()) 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)
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)
def test_RANN_converges_on_ropot_problem(): frc = ForwardAndRecurrentSigmoidConnection(5, 5) nn = FANN([frc]) rpot = generate_remember_pattern_over_time() theta = np.random.randn(nn.get_param_dim()) for i in range(100): grad = np.zeros_like(theta) for X, T in seqEnum(rpot): grad += nn.calculate_gradient(theta, X, T) theta -= grad * 1 error = np.sum(nn.calculate_error(theta, X, T) for X, T in seqEnum(rpot)) assert_less(error, 10.)
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)
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))
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)
def test_FANN_with_bias_dimensions(): fc = FullConnectionWithBias(5, 1) nn = FANN([fc]) assert_equal(nn.input_size, 5) assert_equal(nn.output_size, 1)
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])