public void FeedForward_IfInputLengthDoesNotMatchNumInputs_Throw() { var nn = SampleInputs.GetSample1HiddenLayerPerceptron(); Action action = () => nn.FeedForward(new[] { 0.0 }); action.ShouldThrow <NeuralNetworkException>() .WithMessage($"*Argument 'inputs' should have width {nn.NumInputs}; was 1.*"); }
public void IfTargetLengthDoesNotMatchNumOutputs_Throw() { var nn = SampleInputs.GetSample1HiddenLayerPerceptron(); Action action = () => nn.CalculateGradients(SampleInputs.GetSampleInputs(), new[] { 0.0 }); action.ShouldThrow <NeuralNetworkException>() .WithMessage($"*Argument 'targets' should have width {nn.NumOutputs}; was 1.*"); }
public void ShouldIncludeInputsWithBias() { var inputs = SampleInputs.GetSampleInputs(); var nn = SampleInputs.GetSample1HiddenLayerPerceptron(); var result = nn.FeedForward(inputs); var expected = new double[inputs.Length + 1]; Array.Copy(inputs, expected, inputs.Length); expected[inputs.Length] = 1; result.InputWithBias.Should().Equal(expected); }
public void ShouldCalculateCorrectly_OneHiddenLayer() { var nn = SampleInputs.GetSample1HiddenLayerPerceptron(); var result = nn.FeedForward(SampleInputs.GetSampleInputs()); result.HiddenLayers.Should().HaveCount(1); var expectedHidden = new[] { 0.336375544336332, 0.347853774202261, 0.359228665810271, 1.0 }; result.HiddenLayers[0].ShouldApproximatelyEqual(expectedHidden, 1e-12); var expectedOutput = new[] { 0.449090580148409, 0.550909419851591 }; result.Output.ShouldApproximatelyEqual(expectedOutput, 1e-12); }
public void ShouldCalculateGrads_OneHiddenLayerCase() { // See MultilayerPerceptronTests.xlsx in this folder for calculations of // expected outputs. var nn = SampleInputs.GetSample1HiddenLayerPerceptron(); var result = nn.CalculateGradients(SampleInputs.GetSampleInputs(), SampleInputs.GetSampleTargets()); result.Should().NotBeNullOrEmpty(); result.Should().HaveCount(2); var expectedHiddenGrads = new[] { 0.00488574841598145, 0.00977149683196289, 0.0488574841598144, 0.00484248141480251, 0.00968496282960501, 0.0484248141480251, 0.00479817184666825, 0.0095963436933365, 0.0479817184666825 }; var expectedOutputGrads = new[] { -0.185312455982592, -0.191635920938954, -0.197902455875597, -0.550909419851591, 0.185312455982592, 0.191635920938954, 0.197902455875597, 0.550909419851591 }; result[1].ShouldApproximatelyEqual(expectedOutputGrads, 1e-12); result[0].ShouldApproximatelyEqual(expectedHiddenGrads, 1e-12); }