You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi to all, I'm new on ML.NET and I'm trying to FeaturizeText with TF-IDF.
In the Jupyter Notebook and in .NET Interactive, this code gets an error:
`
using Microsoft.ML;
using Microsoft.ML.Data;
using System;
using Microsoft.ML.Transforms.Text;
using System.Collections.Generic;
namespace SentimentAnalysis
{
public class Input
{
[LoadColumn(0)]
public string Text { get; set; }
[LoadColumn(1)]
public int Rating { get; set; }
}
public class Output
{
public float[] Features { get; set; }
}
class Program
{
public static TextFeaturizingEstimator.Options GetOptions()
{
var vectorizedTextOptions = new TextFeaturizingEstimator.Options()
{
KeepDiacritics = false,
KeepPunctuations = false,
KeepNumbers = true,
CaseMode = TextNormalizingEstimator.CaseMode.Lower,
StopWordsRemoverOptions = new StopWordsRemovingEstimator.Options()
{
Language = TextFeaturizingEstimator.Language.English
},
// ngram options
WordFeatureExtractor = new WordBagEstimator.Options()
{
NgramLength = 1,
UseAllLengths = false, // Produce both unigrams and bigrams
Weighting = NgramExtractingEstimator.WeightingCriteria.TfIdf, // TF-IDF
},
// chargram options
CharFeatureExtractor = null
};
return vectorizedTextOptions;
}
static void Main(string[] args)
{
var context = new MLContext();
var list = new List<Input>()
{
new Input()
{
Text = "This is machine learning example", Rating = 4
},
new Input()
{
Text = "I like .NET", Rating = 5
}
};
var samples = context.Data.LoadFromEnumerable<Input>(list);
var options = GetOptions();
var transformFitted = context.Transforms.Text.FeaturizeText(
"Features",
options,
"Text"
).Fit(samples);
var tfIdfTransformed = transformFitted.Transform(samples);
var predictionEngine = context.Model.CreatePredictionEngine<Input, Output>(transformFitted);
Output prediction = null;
VBuffer<ReadOnlyMemory<char>> slotNames = default;
tfIdfTransformed.Schema["Features"].GetSlotNames(ref slotNames);
var tfIdfColumn = tfIdfTransformed.GetColumn<VBuffer<float>>(tfIdfTransformed.Schema["Features"]);
var slots = slotNames.GetValues();
Console.Write("NGrams: ");
foreach (var featureRow in tfIdfColumn)
{
foreach (var item in featureRow.Items())
{
Console.Write($"{slots[item.Key]} ");
}
Console.WriteLine();
}
}
}
}
`
And I do var slot = slotNames.GetValues() what I'm getting is:
Error: (3.1): error CS8345: The field or a self-implemented property cannot be of type 'ReadOnlySpan <ReadOnlyMemory >', unless it is an instance member of a reference struct.
And in Visual Studio 2019, it works well, but when I try to loop over the columns:
will give another error, complaining about not being able to convert it to object.
Additional formatting won't do anything useful, because either way when formatting it will be trying to convert it into an object, which is impossible for ref struct such as span.
It looks like a serious issue, but I doubt it can be solved.
jonsequitur
changed the title
ReadOnlySpan<ReadOnlyMemory<char>> in .NET Interactive and Jupyter Notebook
[EXTERNAL] ReadOnlySpan<ReadOnlyMemory<char>> in .NET Interactive and Jupyter Notebook
Jul 12, 2021
Hi to all, I'm new on ML.NET and I'm trying to FeaturizeText with TF-IDF.
In the Jupyter Notebook and in .NET Interactive, this code gets an error:
And I do var slot = slotNames.GetValues() what I'm getting is:
Error: (3.1): error CS8345: The field or a self-implemented property cannot be of type 'ReadOnlySpan <ReadOnlyMemory >', unless it is an instance member of a reference struct.
And in Visual Studio 2019, it works well, but when I try to loop over the columns:
It repeats some words.
For example: If I get two input: "This is a test", "I like dotnet", what I got print is:
This is a test
test I like dotnet
It is just an example.
Another question is how can I get do something like this in ML.NET:
This code is in python:
`
`
The text was updated successfully, but these errors were encountered: