forked from allenai/deep_qa
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* added peek across after first noisy or * added peek-across and subtract minimum layer * pylint fixes * Fixes recommended in code review * Added get_config * Fixing a few bugs * Fixed some dtype issues with masks
- Loading branch information
1 parent
7f008e4
commit 0b6a735
Showing
6 changed files
with
126 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
from keras import backend as K | ||
from overrides import overrides | ||
|
||
from deep_qa.layers.masked_layer import MaskedLayer | ||
from deep_qa.tensors.backend import VERY_LARGE_NUMBER | ||
|
||
class SubtractMinimum(MaskedLayer): | ||
''' | ||
This layer is used to normalize across a tensor axis. Normalization is done by finding the | ||
minimum value across the specified axis, and then subtracting that value from all values | ||
(again, across the spcified axis). Note that this also works just fine if you want to find the | ||
minimum across more than one axis. | ||
Inputs: | ||
- A tensor with arbitrary dimension, and a mask of the same shape (currently doesn't | ||
support masks with other shapes). | ||
Output: | ||
- The same tensor, with the minimum across one (or more) of the dimensions subtracted. | ||
Parameters | ||
---------- | ||
axis: int | ||
The axis (or axes) across which to find the minimum. Can be a single int, a list of ints, | ||
or None. We just call `K.min` with this parameter, so anything that's valid there works | ||
here too. | ||
''' | ||
def __init__(self, axis: int, **kwargs): | ||
self.axis = axis | ||
super(SubtractMinimum, self).__init__(**kwargs) | ||
|
||
@overrides | ||
def compute_output_shape(self, input_shape): # pylint: disable=no-self-use | ||
return input_shape | ||
|
||
@overrides | ||
def compute_mask(self, inputs, mask=None): | ||
return mask | ||
|
||
@overrides | ||
def call(self, inputs, mask=None): | ||
if mask is not None: | ||
mask_value = False if K.dtype(mask) == 'bool' else 0 | ||
# Make sure masked values don't affect the input, by adding a very large number. | ||
mask_flipped_and_scaled = K.cast(K.equal(mask, mask_value), "float32") * VERY_LARGE_NUMBER | ||
minimums = K.min(inputs + mask_flipped_and_scaled, axis=self.axis, keepdims=True) | ||
else: | ||
minimums = K.min(inputs, axis=self.axis, keepdims=True) | ||
normalized = inputs - minimums | ||
return normalized | ||
|
||
@overrides | ||
def get_config(self): | ||
base_config = super(SubtractMinimum, self).get_config() | ||
config = {'axis': self.axis} | ||
config.update(base_config) | ||
return config |
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,44 @@ | ||
# pylint: disable=no-self-use | ||
import numpy as np | ||
from numpy.testing import assert_array_almost_equal | ||
|
||
from keras.layers import Input | ||
from keras.models import Model | ||
from deep_qa.layers.subtract_minimum import SubtractMinimum | ||
from deep_qa.layers.backend.add_mask import AddMask | ||
from ..common.test_case import DeepQaTestCase | ||
|
||
|
||
class TestSubtractMinimum(DeepQaTestCase): | ||
def test_general_case(self): | ||
|
||
input_layer = Input(shape=(4, 3,), dtype='float32', name="input") | ||
subtract_minimum_layer = SubtractMinimum(axis=1) | ||
normalized_input = subtract_minimum_layer(input_layer) | ||
|
||
model = Model([input_layer], normalized_input) | ||
# Testing general unmasked 1D case. | ||
unnormalized_tensor = np.array([[[0.1, 0.1, 0.1], | ||
[0.2, 0.3, 0.4], | ||
[0.5, 0.4, 0.6], | ||
[0.5, 0.4, 0.6]]]) | ||
result = model.predict([unnormalized_tensor]) | ||
|
||
assert_array_almost_equal(result, np.array([[[0.0, 0.0, 0.0], | ||
[0.1, 0.2, 0.3], | ||
[0.4, 0.3, 0.5], | ||
[0.4, 0.3, 0.5]]])) | ||
|
||
# Testing masked batched case. | ||
# By setting the mast value to 0.1. should ignore this value when deciding the minimum | ||
mask_layer = AddMask(mask_value=0.1) | ||
masked_input = mask_layer(input_layer) | ||
normalized_masked_input = subtract_minimum_layer(masked_input) | ||
masking_model = Model([input_layer], normalized_masked_input) | ||
|
||
masked_result = masking_model.predict([unnormalized_tensor]) | ||
|
||
assert_array_almost_equal(masked_result, np.array([[[-0.1, -0.2, -0.3], | ||
[0.0, 0.0, 0.0], | ||
[0.3, 0.1, 0.2], | ||
[0.3, 0.1, 0.2]]])) |
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