/// <summary>
        /// Deserializes the object from the stream.
        /// </summary>
        /// <param name="reader">Stream to read from.</param>
        /// <returns>Node object.</returns>
        public override object Read(JsonReader reader)
        {
            if (reader.IsNull())
            {
                return(null);
            }
            else
            {
                var node = (RecurrentNeuron)this.Create();

                node.Label = reader.ReadProperty().Value.ToString();

                node.Id      = (int)reader.ReadProperty().Value;
                node.NodeId  = (int)reader.ReadProperty().Value;
                node.LayerId = (int)reader.ReadProperty().Value;
                node.IsBias  = (bool)reader.ReadProperty().Value;

                var activation = reader.ReadProperty().Value;
                if (activation != null)
                {
                    node.ActivationFunction = Ject.FindType(activation.ToString()).CreateDefault <IFunction>();
                }

                var output = reader.ReadProperty().Value;
                if (output != null)
                {
                    node.ActivationFunction = Ject.FindType(output.ToString()).CreateDefault <IFunction>();
                }

                node.Constrained = (bool)reader.ReadProperty().Value;
                node.delta       = (double)reader.ReadProperty().Value;
                node.Delta       = (double)reader.ReadProperty().Value;
                node.Input       = (double)reader.ReadProperty().Value;
                node.Output      = (double)reader.ReadProperty().Value;

                //TODO: Read in RNN properties

                return(node);
            }
        }
