Skip to content

LightGBM Save/Load round trip loses Softmax OneVersusAllModelParameters #3647

@rauhs

Description

@rauhs

version: 0.11

Related: #1424

When training a softmax Multi classifier with LightGBM a save/load will lose the softmax it seems:

Also inspecting the model we can see it doesn't use ImplSoftmax but ImplRaw.

Reproduce:

    public class GenericSample
    {
      public string A { get; set; }
      public string Label { get; set; }
    }
    public static void ReproduceLightGbmPersistanceBug()
    {
      var data = Enumerable.Range(1, 100).Select(x => new GenericSample { A = $"{x % 20}", Label = $"{x % 10}" });
      var ctx = new MLContext();

      var options = new Options {
        UseSoftmax = true,
      };
      var pipe = ctx.Transforms.Categorical.OneHotEncoding("A")
        .Append(ctx.Transforms.Concatenate("Features", "A"))
        .Append(ctx.Transforms.Conversion.MapValueToKey("Label"))
        .Append(ctx.MulticlassClassification.Trainers.LightGbm(options));
      var dataView = ctx.Data.LoadFromEnumerable(data);
      ITransformer model = pipe.Fit(dataView);
      var scores = model.Transform(dataView).GetColumn<float[]>(ctx,"Score");

      Console.WriteLine($"Min: {scores.Select(x => x.Min()).Min()}");
      Console.WriteLine($"Max: {scores.Select(x => x.Max()).Max()}");

      var memoryStream = new MemoryStream();
      ctx.Model.Save(model, memoryStream);
      model = ctx.Model.Load(memoryStream);

      scores = model.Transform(dataView).GetColumn<float[]>(ctx,"Score");
      Console.WriteLine($"Min: {scores.Select(x => x.Min()).Min()}");
      Console.WriteLine($"Max: {scores.Select(x => x.Max()).Max()}");
    }

Output:

Min: 0.001027671
Max: 0.9907509
Min: -4.843706
Max: 2.027462

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1Priority of the issue for triage purpose: Needs to be fixed soon.bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions