Ejemplo n.º 1
0
 public void ApplyChange(DivisorChange change)
 {
     var divisor = divisors[change.Divisor];
     divisor.ChangePosition(change.Direction);
     lastChanges.AddLast(change);
 }
Ejemplo n.º 2
0
 public void UndoChange(DivisorChange change)
 {
     var divisor = divisors[change.Divisor];
     divisor.ChangePosition(change.Direction == Direction.Left ? Direction.Right : Direction.Left);
 }
Ejemplo n.º 3
0
        protected override double[] DoIteration(List <IGWGraph <NodeData, EdgeData, GraphData> > TrainingGraphs, double[] weightCurrent, int globalIteration)
        {
            var    weights = weightCurrent.ToArray();
            var    weightsSum = new double[weightCurrent.Length];
            int    iteration = 0;
            double mcc = 0.0, mcc2 = 0.0;

            var newPoint = new Point(weights, new int[weights.Length], 0.0);


            {
                double tp = 0.001, tn = CoreResiduesTraining, fp = 0.001, fn = 0.001;

                MemoryPoints.Add(newPoint);
                if (MemoryPoints.Count > memoryPoints)
                {
                    MemoryPoints.RemoveAt(0);
                }

                ReferencePoint = new Point(weights, new int[weights.Length], 1.0);
                for (int i = 0; i < TrainingGraphs.Count; i++)
                {
                    var graph = TrainingGraphs[i];
                    //set scores according to weights
                    #region set weights
                    foreach (var node in graph.Nodes)
                    {
                        var scores = new double[Labels];
                        node.Data.Scores = scores;

                        for (int label = 0; label < Labels; label++)
                        {
                            for (int k = 0; k < weights.Length; k++)
                            {
                                scores[label] += weights[k] * BasisMerkmale[k].Score(node, label);
                            }
                        }
                    }
                    foreach (var edge in graph.Edges)
                    {
                        var scores = new double[Labels, Labels];
                        edge.Data.Scores = scores;

                        for (int label1 = 0; label1 < Labels; label1++)
                        {
                            for (int label2 = 0; label2 < Labels; label2++)
                            {
                                for (int k = 0; k < weights.Length; k++)
                                {
                                    scores[label1, label2] += weights[k] * BasisMerkmale[k].Score(edge, label1, label2);
                                }
                            }
                        }
                    }
                    #endregion

                    //compute labeling with viterbi algorithm
                    var request = new SolveInference(graph as IGWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>, null, Labels, BufferSizeInference);
                    request.RequestInDefaultContext();
                    int[] labeling = request.Solution.Labeling;
                    //check nonequality

                    iteration++;
                    for (int k = 0; k < labeling.Length; k++)
                    {
                        if (graph.Data.ReferenceLabeling[k] > 0)
                        {
                            if (labeling[k] > 0)
                            {
                                tp += 1;
                            }
                            else
                            {
                                fn += 1;
                            }
                        }
                        else
                        {
                            if (labeling[k] > 0)
                            {
                                fp += 1;
                            }
                            else
                            {
                                tn += 1;
                            }
                        }
                    }

                    int[] countsPred = CountPred(graph, labeling);
                    int[] countsRef  = CountPred(graph, graph.Data.ReferenceLabeling);
                    for (int k = 0; k < countsPred.Length; k++)
                    {
                        newPoint.Counts[k]       += countsPred[k];
                        ReferencePoint.Counts[k] += countsRef[k];
                    }
                    //TrackResults(labeling, graph.Data.ReferenceLabeling);
                }
                //WriterResults();
                var sensitivity = tp / (tp + fn);
                var specificity = tn / (tn + fp);
                mcc            = (tp * tn - fp * fn) / Math.Sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
                newPoint.Score = mcc;

                Log.Post("New MCC: " + Math.Round(mcc, 5) + " Sens: " + Math.Round(sensitivity, 5) + " Spec: " + Math.Round(specificity, 5));
            }
            //change Interval borders
            {
                var randomChanges = DivisorChange.RandomInstances(Intervals - 1);

                foreach (var change in randomChanges)
                {
                    var changesFor = random.Next(5);
                    if (changesFor == 0)
                    {
                        ValueMapRasa.ApplyChange(change);
                    }
                    else if (changesFor == 1)
                    {
                        ValueMapEMax01.ApplyChange(change);
                    }
                    else if (changesFor == 2)
                    {
                        ValueMapEMax11.ApplyChange(change);
                    }
                    else if (changesFor == 3)
                    {
                        ValueMapEDiff01.ApplyChange(change);
                    }
                    else if (changesFor == 4)
                    {
                        ValueMapEDiff11.ApplyChange(change);
                    }
                }


                double tp = 0.001, tn = CoreResiduesTraining, fp = 0.001, fn = 0.001;

                //var newPoint = new Point(weights, new int[weights.Length], 0.0);
                //MemoryPoints.Add(newPoint);
                //if (MemoryPoints.Count > memoryPoints)
                //    MemoryPoints.RemoveAt(0);

                //ReferencePoint = new Point(weights, new int[weights.Length], 1.0);
                for (int i = 0; i < TrainingGraphs.Count; i++)
                {
                    var graph = TrainingGraphs[i];
                    SetWeightsCRF(weights, graph);

                    //compute labeling with viterbi algorithm
                    var request = new SolveInference(graph as IGWGraph <ICRFNodeData, ICRFEdgeData, ICRFGraphData>, null, Labels, BufferSizeInference);
                    request.RequestInDefaultContext();
                    int[] labeling = request.Solution.Labeling;
                    //check nonequality

                    for (int k = 0; k < labeling.Length; k++)
                    {
                        if (graph.Data.ReferenceLabeling[k] > 0)
                        {
                            if (labeling[k] > 0)
                            {
                                tp += 1;
                            }
                            else
                            {
                                fn += 1;
                            }
                        }
                        else
                        {
                            if (labeling[k] > 0)
                            {
                                fp += 1;
                            }
                            else
                            {
                                tn += 1;
                            }
                        }
                    }

                    int[] countsPred = CountPred(graph, labeling);
                    int[] countsRef  = CountPred(graph, graph.Data.ReferenceLabeling);
                    //for (int k = 0; k < countsPred.Length; k++)
                    //{
                    //    newPoint.Counts[k] += countsPred[k];
                    //    //ReferencePoint.Counts[k] += countsRef[k];
                    //}
                    //TrackResults(labeling, graph.Data.ReferenceLabeling);
                }
                //WriterResults();
                var sensitivity = tp / (tp + fn);
                var specificity = tn / (tn + fp);
                mcc2 = (tp * tn - fp * fn) / Math.Sqrt((tp + fp) * (tp + fn) * (tn + fp) * (tn + fn));
                //newPoint.Score = mcc2;

                Log.Post("New MCC(IChanged): " + Math.Round(mcc, 5) + " Sens: " + Math.Round(sensitivity, 5) + " Spec: " + Math.Round(specificity, 5));
            }

            if (mcc2 > mcc)
            {
                ValueMapRasa.lastChanges.Clear();
                ValueMapEMax01.lastChanges.Clear();
                ValueMapEMax11.lastChanges.Clear();
                ValueMapEDiff01.lastChanges.Clear();
                ValueMapEDiff11.lastChanges.Clear();
                Log.Post("Intervals changed");

                MemoryPoints.RemoveAt(MemoryPoints.Count - 1);
            }
            else
            {
                ValueMapRasa.UndoLastChanges();
                ValueMapEMax01.UndoLastChanges();
                ValueMapEMax11.UndoLastChanges();
                ValueMapEDiff01.UndoLastChanges();
                ValueMapEDiff11.UndoLastChanges();

                if (MemoryPoints.Count == 1)
                {
                    MemoryPoints.Add(ReferencePoint);
                }

                var deltaomega = new double[weights.Length];
                for (int k = 0; k < MemoryPoints.Count - 1; k++)
                {
                    var pointOne = MemoryPoints[k];
                    for (int l = k + 1; l < MemoryPoints.Count; l++)
                    {
                        var   pointTwo           = MemoryPoints[l];
                        int[] countsRefMinusPred = new int[weights.Length];
                        for (int m = 0; m < weights.Length; m++)
                        {
                            countsRefMinusPred[m] = (pointOne.Counts[m] - pointTwo.Counts[m]);
                        }
                        var weightedScore = 0.0;
                        for (int m = 0; m < weights.Length; m++)
                        {
                            weightedScore += weights[m] * (countsRefMinusPred[m]);
                        }
                        double l2normsq = (countsRefMinusPred.Sum(entry => entry * entry));

                        var loss = 100 * (pointOne.Score - pointTwo.Score);

                        var deltaomegaFactor = (loss - weightedScore) / (l2normsq);
                        for (int m = 0; m < weights.Length; m++)
                        {
                            if (l2normsq > 0)
                            {
                                deltaomega[m] += deltaomegaFactor * countsRefMinusPred[m];
                            }
                        }
                    }
                }

                //if (MemoryPoints.Count >= memoryPoints)
                {
                    //normalize
                    var normFactor = MemoryPoints.Count * (MemoryPoints.Count - 1) / 2;
                    for (int m = 0; m < weights.Length; m++)
                    {
                        deltaomega[m] /= normFactor;
                    }

                    for (int k = 0; k < weights.Length; k++)
                    {
                        weights[k] += deltaomega[k];
                    }
                }
                //else
                //{
                //    for (int k = 0; k < weights.Length; k++)
                //    {
                //        weights[k] = 0.02 * random.NextDouble() - 0.01;
                //    }
                //}
                //if (iteration == 1)
                //MemoryPoints.Remove(ReferencePoint);
            }

            return(weights);
        }