public void TestBias() { double input = 0.3; double inpToOutWeight = 0.4; double biasToOutWeight = 0.5; var sigmoid = new SigmoidFunction(); var nw = new Network(sigmoid, false); var inputNode = new Input(input); var bias = new Bias(); var outputNode = new Perceptron(sigmoid, "Output"); Connection.Create(inpToOutWeight, inputNode, outputNode); Connection.Create(biasToOutWeight, bias, outputNode); nw.Nodes = new Node[][] { new Node[] { inputNode }, new Node[] { outputNode } }; var nwOut = nw.Nodes[1][0].Output; //Output // = Sig(inpToOut.Output + biasToOut.Output) // = Sig((inpToOut.Weight * inpToOut.GetInput()) + (biasToOutWeight * 1)) // = Sig((inpToOut.Weight * inputNode.Output) + (biasToOutWeight)) // = Sig((this.weight * this.input) + (biasToOutWeight)) var expOut = sigmoid.Calculate((inpToOutWeight * input) + biasToOutWeight); Assert.AreEqual(nwOut, expOut); }
public void TestTraining() { var sigmoid = new SigmoidFunction(); var net = new Network(sigmoid, true); net.FillNetwork(2, 2, 2); net.Nodes[0][0].GetOutgoingConnections()[0].Weight = .15; net.Nodes[0][0].GetOutgoingConnections()[1].Weight = .2; net.Nodes[0][1].GetOutgoingConnections()[0].Weight = .25; net.Nodes[0][1].GetOutgoingConnections()[1].Weight = .3; net.Nodes[1][0].GetOutgoingConnections()[0].Weight = .4; net.Nodes[1][0].GetOutgoingConnections()[1].Weight = .45; net.Nodes[1][1].GetOutgoingConnections()[0].Weight = .5; net.Nodes[1][1].GetOutgoingConnections()[1].Weight = .55; var expected = new InputExpectedResult(new double[] { .05, .1 }, new double[] { .01, .99 }); var before = NetworkValidation.Validate(net, new InputExpectedResult[] { expected }, (a, b) => true); var bp = new Backpropagate(net, 0.5); bp.Train(new InputExpectedResult[] { expected }); var after = NetworkValidation.Validate(net, new InputExpectedResult[] { expected }, (a, b) => true); Assert.IsTrue(before.AvgSSE > after.AvgSSE); }
public void TestSigmoid() { SigmoidFunction sf = new SigmoidFunction(); Assert.IsTrue(sf.Calculate(10, 20) > 0.9); Assert.IsTrue(sf.Calculate(-10, -20) < 0.1); Assert.IsTrue(sf.Calculate(-1, 1) > 0.4 && sf.Calculate(-1, 1) < 0.6); }
public void TestNetworkOutput() { var rand = new Random(); double input = (double)(rand.NextDouble()); double weight = (double)(rand.NextDouble()); var sigmoid = new SigmoidFunction(); var nw = new Network(sigmoid, false); var inputNode = new Input(input); var outputNode = new Perceptron(sigmoid, "Output"); Connection.Create(weight, inputNode, outputNode); nw.Nodes = new Node[][] { new Node[] { inputNode }, new Node[] { outputNode } }; var nwOut = nw.CurOutput()[0]; Assert.AreEqual(outputNode.Output, nwOut); var expOut = sigmoid.Calculate(input * weight); Assert.AreEqual(nwOut, expOut); }
public void TestGetInputResult() { var rand = new Random(); double input = (double)(rand.NextDouble() * 100 + 1); double weight = (double)(rand.NextDouble() * 2); var sigmoid = new SigmoidFunction(); var nw = new Network(sigmoid, false); var inputNode = new Input(input); var outputNode = new Perceptron(sigmoid, "Output"); Connection.Create(weight, inputNode, outputNode); nw.Nodes = new Node[][] { new Node[] { inputNode }, new Node[] { outputNode } }; var nwOut = nw.GetInputResult(input)[0]; //Output // = Sig(inpToOut.Output) // = Sig(inpToOut.Weight * inpToOut.GetInput()) // = Sig(inpToOut.Weight * inputNode.Output) // = Sig(this.weight * this.input) var expOut = sigmoid.Calculate(weight * input); Assert.AreEqual(nwOut, expOut); }
public void Init(int imgDim, double learnRate, int microBatchsize, int loadBatchsize, string imgFolder, TransferFunctionType funcType, int inputHeight, int outputHeight, int[] hiddenHeights) { switch(funcType) { case TransferFunctionType.Sigmoid: TransferFunc = new SigmoidFunction(); break; case TransferFunctionType.HyperbolicTangent: TransferFunc = new HyperbolicTangentFunction(); break; } ImageDimensions = imgDim; ImgLoader = new LazyTrainImgLoader(imgFolder, TransferFunc, true, true, imgDim, loadBatchsize/*, TODO: Support non-digits*/); PropertyChanged?.Invoke(this, new PropertyChangedEventArgs("ImgCount")); InitNetwork(learnRate, microBatchsize, inputHeight, outputHeight, hiddenHeights); }