Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Diff with code straight from original repository #2

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
97 changes: 75 additions & 22 deletions RecommenderTutorialFromRepository.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@
using Microsoft.ML.Probabilistic.Factors;
using Range = Microsoft.ML.Probabilistic.Models.Range;

public class OriginalRepository{
public class RecommenderTutorialFromRepository{
static int numUsers = 200;
static int numItems = 200;
static int numTraits = 2;
static int numObs = 20000;
static int numLevels = 2;

// Generates data from the model
void GenerateData(
public static void GenerateData(
int numUsers,
int numItems,
int numTraits,
Expand All @@ -30,7 +36,8 @@ void GenerateData(
Gaussian[] itemBiasPrior,
Gaussian[][] userThresholdsPrior,
double affinityNoiseVariance,
double thresholdsNoiseVariance)
double thresholdsNoiseVariance,
Boolean printGenerated)
{
int[] generatedUserData = new int[numObservations];
int[] generatedItemData = new int[numObservations];
Expand Down Expand Up @@ -71,6 +78,15 @@ void GenerateData(
generatedRatingData[observation] = Util.ArrayInit(numLevels, l => noisyAffinity > noisyThresholds[l]);
}

if (printGenerated) {
Console.WriteLine("| true parameters |");
Console.WriteLine("| --------------- |");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("| {0} {1} |", itemTraits[i][0].ToString("F"), itemTraits[i][1].ToString("F"));
}
}

userData.ObservedValue = generatedUserData;
itemData.ObservedValue = generatedItemData;
ratingData.ObservedValue = generatedRatingData;
Expand All @@ -90,22 +106,22 @@ public static void EvidenceExample()
Console.WriteLine("The probability that a Gaussian(0,1) > 0.5 is {0}", Math.Exp(logEvidence));
}

public void ItemPosteriors()
public static void ItemPosteriors()
{
// This example requires EP
InferenceEngine engine = new InferenceEngine();
if (!(engine.Algorithm is Algorithms.ExpectationPropagation))
if (!(engine.Algorithm is Microsoft.ML.Probabilistic.Algorithms.ExpectationPropagation))
{
Console.WriteLine("This example only runs with Expectation Propagation");
return;
}

// Define counts
int numUsers = 50;
int numItems = 10;
int numTraits = 2;
Variable<int> numObservations = Variable.Observed(100).Named("numObservations");
int numLevels = 2;
int numUsers = RecommenderTutorialFromRepository.numUsers;
int numItems = RecommenderTutorialFromRepository.numItems;
int numTraits = RecommenderTutorialFromRepository.numTraits;
Variable<int> numObservations = Variable.Observed(RecommenderTutorialFromRepository.numObs).Named("numObservations");
int numLevels = RecommenderTutorialFromRepository.numLevels;

// Define ranges
Range user = new Range(numUsers).Named("user");
Expand Down Expand Up @@ -197,12 +213,14 @@ public void ItemPosteriors()
itemBiasPrior.ObservedValue,
userThresholdsPrior.ObservedValue,
affinityNoiseVariance.ObservedValue,
thresholdsNoiseVariance.ObservedValue);
thresholdsNoiseVariance.ObservedValue,
true);

// Allow EP to process the product factor as if running VMP
// as in Stern, Herbrich, Graepel paper.
engine.Compiler.GivePriorityTo(typeof(GaussianProductOp_SHG09));
engine.Compiler.ShowWarnings = true;
engine.Compiler.OptimiseInferenceCode = false;

// Run inference
var userTraitsPosterior = engine.Infer<Gaussian[][]>(userTraits);
Expand All @@ -218,36 +236,51 @@ public void ItemPosteriors()
itemBiasPrior.ObservedValue = itemBiasPosterior;
userThresholdsPrior.ObservedValue = userThresholdsPosterior;

/*
// Make a prediction
numObservations.ObservedValue = 1;
userData.ObservedValue = new int[] { 5 };
itemData.ObservedValue = new int[] { 6 };
ratingData.ClearObservedValue();
*/

Console.WriteLine("| learned parameters |");
Console.WriteLine("| ------------------ |");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("| {0} {1} |", itemTraitsPosterior[i][0].GetMean().ToString("F"), itemTraitsPosterior[i][1].GetMean().ToString("F"));
}

