Created by: liym27
reshape op增强输入类型检查
-
- 检查类型是否为Variable
-
- 检查数据类型是否为float32,float64, int32, int64
在单测中覆盖类型错误和数据类型错误两个异常情况 可手动运行示例,看下错误:
import paddle.fluid as fluid
x1 = fluid.create_lod_tensor(np.array([[-1]]), [[1]], fluid.CPUPlace())
fluid.layers.reshape(x1, shape=[1])
# TypeError: The type of ‘x’ in reshape must be Variable, but received <class 'paddle.fluid.core_avx.LoDTensor'>
import paddle.fluid as fluid
x2 = fluid.layers.data(
name="x",
shape=[2, 25],
append_batch_size=False,
dtype="float16")
fluid.layers.reshape(x2, shape=[2, 2, 5])
#TypeError: The data type of ‘x’ in reshape must be float32, float64, int32 or int64, but received float16.
reshape op增强维度类报错信息
input = np.random.random([2, 25]).astype("float32")
x = fluid.layers.data(name="x", shape=[2, 25], append_batch_size=False, dtype="float32")
shape = fluid.layers.data(name="shape", shape=[1, 4], append_batch_size=False, dtype="float32")
fluid.layers.reshape(x, shape=shape)
1.shape参数中元素不止一个为-1时
exe.run(fluid.default_main_program(),
feed={"x": input,
"shape": np.array([-1, -1, 5, 1]).astype("int32")})
修改前报错:报错内容表述不清楚,没有打印维度信息 Only one input dimension of Attr(shape) can be unknown.
修改后报错 ShapeError: Only one dimension value of 'shape' in ReshapeOp can be -1. But received shape = [-1, -1, 5, 1], shape[1] is also -1.
2.shape参数中元素为0时,0所对应的维度出错
exe.run(fluid.default_main_program(),
feed={"x": input,
"shape": np.array([2, 5, 5, 0]).astype("int32")})
修改前报错:报错内容表述不清楚,没有打印维度信息 The index of dimension to copy from input shape must be less than the size of input shape.
修改后报错 ShapeError: The index of 0 in
shape
must be less than the input tensor X's dimensions. But received shape = [2, 5, 5, 0], shape[3] = 0, X's shape = [2, 25], X's dimensions = 2.
3.shape参数有-1以外的负值
exe.run(fluid.default_main_program(),
feed={"x": input,
"shape": np.array([-1, -5, 5, 1]).astype("int32")})
修改前报错: Each input dimension of Attr(shape) must not be negtive except one unknown dimension.
修改后报错 ShapeError: Each dimension value of 'shape' in ReshapeOp must not be negtive except one unknown dimension. But received shape = [-1, -5, 5, 1], shape[1] = -5.
4.传入无效的shape值,无法正确推断出-1对应的实际值
exe.run(fluid.default_main_program(),
feed={"x": input,
"shape": np.array([-1, 3, 5, 1]).astype("int32")})
修改前报错: Invalid shape is given.
修改后报错 ShapeError: The 'shape' in ReshapeOp is invalid. The input tensor X'size must be divisible by known capacity of 'shape'. But received X's shape = [2, 25], X's size = 50, 'shape' is [-1, 3, 5, 1], known capacity of 'shape' is -15.
5.传入无效的shape值,输入x的size与 shape不匹配
exe.run(fluid.default_main_program(),
feed={"x": input,
"shape": np.array([1, 3, 5, 1]).astype("int32")})
修改前报错: Invalid shape is given.
修改后报错 ShapeError: The 'shape' in ReshapeOp is invalid. The input tensor X'size must be equal to the capacity of 'shape'. But received X's shape = [2, 25], X's size = 50, 'shape' is [1, 3, 5, 1], the capacity of 'shape' is 15.