Skip to content

Commit

Permalink
More documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
russcam committed Nov 19, 2024
1 parent 470870e commit 12e9c40
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 20 deletions.
22 changes: 16 additions & 6 deletions src/RankLib/Learning/IRanker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,13 @@ public interface IRankerParameters
}

/// <summary>
/// A ranker
/// A ranker that can score <see cref="DataPoint"/> and rank data points in a <see cref="RankList"/>.
/// A ranker can be trained by a <see cref="RankerTrainer"/>, or a trained ranker model can be loaded
/// from file
/// </summary>
/// <remarks>
/// Use <see cref="EvaluatorFactory"/> to create an <see cref="Evaluator"/> for training and evaluation.
/// </remarks>
public interface IRanker
{
/// <summary>
Expand Down Expand Up @@ -120,15 +125,20 @@ public interface IRanker
}

/// <summary>
/// A generic ranker
/// A generic ranker that can score <see cref="DataPoint"/> and rank data points in a <see cref="RankList"/>.
/// A ranker can be trained by a <see cref="RankerTrainer"/>, or a trained ranker model can be loaded
/// from file
/// </summary>
/// <typeparam name="TParameters">The type of rank parameters</typeparam>
public interface IRanker<TParameters> : IRanker
where TParameters : IRankerParameters
/// <typeparam name="TRankerParameters">The type of ranker parameters</typeparam>
/// <remarks>
/// Use <see cref="EvaluatorFactory"/> to create an <see cref="Evaluator"/> for training and evaluation.
/// </remarks>
public interface IRanker<TRankerParameters> : IRanker
where TRankerParameters : IRankerParameters
{
/// <summary>
/// Gets or sets the parameters for the ranker.
/// The ranker uses parameters for training
/// </summary>
new TParameters Parameters { get; set; }
new TRankerParameters Parameters { get; set; }
}
12 changes: 9 additions & 3 deletions src/RankLib/Learning/LinearRegression.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,22 +36,25 @@ public class LinearRegression : Ranker<LinearRegressionParameters>
private readonly ILogger<LinearRegression> _logger;
private double[] _weight = [];

public LinearRegression(ILogger<LinearRegression>? logger = null) : base() =>
public LinearRegression(ILogger<LinearRegression>? logger = null) =>
_logger = logger ?? NullLogger<LinearRegression>.Instance;

public LinearRegression(List<RankList> samples, int[] features, MetricScorer scorer,
ILogger<LinearRegression>? logger = null)
: base(samples, features, scorer) =>
_logger = logger ?? NullLogger<LinearRegression>.Instance;

/// <inheritdoc />
public override string Name => RankerName;

/// <inheritdoc />
public override Task InitAsync()
{
_logger.LogInformation("Initializing...");
return Task.CompletedTask;
}

/// <inheritdoc />
public override Task LearnAsync()
{
_logger.LogInformation("Training starts...");
Expand Down Expand Up @@ -110,17 +113,18 @@ public override Task LearnAsync()

TrainingDataScore = SimpleMath.Round(Scorer.Score(Rank(Samples)), 4);
_logger.LogInformation("Finished successfully.");
_logger.LogInformation("{ScorerName} on training data: {ScoreOnTrainingData}", Scorer.Name, TrainingDataScore);
_logger.LogInformation("{Scorer} on training data: {TrainingScore}", Scorer.Name, TrainingDataScore);

if (ValidationSamples != null)
{
ValidationDataScore = Scorer.Score(Rank(ValidationSamples));
_logger.LogInformation("{ScorerName} on validation data: {BestScoreOnValidationData}", Scorer.Name, SimpleMath.Round(ValidationDataScore, 4));
_logger.LogInformation("{Scorer} on validation data: {ValidationScore}", Scorer.Name, SimpleMath.Round(ValidationDataScore, 4));
}

return Task.CompletedTask;
}

/// <inheritdoc />
public override double Eval(DataPoint dataPoint)
{
var score = _weight[^1];
Expand All @@ -130,6 +134,7 @@ public override double Eval(DataPoint dataPoint)
return score;
}

/// <inheritdoc />
public override string GetModel()
{
var output = new StringBuilder()
Expand All @@ -147,6 +152,7 @@ public override string GetModel()
return output.ToString();
}

/// <inheritdoc />
public override void LoadFromString(string model)
{
try
Expand Down
12 changes: 9 additions & 3 deletions src/RankLib/Learning/Ranker.cs
Original file line number Diff line number Diff line change
@@ -1,20 +1,26 @@
using System.Text;
using RankLib.Eval;
using RankLib.Metric;
using RankLib.Utilities;

namespace RankLib.Learning;

/// <summary>
/// Base class for a generic ranker with typed ranker parameters.
/// Base class for a generic ranker that can score <see cref="DataPoint"/> and rank data points in a <see cref="RankList"/>.
/// A ranker can be trained by a <see cref="RankerTrainer"/>, or a trained ranker model can be loaded
/// from file
/// </summary>
/// <typeparam name="TRankerParameters">The type of ranker parameters</typeparam>
/// <remarks>
/// Use <see cref="EvaluatorFactory"/> to create an <see cref="Evaluator"/> for training and evaluation.
/// </remarks>
public abstract class Ranker<TRankerParameters> : IRanker<TRankerParameters>
where TRankerParameters : IRankerParameters, new()
{
private MetricScorer? _scorer;

protected double TrainingDataScore = 0.0;
protected double ValidationDataScore = 0.0;
protected double TrainingDataScore = 0;
protected double ValidationDataScore = 0;

/// <inheritdoc />
public List<RankList> Samples { get; set; } = [];
Expand Down
4 changes: 2 additions & 2 deletions src/RankLib/Learning/RankerTrainer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ public class RankerTrainer
private readonly RankerFactory _rankerFactory;

/// <summary>
/// Initializes a new instance of <see cref="RankerTrainer"/>
/// Instantiates a new instance of <see cref="RankerTrainer"/>
/// </summary>
/// <param name="rankerFactory"></param>
/// <param name="rankerFactory">The ranker factory used to create rankers</param>
public RankerTrainer(RankerFactory rankerFactory) => _rankerFactory = rankerFactory;

/// <summary>
Expand Down
4 changes: 0 additions & 4 deletions src/RankLib/Learning/Sampler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,6 @@

namespace RankLib.Learning;

using System;
using System.Collections.Generic;
using System.Linq;

/// <summary>
/// Samples rank lists from a pool of rank lists
/// </summary>
Expand Down
2 changes: 0 additions & 2 deletions src/RankLib/Parsing/ModelLineProducer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,7 @@ private bool ReadUntil(string fullTextChar, int beginOfLineCursor, int endOfLine
if (fullTextChar[beginOfLineCursor] != '#')
{
for (var j = beginOfLineCursor; j <= endOfLineCursor; j++)
{
model.Append(fullTextChar[j]);
}
}

// Check for ensemble tag
Expand Down

0 comments on commit 12e9c40

Please sign in to comment.