MCPcopy Create free account
hub / github.com/bab2min/tomotopy / train

Method train

src/python/handler/py_LDA.cpp:277–320  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

275}
276
277void LDAModelObject::train(size_t iteration, size_t workers, size_t ps, bool freezeTopics, size_t callbackInterval, PyObject* callback)
278{
279 if (seedGiven && workers != 1 && PyErr_WarnEx(PyExc_RuntimeWarning, "The training result may differ even with fixed seed if `workers` != 1.", 1)) throw py::ExcPropagation{};
280 auto* inst = getInst<tomoto::ILDAModel>();
281 if (!isPrepared)
282 {
283 inst->prepare(true, minWordCnt, minWordDf, removeTopWord);
284 isPrepared = true;
285 }
286
287 if (callback == Py_None) callback = nullptr;
288 if (callback && !PyCallable_Check(callback)) throw py::ValueError{ "`callback` should be a callable object" };
289 if (!callback || callbackInterval <= 0)
290 {
291 callbackInterval = iteration;
292 }
293
294 for (size_t it = 0; it < iteration; it += callbackInterval)
295 {
296 if (callback)
297 {
298 py::UniqueObj args{ py::buildPyTuple(getObject(), it, iteration)};
299 if (callback)
300 {
301 py::UniqueObj ret{ PyObject_CallObject(callback, args.get()) };
302 if (!ret) throw py::ExcPropagation{};
303 }
304 }
305
306 if (inst->train(std::min(callbackInterval, iteration - it), workers, (tomoto::ParallelScheme)ps, !!fixed) < 0)
307 {
308 throw py::RuntimeError{ "Train failed" };
309 }
310 }
311 if (callback)
312 {
313 py::UniqueObj args{ py::buildPyTuple(getObject(), iteration, iteration) };
314 if (callback)
315 {
316 py::UniqueObj ret{ PyObject_CallObject(callback, args.get()) };
317 if (!ret) throw py::ExcPropagation{};
318 }
319 }
320}
321
322py::UniqueObj LDAModelObject::getTopicWords(size_t topicId, size_t topN, bool returnId) const
323{

Callers

nothing calls this directly

Calls 3

buildPyTupleFunction · 0.85
prepareMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected