Skip to content
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

[AutoML] Fix Internationalization bug in generated project. #3725

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
20 changes: 15 additions & 5 deletions src/mlnet/CodeGenerator/CSharp/TrainerGeneratorBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using Microsoft.ML.AutoML;
Expand All @@ -12,7 +13,6 @@ namespace Microsoft.ML.CLI.CodeGenerator.CSharp
{
/// <summary>
/// Supports generation of code for trainers (Binary,Multi,Regression)
/// Ova is an exception though. Need to figure out how to tackle that.
/// </summary>
internal abstract class TrainerGeneratorBase : ITrainerGenerator
{
Expand Down Expand Up @@ -61,17 +61,27 @@ private void Initialize(PipelineNode node)
if (type == typeof(bool))
{
//True to true
value = ((bool)kv.Value).ToString().ToLowerInvariant();
value = ((bool)kv.Value).ToString(CultureInfo.InvariantCulture).ToLowerInvariant();
}
if (type == typeof(float))
{
//0.0 to 0.0f
value = ((float)kv.Value).ToString() + "f";
value = ((float)kv.Value).ToString(CultureInfo.InvariantCulture) + "f";
}

if (type == typeof(int) || type == typeof(double) || type == typeof(long))
if (type == typeof(int))
{
value = (kv.Value).ToString();
value = ((int)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(double))
{
value = ((double)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(long))
{
value = ((long)kv.Value).ToString(CultureInfo.InvariantCulture);
}

if (type == typeof(string))
Expand Down
30 changes: 27 additions & 3 deletions test/mlnet.Tests/TrainerGeneratorTests.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
using System.Collections.Generic;
using System.Globalization;
using System.Threading;
using Microsoft.ML;
using Microsoft.ML.AutoML;
using Microsoft.ML.CLI.CodeGenerator.CSharp;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace mlnet.Tests
{
/****************************
* TODO : Add all trainer tests :
* **************************/
[TestClass]
public class TrainerGeneratorTests
{
[TestMethod]
public void CultureInvariantTest()
{

var context = new MLContext();

var elementProperties = new Dictionary<string, object>()
{
{"LearningRate", 0.1f },
{"NumberOfLeaves", 1 },
};
PipelineNode node = new PipelineNode("LightGbmBinary", PipelineNodeType.Trainer, default(string[]), default(string), elementProperties);
Pipeline pipeline = new Pipeline(new PipelineNode[] { node });

//Set culture to deutsch.
Thread.CurrentThread.CurrentCulture = new CultureInfo("de-DE");
srsaggam marked this conversation as resolved.
Show resolved Hide resolved

CodeGenerator codeGenerator = new CodeGenerator(pipeline, null, null);
var actual = codeGenerator.GenerateTrainerAndUsings();
string expectedTrainerString = "LightGbm(learningRate:0.1f,numberOfLeaves:1,labelColumnName:\"Label\",featureColumnName:\"Features\")";
Assert.AreEqual(expectedTrainerString, actual.Item1);
Assert.IsNull(actual.Item2);

}

[TestMethod]
public void LightGbmBinaryBasicTest()
{
Expand Down