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

Skip to content

Slight code speedups #3

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 8, 2013
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 26 additions & 17 deletions src/TopicModels.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,17 @@ end

function initializeAssignments(model::Model)
for dd in 1:length(model.corpus)
model.assignments[dd] = fill(0, length(model.corpus.documents[dd]))
for ww in 1:length(model.corpus.documents[dd])
word = model.corpus.documents[dd][ww]
@inbounds words = model.corpus.documents[dd]
@inbounds model.assignments[dd] = fill(0, length(words))
for ww in 1:length(words)
@inbounds word = words[ww]
topic = sampleMultinomial(model.alphaPrior)
model.assignments[dd][ww] = topic
@inbounds model.assignments[dd][ww] = topic
updateSufficientStatistics(
word, topic, dd, model.corpus.weights[dd][ww], model)
end
end
return
end

function sampleMultinomial(p::Array{Float64,1})
Expand All @@ -109,11 +111,12 @@ function wordDistribution(word::Int,
out::Vector{Float64})
V = size(model.topics, 2)
for ii in 1:length(out)
out[ii] = (model.documentSums[ii, document] + model.alphaPrior[ii]) *
(model.topics[ii, word] + model.betaPrior) /
(model.topicSums[ii] + V * model.betaPrior)
u = (model.documentSums[ii, document] + model.alphaPrior[ii]) *
(model.topics[ii, word] + model.betaPrior) /
(model.topicSums[ii] + V * model.betaPrior)
@inbounds out[ii] = u
end
return out
return
end

function sampleWord(word::Int,
Expand All @@ -130,32 +133,37 @@ function updateSufficientStatistics(word::Int64,
document::Int64,
scale::Float64,
model::Model)
model.documentSums[topic, document] += scale
model.topicSums[topic] += scale * !model.frozen
model.topics[topic, word] += scale * !model.frozen
fr = float64(!model.frozen)
@inbounds model.documentSums[topic, document] += scale
@inbounds model.topicSums[topic] += scale * fr
@inbounds model.topics[topic, word] += scale * fr
return
end

function sampleDocument(document::Int,
model::Model)
words = model.corpus.documents[document]
@inbounds words = model.corpus.documents[document]
Nw = length(words)
weights = model.corpus.weights[document]
@inbounds weights = model.corpus.weights[document]
K = length(model.alphaPrior)
p = Array(Float64, K)
@inbounds assignments = model.assignments[document]
for ii in 1:Nw
word = words[ii]
oldTopic = model.assignments[document][ii]
@inbounds word = words[ii]
@inbounds oldTopic = assignments[ii]
updateSufficientStatistics(word, oldTopic, document, -weights[ii], model)
newTopic::Int64 = sampleWord(word, document, model, p)
model.assignments[document][ii] = newTopic
newTopic = sampleWord(word, document, model, p)
@inbounds assignments[ii] = newTopic
updateSufficientStatistics(word, newTopic, document, weights[ii], model)
end
return
end

function sampleCorpus(model::Model)
for ii in 1:length(model.corpus)
sampleDocument(ii, model)
end
return
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these just stylistic or do they impact performance in some way?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question: they actually may not impact performance anymore. At one point in the past they could help the compiler from getting confused about the return type of for loops, but that doesn't seem to be true anymore.

end

# Note, files are zero indexed, but we are 1-indexed.
Expand All @@ -171,6 +179,7 @@ function trainModel(model::Model,
println(string("Iteration ", ii, "..."))
sampleCorpus(model)
end
return
end

function topTopicWords(model::Model,
Expand Down