|
|
|
@ -18,6 +18,7 @@ import unittest
|
|
|
|
|
import numpy as np
|
|
|
|
|
|
|
|
|
|
from op_test import OpTest
|
|
|
|
|
import paddle
|
|
|
|
|
import paddle.fluid as fluid
|
|
|
|
|
from paddle.fluid import compiler, Program, program_guard
|
|
|
|
|
|
|
|
|
@ -227,35 +228,43 @@ class TestReshapeUint8Op(TestReshapeInt8Op):
|
|
|
|
|
|
|
|
|
|
# Test python API
|
|
|
|
|
class TestReshapeAPI(unittest.TestCase):
|
|
|
|
|
# situation 1: have shape( list, no tensor), no actual shape(Tensor)
|
|
|
|
|
def test_1(self):
|
|
|
|
|
def _set_paddle_api(self):
|
|
|
|
|
self.fill_constant = paddle.fill_constant
|
|
|
|
|
self.data = paddle.data
|
|
|
|
|
self.reshape = paddle.reshape
|
|
|
|
|
self.to_tensor = paddle.to_tensor
|
|
|
|
|
|
|
|
|
|
def _set_fluid_api(self):
|
|
|
|
|
self.fill_constant = fluid.layers.fill_constant
|
|
|
|
|
self.data = fluid.data
|
|
|
|
|
self.reshape = fluid.layers.reshape
|
|
|
|
|
|
|
|
|
|
def _test_api(self):
|
|
|
|
|
input = np.random.random([2, 25]).astype("float32")
|
|
|
|
|
shape = [2, 5, 5]
|
|
|
|
|
positive_five = fluid.layers.fill_constant([1], "int32", 5)
|
|
|
|
|
x = fluid.layers.data(
|
|
|
|
|
name="x", shape=[2, 25], append_batch_size=False, dtype="float32")
|
|
|
|
|
main_prog = Program()
|
|
|
|
|
with program_guard(main_prog, Program()):
|
|
|
|
|
positive_five = self.fill_constant([1], "int32", 5)
|
|
|
|
|
x = self.data(name="x", shape=[2, 25], dtype="float32")
|
|
|
|
|
|
|
|
|
|
actual_shape = fluid.layers.data(
|
|
|
|
|
name="shape",
|
|
|
|
|
shape=[1, 3],
|
|
|
|
|
append_batch_size=False,
|
|
|
|
|
dtype="float32")
|
|
|
|
|
actual_shape = self.data(name="shape", shape=[3], dtype="int32")
|
|
|
|
|
|
|
|
|
|
# situation 1: have shape( list, no tensor), no actual shape(Tensor)
|
|
|
|
|
out_1 = fluid.layers.reshape(x, shape)
|
|
|
|
|
out_1 = self.reshape(x, shape)
|
|
|
|
|
|
|
|
|
|
# situation 2: have shape(list, no tensor), have actual shape(Tensor)
|
|
|
|
|
out_2 = fluid.layers.reshape(x, shape=shape, actual_shape=actual_shape)
|
|
|
|
|
out_2 = fluid.layers.reshape(
|
|
|
|
|
x, shape=shape, actual_shape=actual_shape)
|
|
|
|
|
|
|
|
|
|
# Situation 3: have shape(list, have tensor), no actual shape(Tensor)
|
|
|
|
|
out_3 = fluid.layers.reshape(x, shape=[positive_five, 10])
|
|
|
|
|
out_3 = self.reshape(x, shape=[positive_five, 10])
|
|
|
|
|
|
|
|
|
|
# Situation 4: have shape(Tensor), no actual shape(Tensor)
|
|
|
|
|
out_4 = fluid.layers.reshape(x, shape=actual_shape)
|
|
|
|
|
out_4 = self.reshape(x, shape=actual_shape)
|
|
|
|
|
|
|
|
|
|
exe = fluid.Executor(place=fluid.CPUPlace())
|
|
|
|
|
res_1, res_2, res_3, res_4 = exe.run(
|
|
|
|
|
fluid.default_main_program(),
|
|
|
|
|
main_prog,
|
|
|
|
|
feed={"x": input,
|
|
|
|
|
"shape": np.array([2, 5, 5]).astype("int32")},
|
|
|
|
|
fetch_list=[out_1, out_2, out_3, out_4])
|
|
|
|
@ -265,76 +274,108 @@ class TestReshapeAPI(unittest.TestCase):
|
|
|
|
|
assert np.array_equal(res_3, input.reshape([5, 10]))
|
|
|
|
|
assert np.array_equal(res_4, input.reshape(shape))
|
|
|
|
|
|
|
|
|
|
def test_paddle_api(self):
|
|
|
|
|
self._set_paddle_api()
|
|
|
|
|
self._test_api()
|
|
|
|
|
|
|
|
|
|
def test_fluid_api(self):
|
|
|
|
|
self._set_fluid_api()
|
|
|
|
|
self._test_api()
|
|
|
|
|
|
|
|
|
|
def test_imperative(self):
|
|
|
|
|
self._set_paddle_api()
|
|
|
|
|
input = np.random.random([2, 25]).astype("float32")
|
|
|
|
|
shape = [2, 5, 5]
|
|
|
|
|
with fluid.dygraph.guard():
|
|
|
|
|
x = self.to_tensor(input)
|
|
|
|
|
positive_five = self.fill_constant([1], "int32", 5)
|
|
|
|
|
|
|
|
|
|
out_1 = self.reshape(x, shape)
|
|
|
|
|
|
|
|
|
|
out_2 = self.reshape(x, shape=[positive_five, 10])
|
|
|
|
|
|
|
|
|
|
shape_tensor = self.to_tensor(np.array([2, 5, 5]).astype("int32"))
|
|
|
|
|
out_3 = self.reshape(x, shape=shape_tensor)
|
|
|
|
|
|
|
|
|
|
assert np.array_equal(out_1.numpy(), input.reshape(shape))
|
|
|
|
|
assert np.array_equal(out_2.numpy(), input.reshape([5, 10]))
|
|
|
|
|
assert np.array_equal(out_3.numpy(), input.reshape(shape))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# Test Input Error
|
|
|
|
|
class TestReshapeOpError(unittest.TestCase):
|
|
|
|
|
def test_errors(self):
|
|
|
|
|
def _set_paddle_api(self):
|
|
|
|
|
self.data = paddle.data
|
|
|
|
|
self.reshape = paddle.reshape
|
|
|
|
|
|
|
|
|
|
def _set_fluid_api(self):
|
|
|
|
|
self.data = fluid.data
|
|
|
|
|
self.reshape = fluid.layers.reshape
|
|
|
|
|
|
|
|
|
|
def _test_errors(self):
|
|
|
|
|
with program_guard(Program(), Program()):
|
|
|
|
|
# The x type of reshape_op must be Variable.
|
|
|
|
|
def test_x_type():
|
|
|
|
|
x1 = fluid.create_lod_tensor(
|
|
|
|
|
np.array([[-1]]), [[1]], fluid.CPUPlace())
|
|
|
|
|
fluid.layers.reshape(x1, shape=[1])
|
|
|
|
|
self.reshape(x1, shape=[1])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_x_type)
|
|
|
|
|
|
|
|
|
|
# The x dtype of reshape_op must be float16, float32, float64, int32 or int64.
|
|
|
|
|
def test_x_dtype():
|
|
|
|
|
x2 = fluid.layers.data(
|
|
|
|
|
name="x2",
|
|
|
|
|
shape=[2, 25],
|
|
|
|
|
append_batch_size=False,
|
|
|
|
|
dtype="bool")
|
|
|
|
|
fluid.layers.reshape(x2, shape=[2, 5, 5])
|
|
|
|
|
x2 = self.data(name="x2", shape=[2, 25], dtype="bool")
|
|
|
|
|
self.reshape(x2, shape=[2, 5, 5])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_x_dtype)
|
|
|
|
|
|
|
|
|
|
def test_x_dtype_float16():
|
|
|
|
|
x_float16 = fluid.layers.data(
|
|
|
|
|
name="x_float16",
|
|
|
|
|
shape=[2, 25],
|
|
|
|
|
append_batch_size=False,
|
|
|
|
|
dtype="float16")
|
|
|
|
|
fluid.layers.reshape(x_float16, shape=[2, 5, 5])
|
|
|
|
|
x_float16 = self.data(
|
|
|
|
|
name="x_float16", shape=[2, 25], dtype="float16")
|
|
|
|
|
self.reshape(x_float16, shape=[2, 5, 5])
|
|
|
|
|
|
|
|
|
|
test_x_dtype_float16()
|
|
|
|
|
|
|
|
|
|
x3 = fluid.layers.data(
|
|
|
|
|
name="x3",
|
|
|
|
|
shape=[2, 25],
|
|
|
|
|
append_batch_size=False,
|
|
|
|
|
dtype="float32")
|
|
|
|
|
x3 = self.data(name="x3", shape=[2, 25], dtype="float32")
|
|
|
|
|
|
|
|
|
|
# The argument shape's type of reshape_op must be list, tuple or Variable.
|
|
|
|
|
def test_shape_type():
|
|
|
|
|
fluid.layers.reshape(x3, shape=1)
|
|
|
|
|
self.reshape(x3, shape=1)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_shape_type)
|
|
|
|
|
|
|
|
|
|
# The argument actual_shape's type of reshape_op must be Variable or None.
|
|
|
|
|
def test_actual_shape_type():
|
|
|
|
|
fluid.layers.reshape(x3, shape=[25, 2], actual_shape=1)
|
|
|
|
|
self.reshape(x3, shape=[25, 2], actual_shape=1)
|
|
|
|
|
|
|
|
|
|
self.assertRaises(TypeError, test_actual_shape_type)
|
|
|
|
|
|
|
|
|
|
# The argument shape have more than one -1.
|
|
|
|
|
def test_shape_1():
|
|
|
|
|
fluid.layers.reshape(x3, shape=[-1, -1, 5])
|
|
|
|
|
self.reshape(x3, shape=[-1, -1, 5])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(AssertionError, test_shape_1)
|
|
|
|
|
|
|
|
|
|
# The argument shape have element 0 whose index exceed the input dimension.
|
|
|
|
|
def test_shape_2():
|
|
|
|
|
fluid.layers.reshape(x3, [2, 5, 5, 0])
|
|
|
|
|
self.reshape(x3, [2, 5, 5, 0])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(AssertionError, test_shape_2)
|
|
|
|
|
|
|
|
|
|
# The argument shape have more than one negative value.
|
|
|
|
|
def test_shape_3():
|
|
|
|
|
fluid.layers.reshape(x3, [-1, -2, 5])
|
|
|
|
|
self.reshape(x3, [-1, -2, 5])
|
|
|
|
|
|
|
|
|
|
self.assertRaises(AssertionError, test_shape_3)
|
|
|
|
|
|
|
|
|
|
def test_paddle_api_error(self):
|
|
|
|
|
self._set_paddle_api()
|
|
|
|
|
self._test_errors()
|
|
|
|
|
|
|
|
|
|
def test_fluid_api_error(self):
|
|
|
|
|
self._set_fluid_api()
|
|
|
|
|
self._test_errors()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
unittest.main()
|
|
|
|
|