Skip to content

Corpus hierarchy #9

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 6 commits into from
May 28, 2020
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
.DS_Store
173 changes: 36 additions & 137 deletions Manifest.toml
Original file line number Diff line number Diff line change
@@ -1,136 +1,5 @@
# This file is machine-generated - editing it directly is not advised

[[Arpack]]
deps = ["Arpack_jll", "Libdl", "LinearAlgebra"]
git-tree-sha1 = "2ff92b71ba1747c5fdd541f8fc87736d82f40ec9"
uuid = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
version = "0.4.0"

[[Arpack_jll]]
deps = ["Libdl", "OpenBLAS_jll", "Pkg"]
git-tree-sha1 = "68a90a692ddc0eb72d69a6993ca26e2a923bf195"
uuid = "68821587-b530-5797-8361-c406ea357684"
version = "3.5.0+2"

[[Base64]]
uuid = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

[[BinaryProvider]]
deps = ["Libdl", "SHA"]
git-tree-sha1 = "5b08ed6036d9d3f0ee6369410b830f8873d4024c"
uuid = "b99e7846-7c00-51b0-8f62-c81ae34c0232"
version = "0.5.8"

[[DataAPI]]
git-tree-sha1 = "674b67f344687a88310213ddfa8a2b3c76cc4252"
uuid = "9a962f9c-6df0-11e9-0e5d-c546b8b5ee8a"
version = "1.1.0"

[[DataStructures]]
deps = ["InteractiveUtils", "OrderedCollections"]
git-tree-sha1 = "5a431d46abf2ef2a4d5d00bd0ae61f651cf854c8"
uuid = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
version = "0.17.10"

[[Dates]]
deps = ["Printf"]
uuid = "ade2ca70-3891-5945-98fb-dc099432e06a"

[[Distributed]]
deps = ["Random", "Serialization", "Sockets"]
uuid = "8ba89e20-285c-5b6f-9357-94700520ee1b"

[[Distributions]]
deps = ["FillArrays", "LinearAlgebra", "PDMats", "Printf", "QuadGK", "Random", "SpecialFunctions", "Statistics", "StatsBase", "StatsFuns"]
git-tree-sha1 = "6b19601c0e98de3a8964ed33ad73e130c7165b1d"
uuid = "31c24e10-a181-5473-b8eb-7969acd0382f"
version = "0.22.4"

[[FillArrays]]
deps = ["LinearAlgebra", "Random", "SparseArrays"]
git-tree-sha1 = "85c6b57e2680fa28d5c8adc798967377646fbf66"
uuid = "1a297f60-69ca-5386-bcde-b61e274b549b"
version = "0.8.5"

[[InteractiveUtils]]
deps = ["Markdown"]
uuid = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[[LibGit2]]
uuid = "76f85450-5226-5b5a-8eaa-529ad045b433"

[[Libdl]]
uuid = "8f399da3-3557-5675-b5ff-fb832c97cbdb"

[[LinearAlgebra]]
deps = ["Libdl"]
uuid = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

[[Logging]]
uuid = "56ddb016-857b-54e1-b83d-db4d58db5568"

[[Markdown]]
deps = ["Base64"]
uuid = "d6f4376e-aef5-505a-96c1-9c027394607a"

[[Missings]]
deps = ["DataAPI"]
git-tree-sha1 = "de0a5ce9e5289f27df672ffabef4d1e5861247d5"
uuid = "e1d29d7a-bbdc-5cf2-9ac0-f12de2c33e28"
version = "0.4.3"

