Permalink
Browse files

Fix LinearMachine behavior after commit fb11967; Closes #53

  • Loading branch information...
1 parent ea46fb4 commit ecfde98fa83c2799bbe0762d523593c83e03f1de @anjos anjos committed May 3, 2012
Showing with 50 additions and 0 deletions.
  1. +48 −0 python/machine/lib/test/linear.py
  2. +2 −0 python/machine/src/linear.cc
@@ -99,11 +99,59 @@ def presumed(ivalue):
[12,0,0],
]
+ # 1D case
maxerr = numpy.ndarray((2,), 'float64')
maxerr.fill(1e-10)
for k in testing:
input = numpy.array(k, 'float64')
self.assertTrue ( (abs(presumed(input) - m(input)) < maxerr).all() )
+ # 2D case
+ output = m(testing)
+ for i, k in enumerate(testing):
+ input = numpy.array(k, 'float64')
+ self.assertTrue ( (abs(presumed(input) - output[i,:]) < maxerr).all() )
+
+ def test03_UserAllocation(self):
+
+ # Tests the correctness of a linear machine
+ c = bob.io.HDF5File(MACHINE)
+ m = bob.machine.LinearMachine(c)
+
+ def presumed(ivalue):
+ """Calculates, by hand, the presumed output given the input"""
+
+ # These are the supposed preloaded values from the file "MACHINE"
+ isub = numpy.array([0., 0.5, 0.5], 'float64')
+ idiv = numpy.array([0.5, 1.0, 1.0], 'float64')
+ w = numpy.array([[0.4, 0.4, 0.2], [0.1, 0.2, 0.7]], 'float64')
+ b = numpy.array([0.3, -3.0], 'float64')
+ act = math.tanh
+
+ return numpy.array([ act((w[i,:]*((ivalue-isub)/idiv)).sum() + b[i]) for i in range(w.shape[0]) ], 'float64')
+
+ testing = [
+ [1,1,1],
+ [0.5,0.2,200],
+ [-27,35.77,0],
+ [12,0,0],
+ ]
+
+ # 1D case
+ maxerr = numpy.ndarray((2,), 'float64')
+ maxerr.fill(1e-10)
+ output = numpy.ndarray((2,), 'float64')
+ for k in testing:
+ input = numpy.array(k, 'float64')
+ m(input, output)
+ self.assertTrue ( (abs(presumed(input) - output) < maxerr).all() )
+
+ # 2D case
+ output = numpy.ndarray((len(testing), 2), 'float64')
+ m(testing, output)
+ for i, k in enumerate(testing):
+ input = numpy.array(k, 'float64')
+ self.assertTrue ( (abs(presumed(input) - output[i,:]) < maxerr).all() )
+
# Instantiates our standard main module for unittests
main = bob.helper.unittest_main(MachineTest)
@@ -74,6 +74,7 @@ static void forward2(const mach::LinearMachine& m, tp::const_ndarray input,
blitz::Array<double,1> output_ = output.bz<double,1>();
m.forward(input.bz<double,1>(), output_);
}
+ break;
case 2:
{
blitz::Array<double,2> input_ = input.bz<double,2>();
@@ -85,6 +86,7 @@ static void forward2(const mach::LinearMachine& m, tp::const_ndarray input,
m.forward(i_, o_);
}
}
+ break;
default:
PYTHON_ERROR(TypeError, "cannot forward arrays of type '%s'", info.str().c_str());
}

0 comments on commit ecfde98

Please sign in to comment.