Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 9 additions & 16 deletions src/Microsoft.ML.Data/Transforms/KeyToVector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -694,29 +694,22 @@ private void SaveAsOnnxCore(OnnxContext ctx, int iinfo, ColInfo info, string src
// because inputs and outputs of a transform are declared with shapes.
Contracts.CheckValue(shape, nameof(shape));

// If Bag is true, the output of ONNX LabelEncoder needs to be fed into ONNX ReduceSum because
// default ONNX LabelEncoder just matches the behavior of Bag=false.
var encodedVariableName = _parent._columns[iinfo].OutputCountVector ? ctx.AddIntermediateVariable(null, "encoded", true) : dstVariableName;

string opType = "Cast";
var castOutput = ctx.AddIntermediateVariable(info.TypeSrc, opType, true);
var castOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, opType);
var castNode = ctx.CreateNode(opType, srcVariableName, castOutput, ctx.GetNodeName(opType), "");
castNode.AddAttribute("to", typeof(long));

opType = "OneHotEncoder";
var categoryRange = info.TypeSrc.GetItemType().GetKeyCountAsInt32(Host);
var encodedVariableName = ctx.AddIntermediateVariable(new VectorDataViewType(NumberDataViewType.Single, 1, categoryRange), "encoded");
var node = ctx.CreateNode(opType, castOutput, encodedVariableName, ctx.GetNodeName(opType));
node.AddAttribute("cats_int64s", Enumerable.Range(1, info.TypeSrc.GetItemType().GetKeyCountAsInt32(Host)).Select(x => (long)x));
node.AddAttribute("cats_int64s", Enumerable.Range(1, categoryRange).Select(x => (long)x));
node.AddAttribute("zeros", true);
if (_parent._columns[iinfo].OutputCountVector)
{
// If input shape is [1, 3], then OneHotEncoder may produce a 3-D tensor. Thus, we need to do a
// reduction along the second last axis to merge the one-hot vectors produced by all input features.
// Note that one input feature got expended to an one-hot vector.
opType = "ReduceSum";
var reduceNode = ctx.CreateNode(opType, encodedVariableName, dstVariableName, ctx.GetNodeName(opType), "");
reduceNode.AddAttribute("axes", new long[] { shape.Count - 1 });
reduceNode.AddAttribute("keepdims", 0);
}

// OneHotEncoder adds one additional dimension, so we remove it below
opType = "Squeeze";
var reduceNode = ctx.CreateNode(opType, encodedVariableName, dstVariableName, ctx.GetNodeName(opType), "");
reduceNode.AddAttribute("axes", new long[] { shape.Count - 1 });
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,19 +130,15 @@
"output": [
"F21"
],
"name": "ReduceSum",
"opType": "ReduceSum",
"name": "Squeeze",
"opType": "Squeeze",
"attribute": [
{
"name": "axes",
"ints": [
"1"
],
"type": "INTS"
},
{
"name": "keepdims",
"type": "INT"
}
]
},
Expand Down Expand Up @@ -659,6 +655,45 @@
}
}
},
{
"name": "Cast",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "encoded",
"type": {
"tensorType": {
"elemType": 1,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
},
{
"dimValue": "10"
}
]
}
}
}
},
{
"name": "F22",
"type": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@
"Cast"
],
"output": [
"F21"
"encoded"
],
"name": "OneHotEncoder",
"opType": "OneHotEncoder",
Expand Down Expand Up @@ -120,6 +120,25 @@
],
"domain": "ai.onnx.ml"
},
{
"input": [
"encoded"
],
"output": [
"F21"
],
"name": "Squeeze",
"opType": "Squeeze",
"attribute": [
{
"name": "axes",
"ints": [
"1"
],
"type": "INTS"
}
]
},
{
"input": [
"F1",
Expand Down Expand Up @@ -985,6 +1004,45 @@
}
}
},
{
"name": "Cast",
"type": {
"tensorType": {
"elemType": 7,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
}
]
}
}
}
},
{
"name": "encoded",
"type": {
"tensorType": {
"elemType": 1,
"shape": {
"dim": [
{
"dimValue": "-1"
},
{
"dimValue": "1"
},
{
"dimValue": "9"
}
]
}
}
}
},
{
"name": "VectorFeaturizerOutput",
"type": {
Expand Down
Loading