Beispiel #1
0
 private Layer(string id, int length     = 1, bool biased = true,
               IActivationFunction aFunc = null, IWeightFunction wFunc    = null,
               IInputFunction iFunc      = null, IWeightInitializer wInit = null)
 {
     this.id     = id;
     this.length = length;
     this.biased = biased;
     if (aFunc != null)
     {
         this.aFunc = aFunc;
     }
     if (wFunc != null)
     {
         this.wFunc = wFunc;
     }
     if (iFunc != null)
     {
         this.iFunc = iFunc;
     }
     if (wInit != null)
     {
         this.wInit = wInit;
     }
     if (biased)
     {
         this.biases = this.wInit != null?
                       this.wInit.Initialize(this.length) :
                           Utils.WeightInitializers.InitZero.Initialize(this.length);
     }
 }
Beispiel #2
0
 private void Fix(Hole hole, IWeightFunction weightFunction)
 {
     foreach (var pix in hole.HolePixels)
     {
         FillPixelWithWeightFunction(hole, pix, weightFunction);
     }
 }
Beispiel #3
0
        private Tuple <Type, object> EvaluateCreateLayer(ParseTreeNode node)
        {
            var layerParams = EvaluateKeyValuePair(node.ChildNodes[0]);

            int  length = 1;
            bool biased = true;
            IActivationFunction aFunc = null;
            IInputFunction      iFunc = null;
            IWeightFunction     wFunc = null;
            IWeightInitializer  wInit = null;

            if (!layerParams.ContainsKey("id"))
            {
                throw new Exception("ID is necessary to initialize a layer!");
            }
            if (layerParams.ContainsKey("length"))
            {
                length = Int32.Parse(layerParams["length"]);
            }
            if (layerParams.ContainsKey("biased"))
            {
                biased = bool.Parse(layerParams["biased"]);
            }
            if (layerParams.ContainsKey("activationFunction"))
            {
                aFunc = (IActivationFunction)(typeof(ActivationFunctions)
                                              .GetField(layerParams["activationFunction"])
                                              .GetValue(null));
            }
            if (layerParams.ContainsKey("weightFunction"))
            {
                wFunc = (IWeightFunction)(typeof(WeightFunctions)
                                          .GetField(layerParams["weightFunction"])
                                          .GetValue(null));
            }
            if (layerParams.ContainsKey("inputFunction"))
            {
                iFunc = (IInputFunction)(typeof(InputFunctions)
                                         .GetField(layerParams["inputFunction"])
                                         .GetValue(null));
            }
            if (layerParams.ContainsKey("weightInitializer"))
            {
                wInit = (IWeightInitializer)(typeof(WeightInitializers)
                                             .GetField(layerParams["weightInitializer"])
                                             .GetValue(null));
            }

            var layer = Layer.Create(layerParams["id"], length: length, aFunc: aFunc,
                                     wFunc: wFunc, iFunc: iFunc, wInit: wInit, biased: biased);

            return(new Tuple <Type, object>(typeof(Layer), layer));
        }
Beispiel #4
0
        private void FillPixelWithWeightFunction(Hole hole, Pixel pix, IWeightFunction weightFunction)
        {
            if (pix.Value != -1)
            {
                return;
            }

            float numerator   = 0;
            float denominator = 0;

            foreach (var yi in hole.Boundary)
            {
                var w = weightFunction.GetWeight(yi, pix);
                numerator   += (w * yi.Value);
                denominator += w;
            }

            pix.Value = numerator / denominator;
        }
Beispiel #5
0
 //rely on weight getting written later
 private bool WriteWeightFunction(JsonWriter writer, IWeightFunction value, JsonSerializer serializer) => value != null;
		//rely on weight getting written later
		private bool WriteWeightFunction(JsonWriter writer, IWeightFunction value, JsonSerializer serializer) => value != null;
Beispiel #7
0
        /// <summary>
        /// Fills holes using weight function
        /// </summary>
        /// <param name="weightFunction"></param>
        public void FillHoleWithWeightFunction(Hole hole, IWeightFunction weightFunction)
        {
            Fix(hole, weightFunction);

            _image.IsHoled = false;
        }
