Skip to content

Commit 88ed4d9

Browse files
authored
Fixes OneHotEncoding Issue (#4974)
* fix for logistic regression * rebase changes * resolving comments * onehotencoding dimension fix * adding shapes to nodes * updating baseline * updating baseline
1 parent 913a1a7 commit 88ed4d9

5 files changed

Lines changed: 119 additions & 1196 deletions

File tree

src/Microsoft.ML.Data/Transforms/KeyToVector.cs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -694,29 +694,22 @@ private void SaveAsOnnxCore(OnnxContext ctx, int iinfo, ColInfo info, string src
694694
// because inputs and outputs of a transform are declared with shapes.
695695
Contracts.CheckValue(shape, nameof(shape));
696696

697-
// If Bag is true, the output of ONNX LabelEncoder needs to be fed into ONNX ReduceSum because
698-
// default ONNX LabelEncoder just matches the behavior of Bag=false.
699-
var encodedVariableName = _parent._columns[iinfo].OutputCountVector ? ctx.AddIntermediateVariable(null, "encoded", true) : dstVariableName;
700-
701697
string opType = "Cast";
702-
var castOutput = ctx.AddIntermediateVariable(info.TypeSrc, opType, true);
698+
var castOutput = ctx.AddIntermediateVariable(NumberDataViewType.Int64, opType);
703699
var castNode = ctx.CreateNode(opType, srcVariableName, castOutput, ctx.GetNodeName(opType), "");
704700
castNode.AddAttribute("to", typeof(long));
705701

706702
opType = "OneHotEncoder";
703+
var categoryRange = info.TypeSrc.GetItemType().GetKeyCountAsInt32(Host);
704+
var encodedVariableName = ctx.AddIntermediateVariable(new VectorDataViewType(NumberDataViewType.Single, 1, categoryRange), "encoded");
707705
var node = ctx.CreateNode(opType, castOutput, encodedVariableName, ctx.GetNodeName(opType));
708-
node.AddAttribute("cats_int64s", Enumerable.Range(1, info.TypeSrc.GetItemType().GetKeyCountAsInt32(Host)).Select(x => (long)x));
706+
node.AddAttribute("cats_int64s", Enumerable.Range(1, categoryRange).Select(x => (long)x));
709707
node.AddAttribute("zeros", true);
710-
if (_parent._columns[iinfo].OutputCountVector)
711-
{
712-
// If input shape is [1, 3], then OneHotEncoder may produce a 3-D tensor. Thus, we need to do a
713-
// reduction along the second last axis to merge the one-hot vectors produced by all input features.
714-
// Note that one input feature got expended to an one-hot vector.
715-
opType = "ReduceSum";
716-
var reduceNode = ctx.CreateNode(opType, encodedVariableName, dstVariableName, ctx.GetNodeName(opType), "");
717-
reduceNode.AddAttribute("axes", new long[] { shape.Count - 1 });
718-
reduceNode.AddAttribute("keepdims", 0);
719-
}
708+
709+
// OneHotEncoder adds one additional dimension, so we remove it below
710+
opType = "Squeeze";
711+
var reduceNode = ctx.CreateNode(opType, encodedVariableName, dstVariableName, ctx.GetNodeName(opType), "");
712+
reduceNode.AddAttribute("axes", new long[] { shape.Count - 1 });
720713
}
721714
}
722715
}

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/ExcludeVariablesInOnnxConversion.txt

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -130,19 +130,15 @@
130130
"output": [
131131
"F21"
132132
],
133-
"name": "ReduceSum",
134-
"opType": "ReduceSum",
133+
"name": "Squeeze",
134+
"opType": "Squeeze",
135135
"attribute": [
136136
{
137137
"name": "axes",
138138
"ints": [
139139
"1"
140140
],
141141
"type": "INTS"
142-
},
143-
{
144-
"name": "keepdims",
145-
"type": "INT"
146142
}
147143
]
148144
},
@@ -659,6 +655,45 @@
659655
}
660656
}
661657
},
658+
{
659+
"name": "Cast",
660+
"type": {
661+
"tensorType": {
662+
"elemType": 7,
663+
"shape": {
664+
"dim": [
665+
{
666+
"dimValue": "-1"
667+
},
668+
{
669+
"dimValue": "1"
670+
}
671+
]
672+
}
673+
}
674+
}
675+
},
676+
{
677+
"name": "encoded",
678+
"type": {
679+
"tensorType": {
680+
"elemType": 1,
681+
"shape": {
682+
"dim": [
683+
{
684+
"dimValue": "-1"
685+
},
686+
{
687+
"dimValue": "1"
688+
},
689+
{
690+
"dimValue": "10"
691+
}
692+
]
693+
}
694+
}
695+
}
696+
},
662697
{
663698
"name": "F22",
664699
"type": {

test/BaselineOutput/Common/Onnx/BinaryClassification/BreastCancer/ModelWithLessIO.txt

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@
9292
"Cast"
9393
],
9494
"output": [
95-
"F21"
95+
"encoded"
9696
],
9797
"name": "OneHotEncoder",
9898
"opType": "OneHotEncoder",
@@ -120,6 +120,25 @@
120120
],
121121
"domain": "ai.onnx.ml"
122122
},
123+
{
124+
"input": [
125+
"encoded"
126+
],
127+
"output": [
128+
"F21"
129+
],
130+
"name": "Squeeze",
131+
"opType": "Squeeze",
132+
"attribute": [
133+
{
134+
"name": "axes",
135+
"ints": [
136+
"1"
137+
],
138+
"type": "INTS"
139+
}
140+
]
141+
},
123142
{
124143
"input": [
125144
"F1",
@@ -985,6 +1004,45 @@
9851004
}
9861005
}
9871006
},
1007+
{
1008+
"name": "Cast",
1009+
"type": {
1010+
"tensorType": {
1011+
"elemType": 7,
1012+
"shape": {
1013+
"dim": [
1014+
{
1015+
"dimValue": "-1"
1016+
},
1017+
{
1018+
"dimValue": "1"
1019+
}
1020+
]
1021+
}
1022+
}
1023+
}
1024+
},
1025+
{
1026+
"name": "encoded",
1027+
"type": {
1028+
"tensorType": {
1029+
"elemType": 1,
1030+
"shape": {
1031+
"dim": [
1032+
{
1033+
"dimValue": "-1"
1034+
},
1035+
{
1036+
"dimValue": "1"
1037+
},
1038+
{
1039+
"dimValue": "9"
1040+
}
1041+
]
1042+
}
1043+
}
1044+
}
1045+
},
9881046
{
9891047
"name": "VectorFeaturizerOutput",
9901048
"type": {

0 commit comments

Comments
 (0)