예제 #1
0
        /// <summary>
        /// Infers parameters for a given cold item.
        /// </summary>
        /// <param name="itemFeatures">The item features.</param>
        /// <returns>A distribution over the item parameters.</returns>
        public ItemParameterDistribution InferItemParameters(SparseFeatureVector itemFeatures)
        {
            ItemParameterDistribution result;

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

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

                AlgorithmUtils.AddFeatureContribution(
                    this.itemParameterDistributionAverage,
                    this.itemFeatureParameterPosteriors,
                    itemFeatures,
                    out traits,
                    out bias);

                result = new ItemParameterDistribution(traits, bias);
            }

            return(result);
        }
예제 #2
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;
        }