From 4ab3355fd7cfbdf7ef733e28f1df822ddb3bed15 Mon Sep 17 00:00:00 2001
From: Andre Anjos <andre.anjos@idiap.ch>
Date: Mon, 13 Jan 2014 18:02:05 +0100
Subject: [PATCH] Add logistic and hyperbolic tangent activation
 implementations

---
 setup.py                  |   2 +
 xbob/machine/identity.cpp |   1 -
 xbob/machine/logistic.cpp | 102 ++++++++++++++++++++++++++++++++++++++
 xbob/machine/tanh.cpp     | 102 ++++++++++++++++++++++++++++++++++++++
 4 files changed, 206 insertions(+), 1 deletion(-)
 create mode 100644 xbob/machine/logistic.cpp
 create mode 100644 xbob/machine/tanh.cpp

diff --git a/setup.py b/setup.py
index 4abc845..77a3d0e 100644
--- a/setup.py
+++ b/setup.py
@@ -54,6 +54,8 @@ setup(
         [
           "xbob/machine/activation.cpp",
           "xbob/machine/identity.cpp",
+          "xbob/machine/tanh.cpp",
+          "xbob/machine/logistic.cpp",
           "xbob/machine/main.cpp",
           ],
         packages = packages,
diff --git a/xbob/machine/identity.cpp b/xbob/machine/identity.cpp
index bcbe039..31ba779 100644
--- a/xbob/machine/identity.cpp
+++ b/xbob/machine/identity.cpp
@@ -16,7 +16,6 @@ Computes :math:`f(z) = z` as activation function.\n\
 \n\
 ");
 
-/* Type definition for PyBobMathLpInteriorPointObject */
 typedef struct {
   PyBobMachineActivationObject parent;
 
diff --git a/xbob/machine/logistic.cpp b/xbob/machine/logistic.cpp
new file mode 100644
index 0000000..2fe3279
--- /dev/null
+++ b/xbob/machine/logistic.cpp
@@ -0,0 +1,102 @@
+/**
+ * @author Andre Anjos <andre.anjos@idiap.ch>
+ * @date Mon 13 Jan 2014 17:25:32 CET
+ *
+ * @brief Implementation of the Logistic Activation function
+ */
+
+#include <xbob.machine/api.h>
+
+PyDoc_STRVAR(s_activationsubtype_str, XBOB_EXT_MODULE_PREFIX ".LogisticActivation");
+
+PyDoc_STRVAR(s_activationsubtype_doc,
+"LogisticActivation() -> new LogisticActivation\n\
+\n\
+Computes :math:`f(z) = 1/(1+ e^{-z})` as activation function.\n\
+\n\
+");
+
+typedef struct {
+  PyBobMachineActivationObject parent;
+
+  /* Type-specific fields go here. */
+  bob::machine::LogisticActivation* base;
+
+} PyBobMachineActivationSubtypeObject;
+
+static int PyBobMachineActivationSubtype_init(PyBobMachineActivationSubtypeObject* self, PyObject* args, PyObject* kwds) {
+
+  /* Parses input arguments in a single shot */
+  static const char* const_kwlist[] = {0};
+  static char** kwlist = const_cast<char**>(const_kwlist);
+
+  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist)) return -1;
+
+  try {
+    self->base = new bob::machine::LogisticActivation();
+  }
+  catch (std::exception& ex) {
+    PyErr_SetString(PyExc_RuntimeError, ex.what());
+  }
+  catch (...) {
+    PyErr_Format(PyExc_RuntimeError, "cannot create new object of type `%s' - unknown exception thrown", s_activationsubtype_str);
+  }
+
+  self->parent.base = self->base;
+
+  if (PyErr_Occurred()) return -1;
+
+  return 0;
+
+}
+
+static void PyBobMachineActivationSubtype_delete (PyBobMachineActivationSubtypeObject* self) {
+
+  delete self->base;
+  self->parent.base = 0;
+  self->base = 0;
+  self->parent.ob_type->tp_free((PyObject*)self);
+
+}
+
+PyTypeObject PyBobMachineActivationSubtype_Type = {
+    PyObject_HEAD_INIT(0)
+    0,                                                  /*ob_size*/
+    0,                                                  /*tp_name*/
+    sizeof(PyBobMachineActivationSubtypeObject),        /*tp_basicsize*/
+    0,                                                  /*tp_itemsize*/
+    (destructor)PyBobMachineActivationSubtype_delete,   /*tp_dealloc*/
+    0,                                                  /*tp_print*/
+    0,                                                  /*tp_getattr*/
+    0,                                                  /*tp_setattr*/
+    0,                                                  /*tp_compare*/
+    0,                                                  /*tp_repr*/
+    0,                                                  /*tp_as_number*/
+    0,                                                  /*tp_as_sequence*/
+    0,                                                  /*tp_as_mapping*/
+    0,                                                  /*tp_hash */
+    0,                                                  /*tp_call*/
+    0,                                                  /*tp_str*/
+    0,                                                  /*tp_getattro*/
+    0,                                                  /*tp_setattro*/
+    0,                                                  /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /*tp_flags*/
+    s_activationsubtype_doc,                            /* tp_doc */
+    0,		                                              /* tp_traverse */
+    0,		                                              /* tp_clear */
+    0,                                                  /* tp_richcompare */
+    0,		                                              /* tp_weaklistoffset */
+    0,		                                              /* tp_iter */
+    0,		                                              /* tp_iternext */
+    0,                                                  /* tp_methods */
+    0,                                                  /* tp_members */
+    0,                                                  /* tp_getset */
+    0,                                                  /* tp_base */
+    0,                                                  /* tp_dict */
+    0,                                                  /* tp_descr_get */
+    0,                                                  /* tp_descr_set */
+    0,                                                  /* tp_dictoffset */
+    (initproc)PyBobMachineActivationSubtype_init,       /* tp_init */
+    0,                                                  /* tp_alloc */
+    0,                                                  /* tp_new */
+};
diff --git a/xbob/machine/tanh.cpp b/xbob/machine/tanh.cpp
new file mode 100644
index 0000000..39692a6
--- /dev/null
+++ b/xbob/machine/tanh.cpp
@@ -0,0 +1,102 @@
+/**
+ * @author Andre Anjos <andre.anjos@idiap.ch>
+ * @date Mon 13 Jan 2014 17:25:32 CET
+ *
+ * @brief Implementation of the HyperbolicTangent Activation function
+ */
+
+#include <xbob.machine/api.h>
+
+PyDoc_STRVAR(s_activationsubtype_str, XBOB_EXT_MODULE_PREFIX ".HyperbolicTangentActivation");
+
+PyDoc_STRVAR(s_activationsubtype_doc,
+"HyperbolicTangentActivation() -> new HyperbolicTangentActivation\n\
+\n\
+Computes :math:`f(z) = \\tanh(z)` as activation function.\n\
+\n\
+");
+
+typedef struct {
+  PyBobMachineActivationObject parent;
+
+  /* Type-specific fields go here. */
+  bob::machine::HyperbolicTangentActivation* base;
+
+} PyBobMachineActivationSubtypeObject;
+
+static int PyBobMachineActivationSubtype_init(PyBobMachineActivationSubtypeObject* self, PyObject* args, PyObject* kwds) {
+
+  /* Parses input arguments in a single shot */
+  static const char* const_kwlist[] = {0};
+  static char** kwlist = const_cast<char**>(const_kwlist);
+
+  if (!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist)) return -1;
+
+  try {
+    self->base = new bob::machine::HyperbolicTangentActivation();
+  }
+  catch (std::exception& ex) {
+    PyErr_SetString(PyExc_RuntimeError, ex.what());
+  }
+  catch (...) {
+    PyErr_Format(PyExc_RuntimeError, "cannot create new object of type `%s' - unknown exception thrown", s_activationsubtype_str);
+  }
+
+  self->parent.base = self->base;
+
+  if (PyErr_Occurred()) return -1;
+
+  return 0;
+
+}
+
+static void PyBobMachineActivationSubtype_delete (PyBobMachineActivationSubtypeObject* self) {
+
+  delete self->base;
+  self->parent.base = 0;
+  self->base = 0;
+  self->parent.ob_type->tp_free((PyObject*)self);
+
+}
+
+PyTypeObject PyBobMachineActivationSubtype_Type = {
+    PyObject_HEAD_INIT(0)
+    0,                                                  /*ob_size*/
+    0,                                                  /*tp_name*/
+    sizeof(PyBobMachineActivationSubtypeObject),        /*tp_basicsize*/
+    0,                                                  /*tp_itemsize*/
+    (destructor)PyBobMachineActivationSubtype_delete,   /*tp_dealloc*/
+    0,                                                  /*tp_print*/
+    0,                                                  /*tp_getattr*/
+    0,                                                  /*tp_setattr*/
+    0,                                                  /*tp_compare*/
+    0,                                                  /*tp_repr*/
+    0,                                                  /*tp_as_number*/
+    0,                                                  /*tp_as_sequence*/
+    0,                                                  /*tp_as_mapping*/
+    0,                                                  /*tp_hash */
+    0,                                                  /*tp_call*/
+    0,                                                  /*tp_str*/
+    0,                                                  /*tp_getattro*/
+    0,                                                  /*tp_setattro*/
+    0,                                                  /*tp_as_buffer*/
+    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,           /*tp_flags*/
+    s_activationsubtype_doc,                            /* tp_doc */
+    0,		                                              /* tp_traverse */
+    0,		                                              /* tp_clear */
+    0,                                                  /* tp_richcompare */
+    0,		                                              /* tp_weaklistoffset */
+    0,		                                              /* tp_iter */
+    0,		                                              /* tp_iternext */
+    0,                                                  /* tp_methods */
+    0,                                                  /* tp_members */
+    0,                                                  /* tp_getset */
+    0,                                                  /* tp_base */
+    0,                                                  /* tp_dict */
+    0,                                                  /* tp_descr_get */
+    0,                                                  /* tp_descr_set */
+    0,                                                  /* tp_dictoffset */
+    (initproc)PyBobMachineActivationSubtype_init,       /* tp_init */
+    0,                                                  /* tp_alloc */
+    0,                                                  /* tp_new */
+};
-- 
GitLab