-
Notifications
You must be signed in to change notification settings - Fork 254
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Filter gradients of IndexedSlices in tf grad sim backend, fixes #828 #829
Changes from 19 commits
f15d7a8
1fd9c38
3dbc6c6
ab16515
87d4ca7
6cad12c
12937d8
219a6a0
764bc56
4a89f48
e77f67f
efde102
22dc19a
557af3b
b9c8f61
6015d50
a3270be
188e1d3
2af3367
a6e437c
ab23c05
e95aaf1
6704c64
876ea61
bf7fad3
0e0c919
09a1674
f443185
8a99be5
f69227b
42cf904
57d04b4
f72d197
308b51f
4bd1f20
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -45,7 +45,7 @@ def __init__(self, | |
device: 'Union[int, str, torch.device, None]' = None, | ||
verbose: bool = False, | ||
): | ||
"""GradientSimilarity explainer. | ||
"""``GradientSimilarity`` explainer. | ||
jklaise marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
The gradient similarity explainer is used to find examples in the training data that the predictor considers | ||
similar to test instances the user wants to explain. It uses the gradients of the loss between the model output | ||
|
@@ -129,21 +129,22 @@ def __init__(self, | |
task_name=task | ||
) | ||
|
||
non_trainable_layers = self.backend.get_non_trainable(self.predictor) | ||
non_trainable_layers = self.backend._get_non_trainable(self.predictor) | ||
if non_trainable_layers: | ||
layers_msg = 'The following layers are not trainable: ' | ||
layers_msg = 'The following tensors are not trainable: ' | ||
layers = ", ".join([f"'{layer}'" for layer in non_trainable_layers if layer is not None]) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think eager tensors don't have names... Although I've forgotten to handle them properly here as I'm enumerating and returning the layer index instead of the name. Ill need to check how that behaves though! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't been able to figure out this behaviour. I've also added an error message for the case where no parameter in the model is trainable. |
||
warning_msg = ("Some layers in the model are not trainable. These layer gradients will not be " | ||
f"included in the computation of gradient similarity. {layers_msg}{layers}") | ||
warnings.warn(warning_msg) | ||
warning_msg = ("Some layers in the model are not trainable. These layers don't have gradients " | ||
"and will not be included in the computation of gradient similarity. " | ||
f"{layers_msg}{layers}") | ||
warnings.warn(warning_msg) # todo: scope warning to this location | ||
|
||
def fit(self, | ||
X_train: np.ndarray, | ||
Y_train: np.ndarray) -> "Explainer": | ||
"""Fit the explainer. | ||
|
||
The GradientSimilarity explainer requires the model gradients over the training data. In the explain method it | ||
compares them to the model gradients for the test instance(s). If ``store_grads`` was set to ``True`` on | ||
The ``GradientSimilarity`` explainer requires the model gradients over the training data. In the explain method | ||
it compares them to the model gradients for the test instance(s). If ``store_grads`` was set to ``True`` on | ||
jklaise marked this conversation as resolved.
Show resolved
Hide resolved
jklaise marked this conversation as resolved.
Show resolved
Hide resolved
|
||
initialization then the gradients are precomputed here and stored. This will speed up the explain method call | ||
but storing the gradients may not be feasible for large models. | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
grad
type should probably be a union here since it can be sparse?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore this, was thinking of the
tensorflow
backend.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No your correct, torch also has
SparseTensors
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ignore me, torch handles sparse tensors by setting a layout attribute on normal tensors!