Пример #1
0
        public static object Predict(object predictionEngine, ClassGenerator classGenerator, object example)
        {
            var methodInfo = predictionEngine.GetType().GetMethod("Predict", new[] { classGenerator.ClassType });
            var prediction = methodInfo.Invoke(predictionEngine, new object[] { example });

            return(prediction);
        }
Пример #2
0
        public static ClassGenerator GenerateLabelClass(string className, string classNamespace)
        {
            Dictionary <string, string> attributes = new Dictionary <string, string>();

            attributes.Add("ColumnNameAttribute", "PredictedLabel");
            ClassGenerator labelClassGenerator = new ClassGenerator(className, classNamespace);

            labelClassGenerator.AddField("PredictedLabels", typeof(string), System.CodeDom.MemberAttributes.Public, attributes);
            labelClassGenerator.Compile();

            return(labelClassGenerator);
        }
Пример #3
0
        public static ClassGenerator GenerateDataSetClass(IEnumerable <MLFeature> features, string className, string classNamespace)
        {
            ClassGenerator classGenerator = new ClassGenerator(className, classNamespace);

            foreach (var item in features)
            {
                classGenerator.AddField(item.Name, item.Type, System.CodeDom.MemberAttributes.Public);
            }
            classGenerator.Compile();

            return(classGenerator);
        }
Пример #4
0
        static void Main(string[] args)
        {
            MLContext mlContext = new MLContext();

            ClassGenerator classGenerator = new ClassGenerator("GeneratedIris", "CustomClass");

            classGenerator.AddField("SepalLength", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("SepalWidth", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("PetalLength", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("PetalWidth", typeof(float), System.CodeDom.MemberAttributes.Public);
            classGenerator.AddField("Label", typeof(string), System.CodeDom.MemberAttributes.Public);
            classGenerator.Compile();

            List <object> generatedDataSet = new List <object>();

            dataset.ToList().ForEach((d) =>
            {
                generatedDataSet.Add(GetDynamicClass(d, classGenerator.GetInstance()));
            });

            var instance = classGenerator.GetInstance().GetType();
            DataViewGenerator listGenerator = new DataViewGenerator("ListIris", "CustomGenerator", instance, classGenerator.NamespaceName);
            var type       = listGenerator.GeneratorType;
            var methodInfo = type.GetMethod("GetDataView");
            var dataView   = methodInfo.Invoke(null, new object[] { generatedDataSet.ToList() });

            IDataView trainingDataView = (IDataView)dataView;

            trainingDataView.Schema.ToList().Add(new DataViewSchema.Column());

            var pipeline = mlContext.Transforms.Conversion.MapValueToKey("Label")
                           .Append(mlContext.Transforms.Concatenate("Features", "SepalLength", "SepalWidth", "PetalLength", "PetalWidth"))
                           .AppendCacheCheckpoint(mlContext)
                           .Append(mlContext.MulticlassClassification.Trainers.StochasticDualCoordinateAscent(labelColumnName: "Label", featureColumnName: "Features"))
                           .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel"));


            TransformerChain <Microsoft.ML.Transforms.KeyToValueMappingTransformer> model = pipeline.Fit(trainingDataView);

            var prediction = model.CreatePredictionEngine <IrisData, IrisPrediction>(mlContext).Predict(
                new IrisData()
            {
                SepalLength = 5.9f,
                SepalWidth  = 3.0f,
                PetalLength = 5.1f,
                PetalWidth  = 1.8f,
            });

            Console.WriteLine(prediction.PredictedLabels);

            Console.ReadLine();
        }
Пример #5
0
        public MLTypesGenerator(string className, string namespaceName, ClassGenerator datSetClass, ClassGenerator labelClass)
        {
            // Add an assembly reference.
            compilerParameters.ReferencedAssemblies.AddRange(new []
            {
                "System.dll",
                "System.Collections.dll",
                "System.Collections.Concurrent.dll",
                "System.Core.dll",
                "netstandard.dll",
                "System.Linq.dll",
                "System.Xml.dll",
                "System.Xml.Linq.dll",
                "System.Linq.Expressions.dll",
                "System.Linq.Parallel.dll",
                "System.Linq.Queryable.dll",
                "Microsoft.ML.Data.dll",
                "Microsoft.ML.Core.dll",
                "Microsoft.Data.DataView.dll",
                $"{datSetClass.namespaceName}.dll",
                $"{labelClass.namespaceName}.dll"
            });
            compilerParameters.GenerateExecutable = false;
            compilerParameters.GenerateInMemory   = false;

            this.OutputDll = $"{namespaceName}.dll";
            compilerParameters.OutputAssembly = this.OutputDll;

            this.namespaceName = namespaceName;
            namespaces         = new CodeNamespace(namespaceName);
            namespaces.Imports.Add(new CodeNamespaceImport("System"));
            namespaces.Imports.Add(new CodeNamespaceImport("System.Collections"));
            namespaces.Imports.Add(new CodeNamespaceImport("System.Collections.Generic"));
            namespaces.Imports.Add(new CodeNamespaceImport("System.Linq"));
            namespaces.Imports.Add(new CodeNamespaceImport("Microsoft.ML"));
            namespaces.Imports.Add(new CodeNamespaceImport("Microsoft.ML.Data"));
            namespaces.Imports.Add(new CodeNamespaceImport("Microsoft.Data.DataView"));
            namespaces.Imports.Add(new CodeNamespaceImport($"{datSetClass.namespaceName}"));
            namespaces.Imports.Add(new CodeNamespaceImport($"{labelClass.namespaceName}"));
            compileUnit.Namespaces.Add(namespaces);

            this.className             = className;
            customClass                = new CodeTypeDeclaration(className);
            customClass.IsClass        = true;
            customClass.TypeAttributes = System.Reflection.TypeAttributes.Public;

            this.datSetClass = datSetClass;
            this.labelClass  = labelClass;
            this.Compile();
        }
Пример #6
0
        public static ClassGenerator GenerateDataSetClass(Type classType, string className, string classNamespace)
        {
            if (classType.GetTypeInfo().IsClass)
            {
                ClassGenerator classGenerator = new ClassGenerator(className, classNamespace);

                foreach (var item in classType.GetFields())
                {
                    classGenerator.AddField(item.Name, item.FieldType, System.CodeDom.MemberAttributes.Public);
                }
                classGenerator.Compile();

                return(classGenerator);
            }
            else
            {
                throw new Exception("Type not a class");
            }
        }
Пример #7
0
        public static MLTypesGenerator CreateTypesGenarator(ClassGenerator classGenerator, ClassGenerator labelClassGenerator)
        {
            MLTypesGenerator typesGenerator = new MLTypesGenerator($"{classGenerator.className}TypesGnerator", $"{classGenerator.className}TypesGneratorNamespace", classGenerator, labelClassGenerator);

            return(typesGenerator);
        }