Loading [MathJax]/jax/output/CommonHTML/config.js
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >ML.Net机器学习

我使用了微软的ML.Net机器学习。我想打印训练过程中使用的处理输入向量。我能打印出来吗?

代码语言:javascript
运行
AI代码解释
复制
    private static string _appPath => Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]);

    //TRAIN_DATA_FILEPATH: has the path to the dataset used to train the model.
    private static string TRAIN_DATA_FILEPATH => Path.Combine(_appPath, "..", "..", "..", "Data", "A.csv");
    //@"C:\Users\taqwa\Desktop\A.csv"
    private static string MODEL_FILEPATH = @"../../../../MyMLAppML.Model/MLModel.zip";

    // Create MLContext to be shared across the model creation workflow objects 
    // Set a random seed for repeatable/deterministic results across multiple trainings.
    private static MLContext mlContext = new MLContext(seed: 1);

    public static void CreateModel()
    {
        // Load Data
        //ModelInput is the input dataset class and has the following String fields: Cases, Algorith and InjuryOrIllness 
        IDataView trainingDataView = mlContext.Data.LoadFromTextFile<ModelInput>(
                                        path: TRAIN_DATA_FILEPATH,
                                        hasHeader: true,   //true if the Header property is not null; otherwise, false. The default is false.
                                        separatorChar: ',',
                                        allowQuoting: true,  //Whether the file can contain columns defined by a quoted string. Whether the input may include quoted values, which can contain separator characters, colons, and distinguish empty values from missing values. When true, consecutive separators denote a missing value and an empty value is denoted by "". When false, consecutive separators denote an empty value.
                                        allowSparse: false); //Whether the file can contain numerical vectors in sparse format.


        // Build training pipeline
        IEstimator<ITransformer> trainingPipeline = BuildTrainingPipeline(mlContext);

        // Evaluate quality of Model
    //    Evaluate(mlContext, trainingDataView, trainingPipeline);

        // Train Model
        ITransformer mlModel = TrainModel(mlContext, trainingDataView, trainingPipeline);

        // Save model
      //  SaveModel(mlContext, mlModel, MODEL_FILEPATH, trainingDataView.Schema);
    }

    public static IEstimator<ITransformer> BuildTrainingPipeline(MLContext mlContext)
    {
        // Data process configuration with pipeline data transformations 
        var dataProcessPipeline = mlContext.Transforms.Conversion.MapValueToKey("Algorithm", "Algorithm")
                                  //MapValueToKey: method to transform the Algorithm column into a numeric key type Algorithm column (a format accepted by classification algorithms) and add it as a new dataset column
                                  .Append(mlContext.Transforms.Categorical.OneHotEncoding(new[] { new InputOutputColumnPair("injuryOrIllness", "injuryOrIllness") }))
                                  //OneHotEncoding: which converts one or more input text columns specified in columns into as many columns of one-hot encoded vectors.
                                  .Append(mlContext.Transforms.Text.FeaturizeText("Cases_tf", "Cases"))
                                  //FeaturizeText which transforms the text (Cases_tf) columns into a numeric vector for each called Cases and Append the featurization to the pipeline
                                  .Append(mlContext.Transforms.Concatenate("Features", new[] { "injuryOrIllness", "Cases_tf" }))
                                  .Append(mlContext.Transforms.NormalizeMinMax("Features", "Features"))
                                  //AppendCacheCheckpoint to cache the DataView so when you iterate over the data multiple times using the cache might get better performance
                                  .AppendCacheCheckpoint(mlContext);


        // Set the training algorithm 
        //Here we used the AveragedPerceptron
        var trainer = mlContext.MulticlassClassification.Trainers.OneVersusAll(mlContext.BinaryClassification.Trainers.AveragedPerceptron(labelColumnName: "Algorithm", numberOfIterations: 10, featureColumnName: "Features"), labelColumnName: "Algorithm")
                                  .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));
        //var trainer = mlContext.MulticlassClassification.Trainers.SdcaMaximumEntropy(labelColumnName: "Algorithm", featureColumnName: "Features")
        //              .Append(mlContext.Transforms.Conversion.MapKeyToValue("PredictedLabel", "PredictedLabel"));

        //OneVersusAllTrainer: which predicts a multiclass target using one-versus-all strategy with the binary classification estimator specified by binaryEstimator.
        var trainingPipeline = dataProcessPipeline.Append(trainer);


        return trainingPipeline;

    }


    public static ITransformer TrainModel(MLContext mlContext, IDataView trainingDataView, IEstimator<ITransformer> trainingPipeline)
    {
        Console.WriteLine("=============== Training  model ===============");

        //Fit(): method trains your model by transforming the dataset and applying the training. and return the trained model.
        ITransformer model = trainingPipeline.Fit(trainingDataView);
        Console.WriteLine($"{trainingDataView.Schema}");



        Console.WriteLine("=============== End of training process ===============");
        return model;
    }

