attributions = self.get_symbolic_attribution()
gradient = None
for alpha in list(np.linspace(1. / self.steps, 1.0, self.steps)):
xs_mod = (np.array(self.xs) * alpha).tolist()
_attr = self.session_run(attributions, xs_mod)
if gradient is None: gradient = np.array(_attr)
else: gradient += np.array(_attr)
results = gradient * (np.array(self.xs) - np.array(self.baseline)) / self.steps
return results[0] if not self.has_multiple_inputs else results
After Change
attributions = self.get_symbolic_attribution()
gradient = None
for alpha in list(np.linspace(1. / self.steps, 1.0, self.steps)):
xs_mod = [xs * alpha for xs in self.xs] if self.has_multiple_inputs else self.xs * alpha
_attr = self.session_run(attributions, xs_mod)
if gradient is None: gradient = _attr
else: gradient = [g + a for g, a in zip(gradient, _attr)]
results = [g * (x - b) / self.steps for g, x, b in zip(
gradient,
self.xs if self.has_multiple_inputs else [self.xs],
self.baseline if self.has_multiple_inputs else [self.baseline])]