[[OpenBLAS_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "e2551d7c25d52f35b76d86a50917a3ba8988f519"
uuid = "4536629a-c528-5b80-bd46-f80d51c5b363"
version = "0.3.7+5"

[[OpenSpecFun_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "65f672edebf3f4e613ddf37db9dcbd7a407e5e90"
uuid = "efe28fd5-8261-553b-a9e1-b2916fc3738e"
version = "0.5.3+1"

[[OrderedCollections]]
deps = ["Random", "Serialization", "Test"]
git-tree-sha1 = "c4c13474d23c60d20a67b217f1d7f22a40edf8f1"
uuid = "bac558e1-5e72-5ebc-8fee-abe8a469f55d"
version = "1.1.0"

[[PDMats]]
deps = ["Arpack", "LinearAlgebra", "SparseArrays", "SuiteSparse", "Test"]
git-tree-sha1 = "5f303510529486bb02ac4d70da8295da38302194"
uuid = "90014a1f-27ba-587c-ab20-58faa44d9150"
version = "0.9.11"

[[Pkg]]
deps = ["Dates", "LibGit2", "Libdl", "Logging", "Markdown", "Printf", "REPL", "Random", "SHA", "Test", "UUIDs"]
uuid = "44cfe95a-1eb2-52ea-b672-e2afdf69b78f"

[[Printf]]
deps = ["Unicode"]
uuid = "de0858da-6303-5e67-8744-51eddeeeb8d7"

[[QuadGK]]
deps = ["DataStructures", "LinearAlgebra"]
git-tree-sha1 = "dc84e810393cfc6294248c9032a9cdacc14a3db4"
uuid = "1fd47b50-473d-5c70-9696-f719f8f3bcdc"
version = "2.3.1"

[[REPL]]
deps = ["InteractiveUtils", "Markdown", "Sockets"]
uuid = "3fa0cd96-eef1-5676-8a61-b3b8758bbffb"

[[Random]]
deps = ["Serialization"]
uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"

[[Rmath]]
deps = ["BinaryProvider", "Libdl", "Random", "Statistics"]
git-tree-sha1 = "2bbddcb984a1d08612d0c4abb5b4774883f6fa98"
uuid = "79098fc4-a85e-5d69-aa6a-4863f24498fa"
version = "0.6.0"

[[SHA]]
uuid = "ea8e919c-243c-51af-8825-aaa63cd721ce"

Expand Down Expand Up @@ -160,12 +29,6 @@ version = "0.10.0"
deps = ["LinearAlgebra", "SparseArrays"]
uuid = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"

[[StatsBase]]
deps = ["DataAPI", "DataStructures", "LinearAlgebra", "Missings", "Printf", "Random", "SortingAlgorithms", "SparseArrays", "Statistics"]
git-tree-sha1 = "be5c7d45daa449d12868f4466dbf5882242cf2d9"
uuid = "2913bbd2-ae8a-5f71-8c99-4fb6c76f3a91"
version = "0.32.1"

[[StatsFuns]]
deps = ["Rmath", "SpecialFunctions"]
git-tree-sha1 = "f290ddd5fdedeadd10e961eb3f4d3340f09d030a"
Expand All @@ -186,3 +49,39 @@ uuid = "cf7118a7-6976-5b1a-9a39-7adc72f591a4"

[[Unicode]]
uuid = "4ec0a83e-493e-50e2-b9ac-8f72acf5a8f5"

[[Zlib_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "fd36a6739e256527287c5444960d0266712cd49e"
uuid = "83775a58-1f1d-513f-b197-d71354ab007a"
version = "1.2.11+8"

[[libass_jll]]
deps = ["Bzip2_jll", "FreeType2_jll", "FriBidi_jll", "Libdl", "Pkg", "Zlib_jll"]
git-tree-sha1 = "3fd3ea3525f2e3d337c54a52b2ca78a5a272bbf5"
uuid = "0ac62f75-1d6f-5e53-bd7c-93b484bb37c0"
version = "0.14.0+0"

[[libfdk_aac_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "0e4ace600c20714a8dd67700c4502714d8473e8e"
uuid = "f638f0a6-7fb0-5443-88ba-1cc74229b280"
version = "0.1.6+1"

[[libvorbis_jll]]
deps = ["Libdl", "Ogg_jll", "Pkg"]
git-tree-sha1 = "71e54fb89ac3e0344c7185d1876fd96b0f246952"
uuid = "f27f6e37-5d2b-51aa-960f-b287f2bc3b7a"
version = "1.3.6+2"

[[x264_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "23664c0757c3740050ca0e22944c786c165ca25a"
uuid = "1270edf5-f2f9-52d2-97e9-ab00b5d0237a"
version = "2019.5.25+1"

[[x265_jll]]
deps = ["Libdl", "Pkg"]
git-tree-sha1 = "9345e417084421a8e91373d6196bc58e660eed2a"
uuid = "dfaa095f-4041-5dcd-9319-2fabd8486b76"
version = "3.0.0+0"
3 changes: 3 additions & 0 deletions Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ authors = ["Jonathan Chang <slycoder @gmail.com>"]
version = "0.1.0"

[deps]
Clustering = "aaaa29a8-35af-508c-8bc3-b662a17a0fe5"
Distributions = "31c24e10-a181-5473-b8eb-7969acd0382f"
Plots = "91a5bcdd-55d7-5caf-9e0b-520d859cae80"
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
SpecialFunctions = "276daf66-3868-5448-9aa4-cd146d93841b"
UMAP = "c4f8c510-2410-5be4-91d7-4fbaeb39457e"

[compat]
julia = "1.3"
Expand Down
46 changes: 39 additions & 7 deletions examples/LDA.jl
Original file line number Diff line number Diff line change
@@ -1,14 +1,46 @@
using TopicModels
using TopicModels, Plots, UMAP

exdir = Pkg.dir("TopicModels", "examples")
##################################################################################################################################
# Fit and Visualize Real-World Text Data

testDocuments = readDocuments(open(joinpath(exdir, "cora.documents")))
exdir = joinpath(dirname(pathof(TopicModels)), "..", "examples")

testDocuments = readDocs(open(joinpath(exdir, "cora.documents")))
testLexicon = readLexicon(open(joinpath(exdir, "cora.lexicon")))

corpus = Corpus(testDocuments)
corpus = Corpus(testDocuments,testLexicon)
model = Model(fill(0.1, 10), fill(0.01,length(testLexicon)), corpus)
state = State(model,corpus)

#@time Juno.@run trainModel(model, state, 30)
@time trainModel(model, state, 30)
topWords = topTopicWords(model, state, 10)

# visualize the fit
@time embedding = umap(state.topics, 2, n_neighbors=10)
maxlabels = vec(map(i->i[1], findmax(state.topics,dims=1)[2]))
scatter(embedding[1,:], embedding[2,:], zcolor=maxlabels, title="UMAP: Max-Likelihood Doc Topics on Learned", marker=(2, 2, :auto, stroke(0)))

##################################################################################################################################
# Fit, Validate, and Visualize Synthetic Data Derived from a Fully-Generative Simulation (Poisson-distributed document-length)

k = 10
lexLength = 1000
corpLambda = 1000 # poisson parameter for random doc length
corpLength = 100
scaleK = 0.01
scaleL = 0.01
testCorpus = LdaCorpus(k, lexLength, corpLambda, corpLength, scaleK, scaleL)

model = Model(fill(0.1, 10), 0.01, length(testLexicon), corpus)
testModel = Model(testCorpus.alpha, testCorpus.beta, testCorpus)
testState = State(testModel, testCorpus)
@time trainModel(testModel, testState, 100)

@time trainModel(model, 30)
# compute validation metrics on a single fit
CorpusARI(testState,testModel,testCorpus) # ARI for max. likelihood. document topics
DocsARI(testState,testCorpus) # ARI for actual word topics

topWords = topTopicWords(model, testLexicon, 21)
# visualize the fit
@time embedding = umap(testState.topics, 2;n_neighbors=10)
maxlabels = vec(map(i->i[1], findmax(CorpusTopics(testCorpus),dims=1)[2]))
scatter(embedding[1,:], embedding[2,:], zcolor=maxlabels, title="UMAP: True on Learned", marker=(2, 2, :auto, stroke(0)))
Loading