take auto sum when output_var.size is not 1.
+import chainer
+from chainer import functions
from chainer_chemistry.saliency.calculator.base_calculator import BaseCalculator # NOQA
@@ -47,6 +49,13 @@ class GradientCalculator(BaseCalculator):
target_var.grad = None # Need to reset grad beforehand of backward.
output_var = self.get_output_var(outputs)
+ # --- type check for output_var ---
+ if output_var.size != 1:
+ self.logger.warning(
+ 'output_var.size is not 1, calculate scalar value. '
+ 'functions.sum is applied.')
+ output_var = functions.sum(output_var)
+
output_var.backward(retain_grad=True)
saliency = target_var.grad
if self.multiply_target:
