Skip to content

Commit e7bfc74

Browse files
committed
minor refactoring and version change
1 parent 4d4e6cd commit e7bfc74

File tree

3 files changed

+21
-20
lines changed

3 files changed

+21
-20
lines changed

pymc/NumpyDeterministics.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616

1717
#transformations (broadcasted)
18-
_generic = ['abs', 'exp', 'log', 'sqrt','expm1', 'log1p]
18+
_generic = ['abs', 'exp', 'log', 'sqrt','expm1', 'log1p']
1919
_trig = ['sin', 'cos', 'tan', 'arcsin', 'arccos', 'arctan']
2020
_hyp_trig = ['sinh', 'cosh', 'tanh', 'arcsinh', 'arccosh', 'arctanh']
2121
_transformation_deterministics = _generic + _trig + _hyp_trig

pymc/PyMCObjects.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,21 @@ def set_value(self,value):
437437

438438
value = property(fget = get_value, fset=set_value, doc="Self's value computed from current values of parents.")
439439

440+
def apply_jacobian(self, parameter, variable, gradient):
441+
try :
442+
jacobian_func = self._jacobians[parameter]
443+
except KeyError:
444+
raise NotImplementedError(repr(self) + " has no jacobian function for parameter " + parameter)
445+
446+
jacobian = jacobian_func.get()
447+
448+
449+
mapping = self._jacobian_formats.get(parameter, 'full')
450+
451+
452+
return self._format_mapping[mapping](self, variable, jacobian, gradient)
453+
454+
440455
def logp_partial_gradient(self, variable, calculation_set = None):
441456
"""
442457
gets the logp gradient of this deterministic with respect to variable
@@ -446,28 +461,15 @@ def logp_partial_gradient(self, variable, calculation_set = None):
446461

447462
if not (variable.dtype in float_dtypes and self.dtype in float_dtypes):
448463
return zeros(shape(variable.value))
449-
450-
#gradient = 0
451464

452465
# loop through all the parameters and add up all the gradients of log p with respect to the approrpiate variable
453466
gradient = __builtin__.sum([child.logp_partial_gradient(self, calculation_set) for child in self.children ])
454467

455468
totalGradient = 0
456469
for parameter, value in self.parents.iteritems():
457470
if value is variable:
458-
459-
try :
460-
jacobian_func = self._jacobians[parameter]
461-
except KeyError:
462-
raise NotImplementedError(repr(self) + " has no jacobian function for parameter " + parameter)
463-
464-
jacobian = jacobian_func.get()
465-
466-
467-
mapping = self._jacobian_formats.get(parameter, 'full')
468-
469471

470-
totalGradient += self._format_mapping[mapping](self, variable, jacobian, gradient)
472+
totalGradient += self.apply_gradient(parameter, variable, gradient )
471473

472474
return np.reshape(totalGradient, shape(variable.value))
473475

@@ -478,20 +480,20 @@ def full_jacobian(self, variable, jacobian, gradient):
478480
def transformation_operation_jacobian(self, variable, jacobian, gradient):
479481
return jacobian * gradient
480482

481-
_bog_history = {}
483+
_BO_history = {}
482484
def broadcast_operation_jacobian(self, variable, jacobian, gradient):
483485

484486
tgradient = jacobian * gradient
485487
try :
486-
axes, lx = self._bog_history[id(variable)]
488+
axes, lx = self._BO_history[id(variable)]
487489
except KeyError:
488490
sshape = array(shape(self.value))
489491
vshape = zeros(sshape.size)
490492
vshape[0:ndim(variable.value)] += array(shape(variable.value))
491493
axes = np.where(sshape != vshape)
492494
lx = size(axes)
493495

494-
self._bog_history[id(variable)] = (axes, lx )
496+
self._BO_history[id(variable)] = (axes, lx )
495497

496498
if lx > 0:
497499
return np.apply_over_axes(np.sum, tgradient, axes)
@@ -506,7 +508,6 @@ def accumulation_operation_jacobian(self, variable, jacobian, gradient):
506508
return gradient * jacobian
507509

508510
def index_operation_jacobian(self, variable, jacobian, gradient):
509-
#index = jacobian
510511
derivative = zeros(variable.shape)
511512
derivative[jacobian] = gradient
512513
return derivative

pymc/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
1111
"""
1212

13-
__version__ = '2.1alpha'
13+
__version__ = '2.2grad'
1414

1515
try:
1616
import numpy

0 commit comments

Comments
 (0)