    def _register_forward_hook(self, stdev: float):
        """
        Register a forward hook on the embedding layer which adds random noise to every embedding.
        Used for one term in the SmoothGrad sum.
        """

        def forward_hook(module, inputs, output):
            # Random noise = N(0, stdev * (max-min))
            scale = output.detach().max() - output.detach().min()
            noise = torch.randn(output.shape, device=output.device) * stdev * scale

            # Add the random noise
            output.add_(noise)

        # Register the hook
        embedding_layer = self.predictor.get_interpretable_layer()
        handle = embedding_layer.register_forward_hook(forward_hook)
        return handle
Register a forward hook on the embedding layer which adds random noise to every embedding.
Used for one term in the SmoothGrad sum.
allennlp/interpret/saliency_interpreters/smooth_gradient.py