internal MatrixFactorizationPredictor(IHostEnvironment env, SafeTrainingAndModelBuffer buffer, KeyType matrixColumnIndexType, KeyType matrixRowIndexType) { Contracts.CheckValue(env, nameof(env)); _host = env.Register(RegistrationName); _host.Assert(matrixColumnIndexType.RawType == typeof(uint)); _host.Assert(matrixRowIndexType.RawType == typeof(uint)); _host.CheckValue(buffer, nameof(buffer)); _host.CheckValue(matrixColumnIndexType, nameof(matrixColumnIndexType)); _host.CheckValue(matrixRowIndexType, nameof(matrixRowIndexType)); buffer.Get(out _numberOfRows, out _numberofColumns, out _approximationRank, out _leftFactorMatrix, out _rightFactorMatrix); _host.Assert(_numberofColumns == matrixColumnIndexType.GetCountAsInt32(_host)); _host.Assert(_numberOfRows == matrixRowIndexType.GetCountAsInt32(_host)); _host.Assert(_leftFactorMatrix.Length == _numberOfRows * _approximationRank); _host.Assert(_rightFactorMatrix.Length == _numberofColumns * _approximationRank); MatrixColumnIndexType = matrixColumnIndexType; MatrixRowIndexType = matrixRowIndexType; }
internal MatrixFactorizationModelParameters(IHostEnvironment env, SafeTrainingAndModelBuffer buffer, KeyType matrixColumnIndexType, KeyType matrixRowIndexType) { Contracts.CheckValue(env, nameof(env)); _host = env.Register(RegistrationName); _host.Assert(matrixColumnIndexType.RawType == typeof(uint)); _host.Assert(matrixRowIndexType.RawType == typeof(uint)); _host.CheckValue(buffer, nameof(buffer)); _host.CheckValue(matrixColumnIndexType, nameof(matrixColumnIndexType)); _host.CheckValue(matrixRowIndexType, nameof(matrixRowIndexType)); buffer.Get(out NumberOfRows, out NumberOfColumns, out ApproximationRank, out var leftFactorMatrix, out var rightFactorMatrix); _leftFactorMatrix = leftFactorMatrix; _rightFactorMatrix = rightFactorMatrix; _host.Assert(NumberOfColumns == matrixColumnIndexType.GetCountAsInt32(_host)); _host.Assert(NumberOfRows == matrixRowIndexType.GetCountAsInt32(_host)); _host.Assert(_leftFactorMatrix.Length == NumberOfRows * ApproximationRank); _host.Assert(_rightFactorMatrix.Length == ApproximationRank * NumberOfColumns); MatrixColumnIndexType = matrixColumnIndexType; MatrixRowIndexType = matrixRowIndexType; }
private static IDataView AppendFloatMapper <TInput>(IHostEnvironment env, IChannel ch, IDataView input, string col, KeyType type, int seed) { // Any key is convertible to ulong, so rather than add special case handling for all possible // key-types we just upfront convert it to the most general type (ulong) and work from there. KeyType dstType = new KeyType(typeof(ulong), type.Count); bool identity; var converter = Conversions.Instance.GetStandardConversion <TInput, ulong>(type, dstType, out identity); var isNa = Conversions.Instance.GetIsNAPredicate <TInput>(type); ulong temp = 0; ValueMapper <TInput, Single> mapper; if (seed == 0) { mapper = (in TInput src, ref Single dst) => { if (isNa(in src)) { dst = Single.NaN; return; } converter(in src, ref temp); dst = (Single)(temp - 1); }; } else { ch.Check(type.Count > 0, "Label must be of known cardinality."); int[] permutation = Utils.GetRandomPermutation(RandomUtils.Create(seed), type.GetCountAsInt32(env)); mapper = (in TInput src, ref Single dst) => { if (isNa(in src)) { dst = Single.NaN; return; } converter(in src, ref temp); dst = (Single)permutation[(int)(temp - 1)]; }; } return(LambdaColumnMapper.Create(env, "Key to Float Mapper", input, col, col, type, NumberType.Float, mapper)); }