def test_leapfrog_method_example(self): hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = np.array([.02,.02]) # Fake gradient calc weights = np.array([.1,.2]) # Fake initialized weights momentum = np.array([.01,.02]) # Fake initial momentum gradient = np.array([.01,.01]) # Another fake gradient momentum = hmc.leapfrog_method(momentum,weights,gradient)
def test_leapfrog_method_must_call_grad_obj_count(self,mock_randint): mock_randint.return_value = 100 hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = np.array([.02,.02]) weights = np.array([.1,.2]) momentum = np.array([.01,.02]) gradient = np.array([.01,.01]) momentum = hmc.leapfrog_method(momentum,weights,gradient) actual = hmc.grad_obj.call_count expected = 100 assert(actual == expected)
def test_leapfrog_method_randomly_generates_tau(self,mock_randint): """ Make sure the number of gradient calculations is randomly chosen. """ hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = np.array([.02,.02]) weights = np.array([.1,.2]) momentum = np.array([.01,.02]) gradient = np.array([.01,.01]) momentum = hmc.leapfrog_method(momentum,weights,gradient) assert(mock_randint.called_once)
def test_leapfrog_method_args_must_be_np_arrays(self): """ When the args aren't numpy arrays, it should fail.""" with nose.tools.assert_raises(TypeError) as te: hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = [.02,.02] weights = np.array([.1,.2]) momentum = np.array([.01,.02]) gradient = np.array([.01,.01]) momentum = hmc.leapfrog_method(momentum,weights,gradient) expected = 'can\'t multiply sequence by non-int of type \'float\'' actual = str(te.exception) assert(actual == expected)
def test_leapfrog_method_return_array_correct_dimensions(self): """ The return value of leapfrog_method should be 1 row with an element for each input column. """ hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = np.array([.02,.02,.4]) weights = np.array([.1,.2,.2]) momentum = np.array([.01,.02,.1]) gradient = np.array([.01,.01,.1]) momentum = hmc.leapfrog_method(momentum,weights,gradient) actual = len(momentum) expected = 3 assert(actual==expected)
def test_leapfrog_method_return_value_must_be_np_array(self): """ The return value of leapfrog_method should be a numpy array; If this were simply an array, a type error would be raised once I multiply momentum by .5 (which is exactly what happens next in the training algorithm). """ hmc = HMCNeuron('fake_arg','fake_arg','fake_arg') hmc.grad_obj = mock.Mock() hmc.grad_obj.return_value = np.array([.02,.02]) weights = np.array([.1,.2]) momentum = np.array([.01,.02]) gradient = np.array([1,1]) momentum = hmc.leapfrog_method(momentum,weights,gradient) momentum * .5