这是我代码的一部分。我尝试打印训练过程中使用的处理或特征化输入向量。

因此,我尝试将(trainingDataView.Schema)打印为Console.WriteLine($"{trainingDataView.Schema}");,但补丁看起来像(非公共成员)。

EN

回答 2

Stack Overflow用户

发布于 2019-11-28 08:41:28

您尝试过使用Preview()方法吗?Preview既可以在IEstimator上使用,也可以在ITransformer上使用。您可以使用GetColumn<>从IDataView获取特定列的值。此外,请查看此文档页面https://docs.microsoft.com/cs-cz/dotnet/machine-learning/how-to-guides/inspect-intermediate-data-ml-net

票数 1
EN

Stack Overflow用户

发布于 2020-03-21 23:40:46

您可以检查数据的模式或迭代每一行。

在第一种情况下,您可以使用:

代码语言:javascript
运行
AI代码解释
复制
var schema = data.Preview();

否则,您可以通过以下方式进行迭代:

代码语言:javascript
运行
AI代码解释
复制
 IEnumerable<ModelInput> inputData = mlContext.Data.CreateEnumerable<ModelInput>(data, reuseRowObject: true);

 foreach (ModelInput row in inputData)
 {
       foreach (var prop in row.GetType().GetProperties())
       {
            Console.WriteLine("{0}={1}", prop.Name, prop.GetValue(row, null));
       }
 }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58247400

