Created by: chenwhql
dygraph.BatchNorm Python API类型检查增强
当此动态图API在静态图下运行时:
-
- 检查input类型是否为Variable
-
- 检查数据类型是否为float16, float32, float64
两个异常情况示例如下(可手动执行):
- type error
import paddle.fluid as fluid
import numpy as np
batch_norm = fluid.dygraph.BatchNorm(10)
x1 = fluid.create_lod_tensor(
np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace())
batch_norm(x1)
# TypeError: The type of 'input' in BatchNorm must be <class 'paddle.fluid.framework.Variable'>, but received <class 'paddle.fluid.core_avx.LoDTensor'>.
- dtype error
import paddle.fluid as fluid
import numpy as np
batch_norm = fluid.dygraph.BatchNorm(10)
x2 = fluid.layers.data(name='x2', shape=[3, 4, 5, 6], dtype="int32")
batch_norm(x2)
# TypeError: The data type of 'input' in BatchNorm must be ['float16', 'float32', 'float64'], but received int32.