Beispiel #1
0
        /// <summary>
        /// Sets the observed values of the user and item metadata - both the features and their corresponding counts.
        /// </summary>
        /// <param name="metadata">The metadata to observe.</param>
        public void SetObservedMetadata(InstanceMetadata metadata)
        {
            Debug.Assert(
                metadata.UserCount > 0 && metadata.ItemCount > 0,
                "The training set must contain at least one user and item.");
            Debug.Assert(
                metadata.UserFeatures.EntityCount == metadata.UserCount,
                "Features should be provided for all users in the training set, and only for them.");
            Debug.Assert(
                metadata.ItemFeatures.EntityCount == metadata.ItemCount,
                "Features should be provided for all items in the training set, and only for them.");

            var thresholdCount = metadata.RatingCount + 1;

            this.inferenceAlgorithm.UserCount                 = metadata.UserCount;
            this.inferenceAlgorithm.ItemCount                 = metadata.ItemCount;
            this.inferenceAlgorithm.UserThresholdCount        = thresholdCount;
            this.inferenceAlgorithm.MiddleUserThresholdIndex  = thresholdCount / 2;
            this.inferenceAlgorithm.UserThresholdPriorMean    = AlgorithmUtils.GetUserThresholdPriorMeans(thresholdCount);
            this.inferenceAlgorithm.UserFeatureCount          = metadata.UserFeatures.FeatureCount;
            this.inferenceAlgorithm.ItemFeatureCount          = metadata.ItemFeatures.FeatureCount;
            this.inferenceAlgorithm.NonZeroUserFeatureValues  = metadata.UserFeatures.NonZeroFeatureValues;
            this.inferenceAlgorithm.NonZeroUserFeatureIndices = metadata.UserFeatures.NonZeroFeatureIndices;
            this.inferenceAlgorithm.NonZeroUserFeatureCounts  = metadata.UserFeatures.NonZeroFeatureCounts;
            this.inferenceAlgorithm.NonZeroItemFeatureValues  = metadata.ItemFeatures.NonZeroFeatureValues;
            this.inferenceAlgorithm.NonZeroItemFeatureIndices = metadata.ItemFeatures.NonZeroFeatureIndices;
            this.inferenceAlgorithm.NonZeroItemFeatureCounts  = metadata.ItemFeatures.NonZeroFeatureCounts;
        }
        /// <summary>
        /// Infers parameters for a given cold user.
        /// </summary>
        /// <param name="userFeatures">The user features.</param>
        /// <returns>A distribution over the user parameters.</returns>
        public UserParameterDistribution InferUserParameters(SparseFeatureVector userFeatures)
        {
            UserParameterDistribution result;

            if (this.userFeatureParameterPosteriors.FeatureCount == 0)
            {
                Debug.Assert(
                    userFeatures.FeatureCount == 0,
                    "The number of user features passed must be equal to the number of user features learned.");

                result = this.userParameterDistributionAverage;
            }
            else
            {
                GaussianArray traits;
                Gaussian      bias;

                AlgorithmUtils.AddFeatureContribution(
                    this.userParameterDistributionAverage,
                    this.userFeatureParameterPosteriors,
                    userFeatures,
                    out traits,
                    out bias);

                result = new UserParameterDistribution(traits, bias, this.userParameterDistributionAverage.Thresholds);
            }

            return(result);
        }