复制
相关文章
Protobuf在Cmake中的正确使用
Protobuf是google开发的一个序列化和反序列化的协议库,我们可以自己设计传递数据的格式,通过.proto文件定义我们的要传递的数据格式。例如,在深度学习中常用的ONNX交换模型就是使用.proto编写的。我们可以通过多种前端(MNN、NCNN、TVM的前端)去读取这个.onnx这个模型,但是首先你要安装protobuf。
老潘
2023/10/19
1.9K0
Protobuf在Cmake中的正确使用
如何正确的在 Android 上使用协程 ?
你还记得是哪一年的 Google IO 正式宣布 Kotlin 成为 Android 一级开发语言吗?是 Google IO 2017 。如今两年时间过去了,站在一名 Android 开发者的角度来看,Kotlin 的生态环境越来越好了,相关的开源项目和学习资料也日渐丰富,身边愿意去使用或者试用 Kotlin 的朋友也变多了。常年混迹掘金的我也能明显感觉到 Kotlin 标签下的文章慢慢变多了(其实仍然少的可怜)。今年的 Google IO 也放出了 Kotlin First 的口号,许多新的 API 和功能特性将优先提供 Kotlin 支持。所以,时至今日,实在找不到安卓开发者不学 Kotlin 的理由了。
路遥TM
2021/08/31
2.9K0
vue中在父组件点击按钮触发子组件的事件
1、父组件的button元素绑定click事件,该事件指向notify方法 2、给子组件注册一个ref=“child” 3、父组件的notify的方法在处理时,使用了$refs.child把事件传递给子组件的parentMsg方法,同时携带着父组件中的参数msg 4、子组件接收到父组件的事件后,调用了parentMsg方法,把接收到的msg放到message数组中
江一铭
2022/06/16
6.6K0
在 Core Data 中查询和使用 count 的若干方法
在 Core Data 中,开发者经常需要面对查询记录数量(count),使用 count 作为谓词或排序条件等需求。本文将介绍在 Core Data 下查询和使用 count 的多种方法,适用于不同的场景。
东坡肘子
2022/07/28
4.9K0
在 Core Data 中查询和使用 count 的若干方法
【译】在正确的线程上观察
尽管很多人了解RxJava的基本逻辑,但是在Observable链和操作符究竟运行在哪个线程,仍然会有许多困惑。
用户1740424
2018/07/23
5410
【译】在正确的线程上观察
在PHP中strpos函数的正确使用方式
首先简单介绍下 strpos 函数,strpos 函数是查找某个字符在字符串中的位置,这里需要明确这个函数的作用,这个函数得到的是位置。 如果存在,返回数字,否则返回的是 false。 而很多时候我们拿这个函数用来判断字符串中是否存在某个字符,一些同学使用的姿势是这样的 // 判断‘沈唁志博客’中是否存在‘博客’这个词 if (strpos('沈唁志博客', '博客')) { // 如果存在执行此处代码 echo '存在'; }else{ // 如果不存在执行此处代码 e
沈唁
2018/05/24
5.5K0
在AppCode中的razor调用HtmlHelper方法和UrlHelper方法
using System.Web.WebPages; using System.Web.Mvc;
javascript.shop
2019/09/04
2.2K0
在OrderStatusActor构造方法中调用StartTimerAsync
TimerCallbackAsync方法以二进制形式接收用户状态。 在示例中,回调在将状态写入日志之前将状态 string 解码回 。
用户7108768
2021/09/26
2K0
关于使用MethodHandle在子类中调用祖父类重写方法的探究
这里直接看Son类的thinking方法(关于为何这样实现,在《深入理解Java虚拟机》读书笔记(七)--虚拟机字节码执行引擎(下)中也解释了)。
Java架构师必看
2021/11/29
9.7K0
5 种在 Vue 3 中定义组件的方法
英文 | https://fadamakis.com/the-5-ways-to-define-a-component-in-vue-3-aeb01ac6f39f
winty
2023/08/23
4310
5 种在 Vue 3 中定义组件的方法
自定义事件在 Vue.js 组件中的应用
Vue.js 组件的自定义事件可以让子组件向父组件传递数据,非常方便实用。在使用自定义事件时,我们可以使用 v-on 来绑定事件,每个 Vue 实例都实现了事件接口,即使用 $on(eventName) 监听事件和使用 $emit(eventName) 触发事件。此外,在父组件中,我们可以使用 v-on 来监听子组件触发的事件。
iOS程序应用
2023/03/15
4.1K0
自定义事件在 Vue.js 组件中的应用
在小程序中调用API在小程序中自定义弹窗组件
表明它是一个组件,我们称之为“子组件” 3. 注意:在组件wxss中不应使用ID选择器、属性选择器和标签名选择器。(只使用class)
九旬
2020/10/23
3.1K0
在处理PowerBuilder的itemchanged事件中,acceptText的使用介绍[通俗易懂]
在窗口的itemchanged事件中,获取当前输入的值时,往往是无法拿到值的,此时值还没有提交,
全栈程序员站长
2022/09/06
1.4K0
内网穿透神器:Ngrok在支付中的正确使用姿势
前言 随着互联网的发展,无论是web服务还是移动APP越来越多的都集成了第三方支付(支付宝、微信、银联)。通常作为服务提供方,支付成功以后都会有一个后端回调URL来通知是否调用者是否支付成功,这个URL必须是公网环境,并且可以被访问到。然而在实际开发测试环境中,我们一般都是在内网开发,所以说对于支付测试是一件比较麻烦的事情。 内网穿透 这时候,我们就需要内网穿透服务来解决第三方服务无法回调的问题了,下面我们来稍微盘点那些流行的内网穿透技术。 Ngrok ngrok 是一个反向代理,通过在公共的端点和本地运
小柒2012
2018/06/06
2.5K0
在XCode中如何使用高级查询
对于一个框架来说,仅有基本的CURD不行,NewLife.XCode同时还提供了一个非常宽松的方式来使用高级查询,以满足各种复杂的查询需求。 (本文同样适用于其它任何数据访问框架) 先上图看一个复杂查询的效果图: image.png 这里有8个固定的查询条件和1个模糊查询条件,加上多表关联(7张表)、分页、统计,如果用传统的做法,这个查询会非常的复杂。 这个页面有XCode实现,核心查询部分共100多行代码,包括一个查询、一个总记录数分页、两个统计(就是业绩、提成等的统计),看看高级查询代码: image
大石头
2018/01/15
5.3K0
在XCode中如何使用高级查询
点击加载更多

相似问题

使用jQuery在单击或焦点上触发事件的正确方法是什么?

13

在带有边界的按钮上使用单击事件的正确方法是什么?

11

在React中的许多其他组件中调用组件方法的正确方法是什么?

17

停止从RTKQ接收流更新的正确方法是什么?

18

在组件上添加单击事件

20
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档