def test_reflection_pad_1d(self): m = ReflectionPad1d(2) inputs = mnp.arange(8).reshape(1, 2, 4) outputs = m(inputs) expected = Tensor([[[2, 1, 0, 1, 2, 3, 2, 1], [6, 5, 4, 5, 6, 7, 6, 5]]]) assert mnp.array_equal(outputs, expected)
def test_reflection_pad_2d(self): m = ReflectionPad2d(2) inputs = mnp.arange(9).reshape(1, 1, 3, 3) outputs = m(inputs) expected = Tensor([[[[8, 7, 6, 7, 8, 7, 6], [5, 4, 3, 4, 5, 4, 3], [2, 1, 0, 1, 2, 1, 0], [5, 4, 3, 4, 5, 4, 3], [8, 7, 6, 7, 8, 7, 6], [5, 4, 3, 4, 5, 4, 3], [2, 1, 0, 1, 2, 1, 0]]]]) assert mnp.array_equal(outputs, expected)
def test_constant_pad_1d(self): m = ConstantPad1d(2, 3.5) inputs = mnp.arange(8, dtype=mindspore.float32).reshape(1, 2, 4) outputs = m(inputs) print(outputs) expected = Tensor([[[3.5, 3.5, 0., 1., 2., 3., 3.5, 3.5], [3.5, 3.5, 4., 5., 6., 7., 3.5, 3.5]]], mindspore.float32) assert mnp.array_equal(outputs, expected) m = ConstantPad1d((3, 1), 3.5) outputs = m(inputs) expected = Tensor([[[3.5, 3.5, 3.5, 0., 1., 2., 3., 3.5], [3.5, 3.5, 3.5, 4., 5., 6., 7., 3.5]]], mindspore.float32) assert mnp.array_equal(outputs, expected)
def test_reflection_pad_3d(self): m = ReflectionPad3d(1) inputs = mnp.arange(8, dtype=mindspore.float32).reshape(1, 1, 2, 2, 2) print(inputs) outputs = m(inputs) print(outputs) expected = Tensor([[[[[7., 6., 7., 6.], [5., 4., 5., 4.], [7., 6., 7., 6.], [5., 4., 5., 4.]], [[3., 2., 3., 2.], [1., 0., 1., 0.], [3., 2., 3., 2.], [1., 0., 1., 0.]], [[7., 6., 7., 6.], [5., 4., 5., 4.], [7., 6., 7., 6.], [5., 4., 5., 4.]], [[3., 2., 3., 2.], [1., 0., 1., 0.], [3., 2., 3., 2.], [1., 0., 1., 0.]]]]], mindspore.float32) assert mnp.array_equal(outputs, expected)
def test_zero_pad_2d(self): m = ZeroPad2d(2) inputs = mnp.arange(9).reshape(1, 1, 3, 3) outputs = m(inputs) expected = Tensor([[[[0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 1, 2, 0, 0], [0, 0, 3, 4, 5, 0, 0], [0, 0, 6, 7, 8, 0, 0], [0, 0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]]]) assert mnp.array_equal(outputs, expected) # using different paddings for different sides m = ZeroPad2d((1, 1, 2, 0)) outputs = m(inputs) print(outputs) expected = Tensor([[[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 1, 2, 0], [0, 3, 4, 5, 0], [0, 6, 7, 8, 0]]]]) assert mnp.array_equal(outputs, expected)
def test_arange(): actual = onp.arange(10) expected = mnp.arange(10).asnumpy() match_array(actual, expected) actual = onp.arange(0, 10) expected = mnp.arange(0, 10).asnumpy() match_array(actual, expected) actual = onp.arange(start=10) expected = mnp.arange(start=10).asnumpy() match_array(actual, expected) actual = onp.arange(start=10, step=0.1) expected = mnp.arange(start=10, step=0.1).asnumpy() match_array(actual, expected, error=6) actual = onp.arange(10, step=0.1) expected = mnp.arange(10, step=0.1).asnumpy() match_array(actual, expected, error=6) actual = onp.arange(0.1, 9.9) expected = mnp.arange(0.1, 9.9).asnumpy() match_array(actual, expected, error=6)
def construct(self, input_x): shape = input_x.shape dim_size = shape[self.dim] reversed_indexes = mnp.arange(dim_size - 1, -1, -1) output = ops.Gather()(input_x, reversed_indexes, self.dim) return output
def arange(start, stop=None, step=None, dtype=None): return mnp.arange(start, stop, step, dtype)