dotnet / dotnet/machinelearning-samples

Convert Sentiment Analysis model to onnx

Open
#874 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
PowerShell
Stars
4.7k
Forks
2.7k
Avg merge
2d 22h
Merged PRs (30d)
1

Description

Hi,

I have been following the tutorial on "Sentiment Analysis" (https://docs.microsoft.com/es-es/dotnet/machine-learning/tutorials/sentiment-analysis), which is really nice, interesting and useful. However, I have stumbled on the first step further. I am trying to export the generated model to onnx with the following code:

```
// Converts model to onnx
ConvertModel2ONNX(mlContext, model, splitDataView.TestSet);
```

mlContext, model and splitDataView come from the functions explainned in the tutorial.

```
public static void ConvertModel2ONNX(MLContext mlContext, ITransformer model, IDataView splitTestSet)
{
using (var stream = File.Create(_onnxPath))
{
mlContext.Model.ConvertToOnnx(model, splitTestSet, stream);
}
}
```

where _onnxPath is the output path for the onnx file.

However, I get the following error.

```
System.Collections.Generic.KeyNotFoundException
HResult=0x80131577
Message=The given key 'SentimentText_TextNormalizer' was not present in the dictionary.
Source=System.Private.CoreLib
StackTrace:
at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.ML.Model.OnnxConverter.OnnxContextImpl.GetVariableName(String colName)
at Microsoft.ML.Transforms.Text.WordTokenizingTransformer.Mapper.SaveAsOnnx(OnnxContext ctx)
at Microsoft.ML.Data.RowToRowMapperTransform.Microsoft.ML.Model.OnnxConverter.ISaveAsOnnx.SaveAsOnnx(OnnxContext ctx)
at Microsoft.ML.Model.OnnxConverter.SaveOnnxCommand.ConvertTransformListToOnnxModel(OnnxContextImpl ctx, IChannel ch, IDataView inputData, IDataView outputData, LinkedList`1 transforms, HashSet`1 inputColumnNamesToDrop, HashSet`1 outputColumnNamesToDrop)
at Microsoft.ML.OnnxExportExtensions.ConvertToOnnxProtobufCore(IHostEnvironment env, OnnxContextImpl ctx, ITransformer transform, IDataView inputData, String[] outputColumnNamesToKeep)
at Microsoft.ML.OnnxExportExtensions.ConvertToOnnxProtobuf(ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, String[] outputColumns)
at Microsoft.ML.OnnxExportExtensions.ConvertToOnnx(ModelOperationsCatalog catalog, ITransformer transform, IDataView inputData, Stream stream)
at SentimentAnalysis.Program.ConvertModel2ONNX(MLContext mlContext, ITransformer model, IDataView splitTestSet) in C:\SentimentAnalysis\Program.cs:line 164
at SentimentAnalysis.Program.Main(String[] args) in C:\SentimentAnalysis\Program.cs:line 41
```

What could be the reason? I do not know if this question has a place here. If not, please forgive me for the inconvenience and ignore my question. Thank you very much.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.