コード例 #1
0
        internal MatrixFactorizationModelParameters(IHostEnvironment env, SafeTrainingAndModelBuffer buffer, KeyDataViewType matrixColumnIndexType, KeyDataViewType 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;
        }
コード例 #2
0
        private static IDataView AppendFloatMapper <TInput>(IHostEnvironment env, IChannel ch, IDataView input,
                                                            string col, KeyDataViewType 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.
            KeyDataViewType dstType = new KeyDataViewType(typeof(ulong), type.Count);
            bool            identity;
            var             converter = Conversions.Instance.GetStandardConversion <TInput, ulong>(type, dstType, out identity);
            var             isNa      = Conversions.Instance.GetIsNAPredicate <TInput>(type);

            ValueMapper <TInput, Single> mapper;

            if (seed == 0)
            {
                mapper =
                    (in TInput src, ref Single dst) =>
                {
                    //Attention: This method is called from multiple threads.
                    //Do not move the temp variable outside this method.
                    //If you do, the variable is shared between the threads and results in a race condition.
                    ulong temp = 0;
                    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) =>
                {
                    //Attention: This method is called from multiple threads.
                    //Do not move the temp variable outside this method.
                    //If you do, the variable is shared between the threads and results in a race condition.
                    ulong temp = 0;
                    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, NumberDataViewType.Single, mapper));
        }