Beispiel #8
0
 /// <summary>
 /// Fixes hole. Default implementation of interface - using supplied weight function
 /// </summary>
 /// <param name="hole"></param>
 public void FixHole(Hole hole, IWeightFunction weightFunction)
 {
     FillHoleWithWeightFunction(hole, weightFunction);
 }
Beispiel #9
0
            public MultiRepresentationWeightFunction <TDictionary> Product(MultiRepresentationWeightFunction <TDictionary> weightFunction)
            {
                if (IsCanonicZero() || weightFunction.IsCanonicZero())
                {
                    return(Zero());
                }

                PointMassWeightFunction pointMass = null;
                IWeightFunction         other     = null;

                if (this.weightFunction is PointMassWeightFunction thisPointMass)
                {
                    pointMass = thisPointMass;
                    other     = weightFunction.weightFunction;
                }
                else if (weightFunction.weightFunction is PointMassWeightFunction otherPointMass)
                {
                    pointMass = otherPointMass;
                    other     = this.weightFunction;
                }
                if (pointMass != null && !other.UsesGroups)
                {
                    var logValue = other.GetLogValue(pointMass.Point);
                    if (double.IsNegativeInfinity(logValue))
                    {
                        return(Zero());
                    }
                    else if (logValue == 0.0)
                    {
                        return(FromPointMass(pointMass));
                    }
                    else
                    {
                        return(FromDictionary(
                                   DictionaryWeightFunction <TDictionary> .FromDistinctWeights(
                                       new[] { new KeyValuePair <TSequence, Weight>(pointMass.Point, Weight.FromLogValue(logValue)) })));
                    }
                }

                TDictionary dictionary = null;

                if (this.weightFunction is TDictionary thisDictionary)
                {
                    if (weightFunction.weightFunction is TDictionary secondDictionary)
                    {
                        return(FromDictionary(thisDictionary.Product(secondDictionary)));
                    }

                    dictionary = thisDictionary;
                    other      = weightFunction.weightFunction;
                }
                else if (weightFunction.weightFunction is TDictionary otherDictionary)
                {
                    dictionary = otherDictionary;
                    other      = this.weightFunction;
                }

                if (dictionary != null && !other.UsesGroups)
                {
                    var resultList = new List <KeyValuePair <TSequence, Weight> >(dictionary.Dictionary.Count);
                    foreach (var kvp in dictionary.Dictionary)
                    {
                        if (!kvp.Value.IsZero)
                        {
                            var otherLogValue = other.GetLogValue(kvp.Key);
                            if (!double.IsNegativeInfinity(otherLogValue))
                            {
                                resultList.Add(new KeyValuePair <TSequence, Weight>(kvp.Key, kvp.Value * Weight.FromLogValue(otherLogValue)));
                            }
                        }
                    }
                    if (resultList.Count == 0)
                    {
                        return(Zero());
                    }
                    else if (resultList.Count == 1 && resultList[0].Value.LogValue == 0.0)
                    {
                        return(FromPoint(resultList[0].Key));
                    }
                    else
                    {
                        return(FromDictionary(
                                   DictionaryWeightFunction <TDictionary> .FromDistinctWeights(resultList)));
                    }
                }

                return(FromAutomaton(AsAutomaton().Product(weightFunction.AsAutomaton())));
            }
Beispiel #10
0
 /// <summary>
 /// Internal Default Constuctor.
 /// </summary>
 internal RankingOptions()
 {
     this.CacheDuration = TimeSpan.Zero;
     this.TopN          = 25;
     this.weightFunc    = WeightFunction.Empty;
 }
Beispiel #11
0
 public RankingOptions(TimeSpan?cacheDuration = null, long topN = -1, IWeightFunction weightFunc = null)
 {
     this.CacheDuration = cacheDuration;
     this.TopN          = topN;
     this.weightFunc    = weightFunc != null ? weightFunc : WeightFunction.Empty;
 }
Beispiel #12
0
 public static Layer Create(string id, int length     = 1, bool biased = false,
                            IActivationFunction aFunc = null, IWeightFunction wFunc    = null,
                            IInputFunction iFunc      = null, IWeightInitializer wInit = null)
 {
     return(new Layer(id, length, biased, aFunc, wFunc, iFunc, wInit));
 }
Beispiel #13
0
 private MultiRepresentationWeightFunction(IWeightFunction weightFunction)
 {
     this.weightFunction = weightFunction;
 }