78
78
79
79
function initializeAssignments (model:: Model )
80
80
for dd in 1 : length (model. corpus)
81
- model. assignments[dd] = fill (0 , length (model. corpus. documents[dd]))
82
- for ww in 1 : length (model. corpus. documents[dd])
83
- word = model. corpus. documents[dd][ww]
81
+ @inbounds words = model. corpus. documents[dd]
82
+ @inbounds model. assignments[dd] = fill (0 , length (words))
83
+ for ww in 1 : length (words)
84
+ @inbounds word = words[ww]
84
85
topic = sampleMultinomial (model. alphaPrior)
85
- model. assignments[dd][ww] = topic
86
+ @inbounds model. assignments[dd][ww] = topic
86
87
updateSufficientStatistics (
87
88
word, topic, dd, model. corpus. weights[dd][ww], model)
88
89
end
89
90
end
91
+ return
90
92
end
91
93
92
94
function sampleMultinomial (p:: Array{Float64,1} )
@@ -109,11 +111,12 @@ function wordDistribution(word::Int,
109
111
out:: Vector{Float64} )
110
112
V = size (model. topics, 2 )
111
113
for ii in 1 : length (out)
112
- out[ii] = (model. documentSums[ii, document] + model. alphaPrior[ii]) *
113
- (model. topics[ii, word] + model. betaPrior) /
114
- (model. topicSums[ii] + V * model. betaPrior)
114
+ u = (model. documentSums[ii, document] + model. alphaPrior[ii]) *
115
+ (model. topics[ii, word] + model. betaPrior) /
116
+ (model. topicSums[ii] + V * model. betaPrior)
117
+ @inbounds out[ii] = u
115
118
end
116
- return out
119
+ return
117
120
end
118
121
119
122
function sampleWord (word:: Int ,
@@ -130,32 +133,37 @@ function updateSufficientStatistics(word::Int64,
130
133
document:: Int64 ,
131
134
scale:: Float64 ,
132
135
model:: Model )
133
- model. documentSums[topic, document] += scale
134
- model. topicSums[topic] += scale * ! model. frozen
135
- model. topics[topic, word] += scale * ! model. frozen
136
+ fr = float64 (! model. frozen)
137
+ @inbounds model. documentSums[topic, document] += scale
138
+ @inbounds model. topicSums[topic] += scale * fr
139
+ @inbounds model. topics[topic, word] += scale * fr
140
+ return
136
141
end
137
142
138
143
function sampleDocument (document:: Int ,
139
144
model:: Model )
140
- words = model. corpus. documents[document]
145
+ @inbounds words = model. corpus. documents[document]
141
146
Nw = length (words)
142
- weights = model. corpus. weights[document]
147
+ @inbounds weights = model. corpus. weights[document]
143
148
K = length (model. alphaPrior)
144
149
p = Array (Float64, K)
150
+ @inbounds assignments = model. assignments[document]
145
151
for ii in 1 : Nw
146
- word = words[ii]
147
- oldTopic = model . assignments[document][ ii]
152
+ @inbounds word = words[ii]
153
+ @inbounds oldTopic = assignments[ii]
148
154
updateSufficientStatistics (word, oldTopic, document, - weights[ii], model)
149
- newTopic:: Int64 = sampleWord (word, document, model, p)
150
- model . assignments[document] [ii] = newTopic
155
+ newTopic = sampleWord (word, document, model, p)
156
+ @inbounds assignments[ii] = newTopic
151
157
updateSufficientStatistics (word, newTopic, document, weights[ii], model)
152
158
end
159
+ return
153
160
end
154
161
155
162
function sampleCorpus (model:: Model )
156
163
for ii in 1 : length (model. corpus)
157
164
sampleDocument (ii, model)
158
165
end
166
+ return
159
167
end
160
168
161
169
# Note, files are zero indexed, but we are 1-indexed.
@@ -171,6 +179,7 @@ function trainModel(model::Model,
171
179
println (string (" Iteration " , ii, " ..." ))
172
180
sampleCorpus (model)
173
181
end
182
+ return
174
183
end
175
184
176
185
function topTopicWords (model:: Model ,
0 commit comments