Skip to content

Commit

Permalink
[topics]: Add bindings for LDA inferencer classes.
Browse files Browse the repository at this point in the history
  • Loading branch information
skystrife committed Apr 30, 2018
1 parent fef345a commit 5ebbb98
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 3 deletions.
2 changes: 1 addition & 1 deletion deps/meta
64 changes: 62 additions & 2 deletions src/metapy_topics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ void metapy_bind_topics(py::module& m)
})
.def("num_topics", &topics::lda_model::num_topics);

py::class_<topics::inferencer>{m_topics, "LDAInferencer"}
.def("term_distribution",
[](const topics::inferencer& inf, topic_id k) {
return py_multinomial{inf.term_distribution(k)};
},
py::arg("k"))
.def("num_topics", &topics::inferencer::num_topics);

py::class_<topics::lda_cvb, topics::lda_model>{m_topics, "LDACollapsedVB"}
.def(py::init<const learn::dataset&, std::size_t, double, double>(),
py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
Expand All @@ -72,6 +80,31 @@ void metapy_bind_topics(py::module& m)
},
py::arg("num_iters"), py::arg("convergence") = 1e-3);

py::class_<topics::lda_cvb::inferencer, topics::inferencer>{m_topics,
"CVBInferencer"}
.def("__init__",
[](topics::inferencer& inf, const std::string& cfgfile) {
py::gil_scoped_release release;
auto config = cpptoml::parse_file(cfgfile);
new (&inf) topics::inferencer(*config);
},
py::arg("cfg_file"))
.def("__init__",
[](topics::inferencer& inf, const std::string& topicsfile,
double alpha) {
py::gil_scoped_release release;
std::ifstream topics_stream{topicsfile};
new (&inf) topics::inferencer(topics_stream, alpha);
},
py::arg("topics_file"), py::arg("alpha"))
.def("infer",
[](const topics::lda_cvb::inferencer& inf,
const learn::feature_vector& doc, std::size_t max_iters,
double convergence) {
return py_multinomial{inf(doc, max_iters, convergence)};
},
py::arg("doc"), py::arg("max_iters"), py::arg("convergence"));

py::class_<topics::lda_gibbs, topics::lda_model>{m_topics, "LDAGibbs"}
.def(py::init<const learn::dataset&, std::size_t, double, double>(),
py::keep_alive<0, 1>(), py::arg("docs"), py::arg("num_topics"),
Expand All @@ -84,6 +117,31 @@ void metapy_bind_topics(py::module& m)
},
py::arg("num_iters"), py::arg("convergence") = 1e-6);

py::class_<topics::lda_gibbs::inferencer, topics::inferencer>{
m_topics, "GibbsInferencer"}
.def("__init__",
[](topics::inferencer& inf, const std::string& cfgfile) {
auto config = cpptoml::parse_file(cfgfile);
new (&inf) topics::inferencer(*config);
},
py::arg("cfg_file"))
.def("__init__",
[](topics::inferencer& inf, const std::string& topicsfile,
double alpha) {
std::ifstream topics_stream{topicsfile};
new (&inf) topics::inferencer(topics_stream, alpha);
},
py::arg("topics_file"), py::arg("alpha"))

.def("infer",
[](const topics::lda_gibbs::inferencer& inf,
const learn::feature_vector& doc, std::size_t num_iters,
std::size_t seed) {
random::xoroshiro128 rng{seed};
return py_multinomial{inf(doc, num_iters, rng)};
},
py::arg("doc"), py::arg("max_iters"), py::arg("rng_seed"));

py::class_<topics::parallel_lda_gibbs, topics::lda_gibbs>{
m_topics, "LDAParallelGibbs"}
.def(py::init<const learn::dataset&, std::size_t, double, double>(),
Expand Down Expand Up @@ -127,8 +185,10 @@ void metapy_bind_topics(py::module& m)

new (&model) topics::topic_model(theta, phi);
})
.def("top_k", [](const topics::topic_model& model, topic_id tid,
std::size_t k) { return model.top_k(tid, k); },
.def("top_k",
[](const topics::topic_model& model, topic_id tid, std::size_t k) {
return model.top_k(tid, k);
},
py::arg("tid"), py::arg("k") = 10)
.def("top_k",
[](const topics::topic_model& model, topic_id tid, std::size_t k,
Expand Down

0 comments on commit 5ebbb98

Please sign in to comment.