Commit 83a3ff06 authored by Manuel Günther's avatar Manuel Günther
Browse files

Moved C++ code into its own Library; removed dependency on Bob

parent 5b673e99
......@@ -9,8 +9,8 @@
*/
#include <algorithm>
#include <bob/core/check.h>
#include <bob/math/linear.h>
#include <bob.core/check.h>
#include <bob.math/linear.h>
#include <bob.learn.mlp/backprop.h>
......
......@@ -11,7 +11,7 @@
namespace bob { namespace learn { namespace mlp {
CrossEntropyLoss::CrossEntropyLoss(boost::shared_ptr<bob::machine::Activation> actfun)
CrossEntropyLoss::CrossEntropyLoss(boost::shared_ptr<bob::learn::activation::Activation> actfun)
: m_actfun(actfun),
m_logistic_activation(m_actfun->unique_identifier() == "bob.machine.Activation.Logistic") {}
......
......@@ -12,10 +12,10 @@
#include <boost/format.hpp>
#include <boost/make_shared.hpp>
#include <bob/core/check.h>
#include <bob/core/array_copy.h>
#include <bob/core/assert.h>
#include <bob/math/linear.h>
#include <bob.core/check.h>
#include <bob.core/array_copy.h>
#include <bob.core/assert.h>
#include <bob.math/linear.h>
#include <bob.learn.mlp/machine.h>
......@@ -24,7 +24,7 @@ bob::learn::mlp::Machine::Machine (size_t input, size_t output):
m_input_div(input),
m_weight(1),
m_bias(1),
m_hidden_activation(boost::make_shared<bob::machine::HyperbolicTangentActivation>()),
m_hidden_activation(boost::make_shared<bob::learn::activation::HyperbolicTangentActivation>()),
m_output_activation(m_hidden_activation),
m_buffer(1)
{
......@@ -40,7 +40,7 @@ bob::learn::mlp::Machine::Machine (size_t input, size_t hidden, size_t output):
m_input_div(input),
m_weight(2),
m_bias(2),
m_hidden_activation(boost::make_shared<bob::machine::HyperbolicTangentActivation>()),
m_hidden_activation(boost::make_shared<bob::learn::activation::HyperbolicTangentActivation>()),
m_output_activation(m_hidden_activation),
m_buffer(2)
{
......@@ -56,7 +56,7 @@ bob::learn::mlp::Machine::Machine (size_t input, const std::vector<size_t>& hidd
m_input_div(input),
m_weight(hidden.size()+1),
m_bias(hidden.size()+1),
m_hidden_activation(boost::make_shared<bob::machine::HyperbolicTangentActivation>()),
m_hidden_activation(boost::make_shared<bob::learn::activation::HyperbolicTangentActivation>()),
m_output_activation(m_hidden_activation),
m_buffer(hidden.size()+1)
{
......@@ -68,7 +68,7 @@ bob::learn::mlp::Machine::Machine (size_t input, const std::vector<size_t>& hidd
}
bob::learn::mlp::Machine::Machine (const std::vector<size_t>& shape):
m_hidden_activation(boost::make_shared<bob::machine::HyperbolicTangentActivation>()),
m_hidden_activation(boost::make_shared<bob::learn::activation::HyperbolicTangentActivation>()),
m_output_activation(m_hidden_activation)
{
resize(shape);
......@@ -94,7 +94,7 @@ bob::learn::mlp::Machine::Machine (const bob::learn::mlp::Machine& other):
}
}
bob::learn::mlp::Machine::Machine (bob::io::HDF5File& config) {
bob::learn::mlp::Machine::Machine (bob::io::base::HDF5File& config) {
load(config);
}
......@@ -144,7 +144,7 @@ bool bob::learn::mlp::Machine::is_similar_to(const bob::learn::mlp::Machine& oth
}
void bob::learn::mlp::Machine::load (bob::io::HDF5File& config) {
void bob::learn::mlp::Machine::load (bob::io::base::HDF5File& config) {
uint8_t nhidden = config.read<uint8_t>("nhidden");
m_weight.resize(nhidden+1);
m_bias.resize(nhidden+1);
......@@ -167,14 +167,14 @@ void bob::learn::mlp::Machine::load (bob::io::HDF5File& config) {
//switch between different versions - support for version 2
if (config.hasAttribute(".", "version")) { //new version
config.cd("hidden_activation");
m_hidden_activation = bob::machine::load_activation(config);
m_hidden_activation = bob::learn::activation::load_activation(config);
config.cd("../output_activation");
m_output_activation = bob::machine::load_activation(config);
m_output_activation = bob::learn::activation::load_activation(config);
config.cd("..");
}
else { //old version
uint32_t act = config.read<uint32_t>("activation");
m_hidden_activation = bob::machine::make_deprecated_activation(act);
m_hidden_activation = bob::learn::activation::make_deprecated_activation(act);
m_output_activation = m_hidden_activation;
}
......@@ -186,7 +186,7 @@ void bob::learn::mlp::Machine::load (bob::io::HDF5File& config) {
}
}
void bob::learn::mlp::Machine::save (bob::io::HDF5File& config) const {
void bob::learn::mlp::Machine::save (bob::io::base::HDF5File& config) const {
config.setAttribute(".", "version", 1);
config.setArray("input_sub", m_input_sub);
config.setArray("input_div", m_input_div);
......
......@@ -9,9 +9,9 @@
*/
#include <algorithm>
#include <bob/core/check.h>
#include <bob/core/array_copy.h>
#include <bob/math/linear.h>
#include <bob.core/check.h>
#include <bob.core/array_copy.h>
#include <bob.math/linear.h>
#include <bob.learn.mlp/rprop.h>
......
......@@ -11,8 +11,8 @@
#include <sys/time.h>
#include <boost/format.hpp>
#include <bob/core/assert.h>
#include <bob/core/array_copy.h>
#include <bob.core/assert.h>
#include <bob.core/array_copy.h>
#include <bob.learn.mlp/shuffler.h>
......
......@@ -13,7 +13,7 @@
namespace bob { namespace learn { namespace mlp {
SquareError::SquareError(boost::shared_ptr<bob::machine::Activation> actfun):
SquareError::SquareError(boost::shared_ptr<bob::learn::activation::Activation> actfun):
m_actfun(actfun) {}
SquareError::~SquareError() {}
......
......@@ -7,9 +7,9 @@
*/
#include <algorithm>
#include <bob/core/assert.h>
#include <bob/core/check.h>
#include <bob/math/linear.h>
#include <bob.core/assert.h>
#include <bob.core/check.h>
#include <bob.math/linear.h>
#include <bob.learn.mlp/trainer.h>
......@@ -131,13 +131,13 @@ void bob::learn::mlp::Trainer::forward_step(const bob::learn::mlp::Machine& mach
const std::vector<blitz::Array<double,2> >& machine_weight = machine.getWeights();
const std::vector<blitz::Array<double,1> >& machine_bias = machine.getBiases();
boost::shared_ptr<bob::machine::Activation> hidden_actfun = machine.getHiddenActivation();
boost::shared_ptr<bob::machine::Activation> output_actfun = machine.getOutputActivation();
boost::shared_ptr<bob::learn::activation::Activation> hidden_actfun = machine.getHiddenActivation();
boost::shared_ptr<bob::learn::activation::Activation> output_actfun = machine.getOutputActivation();
for (size_t k=0; k<machine_weight.size(); ++k) { //for all layers
if (k == 0) bob::math::prod_(input, machine_weight[k], m_output[k]);
else bob::math::prod_(m_output[k-1], machine_weight[k], m_output[k]);
boost::shared_ptr<bob::machine::Activation> cur_actfun =
boost::shared_ptr<bob::learn::activation::Activation> cur_actfun =
(k == (machine_weight.size()-1) ? output_actfun : hidden_actfun );
for (int i=0; i<(int)m_batch_size; ++i) { //for every example
for (int j=0; j<m_output[k].extent(1); ++j) { //for all variables
......@@ -154,7 +154,7 @@ void bob::learn::mlp::Trainer::backward_step
const std::vector<blitz::Array<double,2> >& machine_weight = machine.getWeights();
//last layer
boost::shared_ptr<bob::machine::Activation> output_actfun = machine.getOutputActivation();
boost::shared_ptr<bob::learn::activation::Activation> output_actfun = machine.getOutputActivation();
for (int i=0; i<(int)m_batch_size; ++i) { //for every example
for (int j=0; j<m_error[m_H].extent(1); ++j) { //for all variables
m_error[m_H](i,j) = m_cost->error(m_output[m_H](i,j), target(i,j));
......@@ -162,7 +162,7 @@ void bob::learn::mlp::Trainer::backward_step
}
//all other layers
boost::shared_ptr<bob::machine::Activation> hidden_actfun = machine.getHiddenActivation();
boost::shared_ptr<bob::learn::activation::Activation> hidden_actfun = machine.getHiddenActivation();
for (size_t k=m_H; k>0; --k) {
bob::math::prod_(m_error[k], machine_weight[k].transpose(1,0), m_error[k-1]);
for (int i=0; i<(int)m_batch_size; ++i) { //for every example
......
......@@ -10,14 +10,14 @@
#include <boost/shared_ptr.hpp>
#include <bob.learn.mlp/config.h>
#include "machine.h"
#include "cost.h"
#include "square_error.h"
#include "cross_entropy.h"
#include "shuffler.h"
#include "trainer.h"
#include "backprop.h"
#include "rprop.h"
#include <bob.learn.mlp/machine.h>
#include <bob.learn.mlp/cost.h>
#include <bob.learn.mlp/square_error.h>
#include <bob.learn.mlp/cross_entropy.h>
#include <bob.learn.mlp/shuffler.h>
#include <bob.learn.mlp/trainer.h>
#include <bob.learn.mlp/backprop.h>
#include <bob.learn.mlp/rprop.h>
#define BOB_LEARN_MLP_MODULE_PREFIX bob.learn.mlp
#define BOB_LEARN_MLP_MODULE_NAME _library
......
......@@ -16,8 +16,8 @@
#include <vector>
#include <boost/function.hpp>
#include "machine.h"
#include "trainer.h"
#include <bob.learn.mlp/machine.h>
#include <bob.learn.mlp/trainer.h>
namespace bob { namespace learn { namespace mlp {
......
......@@ -12,7 +12,6 @@
#include <string>
#include <boost/shared_ptr.hpp>
#include "bob/machine/Activation.h"
namespace bob { namespace learn { namespace mlp {
......
......@@ -10,7 +10,8 @@
#ifndef BOB_LEARN_MLP_CROSSENTROPYLOSS_H
#define BOB_LEARN_MLP_CROSSENTROPYLOSS_H
#include "cost.h"
#include <bob.learn.mlp/cost.h>
#include <bob.learn.activation/Activation.h>
namespace bob { namespace learn { namespace mlp {
......@@ -55,7 +56,7 @@ namespace bob { namespace learn { namespace mlp {
* b = \hat{y} - y
* \f]
*/
CrossEntropyLoss(boost::shared_ptr<bob::machine::Activation> actfun);
CrossEntropyLoss(boost::shared_ptr<bob::learn::activation::Activation> actfun);
/**
* Virtualized destructor
......@@ -64,7 +65,7 @@ namespace bob { namespace learn { namespace mlp {
/**
* Tells if this CrossEntropyLoss is set to operate together with a
* bob::machine::LogisticActivation.
* bob::learn::activation::LogisticActivation.
*/
bool logistic_activation() const { return m_logistic_activation; }
......@@ -118,7 +119,7 @@ namespace bob { namespace learn { namespace mlp {
private: //representation
boost::shared_ptr<bob::machine::Activation> m_actfun; //act. function
boost::shared_ptr<bob::learn::activation::Activation> m_actfun; //act. function
bool m_logistic_activation; ///< if 'true', simplify backprop_error()
};
......
......@@ -15,8 +15,8 @@
#include <boost/shared_ptr.hpp>
#include <blitz/array.h>
#include <bob/io/HDF5File.h>
#include <bob/machine/Activation.h>
#include <bob.io.base/HDF5File.h>
#include <bob.learn.activation/Activation.h>
namespace bob { namespace learn { namespace mlp {
......@@ -90,7 +90,7 @@ namespace bob { namespace learn { namespace mlp {
/**
* Starts a new MLP from an existing Configuration object.
*/
Machine (bob::io::HDF5File& config);
Machine (bob::io::base::HDF5File& config);
/**
* Just to virtualise the destructor
......@@ -123,12 +123,12 @@ namespace bob { namespace learn { namespace mlp {
* Loads data from an existing configuration object. Resets the current
* state.
*/
void load (bob::io::HDF5File& config);
void load (bob::io::base::HDF5File& config);
/**
* Saves an existing machine to a Configuration object.
*/
void save (bob::io::HDF5File& config) const;
void save (bob::io::base::HDF5File& config) const;
/**
* Forwards data through the network, outputs the values of each output
......@@ -315,26 +315,26 @@ namespace bob { namespace learn { namespace mlp {
/**
* Returns the currently set activation function for the hidden layers
*/
boost::shared_ptr<bob::machine::Activation> getHiddenActivation() const
boost::shared_ptr<bob::learn::activation::Activation> getHiddenActivation() const
{ return m_hidden_activation; }
/**
* Sets the activation function for each of the hidden layers.
*/
void setHiddenActivation(boost::shared_ptr<bob::machine::Activation> a) {
void setHiddenActivation(boost::shared_ptr<bob::learn::activation::Activation> a) {
m_hidden_activation = a;
}
/**
* Returns the currently set output activation function
*/
boost::shared_ptr<bob::machine::Activation> getOutputActivation() const
boost::shared_ptr<bob::learn::activation::Activation> getOutputActivation() const
{ return m_output_activation; }
/**
* Sets the activation function for the outputs of the last layer.
*/
void setOutputActivation(boost::shared_ptr<bob::machine::Activation> a) {
void setOutputActivation(boost::shared_ptr<bob::learn::activation::Activation> a) {
m_output_activation = a;
}
......@@ -366,8 +366,8 @@ namespace bob { namespace learn { namespace mlp {
blitz::Array<double, 1> m_input_div; ///< input division
std::vector<blitz::Array<double, 2> > m_weight; ///< weights
std::vector<blitz::Array<double, 1> > m_bias; ///< biases for the output
boost::shared_ptr<bob::machine::Activation> m_hidden_activation; ///< currently set activation type
boost::shared_ptr<bob::machine::Activation> m_output_activation; ///< currently set activation type
boost::shared_ptr<bob::learn::activation::Activation> m_hidden_activation; ///< currently set activation type
boost::shared_ptr<bob::learn::activation::Activation> m_output_activation; ///< currently set activation type
mutable std::vector<blitz::Array<double, 1> > m_buffer; ///< buffer for the outputs of each layer
};
......
......@@ -11,7 +11,7 @@
#include <vector>
#include <blitz/array.h>
#include "machine.h"
#include <bob.learn.mlp/machine.h>
namespace bob { namespace learn { namespace mlp {
......
......@@ -17,8 +17,8 @@
#include <vector>
#include <boost/function.hpp>
#include "machine.h"
#include "trainer.h"
#include <bob.learn.mlp/machine.h>
#include <bob.learn.mlp/trainer.h>
namespace bob { namespace learn { namespace mlp {
......
......@@ -10,7 +10,8 @@
#ifndef BOB_LEARN_MLP_SQUAREERROR_H
#define BOB_LEARN_MLP_SQUAREERROR_H
#include "cost.h"
#include <bob.learn.mlp/cost.h>
#include <bob.learn.activation/Activation.h>
namespace bob { namespace learn { namespace mlp {
......@@ -32,7 +33,7 @@ namespace bob { namespace learn { namespace mlp {
/**
* Builds a SquareError functor with an existing activation function.
*/
SquareError(boost::shared_ptr<bob::machine::Activation> actfun);
SquareError(boost::shared_ptr<bob::learn::activation::Activation> actfun);
/**
* Virtualized destructor
......@@ -89,7 +90,7 @@ namespace bob { namespace learn { namespace mlp {
private: //representation
boost::shared_ptr<bob::machine::Activation> m_actfun; //act. function
boost::shared_ptr<bob::learn::activation::Activation> m_actfun; //act. function
};
......
......@@ -12,8 +12,8 @@
#include <vector>
#include <boost/shared_ptr.hpp>
#include "machine.h"
#include "cost.h"
#include <bob.learn.mlp/machine.h>
#include <bob.learn.mlp/cost.h>
namespace bob { namespace learn { namespace mlp {
......
......@@ -5,9 +5,12 @@
* @brief Binds configuration information available from bob
*/
#include <Python.h>
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
#include <bob.blitz/capi.h>
#include <bob.blitz/cleanup.h>
#include <bob/config.h>
#include <string>
#include <cstdlib>
......@@ -16,15 +19,11 @@
#include <boost/version.hpp>
#include <boost/format.hpp>
#ifdef NO_IMPORT_ARRAY
#undef NO_IMPORT_ARRAY
#endif
#include <bob.blitz/capi.h>
#include <bob.blitz/cleanup.h>
#include <bob.core/config.h>
#include <bob.io.base/config.h>
#include <bob.math/config.h>
#include <bob.learn.activation/config.h>
#include <bob.learn.mlp/config.h>
#include <bob.core/config.h>
static int dict_set(PyObject* d, const char* key, const char* value) {
PyObject* v = Py_BuildValue("s", value);
......@@ -84,13 +83,6 @@ static PyObject* python_version() {
return Py_BuildValue("s", f.str().c_str());
}
/**
* Bob version, API version and platform
*/
static PyObject* bob_version() {
return Py_BuildValue("sis", BOB_VERSION, BOB_API_VERSION, BOB_PLATFORM);
}
/**
* Numpy version
*/
......@@ -120,6 +112,13 @@ static PyObject* bob_core_version() {
return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_CORE_API_VERSION));
}
/**
* bob.math c/c++ api version
*/
static PyObject* bob_math_version() {
return Py_BuildValue("{ss}", "api", BOOST_PP_STRINGIZE(BOB_MATH_API_VERSION));
}
/**
* bob.learn.activation c/c++ api version
*/
......@@ -133,16 +132,17 @@ static PyObject* build_version_dictionary() {
if (!retval) return 0;
auto retval_ = make_safe(retval);
if (!dict_steal(retval, "Bob", bob_core_version())) return 0;
if (!dict_set(retval, "Blitz++", BZ_VERSION)) return 0;
if (!dict_steal(retval, "Boost", boost_version())) return 0;
if (!dict_steal(retval, "Compiler", compiler_version())) return 0;
if (!dict_steal(retval, "Python", python_version())) return 0;
if (!dict_steal(retval, "NumPy", numpy_version())) return 0;
if (!dict_steal(retval, "bob.blitz", bob_blitz_version())) return 0;
if (!dict_steal(retval, "bob.io.base", bob_io_base_version())) return 0;
if (!dict_steal(retval, "bob.core", bob_core_version())) return 0;
if (!dict_steal(retval, "bob.math", bob_math_version())) return 0;
if (!dict_steal(retval, "bob.io.base", bob_io_base_version())) return 0;
if (!dict_steal(retval, "bob.learn.activation", bob_learn_activation_version())) return 0;
if (!dict_steal(retval, "Bob", bob_version())) return 0;
Py_INCREF(retval);
return retval;
......
......@@ -10,23 +10,23 @@ extensions = bob.buildout
auto-checkout = *
develop = src/bob.extension
src/bob.blitz
src/bob.core
src/bob.io.base
src/bob.math
src/bob.learn.activation
src/bob.core
.
; options for bob.buildout extension
debug = true
verbose = true
prefixes = /idiap/group/torch5spro/releases/preview/install/linux-x86_64-release
/Users/andre/work/bob/b/dbg/
[sources]
bob.extension = git https://github.com/bioidiap/bob.extension
bob.blitz = git https://github.com/bioidiap/bob.blitz
bob.core = git https://github.com/bioidiap/bob.core
bob.io.base = git https://github.com/bioidiap/bob.io.base
bob.math = git https://github.com/bioidiap/bob.math
bob.learn.activation = git https://github.com/bioidiap/bob.learn.activation
bob.core = git https://github.com/bioidiap/bob.core
[scripts]
recipe = bob.buildout:scripts
......@@ -3,25 +3,12 @@
# Andre Anjos <andre.anjos@idiap.ch>
# Mon 16 Apr 08:18:08 2012 CEST
bob_packages = ['bob.core', 'bob.io.base', 'bob.math', 'bob.learn.activation']
from setuptools import setup, find_packages, dist
dist.Distribution(dict(setup_requires=['bob.blitz', 'bob.io.base', 'bob.learn.activation', 'bob.core']))
from bob.blitz.extension import Extension
import bob.io.base
import bob.core
import bob.learn.activation
import os
package_dir = os.path.dirname(os.path.realpath(__file__))
package_dir = os.path.join(package_dir, 'bob', 'learn', 'mlp', 'include')
include_dirs = [
package_dir,
bob.blitz.get_include(),
bob.io.base.get_include(),
bob.learn.activation.get_include(),
bob.core.get_include(),
]
packages = ['bob-io >= 2.0.0a2', 'bob-machine >= 2.0.0a2']
dist.Distribution(dict(setup_requires=['bob.blitz'] + bob_packages))
from bob.blitz.extension import Extension, Library, build_ext
version = '2.0.0a0'
setup(
......@@ -44,6 +31,7 @@ setup(
'setuptools',
'bob.blitz',
'bob.io.base',
'bob.math',
'bob.learn.activation',
'bob.core',
],
......@@ -57,11 +45,26 @@ setup(
Extension("bob.learn.mlp.version",
[
"bob/learn/mlp/version.cpp",
],
packages = packages,
include_dirs = include_dirs,
],
bob_packages = bob_packages,
version = version,
),
Library("bob.learn.mlp.bob_learn_mlp",
[
"bob/learn/mlp/cxx/roll.cpp",
"bob/learn/mlp/cxx/machine.cpp",
"bob/learn/mlp/cxx/cross_entropy.cpp",
"bob/learn/mlp/cxx/square_error.cpp",
"bob/learn/mlp/cxx/shuffler.cpp",
"bob/learn/mlp/cxx/trainer.cpp",
"bob/learn/mlp/cxx/backprop.cpp",
"bob/learn/mlp/cxx/rprop.cpp",
],
bob_packages = bob_packages,
version = version,
),
),
Extension("bob.learn.mlp._library",
[
"bob/learn/mlp/roll.cpp",
......@@ -72,25 +75,20 @@ setup(
"bob/learn/mlp/cost.cpp",
"bob/learn/mlp/machine.cpp",
"bob/learn/mlp/main.cpp",
"bob/learn/mlp/cxx/roll.cpp",
"bob/learn/mlp/cxx/machine.cpp",
"bob/learn/mlp/cxx/cross_entropy.cpp",
"bob/learn/mlp/cxx/square_error.cpp",
"bob/learn/mlp/cxx/shuffler.cpp",
"bob/learn/mlp/cxx/trainer.cpp",
"bob/learn/mlp/cxx/backprop.cpp",
"bob/learn/mlp/cxx/rprop.cpp",
],
packages = packages,
include_dirs = include_dirs,
],
bob_packages = bob_packages,
version = version,