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.
Merge pull request #2 from allenai/master
Merge from upstream
- Loading branch information
Showing
13 changed files
with
155 additions
and
69 deletions.
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
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,63 @@ | ||
from keras import backend as K | ||
from overrides import overrides | ||
|
||
from ..masked_layer import MaskedLayer | ||
|
||
|
||
class RepeatLike(MaskedLayer): | ||
""" | ||
This ``Layer`` is like :class:`~.repeat.Repeat`, but gets the number of repetitions to use from | ||
a second input tensor. This allows doing a number of repetitions that is unknown at graph | ||
compilation time, and is necessary when the ``repetitions`` argument to ``Repeat`` would be | ||
``None``. | ||
If the mask is not ``None``, we must be able to call ``K.expand_dims`` using the same axis | ||
parameter as we do for the input. | ||
Input: | ||
- A tensor of arbitrary shape, which we will expand and tile. | ||
- A second tensor whose shape along one dimension we will copy | ||
Output: | ||
- The input tensor repeated along one of the dimensions. | ||
Parameters | ||
---------- | ||
axis: int | ||
We will add a dimension to the input tensor at this axis. | ||
copy_from_axis: int | ||
We will copy the dimension from the second tensor at this axis. | ||
""" | ||
def __init__(self, axis: int, copy_from_axis: int, **kwargs): | ||
self.axis = axis | ||
self.copy_from_axis = copy_from_axis | ||
super(RepeatLike, self).__init__(**kwargs) | ||
|
||
@overrides | ||
def compute_mask(self, inputs, mask=None): | ||
# pylint: disable=unused-argument | ||
if mask is None or mask[0] is None: | ||
return None | ||
return self.__repeat_tensor(mask[0], inputs[1]) | ||
|
||
@overrides | ||
def compute_output_shape(self, input_shape): | ||
return input_shape[0][:self.axis] + (input_shape[1][self.copy_from_axis],) + input_shape[0][self.axis:] | ||
|
||
@overrides | ||
def call(self, inputs, mask=None): | ||
return self.__repeat_tensor(inputs[0], inputs[1]) | ||
|
||
def __repeat_tensor(self, to_repeat, to_copy): | ||
expanded = K.expand_dims(to_repeat, self.axis) | ||
ones = [1] * K.ndim(expanded) | ||
num_repetitions = K.shape(to_copy)[self.copy_from_axis] | ||
tile_shape = K.concatenate([ones[:self.axis], [num_repetitions], ones[self.axis+1:]], 0) | ||
return K.tile(expanded, tile_shape) | ||
|
||
@overrides | ||
def get_config(self): | ||
base_config = super(RepeatLike, self).get_config() | ||
config = {'axis': self.axis, 'copy_from_axis': self.copy_from_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
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
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,23 @@ | ||
# pylint: disable=no-self-use,invalid-name | ||
|
||
import numpy | ||
from keras.layers import Input | ||
from keras.models import Model | ||
|
||
from deep_qa.layers.backend import RepeatLike | ||
|
||
class TestRepeatLikeLayer: | ||
def test_call_works_on_simple_input(self): | ||
batch_size = 2 | ||
input_length = 3 | ||
repetitions = 4 | ||
input_layer = Input(shape=(input_length,), dtype='float32') | ||
input_layer_2 = Input(shape=(None,), dtype='float32') | ||
repeat_output = RepeatLike(axis=1, copy_from_axis=1)([input_layer, input_layer_2]) | ||
model = Model(inputs=[input_layer, input_layer_2], outputs=[repeat_output]) | ||
input_tensor = numpy.asarray([[2, 5, 3], [-1, -4, -2]]) | ||
input_tensor_2 = numpy.ones((batch_size, repetitions)) | ||
repeat_tensor = model.predict([input_tensor, input_tensor_2]) | ||
assert repeat_tensor.shape == (batch_size, repetitions, input_length) | ||
for i in range(repetitions): | ||
numpy.testing.assert_almost_equal(repeat_tensor[:, i, :], [[2, 5, 3], [-1, -4, -2]]) |