/*
Bernoulli[] predictedRating = engine.Infer<Bernoulli[][]>(ratingData)[0];
Console.WriteLine("Predicted rating:");
foreach (var rating in predictedRating)
{
Console.WriteLine(rating);
}
*/
}

public void Evidence()
public static void Evidence()
{
Variable<bool> evidence = Variable.Bernoulli(0.5).Named("evidence");
IfBlock block = Variable.If(evidence);
// Model
// This example requires EP
InferenceEngine engine = new InferenceEngine();
if (!(engine.Algorithm is Algorithms.ExpectationPropagation))
if (!(engine.Algorithm is Microsoft.ML.Probabilistic.Algorithms.ExpectationPropagation))
{
Console.WriteLine("This example only runs with Expectation Propagation");
return;
}
engine.Compiler.OptimiseInferenceCode = false;

// Define counts
int numUsers = 50;
int numItems = 10;
int numTraits = 2;
Variable<int> numObservations = Variable.Observed(100).Named("numObservations");
int numLevels = 2;
int numUsers = RecommenderTutorialFromRepository.numUsers;
int numItems = RecommenderTutorialFromRepository.numItems;
int numTraits = RecommenderTutorialFromRepository.numTraits;
Variable<int> numObservations = Variable.Observed(RecommenderTutorialFromRepository.numObs).Named("numObservations");
int numLevels = RecommenderTutorialFromRepository.numLevels;

// Define ranges
Range user = new Range(numUsers).Named("user");
Expand Down Expand Up @@ -308,7 +341,6 @@ public void Evidence()
Variable<double> affinityNoiseVariance = Variable.Observed(0.1).Named("affinityNoiseVariance");
Variable<double> thresholdsNoiseVariance = Variable.Observed(0.1).Named("thresholdsNoiseVariance");

// Model
using (Variable.ForEach(observation))
{
VariableArray<double> products = Variable.Array<double>(trait).Named("products");
Expand Down Expand Up @@ -339,12 +371,15 @@ public void Evidence()
itemBiasPrior.ObservedValue,
userThresholdsPrior.ObservedValue,
affinityNoiseVariance.ObservedValue,
thresholdsNoiseVariance.ObservedValue);
thresholdsNoiseVariance.ObservedValue,
false);

// Allow EP to process the product factor as if running VMP
// as in Stern, Herbrich, Graepel paper.
engine.Compiler.GivePriorityTo(typeof(GaussianProductOp_SHG09));
engine.Compiler.ShowWarnings = true;
engine.Compiler.OptimiseInferenceCode = false;
block.CloseBlock();

// Run inference
var userTraitsPosterior = engine.Infer<Gaussian[][]>(userTraits);
Expand All @@ -360,17 +395,35 @@ public void Evidence()
itemBiasPrior.ObservedValue = itemBiasPosterior;
userThresholdsPrior.ObservedValue = userThresholdsPosterior;

/* //Print posteriors
Console.WriteLine("| learned parameters |");
Console.WriteLine("| ------------------ |");
for (int i = 0; i < 5; i++)
{
Console.WriteLine("| {0} {1} |", itemTraitsPosterior[i][0].GetMean().ToString("F"), itemTraitsPosterior[i][1].GetMean().ToString("F"));
}
*/

double logEvidence = engine.Infer<Bernoulli>(evidence).LogOdds;
double modelEvidence = System.Math.Exp(logEvidence);
double geo_mean = System.Math.Exp(logEvidence/RecommenderTutorialFromRepository.numObs);
Console.WriteLine("\nEvidence:");
Console.WriteLine("\n| | |\n| -------- | - |\n| evidence | {0} |\n| log(evidence) | {1} |\n| geo_mean | {2} |\n", modelEvidence, logEvidence.ToString("E2"), geo_mean);

/*
// Make a prediction
Bernoulli[] predictedRating = engine.Infer<Bernoulli[][]>(ratingData)[0];
numObservations.ObservedValue = 1;
userData.ObservedValue = new int[] { 5 };
itemData.ObservedValue = new int[] { 6 };
ratingData.ClearObservedValue();

Bernoulli[] predictedRating = engine.Infer<Bernoulli[][]>(ratingData)[0];

Console.WriteLine("Predicted rating:");
foreach (var rating in predictedRating)
{
Console.WriteLine(rating);
}
*/
}
}
}