Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 9e37b17

Browse files
Slight code speedups
Assert indexing operations are in bounds Pull indexing operations out of loops when possible
1 parent d66c994 commit 9e37b17

File tree

1 file changed

+26
-17
lines changed

1 file changed

+26
-17
lines changed

src/TopicModels.jl

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,15 +78,17 @@ end
7878

7979
function initializeAssignments(model::Model)
8080
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]
8485
topic = sampleMultinomial(model.alphaPrior)
85-
model.assignments[dd][ww] = topic
86+
@inbounds model.assignments[dd][ww] = topic
8687
updateSufficientStatistics(
8788
word, topic, dd, model.corpus.weights[dd][ww], model)
8889
end
8990
end
91+
return
9092
end
9193

9294
function sampleMultinomial(p::Array{Float64,1})
@@ -109,11 +111,12 @@ function wordDistribution(word::Int,
109111
out::Vector{Float64})
110112
V = size(model.topics, 2)
111113
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
115118
end
116-
return out
119+
return
117120
end
118121

119122
function sampleWord(word::Int,
@@ -130,32 +133,37 @@ function updateSufficientStatistics(word::Int64,
130133
document::Int64,
131134
scale::Float64,
132135
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
136141
end
137142

138143
function sampleDocument(document::Int,
139144
model::Model)
140-
words = model.corpus.documents[document]
145+
@inbounds words = model.corpus.documents[document]
141146
Nw = length(words)
142-
weights = model.corpus.weights[document]
147+
@inbounds weights = model.corpus.weights[document]
143148
K = length(model.alphaPrior)
144149
p = Array(Float64, K)
150+
@inbounds assignments = model.assignments[document]
145151
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]
148154
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
151157
updateSufficientStatistics(word, newTopic, document, weights[ii], model)
152158
end
159+
return
153160
end
154161

155162
function sampleCorpus(model::Model)
156163
for ii in 1:length(model.corpus)
157164
sampleDocument(ii, model)
158165
end
166+
return
159167
end
160168

161169
# Note, files are zero indexed, but we are 1-indexed.
@@ -171,6 +179,7 @@ function trainModel(model::Model,
171179
println(string("Iteration ", ii, "..."))
172180
sampleCorpus(model)
173181
end
182+
return
174183
end
175184

176185
function topTopicWords(model::Model,

0 commit comments

Comments
 (0)