-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathModels.cs
193 lines (155 loc) · 8.02 KB
/
Models.cs
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
//
// MoG.cs
//
// Author:
// Tom Diethe <[email protected]>
//
// Copyright (c) 2016 University of Bristol
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
using System;
using System.Linq;
using MicrosoftResearch.Infer;
using MicrosoftResearch.Infer.Distributions;
using MicrosoftResearch.Infer.Maths;
using MicrosoftResearch.Infer.Models;
namespace DGMM
{
public static class Models
{
public static void MixtureOfGaussians(Vector[] x, int numberOfComponents)
{
var numExamples = x.Length;
var numDims = x[0].Count;
var numComponents = Variable.Observed(numberOfComponents).Named("numberOfComponents");
var component = new Range(numComponents).Named("component");
var example = new Range(numExamples).Named("numExamples");
var weights = Variable.DirichletUniform(component);
// Mixture component means
var means = Variable.Array<Vector>(component).Named("means");
var precs = Variable.Array<PositiveDefiniteMatrix>(component).Named("precs");
double scale = 1;
means[component] = Variable.VectorGaussianFromMeanAndPrecision(
Vector.Constant(numDims, 0.0),
PositiveDefiniteMatrix.IdentityScaledBy(numDims, scale)).ForEach(component);
precs[component] = Variable.WishartFromShapeAndScale(
1.0/scale, PositiveDefiniteMatrix.IdentityScaledBy(numDims, scale)).ForEach(component);
// Create a variable array which will hold the data
var data = Variable.Array<Vector>(example).Named("x");
// Create latent indicator variable for each data point
var z = Variable.Array<int>(example).Named("z");
// The mixture of Gaussians model
using (Variable.ForEach(example))
{
z[example] = Variable.Discrete(weights);
using (Variable.Switch(z[example]))
{
data[example] = Variable.VectorGaussianFromMeanAndPrecision(
means[z[example]],
precs[z[example]]);
}
}
// Attach some generated data
data.ObservedValue = x;
// Initialise messages randomly so as to break symmetry
var zinit = Enumerable.Range(0, numExamples).Select(
ia => Discrete.PointMass(Rand.Int(numberOfComponents), numberOfComponents)
).ToArray();
z.InitialiseTo(Distribution<int>.Array(zinit));
// The inference
var ie = new InferenceEngine(new ExpectationPropagation { DefaultNumberOfIterations = 100 });
Console.WriteLine("Dist over pi=" + ie.Infer(weights));
Console.WriteLine("Dist over means=\n" + ie.Infer(means));
Console.WriteLine("Dist over precs=\n" + ie.Infer(precs));
}
public static void SwitchOverMixtures(Vector[] x, int[] numberOfComponents)
{
var numExamples = x.Length;
var numDims = x[0].Count;
// ToDo; better name for loop over numbers of components
var kk = new Range(numberOfComponents.Length).Named("kk");
//var ks = Variable.Discrete(Vector.Constant(numberOfComponents.Length, 1.0 / numberOfComponents.Length)).Named("ks");
//ks.SetValueRange(kk);
var numComponents = Variable.Array<int>(kk).Named("numberOfComponents");
var example = new Range(numExamples).Named("numExamples");
var component = new Range(numComponents[kk]).Named("component");
var weights = Variable.DirichletUniform(component);
var outerWeights = Variable.DirichletUniform(kk).Named("outerWeights");
var ks = Variable.Discrete(outerWeights).Named("ks");
// Mixture component means
var means = Variable.Array(Variable.Array<Vector>(component), kk).Named("means");
var precs = Variable.Array(Variable.Array<PositiveDefiniteMatrix>(component), kk).Named("precs");
// Create latent indicator variable for each data point
var z = Variable.Array(Variable.Array<int>(example), kk).Named("z");
using (Variable.ForEach(kk))
{
double scale = 0.01;
means[kk][component] = Variable.VectorGaussianFromMeanAndPrecision(
Vector.Constant(numDims, 0.0),
PositiveDefiniteMatrix.IdentityScaledBy(numDims, scale)).ForEach(component);
precs[kk][component] = Variable.WishartFromShapeAndScale(
1.0/scale, PositiveDefiniteMatrix.IdentityScaledBy(numDims, scale)).ForEach(component);
using (Variable.ForEach(example))
{
z[kk][example] = Variable.Discrete(weights);
}
}
using (var block = Variable.Switch(ks))
{
// Create a variable array which will hold the data
var data = Variable.Array<Vector>(example).Named("x");
// The mixture of Gaussians model
using (Variable.ForEach(example))
{
using (Variable.Switch(z[ks][example]))
{
data[example] = Variable.VectorGaussianFromMeanAndPrecision(
means[ks][z[ks][example]],
precs[ks][z[ks][example]]);
}
}
// Attach some generated data
data.ObservedValue = x;
}
// Initialise messages randomly so as to break symmetry
var zinit = new Discrete[numberOfComponents.Length][];
for (int i = 0; i < numberOfComponents.Length; i++)
{
zinit[i] = new Discrete[numExamples];
for (int j = 0; j < numExamples; j++)
{
zinit[i][j] = Discrete.PointMass(Rand.Int(numberOfComponents[i]), numberOfComponents[i]);
}
}
z.InitialiseTo(Distribution<int>.Array(zinit));
// Also initialise ks randomly
var ksInit = Discrete.PointMass(Rand.Int(numberOfComponents.Length), numberOfComponents.Length);
ks.InitialiseTo(ksInit);
numComponents.ObservedValue = numberOfComponents;
// The inference
//var ie = new InferenceEngine(new ExpectationPropagation { DefaultNumberOfIterations = 100 });
var ie = new InferenceEngine(new GibbsSampling { DefaultNumberOfIterations = 1000, BurnIn = 200, Thin = 200 });
//Console.WriteLine("Dist over ks=" + ie.Infer(ks));
Console.WriteLine("Dist over phi=" + ie.Infer(outerWeights));
//Console.WriteLine("Dist over pi=" + ie.Infer(weights));
//Console.WriteLine("Dist over means=\n" + ie.Infer(means));
//Console.WriteLine("Dist over precs=\n" + ie.Infer(precs));
}
}
}