def test_reshape_with_batch_and_time_ranks_with_folding_and_explicit_unfolding( self): time_rank = 8 in_space = FloatBox(shape=(2, 3), add_batch_rank=True, add_time_rank=True, time_major=True) reshape_fold = ReShape(fold_time_rank=True, scope="fold-time-rank") reshape_unfold = ReShape(unfold_time_rank=time_rank, scope="unfold-time-rank", time_major=True) def custom_call(self_, inputs): folded = reshape_fold.call(inputs) unfolded = reshape_unfold.call( folded) # no need for orig input here as unfolding is explicit return unfolded stack = Stack(reshape_fold, reshape_unfold, api_methods={("call", custom_call)}) test = ComponentTest(component=stack, input_spaces=dict(inputs=in_space)) # seq-len=time_rank, batch-size=n inputs = in_space.sample(size=(time_rank, 12)) test.test(("call", inputs), expected_outputs=inputs)
def test_reshape_with_batch_and_time_ranks_with_folding_and_unfolding_0D_shape( self): # Flip time and batch rank via folding, then unfolding. in_space = FloatBox(shape=(), add_batch_rank=True, add_time_rank=True, time_major=True) reshape_fold = ReShape(fold_time_rank=True, scope="fold-time-rank") reshape_unfold = ReShape(unfold_time_rank=True, scope="unfold-time-rank", time_major=True) def custom_apply(self_, inputs): folded = reshape_fold.apply(inputs) unfolded = reshape_unfold.apply(folded, inputs) return unfolded stack = Stack(reshape_fold, reshape_unfold, api_methods={("apply", custom_apply)}) test = ComponentTest(component=stack, input_spaces=dict(inputs=in_space)) # seq-len=16, batch-size=8 inputs = in_space.sample(size=(16, 8)) test.test(("apply", inputs), expected_outputs=inputs)
def test_reshape_with_batch_and_time_ranks_and_with_folding_and_unfolding( self): # Flip time and batch rank via folding, then unfolding. in_space = FloatBox(shape=(3, 2), add_batch_rank=True, add_time_rank=True, time_major=False) reshape_fold = ReShape(fold_time_rank=True) reshape_unfold = ReShape(unfold_time_rank=True, time_major=False) def custom_call(self_, inputs): folded = reshape_fold.call(inputs) unfolded = reshape_unfold.call(folded, inputs) return unfolded stack = Stack(reshape_fold, reshape_unfold, api_methods={("call", custom_call)}) test = ComponentTest(component=stack, input_spaces=dict(inputs=in_space)) # batch-size=4, seq-len=2 inputs = in_space.sample(size=(4, 2)) test.test(("call", inputs), expected_outputs=inputs)
def test_reshape_with_batch_vs_time_flipping_with_folding_and_unfolding( self): # Flip time and batch rank via folding, then unfolding. in_space = FloatBox(shape=(3, 2), add_batch_rank=True, add_time_rank=True, time_major=False) reshape_fold = ReShape(fold_time_rank=True, scope="fold-time-rank") reshape_unfold = ReShape(unfold_time_rank=True, flip_batch_and_time_rank=True, scope="unfold-time-rank-with-flip", time_major=True) def custom_apply(self, inputs): folded = reshape_fold.apply(inputs) unfolded = reshape_unfold.apply(folded, inputs) return unfolded stack = Stack(reshape_fold, reshape_unfold, api_methods={("apply", custom_apply)}) test = ComponentTest(component=stack, input_spaces=dict(inputs=in_space)) #test.test("reset") # batch-size=4, seq-len=2 inputs = in_space.sample(size=(4, 2)) # Flip the first two dimensions. expected = np.transpose(inputs, axes=(1, 0, 2, 3)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_time_rank(self): # Test with time-rank instead of batch-rank. in_space = FloatBox(shape=(4, ), add_batch_rank=False, add_time_rank=True) reshape = ReShape(new_shape=(2, 2)) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") inputs = in_space.sample(size=3) expected = np.reshape(inputs, newshape=(3, 2, 2)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape(self): reshape = ReShape(new_shape=(3, 2)) test = ComponentTest( component=reshape, input_spaces=dict( inputs=FloatBox(shape=(6, ), add_batch_rank=True))) test.test("reset") # Batch=2 inputs = np.array([[1, 2, 3, 4, 5, 6], [7, 8, 9, 10, 11, 12]]) expected = np.array([[[1, 2], [3, 4], [5, 6]], [[7, 8], [9, 10], [11, 12]]]) test.test(("call", inputs), expected_outputs=expected)
def test_reshape_with_flatten_option_with_0D_shape(self): # Test flattening int with shape=(). in_space = IntBox(3, shape=(), add_batch_rank=True) reshape = ReShape(flatten=True, flatten_categories=3) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # Time-rank=5, Batch=2 inputs = in_space.sample(size=4) # Expect a by-int-category one-hot flattening. expected = one_hot(inputs, depth=3) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_flatten_option_only_time_rank(self): # Test flattening while leaving batch and time rank as is. in_space = FloatBox(shape=(2, 3), add_batch_rank=False, add_time_rank=True) reshape = ReShape(flatten=True) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # Time-rank=5, Batch=2 inputs = in_space.sample(size=3) expected = np.reshape(inputs, newshape=(3, 6)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_batch_and_time_ranks_and_flattening(self): in_space = FloatBox(shape=(6, 4, 2), add_batch_rank=True, add_time_rank=True, time_major=False) reshape = ReShape(flatten=True) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # batch-size=1, seq-len=3 inputs = in_space.sample(size=(1, 3)) # Reshape without the first two ranks. expected = np.reshape(inputs, newshape=(1, 3, 48)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_time_and_batch_ranks_and_reshaping(self): in_space = FloatBox(shape=(5, 8), add_batch_rank=True, add_time_rank=True, time_major=True) reshape = ReShape(new_shape=(4, 10)) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # seq-len=2, batch-size=4 inputs = in_space.sample(size=(2, 4)) # Reshape without the first two ranks. expected = np.reshape(inputs, newshape=(2, 4, 4, 10)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_flatten_option(self): # Test flattening while leaving batch and time rank as is. in_space = FloatBox(shape=(2, 3, 4), add_batch_rank=True, add_time_rank=True, time_major=True) reshape = ReShape(flatten=True) test = ComponentTest(component=reshape, input_spaces=dict(inputs=in_space)) test.test("reset") # Time-rank=5, Batch=2 inputs = in_space.sample(size=(5, 2)) expected = np.reshape(inputs, newshape=(5, 2, 24)) test.test(("call", inputs), expected_outputs=expected)
def test_reshape_with_time_rank_folding(self): # Fold time rank into batch rank. in_space = FloatBox(shape=(4, 4), add_batch_rank=True, add_time_rank=True, time_major=True) reshape = ReShape(fold_time_rank=True) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # seq-len=3, batch-size=2 inputs = in_space.sample(size=(3, 2)) expected = np.reshape(inputs, newshape=(6, 4, 4)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_flatten_option_with_categories(self): # Test flattening while leaving batch and time rank as is, but flattening out int categories. in_space = IntBox(2, shape=(2, 3, 4), add_batch_rank=True, add_time_rank=True, time_major=False) reshape = ReShape(flatten=True, flatten_categories=2) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # Batch=3, time-rank=5 inputs = in_space.sample(size=(3, 5)) expected = np.reshape(one_hot(inputs, depth=2), newshape=(3, 5, 48)).astype(dtype=np.float32) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_flatten_option_without_categories(self): # Test flattening while leaving batch and time rank as is. in_space = IntBox(3, shape=(2, 3, 4), add_batch_rank=True, add_time_rank=True, time_major=False) reshape = ReShape(flatten=True, flatten_categories=False) test = ComponentTest(component=reshape, input_spaces=dict(inputs=in_space)) test.test("reset") # Batch=3, time-rank=5 inputs = in_space.sample(size=(3, 5)) expected = np.reshape(inputs, newshape=(3, 5, 24)).astype(dtype=np.float32) test.test(("call", inputs), expected_outputs=expected)
def test_reshape_python_with_time_rank_unfolding(self): # Unfold time rank from batch rank with given time-dimension (2 out of 8 -> batch will be 4 after unfolding). in_space = FloatBox(shape=(4, 4), add_batch_rank=True, add_time_rank=False) in_space_before_folding = FloatBox(shape=(4, 4), add_batch_rank=True, add_time_rank=True) reshape = ReShape(unfold_time_rank=True, backend="python") reshape.create_variables( dict(preprocessing_inputs=in_space, input_before_time_rank_folding=in_space_before_folding)) # seq-len=2, batch-size=4 -> unfold from 8. inputs = in_space.sample(size=8) inputs_before_folding = in_space_before_folding.sample(size=(4, 2)) expected = np.reshape(inputs, newshape=(4, 2, 4, 4)) out = reshape._graph_fn_apply(inputs, inputs_before_folding) recursive_assert_almost_equal(out, expected)
def test_reshape_with_batch_vs_time_flipping_and_flattening(self): # Flip time and batch rank AND reshape. in_space = FloatBox(shape=(6, 4, 2), add_batch_rank=True, add_time_rank=True, time_major=False) # If reshaper has a different time-major as input space, we must set the flip_batch_and_time_rank flag. reshape = ReShape(time_major=True, flip_batch_and_time_rank=True, flatten=True) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # batch-size=1, seq-len=3 inputs = in_space.sample(size=(1, 3)) # Flip the first two dimensions. expected = np.transpose(inputs, axes=(1, 0, 2, 3, 4)) # Do the actual reshape (not touching the first two dimensions). expected = np.reshape(expected, newshape=(3, 1, 48)) test.test(("apply", inputs), expected_outputs=expected)
def test_reshape_with_batch_vs_time_flipping_and_reshaping(self): # Flip time and batch rank AND reshape. in_space = FloatBox(shape=(5, 8), add_batch_rank=True, add_time_rank=True, time_major=True) # If time_major of input Space and reshaper are different, we have to set the "flip"-flag. reshape = ReShape(time_major=False, flip_batch_and_time_rank=True, new_shape=(4, 10)) test = ComponentTest(component=reshape, input_spaces=dict(preprocessing_inputs=in_space)) test.test("reset") # seq-len=2, batch-size=4 inputs = in_space.sample(size=(2, 4)) # Flip the first two dimensions. expected = np.transpose(inputs, axes=(1, 0, 2, 3)) # Do the actual reshape (not touching the first two dimensions). expected = np.reshape(expected, newshape=(4, 2, 4, 10)) test.test(("apply", inputs), expected_outputs=expected)
def test_split_graph_on_reshape_flatten(self): space = Dict.from_spec(dict(a=Tuple(FloatBox(shape=(1, 1, 2)), FloatBox(shape=(1, 2, 2))), b=FloatBox(shape=(2, 2, 3)), c=dict(type=float, shape=(2, )), d=IntBox(3)), add_batch_rank=True) flatten = ReShape(flatten=True, flatten_categories={"d": 3}) test = ComponentTest(component=flatten, input_spaces=dict(preprocessing_inputs=space)) input_ = dict(a=(np.array([[[[3.0, 5.0]]], [[[1.0, 5.2]]]]), np.array([[[[3.1, 3.2], [3.3, 3.4]]], [[[3.5, 3.6], [3.7, 3.8]]]])), b=np.array([[[[0.01, 0.02, 0.03], [0.04, 0.05, 0.06]], [[0.07, 0.08, 0.09], [0.10, 0.11, 0.12]]], [[[0.13, 0.14, 0.15], [0.16, 0.17, 0.18]], [[0.19, 0.20, 0.21], [0.22, 0.23, 0.24]]]]), c=np.array([[0.1, 0.2], [0.3, 0.4]]), d=np.array([2, 0])) expected = dict( a=(np.array([[3.0, 5.0], [1.0, 5.2]], dtype=np.float32), np.array([[3.1, 3.2, 3.3, 3.4], [3.5, 3.6, 3.7, 3.8]], dtype=np.float32)), b=np.array([[ 0.01, 0.02, 0.03, 0.04, 0.05, 0.06, 0.07, 0.08, 0.09, 0.10, 0.11, 0.12 ], [ 0.13, 0.14, 0.15, 0.16, 0.17, 0.18, 0.19, 0.20, 0.21, 0.22, 0.23, 0.24 ]]), c=np.array([[0.1, 0.2], [0.3, 0.4]], dtype=np.float32), d=np.array([[0.0, 0.0, 1.0], [1.0, 0.0, 0.0]]) # category (one-hot) flatten ) test.test("reset") test.test(("apply", input_), expected_outputs=expected)
def test_reshape_with_time_rank_unfolding(self): # Unfold time rank from batch rank with given time-dimension (2 out of 8 -> batch will be 4 after unfolding). in_space = FloatBox(shape=(4, 4), add_batch_rank=True, add_time_rank=False) in_space_before_folding = FloatBox(shape=(4, 4), add_batch_rank=True, add_time_rank=True) reshape = ReShape(unfold_time_rank=True) test = ComponentTest( component=reshape, input_spaces=dict( preprocessing_inputs=in_space, input_before_time_rank_folding=in_space_before_folding)) test.test("reset") # seq-len=2, batch-size=4 -> unfold from 8. inputs = in_space.sample(size=8) inputs_before_folding = in_space_before_folding.sample(size=(4, 2)) expected = np.reshape(inputs, newshape=(4, 2, 4, 4)) test.test(("apply", (inputs, inputs_before_folding)), expected_outputs=expected)