Commit 580a3930 authored by Manuel Günther's avatar Manuel Günther
Browse files

Fixed #3 by replacing all PyNumber_Check instances by PyArray_IsAnyScalar; added test case

parent c3af3cf7
......@@ -297,7 +297,7 @@ static PyObject* PyBobLearnCost_f
arg = PyList_GET_ITEM(tmp, 0);
}
if (PyNumber_Check(arg) && !(PyArray_Check(arg) || PyBlitzArray_Check(arg)))
if (PyArray_IsAnyScalar(arg))
return apply_scalar(self, s_f_str,
boost::bind(&bob::learn::mlp::Cost::f, self->cxx, _1, _2), args, kwds);
......@@ -346,7 +346,7 @@ static PyObject* PyBobLearnCost_f_prime
arg = PyList_GET_ITEM(tmp, 0);
}
if (PyNumber_Check(arg) && !(PyArray_Check(arg) || PyBlitzArray_Check(arg)))
if (PyArray_IsAnyScalar(arg))
return apply_scalar(self, s_f_prime_str,
boost::bind(&bob::learn::mlp::Cost::f_prime,
self->cxx, _1, _2), args, kwds);
......@@ -409,7 +409,7 @@ static PyObject* PyBobLearnCost_error
arg = PyList_GET_ITEM(tmp, 0);
}
if (PyNumber_Check(arg) && !(PyArray_Check(arg) || PyBlitzArray_Check(arg)))
if (PyArray_IsAnyScalar(arg))
return apply_scalar(self, s_error_str,
boost::bind(&bob::learn::mlp::Cost::error, self->cxx, _1, _2), args, kwds);
......
......@@ -275,7 +275,7 @@ static PyObject* PyBobLearnMLPMachine_getWeights
static int PyBobLearnMLPMachine_setWeights (PyBobLearnMLPMachineObject* self,
PyObject* weights, void* /*closure*/) {
if (PyNumber_Check(weights)) {
if (PyArray_IsAnyScalar(weights)){
double v = PyFloat_AsDouble(weights);
if (PyErr_Occurred()) return -1;
self->cxx->setWeights(v);
......@@ -361,7 +361,7 @@ static PyObject* PyBobLearnMLPMachine_getBiases
static int PyBobLearnMLPMachine_setBiases (PyBobLearnMLPMachineObject* self,
PyObject* biases, void* /*closure*/) {
if (PyNumber_Check(biases)) {
if (PyArray_IsAnyScalar(biases)){
double v = PyFloat_AsDouble(biases);
if (PyErr_Occurred()) return -1;
self->cxx->setBiases(v);
......@@ -435,7 +435,7 @@ static PyObject* PyBobLearnMLPMachine_getInputSubtraction
static int PyBobLearnMLPMachine_setInputSubtraction
(PyBobLearnMLPMachineObject* self, PyObject* o, void* /*closure*/) {
if (PyNumber_Check(o)) {
if (PyArray_IsAnyScalar(o)){
double v = PyFloat_AsDouble(o);
if (PyErr_Occurred()) return -1;
self->cxx->setInputSubtraction(v);
......@@ -482,7 +482,7 @@ static PyObject* PyBobLearnMLPMachine_getInputDivision
static int PyBobLearnMLPMachine_setInputDivision (PyBobLearnMLPMachineObject* self,
PyObject* o, void* /*closure*/) {
if (PyNumber_Check(o)) {
if (PyArray_IsAnyScalar(o)) {
double v = PyFloat_AsDouble(o);
if (PyErr_Occurred()) return -1;
self->cxx->setInputDivision(v);
......
......@@ -144,6 +144,7 @@ def test_100in_100_10_4out():
X = numpy.random.rand(20,100)
assert numpy.allclose(m(X), pymac.forward(X), rtol=1e-10, atol=1e-15)
def test_resize():
m = Machine((2,3,5,1))
......@@ -175,6 +176,18 @@ def test_resize():
X = numpy.random.rand(10,2)
assert numpy.allclose(m(X), pymac.forward(X), rtol=1e-10, atol=1e-15)
# check that the input subtract and input_divide can be set
m.input_subtract = 4
assert (m.input_subtract == 4).all()
m.input_divide = 2
assert (m.input_divide == 2).all()
m.input_subtract = numpy.arange(2.)
assert m.input_subtract[0] == 0
assert m.input_subtract[1] == 1
m.input_divide = numpy.arange(2.)+1
assert m.input_divide[0] == 1
assert m.input_divide[1] == 2
def test_checks():
# tests if Machines check wrong settings
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment