public void AddOutboundConnection_ValidatesArgs() { // Setup var network = new BackpropagationNetwork(new Mock<IErrorCalculator>().Object); // Execute/Verify network.AddOutboundConnection(null); }
public void ApplyWeightAdjustments_PropogatesThroughNetwork() { //// SETUP const double ErrorSignal = -2.3d; const float Momentum = 0.9f; const float LearningRate = 0.1f; // Create 2 inbound and 2 outbound mock connections. var mockInbound1 = new Mock<ISupervisedLearnerConnection>(); var mockInbound2 = new Mock<ISupervisedLearnerConnection>(); var mockOutbound1 = new Mock<ISupervisedLearnerConnection>(); var mockOutbound2 = new Mock<ISupervisedLearnerConnection>(); // program the mock outbounds such that both are reporting error signals mockOutbound1.SetupGet(mock => mock.IsReportingError).Returns(true); mockOutbound2.SetupGet(mock => mock.IsReportingError).Returns(true); // Create 2 input nodes and 2 output nodes. var mockInputNode1 = new Mock<ISupervisedLearnerNode>(); var mockInputNode2 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode1 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode2 = new Mock<ISupervisedLearnerNode>(); // Program nodes to report input/output sizes. mockInputNode1.SetupGet(mock => mock.InputSize).Returns(1); mockInputNode2.SetupGet(mock => mock.InputSize).Returns(1); mockOutputNode1.SetupGet(mock => mock.OutputSize).Returns(1); mockOutputNode2.SetupGet(mock => mock.OutputSize).Returns(1); // Program nodes to provide values. mockInputNode1.SetupGet(mock => mock.CachedErrors).Returns(new[] { ErrorSignal }); mockInputNode2.SetupGet(mock => mock.CachedErrors).Returns(new[] { ErrorSignal }); mockOutputNode1.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); mockOutputNode2.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); // Create the test object. var network = new BackpropagationNetwork(new Mock<IErrorCalculator>().Object); network.AddInboundConnection(mockInbound1.Object); network.AddInboundConnection(mockInbound2.Object); network.AddOutboundConnection(mockOutbound1.Object); network.AddOutboundConnection(mockOutbound2.Object); network.AddInputNode(mockInputNode1.Object); network.AddInputNode(mockInputNode2.Object); network.AddOutputNode(mockOutputNode1.Object); network.AddOutputNode(mockOutputNode2.Object); network.Fire(new[] { 0.0d }); network.CalculateError(ErrorSignal); // EXECUTION network.ApplyWeightAdjustments(LearningRate, Momentum); // VERIFICATION mockInputNode1.Verify(mock => mock.ApplyWeightAdjustments(LearningRate, Momentum), Times.Once()); mockInputNode2.Verify(mock => mock.ApplyWeightAdjustments(LearningRate, Momentum), Times.Once()); mockOutbound1.Verify(mock => mock.ApplyWeightAdjustments(LearningRate, Momentum), Times.Once()); mockOutbound2.Verify(mock => mock.ApplyWeightAdjustments(LearningRate, Momentum), Times.Once()); }
public void CalculateError_DoesNotCalculateUntilAllConnectionsReport() { //// SETUP // Create 2 inbound and 2 outbound mock connections. var mockInbound1 = new Mock<ISupervisedLearnerConnection>(); var mockInbound2 = new Mock<ISupervisedLearnerConnection>(); var mockOutboundReporting = new Mock<ISupervisedLearnerConnection>(); var mockOutboundNotReporting = new Mock<ISupervisedLearnerConnection>(); // program the mock outbounds such that one is reporting and one isn't mockOutboundReporting.SetupGet(mock => mock.IsReportingError).Returns(true); mockOutboundNotReporting.SetupGet(mock => mock.IsReportingError).Returns(false); // Create 2 input nodes and 2 output nodes. var mockInputNode1 = new Mock<ISupervisedLearnerNode>(); var mockInputNode2 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode1 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode2 = new Mock<ISupervisedLearnerNode>(); // Create the test object. var network = new BackpropagationNetwork(new Mock<IErrorCalculator>().Object); network.AddInboundConnection(mockInbound1.Object); network.AddInboundConnection(mockInbound2.Object); network.AddOutboundConnection(mockOutboundReporting.Object); network.AddOutboundConnection(mockOutboundNotReporting.Object); network.AddInputNode(mockInputNode1.Object); network.AddInputNode(mockInputNode2.Object); network.AddOutputNode(mockOutputNode1.Object); network.AddOutputNode(mockOutputNode2.Object); // EXECUTION const double ErrorSignal = -2.3d; network.CalculateError(ErrorSignal); // VERIFICATION: The IsReporting signals were checked... mockOutboundReporting.Verify(mock => mock.IsReportingError, Times.Exactly(1)); mockOutboundNotReporting.Verify(mock => mock.IsReportingError, Times.Exactly(1)); // ...but no calculation activities occurred. mockInbound1.Verify(mock => mock.ReportError(It.IsAny<double>()), Times.Never()); mockInbound2.Verify(mock => mock.ReportError(It.IsAny<double>()), Times.Never()); mockOutboundReporting.Verify(mock => mock.ClearReportingFlag(), Times.Never()); mockOutboundNotReporting.Verify(mock => mock.ClearReportingFlag(), Times.Never()); mockInputNode1.VerifyGet(mock => mock.CachedErrors, Times.Never()); mockInputNode2.VerifyGet(mock => mock.CachedErrors, Times.Never()); mockOutputNode1.Verify(mock => mock.CalculateError(It.IsAny<double>()), Times.Never()); mockOutputNode2.Verify(mock => mock.CalculateError(It.IsAny<double>()), Times.Never()); }
public void CalculateError_DistributesSignals() { //// SETUP const double ErrorSignal1 = -0.5d; const double ErrorSignal2 = 1.5d; const double DigitizedErrorSignal1 = -1.0d; const double DigitizedErrorSignal2 = 1.0d; const double NetworkErrorSignal1 = -1.0d; const double NetworkErrorSignal2 = 1.0d; var expectedNetworkErrors = new[] { NetworkErrorSignal1, NetworkErrorSignal2 }; // Create 2 inbound and 2 outbound mock connections. var mockInbound1 = new Mock<ISupervisedLearnerConnection>(); var mockInbound2 = new Mock<ISupervisedLearnerConnection>(); var mockOutbound1 = new Mock<ISupervisedLearnerConnection>(); var mockOutbound2 = new Mock<ISupervisedLearnerConnection>(); // program the mock outbounds such that all are reporting their respective errors mockOutbound1.SetupGet(mock => mock.ErrorSignal).Returns(ErrorSignal1); mockOutbound2.SetupGet(mock => mock.ErrorSignal).Returns(ErrorSignal2); mockOutbound1.SetupGet(mock => mock.IsReportingError).Returns(true); mockOutbound2.SetupGet(mock => mock.IsReportingError).Returns(true); // Create 2 input nodes and 5 output nodes. var mockInputNode1 = new Mock<ISupervisedLearnerNode>(); var mockInputNode2 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode1 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode2 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode3 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode4 = new Mock<ISupervisedLearnerNode>(); var mockOutputNode5 = new Mock<ISupervisedLearnerNode>(); // Program nodes to report input/output sizes. mockInputNode1.SetupGet(mock => mock.InputSize).Returns(1); mockInputNode2.SetupGet(mock => mock.InputSize).Returns(1); mockOutputNode1.SetupGet(mock => mock.OutputSize).Returns(1); mockOutputNode2.SetupGet(mock => mock.OutputSize).Returns(1); mockOutputNode3.SetupGet(mock => mock.OutputSize).Returns(1); mockOutputNode4.SetupGet(mock => mock.OutputSize).Returns(1); mockOutputNode5.SetupGet(mock => mock.OutputSize).Returns(1); // Program nodes to provide values. mockInputNode1.SetupGet(mock => mock.CachedErrors).Returns(new[] { NetworkErrorSignal1 }); mockInputNode2.SetupGet(mock => mock.CachedErrors).Returns(new[] { NetworkErrorSignal2 }); mockOutputNode1.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); mockOutputNode2.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); mockOutputNode3.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); mockOutputNode4.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); mockOutputNode5.SetupGet(mock => mock.CachedOutputs).Returns(new[] { 0.0d }); // Create the test object. var network = new BackpropagationNetwork(new Mock<IErrorCalculator>().Object); network.AddInboundConnection(mockInbound1.Object); network.AddInboundConnection(mockInbound2.Object); network.AddOutboundConnection(mockOutbound1.Object); network.AddOutboundConnection(mockOutbound2.Object); network.AddInputNode(mockInputNode1.Object); network.AddInputNode(mockInputNode2.Object); network.AddOutputNode(mockOutputNode1.Object); network.AddOutputNode(mockOutputNode2.Object); network.AddOutputNode(mockOutputNode3.Object); network.AddOutputNode(mockOutputNode4.Object); network.AddOutputNode(mockOutputNode5.Object); // EXECUTION network.Fire(new[] { 0.0d }); network.CalculateError(ErrorSignal1); // VERIFICATION: The IsReporting signals were checked... mockOutbound1.Verify(mock => mock.IsReportingError, Times.Exactly(1)); mockOutbound2.Verify(mock => mock.IsReportingError, Times.Exactly(1)); // ...and calculation activities occurred. mockInbound1.Verify(mock => mock.ReportError(NetworkErrorSignal1), Times.Once()); mockInbound2.Verify(mock => mock.ReportError(NetworkErrorSignal2), Times.Once()); mockOutbound1.VerifyGet(mock => mock.ErrorSignal, Times.Once()); mockOutbound1.Verify(mock => mock.ClearReportingFlag(), Times.Once()); mockOutbound2.VerifyGet(mock => mock.ErrorSignal, Times.Once()); mockOutbound2.Verify(mock => mock.ClearReportingFlag(), Times.Once()); mockInputNode1.VerifyGet(mock => mock.CachedErrors, Times.AtLeastOnce()); mockInputNode2.VerifyGet(mock => mock.CachedErrors, Times.AtLeastOnce()); mockOutputNode1.Verify(mock => mock.CalculateError(DigitizedErrorSignal1), Times.Once()); mockOutputNode2.Verify(mock => mock.CalculateError(DigitizedErrorSignal1), Times.Once()); mockOutputNode3.Verify(mock => mock.CalculateError(DigitizedErrorSignal1), Times.Once()); mockOutputNode4.Verify(mock => mock.CalculateError(DigitizedErrorSignal2), Times.Once()); mockOutputNode5.Verify(mock => mock.CalculateError(DigitizedErrorSignal2), Times.Once()); Assert.AreEqual(2, network.CachedErrors.Length); Assert.AreEqual(expectedNetworkErrors, network.CachedErrors); }