Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_relu_linear_deeplift_batch(self):
model = ReLULinearDeepLiftModel(inplace=True)
x1 = torch.tensor([[-10.0, 1.0, -5.0], [2.0, 3.0, 4.0]], requires_grad=True)
x2 = torch.tensor([[3.0, 3.0, 1.0], [2.3, 5.0, 4.0]], requires_grad=True)
inputs = (x1, x2)
baselines = (torch.zeros(1, 3), torch.rand(1, 3) * 0.001)
# expected = [[[0.0, 0.0]], [[6.0, 2.0]]]
self._deeplift_assert(model, DeepLift(model), inputs, baselines)
def test_reusable_modules(self):
model = BasicModelWithReusableModules()
input = torch.rand(1, 3)
dl = DeepLift(model)
with self.assertRaises(RuntimeError):
dl.attribute(input, target=0)
def test_convnet_with_maxpool3d(self):
input = 100 * torch.randn(2, 1, 10, 10, 10, requires_grad=True)
baseline = 20 * torch.randn(2, 1, 10, 10, 10)
model = BasicModel_ConvNet_MaxPool3d()
dl = DeepLift(model)
self.softmax_classification(model, dl, input, baseline, torch.tensor(2))
def test_relu_deeplift_batch(self):
x1 = torch.tensor([[1.0], [1.0], [1.0], [1.0]], requires_grad=True)
x2 = torch.tensor([[2.0], [2.0], [2.0], [2.0]], requires_grad=True)
b1 = torch.tensor([[0.0], [0.0], [0.0], [0.0]], requires_grad=True)
b2 = torch.tensor([[0.0], [0.0], [0.0], [0.0]], requires_grad=True)
inputs = (x1, x2)
baselines = (b1, b2)
model = ReLUDeepLiftModel()
self._deeplift_assert(model, DeepLift(model), inputs, baselines)
def test_softmax_classification_batch_multi_target(self):
num_in = 40
inputs = torch.arange(0.0, num_in * 3.0, requires_grad=True).reshape(3, num_in)
baselines = torch.arange(1.0, num_in + 1).reshape(1, num_in)
model = SoftmaxDeepLiftModel(num_in, 20, 10)
dl = DeepLift(model)
self.softmax_classification(
model, dl, inputs, baselines, torch.tensor([2, 2, 2])
)
def _deeplift_helper(self, model, inputs, baselines, expected):
dl = DeepLift(model)
# Run attribution multiple times to make sure that it is working as
# expected
for _ in range(5):
model.zero_grad()
attributions = dl.attribute(inputs, baselines)
self.assertEqual(
[attribution.detach().numpy().tolist() for attribution in attributions],
expected,
)
self._assert_attributions(model, inputs, baselines, attributions)
def test_softmax_classification_batch_zero_baseline(self):
num_in = 40
input = torch.arange(0.0, num_in * 3.0, requires_grad=True).reshape(3, num_in)
baselines = 0 * input
model = SoftmaxDeepLiftModel(num_in, 20, 10)
dl = DeepLift(model)
self.softmax_classification(model, dl, input, baselines, torch.tensor(2))
corresponding sized tensors is returned.
Examples::
>>> # ImageClassifier takes a single input tensor of images Nx3x32x32,
>>> # and returns an Nx10 tensor of class probabilities.
>>> net = ImageClassifier()
>>> # creates an instance of LayerDeepLift to interpret target
>>> # class 1 with respect to conv4 layer.
>>> dl = NeuronDeepLift(net, net.conv4)
>>> input = torch.randn(1, 3, 32, 32, requires_grad=True)
>>> # Computes deeplift attribution scores for conv4 layer and neuron
>>> # index (4,1,2).
>>> attribution = dl.attribute(input, (4,1,2))
"""
dl = DeepLift(self.forward_func)
dl.gradient_func = construct_neuron_grad_fn(
self.layer,
neuron_index,
attribute_to_neuron_input=attribute_to_neuron_input,
)
return dl.attribute(
inputs,
baselines,
additional_forward_args=additional_forward_args,
custom_attribution_func=custom_attribution_func,
)
def __init__(self, model):
r"""
Args:
model (nn.Module): The reference to PyTorch model instance.
"""
DeepLift.__init__(self, model)