Exemple #2
0
        public void Test_Univariate_Back_Conversions()
        {
            // boolean
            Assert.Equal(true, Ject.Convert(1, typeof(bool)));
            Assert.Equal(false, Ject.Convert(0, typeof(bool)));
            Assert.Equal(false, Ject.Convert(-1, typeof(bool)));

            // numeric types

            Assert.Equal((Byte)1, Ject.Convert(1d, typeof(Byte)));                               // byte
            Assert.Equal((SByte)1, Ject.Convert(1d, typeof(SByte)));                             // sbyte
            Assert.Equal((Decimal)0.4m, Ject.Convert(0.4d, typeof(Decimal)));                    // decimal
            Assert.Equal((Double)0.1d, Ject.Convert(0.1d, typeof(Double)));                      // double
            Assert.Equal((Single)300f, Ject.Convert(300d, typeof(Single)));                      // single
            Assert.Equal((Int16)1, Ject.Convert(1d, typeof(Int16)));                             // int16
            Assert.Equal((UInt16)2, Ject.Convert(2d, typeof(UInt16)));                           // uint16
            Assert.Equal((Int32)2323432, Ject.Convert(2323432, typeof(Int32)));                  // int32
            Assert.Equal((UInt32)2323432, Ject.Convert(2323432d, typeof(UInt32)));               // uint32
            Assert.Equal((Int64)1232323434345, Ject.Convert(1232323434345d, typeof(Int64)));     // int64
            Assert.Equal((UInt64)1232323434345, Ject.Convert(1232323434345d, typeof(UInt64)));   // uint64
            Assert.Equal((long)1232323434345, Ject.Convert(1232323434345d, typeof(long)));       // long

            // enum
            Assert.Equal(FakeEnum.Item0, Ject.Convert(0d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item1, Ject.Convert(1d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item2, Ject.Convert(2d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item3, Ject.Convert(3d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item4, Ject.Convert(4d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item5, Ject.Convert(5d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item6, Ject.Convert(6d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item7, Ject.Convert(7d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item8, Ject.Convert(8d, typeof(FakeEnum)));
            Assert.Equal(FakeEnum.Item9, Ject.Convert(9d, typeof(FakeEnum)));

            // char
            Assert.Equal('A', Ject.Convert(65d, typeof(char)));

            // timespan
            Assert.Equal(TimeSpan.FromSeconds(300), Ject.Convert(300d, typeof(TimeSpan)));
        }
Exemple #3
0
        /// <summary>
        ///   Deserializes a generic model object from the stream.
        /// </summary>
        /// <param name="reader">A JSON Reader.</param>
        /// <returns></returns>
        public override object Read(JsonReader reader)
        {
            if (reader.IsNull())
            {
                return(null);
            }
            var model = (numl.Supervised.Model)Create();

            model.Descriptor        = (Descriptor)reader.ReadProperty().Value;
            model.NormalizeFeatures = (bool)reader.ReadProperty().Value;

            var normalizer = Ject.FindType(reader.ReadProperty().Value.ToString());

            if (normalizer != null)
            {
                model.FeatureNormalizer = (INormalizer)Activator.CreateInstance(normalizer);
            }

            model.FeatureProperties = (Summary)reader.ReadProperty().Value;

            return(model);
        }
Exemple #4
0
        /// <summary>Gets best split.</summary>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <param name="used">The used.</param>
        /// <returns>The best split.</returns>
        private Tuple <int, double, Impurity> GetBestSplit(Matrix x, Vector y, List <int> used)
        {
            double bestGain    = -1;
            var    bestFeature = -1;

            Impurity bestMeasure = null;

            for (var i = 0; i < x.Cols; i++)
            {
                // already used?
                if (used.Contains(i))
                {
                    continue;
                }

                var measure = (Impurity)Ject.Create(ImpurityType);

                // get appropriate column vector
                var feature = x.Col(i);
                // get appropriate feature at index i
                // (important on because of multivalued
                // cols)
                var property = Descriptor.At(i);
                // if discrete, calculate full relative gain
                var gain = property.Discrete ? measure.RelativeGain(y, feature) : measure.SegmentedRelativeGain(y, feature, Width);

                // best one?
                if (!(gain > bestGain))
                {
                    continue;
                }

                bestGain    = gain;
                bestFeature = i;
                bestMeasure = measure;
            }

            return(new Tuple <int, double, Impurity>(bestFeature, bestGain, bestMeasure));
        }
        /// <summary>
        /// Reads serialized neural network model
        /// </summary>
        /// <param name="reader">JsonReader</param>
        /// <returns>Deserialized neural network model</returns>
        public override object Read(JsonReader reader)
        {
            if (reader.IsNull())
            {
                return(null);
            }
            else
            {
                var model          = base.Read(reader) as NeuralNetworkModel;
                var outputFunction = reader.ReadProperty().Value;
                if (outputFunction != null)
                {
                    var type = Ject.FindType(outputFunction.ToString());
                    model.OutputFunction = (IFunction)Activator.CreateInstance(type);
                }

                model.Network = reader.ReadProperty().Value as Network;


                return(model);
            }
        }
        public void Test_Univariate_Conversions()
        {
            // boolean
            Assert.AreEqual(1, Ject.Convert(true));
            Assert.AreEqual(-1, Ject.Convert(false));

            // numeric types
            Assert.AreEqual(1d, Ject.Convert((Byte)1));                            // byte
            Assert.AreEqual(1d, Ject.Convert((SByte)1));                           // sbyte
            Assert.AreEqual(0.4d, Ject.Convert((Decimal)0.4m));                    // decimal
            Assert.AreEqual(0.1d, Ject.Convert((Double)0.1d));                     // double
            Assert.AreEqual(300d, Ject.Convert((Single)300f));                     // single
            Assert.AreEqual(1d, Ject.Convert((Int16)1));                           // int16
            Assert.AreEqual(2d, Ject.Convert((UInt16)2));                          // uint16
            Assert.AreEqual(2323432, Ject.Convert((Int32)2323432));                // int32
            Assert.AreEqual(2323432d, Ject.Convert((UInt32)2323432));              // uint32
            Assert.AreEqual(1232323434345d, Ject.Convert((Int64)1232323434345));   // int64
            Assert.AreEqual(1232323434345d, Ject.Convert((UInt64)1232323434345));  // uint64
            Assert.AreEqual(1232323434345d, Ject.Convert((long)1232323434345));    // long

            // enum
            Assert.AreEqual(0d, Ject.Convert(FakeEnum.Item0));
            Assert.AreEqual(1d, Ject.Convert(FakeEnum.Item1));
            Assert.AreEqual(2d, Ject.Convert(FakeEnum.Item2));
            Assert.AreEqual(3d, Ject.Convert(FakeEnum.Item3));
            Assert.AreEqual(4d, Ject.Convert(FakeEnum.Item4));
            Assert.AreEqual(5d, Ject.Convert(FakeEnum.Item5));
            Assert.AreEqual(6d, Ject.Convert(FakeEnum.Item6));
            Assert.AreEqual(7d, Ject.Convert(FakeEnum.Item7));
            Assert.AreEqual(8d, Ject.Convert(FakeEnum.Item8));
            Assert.AreEqual(9d, Ject.Convert(FakeEnum.Item9));

            // char
            Assert.AreEqual(65d, Ject.Convert('A'));

            // timespan
            Assert.AreEqual(300d, Ject.Convert(TimeSpan.FromSeconds(300)));
        }
        /// <summary>Preprocess data set to create dictionary.</summary>
        /// <param name="examples">.</param>
        public override void PreProcess(IEnumerable <object> examples)
        {
            var q = from s in examples
                    select Ject.Get(s, Name).ToString();

            if (AsEnum)
            {
                Dictionary = StringHelpers.BuildEnumDictionary(q).Select(kv => kv.Key).ToArray();
            }
            else
            {
                switch (SplitType)
                {
                case StringSplitType.Character:
                    Dictionary = StringHelpers.BuildCharDictionary(q, Exclude).Select(kv => kv.Key).ToArray();
                    break;

                case StringSplitType.Word:
                    Dictionary = StringHelpers.BuildWordDictionary(q, Separator, Exclude).Select(kv => kv.Key).ToArray();
                    break;
                }
            }
        }
Exemple #8
0
        public override object Read(JsonReader reader)
        {
            var name = reader.ReadProperty().Value.ToString();

            Type type     = null;
            var  typeName = reader.ReadProperty().Value;

            if (typeName != null)
            {
                type = Ject.FindType(typeName.ToString());
            }

            var start    = int.Parse(reader.ReadProperty().Value.ToString());
            var discrete = (bool)reader.ReadProperty().Value;

            var p = (Property)Create();

            p.Name     = name;
            p.Type     = type;
            p.Start    = start;
            p.Discrete = discrete;
            return(p);
        }
Exemple #9
0
 public void Remove(string path)
 {
     Ject.RemoveAt(Ject.FindIndex(x => x.Contains(path)));
 }
Exemple #10
0
 /// <summary>
 /// Writes a simple type to the underlying stream (see <seealso cref="TypeHelpers.IsSimpleType(Type, Type[])"/>)
 /// </summary>
 /// <param name="value"></param>
 private void WriteSimpleType(object value)
 {
     _writer.Write(Ject.Convert(value).ToString("r", CultureInfo.InvariantCulture));
 }
Exemple #11
0
 /// <summary>Convert the numeric representation back to the original type.</summary>
 /// <param name="val">.</param>
 /// <returns>An object.</returns>
 public virtual object Convert(double val)
 {
     return(Ject.Convert(val, this.Type));
 }
Exemple #12
0
        /// <summary>Preprocess data set to create category list.</summary>
        /// <param name="examples">.</param>
        public override void PreProcess(IEnumerable <object> examples)
        {
            var q = examples.Select(s => (Guid)Ject.Get(s, Name)).Distinct();

            Categories = q.ToArray();
        }
Exemple #13
0
 /// <summary>
 /// Returns the raw value from the object for the supplied property.
 /// </summary>
 /// <param name="o">Object to process.</param>
 /// <param name="property">Property value of the object to return.</param>
 /// <returns></returns>
 public object GetValue(object o, Property property)
 {
     return(Ject.Get(o, property.Name));
 }
Exemple #14
0
        /// <summary>Generates a model.</summary>
        /// <param name="generator">Model generator used.</param>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <param name="examples">Source data.</param>
        /// <param name="trainingPct">The training pct.</param>
        /// <param name="total">Number of Examples</param>
        /// <returns>The model.</returns>
        private static LearningModel GenerateModel(
            IGenerator generator,
            Matrix x,
            Vector y,
            IEnumerable <object> examples,
            double trainingPct,
            int total)
        {
            var descriptor = generator.Descriptor;
            //var total = examples.Count();
            var trainingCount = (int)System.Math.Floor(total * trainingPct);

            // 100 - trainingPercentage for testing
            var testingSlice = GetTestPoints(total - trainingCount, total).ToArray();

            // trainingPercentage for training
            var trainingSlice = GetTrainingPoints(testingSlice, total).ToArray();

            // training
            var x_t = x.Slice(trainingSlice);
            var y_t = y.Slice(trainingSlice);

            // generate model
            var model = generator.Generate(x_t, y_t);

            model.Descriptor = descriptor;

            var score = new Score();

            if (testingSlice.Count() > 0)
            {
                // testing
                var test   = GetTestExamples(testingSlice, examples);
                var y_pred = new Vector(test.Length);
                var y_test = descriptor.ToExamples(test).Item2;

                var isBinary = y_test.IsBinary();
                if (isBinary)
                {
                    y_test = y_test.ToBinary(f => f == 1d, 1.0, 0.0);
                }

                for (var j = 0; j < test.Length; j++)
                {
                    // items under test
                    var o = test[j];

                    // make prediction
                    var features = descriptor.Convert(o, false).ToVector();
                    // --- temp changes ---
                    var val  = model.Predict(features);
                    var pred = descriptor.Label.Convert(val);

                    var truth = Ject.Get(o, descriptor.Label.Name);

                    if (truth.Equals(pred))
                    {
                        y_pred[j] = y_test[j];
                    }
                    else
                    {
                        y_pred[j] = isBinary ? (y_test[j] >= 1d ? 0d : 1d) : val;
                    }
                }

                // score predictions
                score = Score.ScorePredictions(y_pred, y_test);
            }

            return(new LearningModel {
                Generator = generator, Model = model, Score = score
            });
        }
Exemple #15
0
        /// <summary>Generates a model.</summary>
        /// <param name="generator">Model generator used.</param>
        /// <param name="x">The Matrix to process.</param>
        /// <param name="y">The Vector to process.</param>
        /// <param name="examples">Source data.</param>
        /// <param name="trainingPct">The training pct.</param>
        /// <param name="total">Number of Examples</param>
        /// <returns>The model.</returns>
        private static LearningModel GenerateModel(
            IGenerator generator,
            Matrix x,
            Vector y,
            IEnumerable <object> examples,
            double trainingPct,
            int total)
        {
            var descriptor = generator.Descriptor;

            // var total = examples.Count();
            var trainingCount = (int)System.Math.Floor(total * trainingPct);

            // 100 - trainingPercentage for testing
            var testingSlice = GetTestPoints(total - trainingCount, total).ToArray();

            // trainingPercentage for training
            var trainingSlice = GetTrainingPoints(testingSlice, total).ToArray();

            // training
            var x_t = x.Slice(trainingSlice);
            var y_t = y.Slice(trainingSlice);

            // generate model
            var model = generator.Generate(x_t, y_t);

            model.Descriptor = descriptor;

            // testing
            var    test     = GetTestExamples(testingSlice, examples);
            double accuracy = 0;

            for (var j = 0; j < test.Length; j++)
            {
                // items under test
                var o = test[j];

                // get truth
                var truth = Ject.Get(o, descriptor.Label.Name);

                // if truth is a string, sanitize
                if (descriptor.Label.Type == typeof(string))
                {
                    truth = StringHelpers.Sanitize(truth.ToString());
                }

                // make prediction
                var features = descriptor.Convert(o, false).ToVector();

                var p    = model.Predict(features);
                var pred = descriptor.Label.Convert(p);

                // assess accuracy
                if (truth.Equals(pred))
                {
                    accuracy += 1;
                }
            }

            // get percentage correct
            accuracy /= test.Length;

            return(new LearningModel {
                Generator = generator, Model = model, Accuracy = accuracy
            });
        }
Exemple #16
0
 public string[] GetData()
 {
     return(Ject.Select(x => x.GetLastPart()).ToArray());
 }
Exemple #17
0
 public void Add(string path)
 {
     Ject.Add(path);
     CurrentIndex = Ject.FindIndex(x => x == path);
 }