-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPredictModel.cs
More file actions
executable file
·123 lines (93 loc) · 3.75 KB
/
PredictModel.cs
File metadata and controls
executable file
·123 lines (93 loc) · 3.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using Microsoft.ML.Probabilistic.Distributions;
using Microsoft.ML.Probabilistic.Math;
using Microsoft.ML.Probabilistic.Models;
namespace Predict
{
public class PredictModel
{
Variable<int> numExamples;
VariableArray<int> exampleSize;
VariableArray<int> rankSize;
Range example; // over examples in a dataset
Range item; // over items in an examples
Range rank; // over item pairs
VariableArray<VariableArray<double>, double[][]> scores;
VariableArray<VariableArray<Vector>, Vector[][]> features;
VariableArray<VariableArray<bool>, bool[][]> ranks;
Variable<VectorGaussian> wPrior;
Variable<Vector> w;
Variable<Gamma> scoresNoisePrior;
Variable<double> scoresNoise;
public InferenceEngine engine;
public PredictModel()
{
//
// Dataset size
//
numExamples = Variable.New<int>();
example = new Range(numExamples);
//
// Jagged arrays for (items, features)
//
exampleSize = Variable.Array<int>(example);
item = new Range(exampleSize[example]);
scores = Variable.Array(Variable.Array<double>(item), example);
features = Variable.Array(Variable.Array<Vector>(item), example);
//
// Jagged array for item pair ranks
//
rankSize = Variable.Array<int>(example);
rank = new Range(rankSize[example]);
ranks = Variable.Array(Variable.Array<bool>(rank), example);
//
// Model parameters
//
wPrior = Variable.New<VectorGaussian>();
w = Variable.Random<Vector, VectorGaussian>(wPrior);
//
// Model
//
scoresNoisePrior = Variable.New<Gamma>();
scoresNoise = Variable.Random<double, Gamma>(scoresNoisePrior);
using (Variable.ForEach(example))
{
using (Variable.ForEach(item))
{
var mean = Variable.InnerProduct(w, features[example][item]);
scores[example][item] = Variable.GaussianFromMeanAndPrecision(mean, scoresNoise);
}
using (ForEachBlock pairBlock = Variable.ForEach(rank))
{
var idx = pairBlock.Index;
var diff = scores[example][idx + 1] - scores[example][idx];
var positiveDiff = diff > 0;
using (Variable.If(positiveDiff))
ranks[example][rank].SetTo(Variable.Bernoulli(0.999));
using (Variable.IfNot(positiveDiff))
ranks[example][rank].SetTo(Variable.Bernoulli(0.001));
}
}
//
// Inference engine
//
engine = new InferenceEngine();
engine.NumberOfIterations = 5;
engine.Compiler.UseParallelForLoops = false;
}
public void SetPriors(VectorGaussian wPriorDist, Gamma scoresNoisePriorDist)
{
wPrior.ObservedValue = wPriorDist;
scoresNoisePrior.ObservedValue = scoresNoisePriorDist;
}
public void Predict(int[] itemSizesData, int[] pairwiseSizesData,
Vector[][] featuresData,
out Bernoulli[][] ranksPosterior)
{
numExamples.ObservedValue = itemSizesData.Length;
exampleSize.ObservedValue = itemSizesData;
rankSize.ObservedValue = pairwiseSizesData;
features.ObservedValue = featuresData;
ranksPosterior = engine.Infer<Bernoulli[][]>(ranks);
}
}
}