Skip to content

Changed Binarizer node to be cast to the type of the predicted label … #4818

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 11, 2020
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: 12 additions & 13 deletions src/Microsoft.ML.Data/Scorers/BinaryClassifierScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Diagnostics;
using Microsoft.ML;
using Microsoft.ML.Data;
using Microsoft.ML.Internal.Utilities;
Expand Down Expand Up @@ -198,28 +199,26 @@ private protected override void SaveAsOnnxCore(OnnxContext ctx)
for (int iinfo = 0; iinfo < Bindings.InfoCount; ++iinfo)
outColumnNames[iinfo] = Bindings.GetColumnName(Bindings.MapIinfoToCol(iinfo));

/* If the probability column was generated, then the classification threshold is set to 0.5. Otherwise,
the predicted label is based on the sign of the score.
*/
string opType = "Binarizer";
OnnxNode node;
var binarizerOutput = ctx.AddIntermediateVariable(null, "BinarizerOutput", true);

if (Bindings.InfoCount >= 3)
{
Host.Assert(ctx.ContainsColumn(outColumnNames[2]));
node = ctx.CreateNode(opType, ctx.GetVariableName(outColumnNames[2]), binarizerOutput, ctx.GetNodeName(opType));
node.AddAttribute("threshold", 0.5);
}
string scoreColumn;
if (Bindings.RowMapper.OutputSchema[Bindings.ScoreColumnIndex].Name == "Score")
scoreColumn = outColumnNames[1];
else
{
node = ctx.CreateNode(opType, ctx.GetVariableName(outColumnNames[1]), binarizerOutput, ctx.GetNodeName(opType));
node.AddAttribute("threshold", 0.0);
Host.Assert(Bindings.InfoCount >= 3);
scoreColumn = outColumnNames[2];
}
node = ctx.CreateNode(opType, ctx.GetVariableName(scoreColumn), binarizerOutput, ctx.GetNodeName(opType));
node.AddAttribute("threshold", _threshold);

opType = "Cast";
node = ctx.CreateNode(opType, binarizerOutput, ctx.GetVariableName(outColumnNames[0]), ctx.GetNodeName(opType), "");
var t = InternalDataKindExtensions.ToInternalDataKind(DataKind.Boolean).ToType();
node.AddAttribute("to", t);
var predictedLabelCol = OutputSchema.GetColumnOrNull(outColumnNames[0]);
Host.Assert(predictedLabelCol.HasValue);
node.AddAttribute("to", predictedLabelCol.Value.Type.RawType);
}

private protected override IDataTransform ApplyToDataCore(IHostEnvironment env, IDataView newSource)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@
},
{
"input": [
"Probability"
"Score"
],
"output": [
"BinarizerOutput"
Expand All @@ -423,7 +423,6 @@
"attribute": [
{
"name": "threshold",
"f": 0.5,
"type": "FLOAT"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@
},
{
"input": [
"Probability"
"Score"
],
"output": [
"BinarizerOutput"
Expand All @@ -749,7 +749,6 @@
"attribute": [
{
"name": "threshold",
"f": 0.5,
"type": "FLOAT"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@
},
{
"input": [
"Probability"
"Score"
],
"output": [
"BinarizerOutput"
Expand All @@ -376,7 +376,6 @@
"attribute": [
{
"name": "threshold",
"f": 0.5,
"type": "FLOAT"
}
],
Expand Down