Esempio n. 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;
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="ParameterDistributions"/> class.
        /// Each contained distribution is initialized to uniform.
        /// </summary>
        /// <param name="metaData">The instance meta data.</param>
        /// <param name="traitCount">The number of traits.</param>
        public ParameterDistributions(InstanceMetadata metaData, int traitCount)
        {
            // Check input data
            Debug.Assert(metaData.UserCount > 0, "The number of users must be positive.");
            Debug.Assert(metaData.ItemCount > 0, "The number of items must be positive.");
            Debug.Assert(metaData.RatingCount + 1 > 0, "The number of user thresholds must be positive.");
            Debug.Assert(metaData.UserFeatures.FeatureCount >= 0, "The number of user features must be non-negative.");
            Debug.Assert(metaData.ItemFeatures.FeatureCount >= 0, "The number of item features must be non-negative.");
            Debug.Assert(traitCount >= 0, "The number of traits must be non-negative.");

            // The value to which each element of the contained distributions will be initialized
            var initialValue = Gaussian.Uniform();

            // Initialize contained distributions.
            this.UserTraitDistribution     = new GaussianMatrix(new GaussianArray(initialValue, traitCount), metaData.UserCount);
            this.UserBiasDistribution      = new GaussianArray(initialValue, metaData.UserCount);
            this.UserThresholdDistribution = new GaussianMatrix(
                new GaussianArray(initialValue, metaData.RatingCount + 1), metaData.UserCount);
            this.ItemTraitDistribution = new GaussianMatrix(new GaussianArray(initialValue, traitCount), metaData.ItemCount);
            this.ItemBiasDistribution  = new GaussianArray(initialValue, metaData.ItemCount);

            this.UserFeature = new FeatureParameterDistribution(traitCount, metaData.UserFeatures.FeatureCount, initialValue);
            this.ItemFeature = new FeatureParameterDistribution(traitCount, metaData.ItemFeatures.FeatureCount, initialValue);
        }