@@ -28,19 +28,19 @@ GMM(x::Vector{T}) where T <: AbstractFloat = GMM(reshape(x, length(x), 1)) # st
28
28
29
29
# # constructors based on data or matrix
30
30
function GMM (n:: Int , x:: DataOrMatrix{T} ; method:: Symbol = :kmeans , kind= :diag ,
31
- nInit:: Int = 50 , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 ) where T <: AbstractFloat
31
+ nInit:: Int = 50 , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 , parallel :: Bool = true ) where T <: AbstractFloat
32
32
if n < 2
33
33
return GMM (x, kind= kind)
34
34
elseif method == :split
35
- return GMM2 (n, x, kind= kind, nIter= nIter, nFinal= nFinal, sparse= sparse)
35
+ return GMM2 (n, x, kind= kind, nIter= nIter, nFinal= nFinal, sparse= sparse, parallel = parallel )
36
36
elseif method == :kmeans
37
- return GMMk (n, x, kind= kind, nInit= nInit, nIter= nIter, sparse= sparse)
37
+ return GMMk (n, x, kind= kind, nInit= nInit, nIter= nIter, sparse= sparse, parallel = parallel )
38
38
else
39
39
error (" Unknown method " , method)
40
40
end
41
41
end
42
42
# # a 1-dimensional Gaussian can be initialized with a vector, skip kind=
43
- GMM (n:: Int , x:: Vector{T} ; method:: Symbol = :kmeans , nInit:: Int = 50 , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 ) where T <: AbstractFloat = GMM (n, reshape (x, length (x), 1 ); method= method, kind= :diag , nInit= nInit, nIter= nIter, nFinal= nFinal, sparse= sparse)
43
+ GMM (n:: Int , x:: Vector{T} ; method:: Symbol = :kmeans , nInit:: Int = 50 , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 , parallel :: Bool = true ) where T <: AbstractFloat = GMM (n, reshape (x, length (x), 1 ); method= method, kind= :diag , nInit= nInit, nIter= nIter, nFinal= nFinal, sparse= sparse, parallel = parallel )
44
44
45
45
# # we sometimes end up with pathological gmms
46
46
function sanitycheck! (gmm:: GMM )
73
73
74
74
75
75
# # initialize GMM using Clustering.kmeans (which uses a method similar to kmeans++)
76
- function GMMk (n:: Int , x:: DataOrMatrix{T} ; kind= :diag , nInit:: Int = 50 , nIter:: Int = 10 , sparse= 0 ) where T <: AbstractFloat
76
+ function GMMk (n:: Int , x:: DataOrMatrix{T} ; kind= :diag , nInit:: Int = 50 , nIter:: Int = 10 , sparse= 0 , parallel :: Bool = true ) where T <: AbstractFloat
77
77
nₓ, d = size (x)
78
78
hist = [History (@sprintf (" Initializing GMM, %d Gaussians %s covariance %d dimensions using %d data points" , n, diag, d, nₓ))]
79
79
@info (last (hist). s)
@@ -141,22 +141,22 @@ function GMMk(n::Int, x::DataOrMatrix{T}; kind=:diag, nInit::Int=50, nIter::Int=
141
141
@info (last (hist). s)
142
142
gmm = GMM (w, μ, Σ, hist, nxx)
143
143
sanitycheck! (gmm)
144
- em! (gmm, x; nIter= nIter, sparse= sparse)
144
+ em! (gmm, x; nIter= nIter, sparse= sparse, parallel = parallel )
145
145
return gmm
146
146
end
147
147
148
148
# # Train a GMM by consecutively splitting all means. n most be a power of 2
149
149
# # This kind of initialization is deterministic, but doesn't work particularily well, its seems
150
150
# # We start with one Gaussian, and consecutively split.
151
- function GMM2 (n:: Int , x:: DataOrMatrix ; kind= :diag , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 )
151
+ function GMM2 (n:: Int , x:: DataOrMatrix ; kind= :diag , nIter:: Int = 10 , nFinal:: Int = nIter, sparse= 0 , parallel :: Bool = true )
152
152
log2n = round (Int,log2 (n))
153
153
2 ^ log2n == n || error (" n must be power of 2" )
154
154
gmm = GMM (x, kind= kind)
155
155
tll = [avll (gmm, x)]
156
156
@info (" 0: avll = " , tll[1 ])
157
157
for i in 1 : log2n
158
158
gmm = gmmsplit (gmm)
159
- avll = em! (gmm, x; nIter= (i== log2n ? nFinal : nIter), sparse= sparse)
159
+ avll = em! (gmm, x; nIter= (i== log2n ? nFinal : nIter), sparse= sparse, parallel = parallel )
160
160
@info (i, avll)
161
161
append! (tll, avll)
162
162
end
235
235
# the log-likelihood history, per data frame per dimension
236
236
# # Note: 0 iterations is allowed, this just computes the average log likelihood
237
237
# # of the data and stores this in the history.
238
- function em! (gmm:: GMM , x:: DataOrMatrix ; nIter:: Int = 10 , varfloor:: Float64 = 1e-3 , sparse= 0 , debug= 1 )
238
+ function em! (gmm:: GMM , x:: DataOrMatrix ; nIter:: Int = 10 , varfloor:: Float64 = 1e-3 , sparse= 0 , parallel :: Bool = true , debug= 1 )
239
239
size (x,2 )== gmm. d || error (" Inconsistent size gmm and x" )
240
240
d = gmm. d # dim
241
241
ng = gmm. n # n gaussians
@@ -247,7 +247,7 @@ function em!(gmm::GMM, x::DataOrMatrix; nIter::Int = 10, varfloor::Float64=1e-3,
247
247
248
248
for i in 1 : nIter
249
249
# # E-step
250
- nₓ, ll[i], N, F, S = stats (gmm, x, parallel= true )
250
+ nₓ, ll[i], N, F, S = stats (gmm, x, parallel= parallel )
251
251
# # M-step
252
252
gmm. w = N / nₓ
253
253
gmm. μ = F ./ N
0 commit comments