ML.NET Cookbook:(18)如何在文本数据上训练模型?
一般來說,所有的ML.NET學(xué)習(xí)器都希望這些特征是一個(gè)浮點(diǎn)向量。因此,如果您的一些數(shù)據(jù)不是一個(gè)float,您需要將其轉(zhuǎn)換為float。
如果我們想學(xué)習(xí)文本數(shù)據(jù),我們需要從文本中“提取特征”。NLP(自然語言處理)的整個(gè)研究領(lǐng)域都在處理這個(gè)問題。在ML.NET中,我們提供了一些文本特征提取的基本機(jī)制:
文本規(guī)范化(刪除標(biāo)點(diǎn)符號(hào)、變音符號(hào)、切換為小寫等)
基于分隔符的標(biāo)記化。
停用詞刪除。
Ngram和skip-gram提取。
TF-IDF重縮放。
詞袋轉(zhuǎn)換。
NET提供了一個(gè)名為TextFeaturizer的“一站式”操作,它將上述步驟作為一個(gè)大的“文本特征化”來運(yùn)行。我們已經(jīng)在文本數(shù)據(jù)集上對(duì)它進(jìn)行了廣泛的測(cè)試,我們確信它的性能相當(dāng)好,而不需要深入研究操作。
但是,我們還提供了一些基本操作,讓您可以自定義NLP處理。下面是我們使用它們的例子。
維基百科 detox 數(shù)據(jù)集:
Sentiment???SentimentText 1?Stop?trolling,?zapatancas,?calling?me?a?liar?merely?demonstartes?that?you?arer?Zapatancas.?You?may?choose?to?chase?every?legitimate?editor?from?this?site?and?ignore?me?but?I?am?an?editor?with?a?record?that?isnt?99%?trolling?and?therefore?my?wishes?are?not?to?be?completely?ignored?by?a?sockpuppet?like?yourself.?The?consensus?is?overwhelmingly?against?you?and?your?trollin?g?lover?Zapatancas,?? 1?:::::?Why?are?you?threatening?me??I'm?not?being?disruptive,?its?you?who?is?being?disruptive.??? 0?"?*::Your?POV?and?propaganda?pushing?is?dully?noted.?However?listing?interesting?facts?in?a?netral?and?unacusitory?tone?is?not?POV.?You?seem?to?be?confusing?Censorship?with?POV?monitoring.?I?see?nothing?POV?expressed?in?the?listing?of?intersting?facts.?If?you?want?to?contribute?more?facts?or?edit?wording?of?the?cited?fact?to?make?them?sound?more?netral?then?go?ahead.?No?need?to?CENSOR?interesting?factual?information.?" 0?::::::::This?is?a?gross?exaggeration.?Nobody?is?setting?a?kangaroo?court.?There?was?a?simple?addition?concerning?the?airline.?It?is?the?only?one?disputed?here.??? //?創(chuàng)建加載器:定義數(shù)據(jù)列以及它們?cè)谖谋疚募械奈恢谩?var?loader?=?mlContext.Data.CreateTextLoader(new[]?{new?TextLoader.Column("IsToxic",?DataKind.Boolean,?0),new?TextLoader.Column("Message",?DataKind.String,?1),},hasHeader:?true );//?加載數(shù)據(jù)。 var?data?=?loader.Load(dataPath);//?檢查從文件中讀取的消息文本。 var?messageTexts?=?data.GetColumn<string>(data.Schema["Message"]).Take(20).ToArray();//?應(yīng)用ML.NET支持的各種文本操作。 var?pipeline?=//?一站式運(yùn)行全部文本特征化。mlContext.Transforms.Text.FeaturizeText("TextFeatures",?"Message")//?為以后的轉(zhuǎn)換規(guī)范化消息文本.Append(mlContext.Transforms.Text.NormalizeText("NormalizedMessage",?"Message"))// NLP管道1:詞袋。.Append(mlContext.Transforms.Text.ProduceWordBags("BagOfWords",?"NormalizedMessage"))// NLP管道2:bag of bigrams,使用散列而不是字典索引。.Append(mlContext.Transforms.Text.ProduceHashedWordBags("BagOfBigrams","NormalizedMessage",?ngramLength:?2,?useAllLengths:?false))// NLP管道3:具有TF-IDF加權(quán)的三字符序列包。.Append(mlContext.Transforms.Text.TokenizeIntoCharactersAsKeys("MessageChars",?"Message")).Append(mlContext.Transforms.Text.ProduceNgrams("BagOfTrichar",?"MessageChars",?ngramLength:?3,?weighting:?NgramExtractingEstimator.WeightingCriteria.TfIdf))// NLP管道4:詞嵌入。.Append(mlContext.Transforms.Text.TokenizeIntoWords("TokenizedMessage",?"NormalizedMessage")).Append(mlContext.Transforms.Text.ApplyWordEmbedding("Embeddings",?"TokenizedMessage",WordEmbeddingEstimator.PretrainedModelKind.SentimentSpecificWordEmbedding));//?讓我們訓(xùn)練管道,然后將其應(yīng)用于相同的數(shù)據(jù)。 //?請(qǐng)注意,即使在70KB的小數(shù)據(jù)集上,上面的管道也可能需要一分鐘才能完成訓(xùn)練。 var?transformedData?=?pipeline.Fit(data).Transform(data);//?檢查結(jié)果數(shù)據(jù)集的某些列。 var?embeddings?=?transformedData.GetColumn<float[]>(mlContext,?"Embeddings").Take(10).ToArray(); var?unigrams?=?transformedData.GetColumn<float[]>(mlContext,?"BagOfWords").Take(10).ToArray();歡迎關(guān)注我的個(gè)人公眾號(hào)”My IO“
總結(jié)
以上是生活随笔為你收集整理的ML.NET Cookbook:(18)如何在文本数据上训练模型?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 拿到阿里云服务器后的基本安全配置
- 下一篇: 设计模式6大基本原则