Skip to content

Commit 67e9f4d

Browse files
author
Ivan Matantsev
committed
minor changes + make it pass tests.
1 parent 8806dac commit 67e9f4d

File tree

4 files changed

+28
-20
lines changed

4 files changed

+28
-20
lines changed

src/Microsoft.ML.Recommender/MatrixFactorizationPredictor.cs

+8-8
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,15 @@ private static VersionInfo GetVersionInfo()
5757
public readonly int NumberOfColumns;
5858
///<summary> The rank of the factor matrices.</summary>
5959
public readonly int ApproximationRank;
60-
// Packed _numberOfRows by _approximationRank matrix.
60+
// Packed NumberOfRows by ApproximationRank matrix.
6161
private readonly float[] _leftFactorMatrix;
62-
// Packed _approximationRank by _numberofColumns matrix.
62+
// Packed ApproximationRank by NumberOfColumns matrix.
6363
private readonly float[] _rightFactorMatrix;
6464

6565
/// <summary>
6666
/// Returns left approximation matrix.
6767
/// </summary>
68-
/// <returns>Flattened into one dimension array with size equal to <see cref="NumberOfRows"/> * <see cref="ApproximationRank"/></returns>
68+
/// <returns>Flattened into one-dimensional array with size equal to <see cref="NumberOfRows"/> * <see cref="ApproximationRank"/></returns>
6969
public float[] GetLeftFactorMatrix()
7070
{
7171
float[] result = new float[NumberOfRows * ApproximationRank];
@@ -75,7 +75,7 @@ public float[] GetLeftFactorMatrix()
7575
/// <summary>
7676
/// Returns right approximation matrix.
7777
/// </summary>
78-
/// <returns>Flattened into one dimension array with size equal to <see cref="ApproximationRank"/> * <see cref="NumberOfColumns"/></returns>
78+
/// <returns>Flattened into one-dimensional array with size equal to <see cref="ApproximationRank"/> * <see cref="NumberOfColumns"/></returns>
7979
public float[] GetRightFactorMatrix()
8080
{
8181
float[] result = new float[ApproximationRank* NumberOfColumns];
@@ -145,8 +145,8 @@ private MatrixFactorizationModelParameters(IHostEnvironment env, ModelLoadContex
145145
_leftFactorMatrix = Utils.ReadSingleArray(ctx.Reader, checked(NumberOfRows * ApproximationRank));
146146
_rightFactorMatrix = Utils.ReadSingleArray(ctx.Reader, checked(NumberOfColumns * ApproximationRank));
147147

148-
MatrixColumnIndexType = new KeyType(typeof(uint), NumberOfRows);
149-
MatrixRowIndexType = new KeyType(typeof(uint), NumberOfColumns);
148+
MatrixColumnIndexType = new KeyType(typeof(uint), NumberOfColumns);
149+
MatrixRowIndexType = new KeyType(typeof(uint), NumberOfRows);
150150
}
151151

152152
/// <summary>
@@ -183,8 +183,8 @@ public void Save(ModelSaveContext ctx)
183183
ctx.Writer.Write(ApproximationRank);
184184
_host.Check(Utils.Size(_leftFactorMatrix) == NumberOfRows * ApproximationRank, "Unexpected matrix size of a factor matrix (matrix P in LIBMF paper)");
185185
_host.Check(Utils.Size(_rightFactorMatrix) == NumberOfColumns * ApproximationRank, "Unexpected matrix size of a factor matrix (matrix Q in LIBMF paper)");
186-
Utils.WriteSinglesNoCount(ctx.Writer, _leftFactorMatrix.AsSpan(0, NumberOfRows * ApproximationRank));
187-
Utils.WriteSinglesNoCount(ctx.Writer, _rightFactorMatrix.AsSpan(0, NumberOfColumns * ApproximationRank));
186+
Utils.WriteSinglesNoCount(ctx.Writer, _leftFactorMatrix.AsSpan(0, _leftFactorMatrix.Length));
187+
Utils.WriteSinglesNoCount(ctx.Writer, _rightFactorMatrix.AsSpan(0, _rightFactorMatrix.Length));
188188
}
189189

190190
/// <summary>

src/Microsoft.ML.Recommender/MatrixFactorizationTrainer.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ public sealed class Options
149149
public double Lambda = Defaults.Lambda;
150150

151151
/// <summary>
152-
/// Approximation rank.
152+
/// Rank of approximation matrixes.
153153
/// </summary>
154154
/// <remarks>
155155
/// If input data has size of m by n we would build two approximation matrixes m by k and k by n where k is approxiation rank.
@@ -338,7 +338,7 @@ internal MatrixFactorizationTrainer(IHostEnvironment env, Options options) : bas
338338
/// <param name="matrixColumnIndexColumnName">The name of the column hosting the matrix's column IDs.</param>
339339
/// <param name="matrixRowIndexColumnName">The name of the column hosting the matrix's row IDs.</param>
340340
/// <param name="labelColumn">The name of the label column.</param>
341-
/// <param name="approximationRank">Approximation rank.</param>
341+
/// <param name="approximationRank">Rank of approximation matrixes.</param>
342342
/// <param name="learningRate">Initial learning rate. It specifies the speed of the training algorithm.</param>
343343
/// <param name="numIterations">Number of training iterations.</param>
344344
[BestFriend]

src/Microsoft.ML.Recommender/RecommenderCatalog.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ internal RecommendationTrainers(RecommendationCatalog catalog)
5555
/// <param name="matrixColumnIndexColumnName">The name of the column hosting the matrix's column IDs.</param>
5656
/// <param name="matrixRowIndexColumnName">The name of the column hosting the matrix's row IDs.</param>
5757
/// <param name="labelColumn">The name of the label column.</param>
58-
/// <param name="approximationRank">Approximation rank.</param>
58+
/// <param name="approximationRank">Rank of approximation matrixes.</param>
5959
/// <param name="learningRate">Initial learning rate. It specifies the speed of the training algorithm.</param>
6060
/// <param name="numIterations">Number of training iterations.</param>
6161
/// <example>

test/Microsoft.ML.Tests/TrainerEstimators/MatrixFactorizationTests.cs

+17-9
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ namespace Microsoft.ML.Tests.TrainerEstimators
1818
public partial class TrainerEstimators : TestDataPipeBase
1919
{
2020
[ConditionalFact(typeof(Environment), nameof(Environment.Is64BitProcess))] // This test is being fixed as part of issue #1441.
21-
public void MatrixFactorization_Estimator()
21+
public void MatrixFactorizationEstimator()
2222
{
2323
string labelColumnName = "Label";
2424
string matrixColumnIndexColumnName = "Col";
@@ -39,11 +39,11 @@ public void MatrixFactorization_Estimator()
3939
MatrixRowIndexColumnName = matrixRowIndexColumnName,
4040
LabelColumnName = labelColumnName,
4141
NumIterations = 3,
42-
NumThreads = 1,
43-
K = 4,
42+
NumThreads = 1,
43+
ApproximationRank = 4,
4444
};
4545

46-
var est = new MatrixFactorizationTrainer(Env, options);
46+
var est = ML.Recommendation().Trainers.MatrixFactorization(options);
4747

4848
TestEstimatorCore(est, data, invalidInput: invalidData);
4949

@@ -68,20 +68,28 @@ public void MatrixFactorizationSimpleTrainAndPredict()
6868
var data = reader.Read(new MultiFileSource(GetDataPath(TestDatasets.trivialMatrixFactorization.trainFilename)));
6969

7070
// Create a pipeline with a single operator.
71-
var options = new MatrixFactorizationTrainer.Options {
71+
var options = new MatrixFactorizationTrainer.Options
72+
{
7273
MatrixColumnIndexColumnName = userColumnName,
7374
MatrixRowIndexColumnName = itemColumnName,
7475
LabelColumnName = labelColumnName,
7576
NumIterations = 3,
7677
NumThreads = 1, // To eliminate randomness, # of threads must be 1.
77-
K = 7,
78+
ApproximationRank = 7,
7879
};
7980

8081
var pipeline = mlContext.Recommendation().Trainers.MatrixFactorization(options);
8182

8283
// Train a matrix factorization model.
8384
var model = pipeline.Fit(data);
8485

86+
// Les's validate content of the model.
87+
Assert.Equal(model.Model.ApproximationRank, options.ApproximationRank);
88+
var leftMatrix = model.Model.GetLeftFactorMatrix();
89+
var rightMatrix = model.Model.GetRightFactorMatrix();
90+
Assert.Equal(leftMatrix.Length, model.Model.NumberOfRows * model.Model.ApproximationRank);
91+
Assert.Equal(rightMatrix.Length, model.Model.NumberOfColumns * model.Model.ApproximationRank);
92+
8593
// Read the test data set as an IDataView
8694
var testData = reader.Read(new MultiFileSource(GetDataPath(TestDatasets.trivialMatrixFactorization.testFilename)));
8795

@@ -197,7 +205,7 @@ public void MatrixFactorizationInMemoryData()
197205
LabelColumnName = nameof(MatrixElement.Value),
198206
NumIterations = 10,
199207
NumThreads = 1, // To eliminate randomness, # of threads must be 1.
200-
K = 32,
208+
ApproximationRank = 32,
201209
};
202210

203211
var pipeline = mlContext.Recommendation().Trainers.MatrixFactorization(options);
@@ -287,7 +295,7 @@ public void MatrixFactorizationInMemoryDataZeroBaseIndex()
287295
LabelColumnName = nameof(MatrixElement.Value),
288296
NumIterations = 100,
289297
NumThreads = 1, // To eliminate randomness, # of threads must be 1.
290-
K = 32,
298+
ApproximationRank = 32,
291299
LearningRate = 0.5,
292300
};
293301

@@ -409,7 +417,7 @@ public void OneClassMatrixFactorizationInMemoryDataZeroBaseIndex()
409417
NumIterations = 100,
410418
NumThreads = 1, // To eliminate randomness, # of threads must be 1.
411419
Lambda = 0.025, // Let's test non-default regularization coefficient.
412-
K = 16,
420+
ApproximationRank = 16,
413421
Alpha = 0.01, // Importance coefficient of loss function over matrix elements not specified in the input matrix.
414422
C = 0.15, // Desired value for matrix elements not specified in the input matrix.
415423
};

0 commit comments

Comments
 (0)