public NeuronalNetwork(int inputLength, ActivationFunctions activationFunction, CostFunctions costFunction = CostFunctions.SquaredMean) { this.activationFunction = activationFunction; layers = new List <Layer>(); this.inputLength = inputLength; this.costFunction = costFunction; }
public Model(Layer[] layers, Connection[] connections, CostFunctions cost_function) { Layers = layers; Connections = connections; OLIndex = layers.Length - 1; switch (cost_function) { case CostFunctions.MeanSquareSrror: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanSquareErrorSigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanSquareErrorTanH; } break; case CostFunctions.MeanAbsoluteError: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanAbsoluteErrorSigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanAbsoluteErrorTanH; } break; case CostFunctions.CrossEntropy: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaCorssEntorpySigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaCorssEntorpyTanH; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Softmax) { Delta_OutputLayer = DeltaCorssEntorpySoftmax; } break; default: throw new ArgumentException("Cost function can't be set"); } }
public void SupervisedBackProp(double[] input, double[] Y, CostFunctions costFunction, List <List <NeuronValues> > startingvs, out double cost, out List <List <List <NeuronValues> > > grads, out List <double[]> biasGrads) { SupervisedBackProp(new List <double[]>() { input }, new List <double[]>() { Y }, costFunction, startingvs, out cost, out grads, out biasGrads); }
public NeuronalNetwork(int inputLength, ActivationFunctions activation, int[] topology, CostFunctions costFunction = CostFunctions.SquaredMean) { this.inputLength = inputLength; this.activationFunction = activation; this.costFunction = costFunction; layers = new List <Layer>(); for (int i = 0; i < topology.Length; i++) { int prevLength = i == 0 ? inputLength : topology[i - 1]; layers.Add(new Layer(prevLength, topology[i])); } }
public static double GetCostOf(double[] output, double[] expected, CostFunctions costFunction) { switch (costFunction) { case CostFunctions.SquaredMean: return(SquaredMeanEmpiricalLoss(output, expected)); case CostFunctions.BinaryCrossEntropy: return(BinaryCrossEntropyEmpiricalLoss(output, expected)); default: throw new NotImplementedException(); } }
public NeuralNetwork(Layer[] layers, LConnection[] layers_connections, CostFunctions cost_function, double learing_rate) { Layers = layers; LConnections = layers_connections; OLIndex = layers.Length - 1; foreach (var item in LConnections) { item.LearningRate = learing_rate; } switch (cost_function) { case CostFunctions.MeanSquareSrror: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanSquareErrorSigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanSquareErrorTanH; } break; case CostFunctions.MeanAbsoluteError: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanAbsoluteErrorSigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanAbsoluteErrorTanH; } break; case CostFunctions.CrossEntropy: if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaCorssEntorpySigmoid; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaCorssEntorpyTanH; } else if (Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Softmax) { Delta_OutputLayer = DeltaCorssEntorpySoftmax; } break; default: throw new ArgumentException("Cost function can't be set"); } }
public static List <double> GetCostGradients(double[] output, double[] expected, CostFunctions costFunction, out double cost) { cost = GetCostOf(output, expected, costFunction); List <double> Gradients = new List <double>(); for (int i = 0; i < Math.Min(output.Length, expected.Length); i++) { if (double.IsNaN(expected[i])) { Gradients.Add(0); } else { Gradients.Add(Derivatives.DerivativeOf(output[i], expected[i], costFunction)); } } return(Gradients); }
/// <summary> /// if expected has NaN it has not expected output /// </summary> public void SupervisedBackProp(List <double[]> input, List <double[]> expectedOutput, CostFunctions costFunction, List <List <NeuronValues> > startingStates, out double cost, out List <List <List <NeuronValues> > > grads, out List <double[]> biasGrads) { cost = 0; if (input.Count != expectedOutput.Count) { throw new ArgumentOutOfRangeException(); } var copy = new TemporalNetwork(ActivationFunction, TemporalLayers); copy.SetTemporalStates(startingStates); List <double[]> costs = new List <double[]>(); for (int t = 0; t < input.Count; t++) { double[] outputT = copy.ExecuteNetwork(input[t]); costs.Add(new double[expectedOutput[t].Length]); cost += Cost.GetCostOf(outputT, expectedOutput[t], costFunction);//REPAIR for (int i = 0; i < expectedOutput[i].Length; i++) { costs[t][i] = Derivatives.DerivativeOf(outputT[i], expectedOutput[t][i], costFunction); } } GetGradients(costs, input, startingStates, out grads, out biasGrads); }
/// Solves the optimization problem. /// Result will be put in #SolutionType and (if solution is valid) #ResultGraph. public void Run(ISolver solver) { if (MinArcCount > 0 || MaxArcCount < int.MaxValue || ArcCountWeight != 0) { CostFunctions.Add(new CostFunction(cost: (arc => 1.0), lowerBound: MinArcCount > 0 ? (double)MinArcCount - 0.5 : double.NegativeInfinity, upperBound: MaxArcCount < int.MaxValue ? (double)MaxArcCount + 0.5 : double.PositiveInfinity, objectiveWeight: ArcCountWeight)); } Problem problem = new Problem(); problem.Mode = OptimizationMode.Minimize; // a binary variable for the inclusion of each arc foreach (Arc a in Graph.Arcs()) { Variable v = problem.GetVariable(a); v.Type = VariableType.Binary; } // constraints and objective for each cost function foreach (CostFunction c in CostFunctions) { if (c.ObjectiveWeight != 0 || c.LowerBound > double.MinValue || c.UpperBound < double.MaxValue) { Expression cSum = 0.0; foreach (Arc a in Graph.Arcs()) { cSum.Add(problem.GetVariable(a), c.Cost(a)); } if (c.ObjectiveWeight != 0) { problem.Objective += c.ObjectiveWeight * cSum; } if (c.LowerBound > double.MinValue) { problem.Constraints.Add(cSum >= c.LowerBound); } if (c.UpperBound < double.MaxValue) { problem.Constraints.Add(cSum <= c.UpperBound); } } } // constraints for degrees if (MinInDegree != null || MaxInDegree != null || MinOutDegree != null || MaxOutDegree != null || MinDegree != null || MaxDegree != null) { foreach (Node n in Graph.Nodes()) { double myMinInDegree = (MinInDegree != null ? MinInDegree(n) : double.NegativeInfinity); double myMaxInDegree = (MaxInDegree != null ? MaxInDegree(n) : double.PositiveInfinity); double myMinOutDegree = (MinOutDegree != null ? MinOutDegree(n) : double.NegativeInfinity); double myMaxOutDegree = (MaxOutDegree != null ? MaxOutDegree(n) : double.PositiveInfinity); double myMinDegree = (MinDegree != null ? MinDegree(n) : double.NegativeInfinity); double myMaxDegree = (MaxDegree != null ? MaxDegree(n) : double.PositiveInfinity); if (myMinInDegree > double.MinValue || myMaxInDegree < double.MaxValue || myMinOutDegree > double.MinValue || myMaxOutDegree < double.MaxValue || myMinDegree > double.MinValue || myMaxDegree < double.MaxValue) { Expression inDegree = 0; Expression outDegree = 0; Expression degree = 0; foreach (Arc a in Graph.Arcs(n)) { double weight = (DegreeWeight != null ? DegreeWeight(a) : 1); if (weight != 0) { Node u = Graph.U(a); Node v = Graph.V(a); bool isEdge = Graph.IsEdge(a); bool isLoop = u == v; Variable avar = problem.GetVariable(a); degree.Add(avar, isLoop ? 2 * weight : weight); if (u == n || isEdge) { outDegree.Add(avar, (isLoop && isEdge) ? 2 * weight : weight); } if (v == n || isEdge) { inDegree.Add(avar, (isLoop && isEdge) ? 2 * weight : weight); } } } if (myMinInDegree > double.MinValue) { problem.Constraints.Add(inDegree >= myMinInDegree); } if (myMaxInDegree < double.MaxValue) { problem.Constraints.Add(inDegree <= myMaxInDegree); } if (myMinOutDegree > double.MinValue) { problem.Constraints.Add(outDegree >= myMinOutDegree); } if (myMaxOutDegree < double.MaxValue) { problem.Constraints.Add(outDegree <= myMaxOutDegree); } if (myMinDegree > double.MinValue) { problem.Constraints.Add(degree >= myMinDegree); } if (myMaxDegree < double.MaxValue) { problem.Constraints.Add(degree <= myMaxDegree); } } } } Solution solution = solver.Solve(problem); SolutionType = solution.Type; if (solution.Valid) { ResultGraph = new Subgraph(Graph); foreach (Arc arc in Graph.Arcs()) { ResultGraph.Enable(arc, solution[problem.GetVariable(arc)] >= 0.5); } } else { ResultGraph = null; } }
//Links creation based on obstacles, vehicle action and initial weights void initLinks() { foreach (var item in nodes) { if (item is NavNode && item.isActive) { //Find max y and x respect to vehicle action double xMaxCoord = ((NavNode)item).xCoord + vehicleAction; double yMaxCoord = ((NavNode)item).yCoord + vehicleAction; int maxX = getCellFromCoord(xMaxCoord, yMaxCoord)[0]; int maxY = getCellFromCoord(xMaxCoord, yMaxCoord)[1]; //Find min y and x respect to vehicle action int minX; int minY; double xMinCoord = ((NavNode)item).xCoord - vehicleAction; double yMinCoord = ((NavNode)item).yCoord - vehicleAction; minX = getCellFromCoord(xMinCoord, yMinCoord)[0]; minY = getCellFromCoord(xMinCoord, yMinCoord)[1]; //Clamp if (maxX > rows - 1) { maxX = rows - 1; } if (minX < 0) { minX = 0; } if (maxY > cols - 1) { maxY = cols - 1; } if (minY < 0) { minY = 0; } //Connect nodes depending on vehicle action and obstacles. Initial weight based on initial cost function params for (int i = minX; i <= maxX; i++) { for (int j = minY; j <= maxY; j++) { //Find attachable nodes NavNode target = (NavNode)getNodeByID(getNodeIdFromCell(i, j)); double euclDistToTarget = CostFunctions.euclideanDist((NavNode)item, target); bool check = ((item.getId() != target.getId()) && (target.isActive) && ((euclDistToTarget <= vehicleAction))); if (check) { //TODO: find a smart way to manage weights //choose initial weights int count = target.nvisited; double alpha = CostFunctions.alpha; double comm = CostFunctions.commCost((NavNode)item, target, resolution, rows, cols, nodes); double W = count + comm + alpha; // And finally, link item.addLink(target, W); } } } } } }
private int OLIndex; //Output layer Index of output model public Model(NeuralNetwork[] neural_networks, NNConnection[] neural_networks_Connections, CostFunctions cost_function, double learing_rate = 0.1) { NeuralNetworks = neural_networks; NNConnections = neural_networks_Connections; ONNIndex = NeuralNetworks.Length - 1; OLIndex = NeuralNetworks[ONNIndex].OLIndex; switch (cost_function) { case CostFunctions.MeanSquareSrror: if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanSquareErrorSigmoid; } else if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanSquareErrorTanH; } break; case CostFunctions.MeanAbsoluteError: if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaMeanAbsoluteErrorSigmoid; } else if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaMeanAbsoluteErrorTanH; } break; case CostFunctions.CrossEntropy: if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Sigmoid) { Delta_OutputLayer = DeltaCorssEntorpySigmoid; } else if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.TanH) { Delta_OutputLayer = DeltaCorssEntorpyTanH; } else if (NeuralNetworks[ONNIndex].Layers[OLIndex].GetActivatonFunction() == Layer.ActivationFunction.Softmax) { Delta_OutputLayer = DeltaCorssEntorpySoftmax; } break; default: throw new ArgumentException("Cost function can't be set"); } foreach (var item in NNConnections) { item.Model_Connection_LearningRate = learing_rate; } }
/// <param name="expected">In case of Reinforcement learning expected is reward</param> public static double DerivativeOf(double neuronActivation, double expected, CostFunctions costFunction) { if (double.IsNaN(expected)) { return(0); } switch (costFunction) { case CostFunctions.BinaryCrossEntropy: throw new NotImplementedException(); case CostFunctions.SquaredMean: return(SquaredMeanErrorDerivative(neuronActivation, expected)); case CostFunctions.logLikelyhoodTerm: return(LogLikelyhoodTermDerivative(neuronActivation, expected)); default: throw new NotImplementedException(); } }