-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
414 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
from keras import Layer | ||
from keras import ops | ||
|
||
|
||
class ExplanationSparsityRegularization(Layer): | ||
|
||
def __init__(self, | ||
factor: float = 1.0, | ||
**kwargs): | ||
super(ExplanationSparsityRegularization, self).__init__(**kwargs) | ||
self.factor = factor | ||
|
||
def build(self, input_shape): | ||
super(ExplanationSparsityRegularization, self).build(input_shape) | ||
|
||
def call(self, inputs, **kwargs): | ||
r"""Computes a loss from importance scores. | ||
Args: | ||
inputs: Importance tensor of shape ([batch], [N], K) . | ||
Returns: | ||
None. | ||
""" | ||
# importances: ([batch], [N], K) | ||
importances = inputs | ||
|
||
loss = ops.mean(ops.abs(importances)) | ||
loss = loss * self.factor | ||
self.add_loss(loss) | ||
return loss |
Empty file.
Oops, something went wrong.