Example #1
0
        public void When_querying_the_network_after_training_for_the_XOR_problem_it_should_return_the_correct_output()
        {
            // Arrange
            const int    numberOfEpochs = 10000;
            const double learningRate   = 0.3;
            const double momentum       = 0.1;

            var network = new NeuralNetworkBuilder()
                          .Using(new XORNetworkLayout())
                          .Build();

            network.Train(new[]
            {
                new TrainingExample(new[] { 0.0, 0.0 }, new[] { 0.0 }),
                new TrainingExample(new[] { 0.0, 1.0 }, new[] { 1.0 }),
                new TrainingExample(new[] { 1.0, 0.0 }, new[] { 1.0 }),
                new TrainingExample(new[] { 1.0, 1.0 }, new[] { 0.0 })
            }, numberOfEpochs, learningRate, momentum);

            // Act
            double output_0_0 = network.Query(new[] { 0.0, 0.0 }).Single();
            double output_0_1 = network.Query(new[] { 0.0, 1.0 }).Single();
            double output_1_0 = network.Query(new[] { 1.0, 0.0 }).Single();
            double output_1_1 = network.Query(new[] { 1.0, 1.0 }).Single();

            // Assert
            output_0_0.Should().BeApproximately(0.0, 0.05);
            output_0_1.Should().BeApproximately(1.0, 0.05);
            output_1_0.Should().BeApproximately(1.0, 0.05);
            output_1_1.Should().BeApproximately(0.0, 0.05);
        }
        public void When_querying_the_network_after_training_it_should_yield_the_correct_output()
        {
            // Arrange
            var network = new NeuralNetworkBuilder()
                          .Using(new XORNetworkLayout())
                          .Build();

            network.Train(new[]
            {
                new TrainingExample(new[] { 0.0, 0.0 }, new[] { 0.0 }),
                new TrainingExample(new[] { 0.0, 1.0 }, new[] { 1.0 }),
                new TrainingExample(new[] { 1.0, 0.0 }, new[] { 1.0 }),
                new TrainingExample(new[] { 1.0, 1.0 }, new[] { 0.0 })
            }, 10000, 0.3, 0.1);

            // Act
            double output_0_0 = network.Query(new[] { 0.0, 0.0 }).Single();
            double output_0_1 = network.Query(new[] { 0.0, 1.0 }).Single();
            double output_1_0 = network.Query(new[] { 1.0, 0.0 }).Single();
            double output_1_1 = network.Query(new[] { 1.0, 1.0 }).Single();

            // Assert
            output_0_0.Should().BeApproximately(0.0, 0.05);
            output_0_1.Should().BeApproximately(1.0, 0.05);
            output_1_0.Should().BeApproximately(1.0, 0.05);
            output_1_1.Should().BeApproximately(0.0, 0.05);
        }
        public void When_querying_a_network_that_has_known_weights_and_biases_it_should_yield_the_expected_output()
        {
            // Arrange
            var network = new NeuralNetworkBuilder()
                          .Using(new TwoLayerNetworkProvider())
                          .Build();

            // Act
            double[] networkOutput = network.Query(new[] { 1.0, -2.0, 3.0 });

            // Assert
            networkOutput[0].Should().BeApproximately(0.5164, 0.00005);
            networkOutput[1].Should().BeApproximately(0.5172, 0.00005);
        }
Example #4
0
        public void When_querying_a_network_with_known_weights_and_biases_it_should_return_the_expected_output()
        {
            // Arrange
            var network = new NeuralNetworkBuilder()
                          .Using(new TwoLayerNetworkProvider())
                          .Build();

            // Act
            double[] networkOutput = network.Query(new[] { 0.1, 0.2, 0.3 });

            // Assert
            networkOutput.Should().HaveCount(2);
            networkOutput[0].Should().BeApproximately(0.6918, 0.00005);
            networkOutput[1].Should().BeApproximately(0.8596, 0.00005);
        }
Example #5
0
        public void When_querying_the_network_after_training_it_should_return_the_correct_output()
        {
            // Arrange
            const int    numberOfEpochs = 20000;
            const double learningRate   = 0.3;
            const double momentum       = 0.1;

            var network = new NeuralNetworkBuilder()
                          .Using(new TwoLayerNetworkProvider())
                          .Build();

            network.Train(new[]
            {
                new TrainingExample(new[] { 0.1, 0.2, 0.3 }, new[] { 1.0, 0.0 })
            }, numberOfEpochs, learningRate, momentum);

            // Act
            double[] output = network.Query(new[] { 0.1, 0.2, 0.3 });

            // Assert
            output[0].Should().BeApproximately(1.0, 0.005);
            output[1].Should().BeApproximately(0.0, 0.005);
        }