diff --git a/buildout.cfg b/buildout.cfg index fe5e59531a787a98db97b9b35c90cdee85c4276f..4a6fbef556b04231a9e578469160cfed6273d6e2 100644 --- a/buildout.cfg +++ b/buildout.cfg @@ -12,6 +12,7 @@ develop = src/xbob.extension src/xbob.blitz src/xbob.io src/xbob.learn.activation + src/xbob.core . ; options for xbob.buildout extension @@ -25,6 +26,7 @@ xbob.extension = git https://github.com/bioidiap/xbob.extension branch=prototype xbob.blitz = git https://github.com/bioidiap/xbob.blitz xbob.io = git https://github.com/bioidiap/xbob.io xbob.learn.activation = git https://github.com/bioidiap/xbob.learn.activation +xbob.core = git https://github.com/bioidiap/xbob.core [scripts] recipe = xbob.buildout:scripts diff --git a/setup.py b/setup.py index a4643587d804624469519ea6fdffb7ea099da14e..a797ee5d07f1214bcccc09015ecd30ef7d0329fe 100644 --- a/setup.py +++ b/setup.py @@ -4,9 +4,10 @@ # Mon 16 Apr 08:18:08 2012 CEST from setuptools import setup, find_packages, dist -dist.Distribution(dict(setup_requires=['xbob.blitz', 'xbob.io', 'xbob.learn.activation'])) +dist.Distribution(dict(setup_requires=['xbob.blitz', 'xbob.io', 'xbob.learn.activation', 'xbob.core'])) from xbob.blitz.extension import Extension import xbob.io +import xbob.core import xbob.learn.activation import os @@ -16,7 +17,8 @@ include_dirs = [ package_dir, xbob.blitz.get_include(), xbob.io.get_include(), - xbob.learn.activation.get_include() + xbob.learn.activation.get_include(), + xbob.core.get_include(), ] packages = ['bob-machine >= 1.2.2', 'bob-trainer >= 1.2.2'] @@ -42,6 +44,7 @@ setup( 'xbob.blitz', 'xbob.io', 'xbob.learn.activation', + 'xbob.core', ], namespace_packages=[ diff --git a/xbob/learn/mlp/machine.cpp b/xbob/learn/mlp/machine.cpp index 839d6fe57314758f45c17c8dbd756ebb3166ae66..ed3672df7aaa62cd096be467687fded8322ee63c 100644 --- a/xbob/learn/mlp/machine.cpp +++ b/xbob/learn/mlp/machine.cpp @@ -13,6 +13,7 @@ #include <xbob.io/api.h> #include <xbob.learn.activation/api.h> #include <xbob.learn.mlp/api.h> +#include <xbob.core/random.h> #include <structmember.h> /**************************************** @@ -942,6 +943,52 @@ static PyObject* PyBobLearnMLPMachine_IsSimilarTo } +PyDoc_STRVAR(s_randomize_str, "randomize"); +PyDoc_STRVAR(s_randomize_doc, +"o.randomize([lower_bound, [upper_bound, [rng]]]) -> None\n\ +\n\ +Resets parameters of this MLP using a random number generator.\n\ +\n\ +Sets all weights and biases of this MLP, with random values\n\ +between :math:`[-0.1, 0.1)` as advised in textbooks.\n\nValues\n\ +are drawn using ``boost::uniform_real`` class. The seed is\n\ +picked using a time-based algorithm. Different calls spaced\n\ +of at least 10 microseconds (machine clock) will be seeded\n\ +differently. If lower and upper bound values are given, then\n\ +new parameters are taken from ``[lower_bound, upper_bound)``,\n\ +according to the ``boost::random`` documentation. The user may\n\ +also pass the random number generator to be used. This allows\n\ +you to set the seed to a specific value before randomizing\n\ +the MLP parameters. If not set, this method will use an\n\ +internal random number generator with a seed which is based\n\ +on the current time.\n\ +"); + +static PyObject* PyBobLearnMLPMachine_Randomize +(PyBobLearnMLPMachineObject* self, PyObject* args, PyObject* kwds) { + + /* Parses input arguments in a single shot */ + static const char* const_kwlist[] = {"lower_bound", "upper_bound", "rng", 0}; + static char** kwlist = const_cast<char**>(const_kwlist); + + double lower_bound = -0.1; + double upper_bound = 0.1; + PyBoostMt19937Object* rng = 0; + + if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ddO!", kwlist, + &lower_bound, &upper_bound, &PyBoostMt19937_Check, &rng)) return 0; + + if (rng) { + self->cxx->randomize(*rng->rng, lower_bound, upper_bound); + } + else { + self->cxx->randomize(lower_bound, upper_bound); + } + + Py_RETURN_NONE; + +} + static PyMethodDef PyBobLearnMLPMachine_methods[] = { { s_forward_str, @@ -967,6 +1014,12 @@ static PyMethodDef PyBobLearnMLPMachine_methods[] = { METH_VARARGS|METH_KEYWORDS, s_is_similar_to_doc }, + { + s_randomize_str, + (PyCFunction)PyBobLearnMLPMachine_Randomize, + METH_VARARGS|METH_KEYWORDS, + s_randomize_doc + }, {0} /* Sentinel */ }; diff --git a/xbob/learn/mlp/main.cpp b/xbob/learn/mlp/main.cpp index c9ba4d061348375f26de93591295ce93638e11b9..0982f400eb4736573ed01c4c4982e4cea121c4b2 100644 --- a/xbob/learn/mlp/main.cpp +++ b/xbob/learn/mlp/main.cpp @@ -15,6 +15,7 @@ #include <xbob.blitz/cleanup.h> #include <xbob.io/api.h> #include <xbob.learn.activation/api.h> +#include <xbob.core/random.h> static PyMethodDef module_methods[] = { {0} /* Sentinel */ @@ -91,10 +92,30 @@ static PyObject* create_module (void) { if (c_api_object) PyModule_AddObject(m, "_C_API", c_api_object); - /* imports xbob.learn.activation C-API + dependencies */ - if (import_xbob_blitz() < 0) return 0; - if (import_xbob_io() < 0) return 0; - if (import_xbob_learn_activation() < 0) return 0; + /* imports dependencies */ + if (import_xbob_blitz() < 0) { + PyErr_Print(); + PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME); + return 0; + } + + if (import_xbob_io() < 0) { + PyErr_Print(); + PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME); + return 0; + } + + if (import_xbob_learn_activation() < 0) { + PyErr_Print(); + PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME); + return 0; + } + + if (import_xbob_core_random() < 0) { + PyErr_Print(); + PyErr_Format(PyExc_ImportError, "cannot import `%s'", XBOB_EXT_MODULE_NAME); + return 0; + } Py_INCREF(m); return m; diff --git a/xbob/learn/mlp/version.cpp b/xbob/learn/mlp/version.cpp index 7608cc52562da4a3bd4487a0e4e4409e93d523b5..95de81a48cfe2882f283b74b213f42935541b37c 100644 --- a/xbob/learn/mlp/version.cpp +++ b/xbob/learn/mlp/version.cpp @@ -24,6 +24,7 @@ #include <xbob.io/config.h> #include <xbob.learn.activation/config.h> #include <xbob.learn.mlp/config.h> +#include <xbob.core/config.h> static int dict_set(PyObject* d, const char* key, const char* value) { PyObject* v = Py_BuildValue("s", value); @@ -113,7 +114,14 @@ static PyObject* xbob_io_version() { } /** - * xbob.io c/c++ api version + * xbob.core c/c++ api version + */ +static PyObject* xbob_core_version() { + return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(XBOB_CORE_API_VERSION)); +} + +/** + * xbob.learn.activation c/c++ api version */ static PyObject* xbob_learn_activation_version() { return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(XBOB_LEARN_ACTIVATION_API_VERSION)); @@ -132,6 +140,7 @@ static PyObject* build_version_dictionary() { if (!dict_steal(retval, "NumPy", numpy_version())) return 0; if (!dict_steal(retval, "xbob.blitz", xbob_blitz_version())) return 0; if (!dict_steal(retval, "xbob.io", xbob_io_version())) return 0; + if (!dict_steal(retval, "xbob.core", xbob_core_version())) return 0; if (!dict_steal(retval, "xbob.learn.activation", xbob_learn_activation_version())) return 0; if (!dict_steal(retval, "Bob", bob_version())) return 0; @@ -178,7 +187,7 @@ static PyObject* create_module (void) { if (!externals) return 0; if (PyModule_AddObject(m, "externals", externals) < 0) return 0; - /* imports xbob.io C-API + dependencies */ + /* imports xbob.blitz C-API + dependencies */ if (import_xbob_blitz() < 0) return 0; Py_INCREF(m);