@@ -254,6 +254,31 @@ public void MatrixFactorizationInMemoryData()
254
254
Assert . True ( pred . Score != 0 ) ;
255
255
}
256
256
257
+ internal class MatrixElementZeroBased256By256
258
+ {
259
+ // Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
260
+ [ KeyType ( _matrixColumnCount ) ]
261
+ public uint MatrixColumnIndex ;
262
+ // Matrix row index starts from 0 and is at most _synthesizedMatrixRowCount.
263
+ [ KeyType ( _matrixRowCount ) ]
264
+ public uint MatrixRowIndex ;
265
+ // The value at the MatrixColumnIndex-th column and the MatrixRowIndex-th row in the considered matrix.
266
+ public float Value ;
267
+ }
268
+
269
+ internal class MatrixElementZeroBasedForScore256By256
270
+ {
271
+ // Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
272
+ // Contieuous=true means that all values from 0 to _synthesizedMatrixColumnCount are allowed keys.
273
+ [ KeyType ( _matrixColumnCount ) ]
274
+ public uint MatrixColumnIndex ;
275
+ // Matrix row index starts from 0 and is at most _synthesizedMatrixRowCount.
276
+ // Contieuous=true means that all values from 0 to _synthesizedMatrixRowCount are allowed keys.
277
+ [ KeyType ( _matrixRowCount ) ]
278
+ public uint MatrixRowIndex ;
279
+ public float Score ;
280
+ }
281
+
257
282
internal class MatrixElementZeroBased
258
283
{
259
284
// Matrix column index starts from 0 and is at most _synthesizedMatrixColumnCount.
@@ -605,15 +630,18 @@ public void OneClassMatrixFactorizationWithUnseenColumnAndRow()
605
630
CompareNumbersWithTolerance ( 0.00316973357 , testResults [ 2 ] . Score , digitsOfPrecision : 5 ) ;
606
631
}
607
632
633
+ const int _matrixColumnCount = 256 ;
634
+ const int _matrixRowCount = 256 ;
635
+
608
636
[ MatrixFactorizationFact ]
609
637
public void InspectMatrixFactorizationModel ( )
610
638
{
611
639
// Create an in-memory matrix as a list of tuples (column index, row index, value).
612
640
// Iterators i and j are column and row indexes, respectively.
613
- var dataMatrix = new List < MatrixElementZeroBased > ( ) ;
614
- for ( uint i = 0 ; i < _synthesizedMatrixColumnCount ; ++ i )
615
- for ( uint j = 0 ; j < _synthesizedMatrixRowCount ; ++ j )
616
- dataMatrix . Add ( new MatrixElementZeroBased ( ) { MatrixColumnIndex = i , MatrixRowIndex = j , Value = ( i + j ) % 5 } ) ;
641
+ var dataMatrix = new List < MatrixElementZeroBased256By256 > ( ) ;
642
+ for ( uint i = 0 ; i < _matrixColumnCount ; ++ i )
643
+ for ( uint j = 0 ; j < _matrixRowCount ; ++ j )
644
+ dataMatrix . Add ( new MatrixElementZeroBased256By256 ( ) { MatrixColumnIndex = i , MatrixRowIndex = j , Value = ( i + j ) % 5 } ) ;
617
645
618
646
// Convert the in-memory matrix into an IDataView so that ML.NET components can consume it.
619
647
var dataView = ML . Data . LoadFromEnumerable ( dataMatrix ) ;
@@ -629,7 +657,7 @@ public void InspectMatrixFactorizationModel()
629
657
LabelColumnName = nameof ( MatrixElement . Value ) ,
630
658
NumberOfIterations = 100 ,
631
659
NumberOfThreads = 1 , // To eliminate randomness, # of threads must be 1.
632
- ApproximationRank = 32 ,
660
+ ApproximationRank = 64 ,
633
661
LearningRate = 0.5 ,
634
662
} ;
635
663
@@ -639,20 +667,20 @@ public void InspectMatrixFactorizationModel()
639
667
var model = pipeline . Fit ( dataView ) ;
640
668
641
669
// Check if the expected types in the trained model are expected.
642
- Assert . True ( model . MatrixColumnIndexColumnName == nameof ( MatrixElementZeroBased . MatrixColumnIndex ) ) ;
643
- Assert . True ( model . MatrixRowIndexColumnName == nameof ( MatrixElementZeroBased . MatrixRowIndex ) ) ;
670
+ Assert . True ( model . MatrixColumnIndexColumnName == nameof ( MatrixElementZeroBased256By256 . MatrixColumnIndex ) ) ;
671
+ Assert . True ( model . MatrixRowIndexColumnName == nameof ( MatrixElementZeroBased256By256 . MatrixRowIndex ) ) ;
644
672
var matColKeyType = model . MatrixColumnIndexColumnType as KeyDataViewType ;
645
673
Assert . NotNull ( matColKeyType ) ;
646
674
var matRowKeyType = model . MatrixRowIndexColumnType as KeyDataViewType ;
647
675
Assert . NotNull ( matRowKeyType ) ;
648
- Assert . True ( matColKeyType . Count == _synthesizedMatrixColumnCount ) ;
649
- Assert . True ( matRowKeyType . Count == _synthesizedMatrixRowCount ) ;
676
+ Assert . True ( matColKeyType . Count == _matrixColumnCount ) ;
677
+ Assert . True ( matRowKeyType . Count == _matrixRowCount ) ;
650
678
651
679
// Create a test set with assigning scores. It stands for the 2nd column of the training matrix.
652
- var testMatrix = new List < MatrixElementZeroBased > ( ) ;
680
+ var testMatrix = new List < MatrixElementZeroBasedForScore256By256 > ( ) ;
653
681
for ( /* column index */ uint i = 1 ; i < 2 ; ++ i )
654
- for ( /* row index */ uint j = 0 ; j < _synthesizedMatrixRowCount ; ++ j )
655
- testMatrix . Add ( new MatrixElementZeroBased ( ) { MatrixColumnIndex = i , MatrixRowIndex = j } ) ;
682
+ for ( /* row index */ uint j = 0 ; j < _matrixRowCount ; ++ j )
683
+ testMatrix . Add ( new MatrixElementZeroBasedForScore256By256 ( ) { MatrixColumnIndex = i , MatrixRowIndex = j , Score = 0 } ) ;
656
684
657
685
// Load test set as IDataView.
658
686
var testData = ML . Data . LoadFromEnumerable ( testMatrix ) ;
@@ -661,7 +689,7 @@ public void InspectMatrixFactorizationModel()
661
689
var transformedTestData = model . Transform ( testData ) ;
662
690
663
691
// Load back predictions on the 2nd column as IEnumerable<MatrixElementZeroBasedForScore>.
664
- var predictions = mlContext . Data . CreateEnumerable < MatrixElementZeroBasedForScore > ( transformedTestData , false ) . ToList ( ) ;
692
+ var predictions = mlContext . Data . CreateEnumerable < MatrixElementZeroBasedForScore256By256 > ( transformedTestData , false ) . ToList ( ) ;
665
693
666
694
// Inspect the trained model.
667
695
int m = model . Model . NumberOfRows ;
0 commit comments