Assert

paddle.fluid.layers.control_flow. Assert ( cond, data=None, summarize=20, name=None ) [source]

This API creates an op that asserts the given condition is true. If the condition is false, prints the tensors in data. summarize specifies the number of the elements in the tensors to print.

Parameters
  • cond (Variable) – The boolean condition tensor whose numel should be 1.

  • data (list|tuple, optional) – list or tuple of tensors to print when condition is not true. If it’s None, no tensor will be printed. The default value is None.

  • summarize (int, optional) – Number of elements in the tensor to be printed. If its value is -1, then all elements in the tensor will be printed. The default value is 20.

  • name (str, optional) – The default value is None . Normally users don’t have to set this parameter. For more information, please refer to Name .

Returns

the created operation.

Return type

Operator

Raises
  • TypeError – If cond is not boolean Variable.

  • TypeError – If data is not a list or tuple or None.

  • TypeError – If summarize is not int.

  • TypeError – If name is not a string or None .

  • fluid.core.EnforceNotMet – If the condition is False in running time.

Examples

import paddle.fluid as fluid
import paddle.fluid.layers as layers

x = layers.fill_constant(shape=[2, 3], dtype='float32', value=2.0)
condition = layers.reduce_max(x) < 1.0 # False
layers.Assert(condition, [x], 10, "example_assert_layer")

exe = fluid.Executor()
try:
    exe.run(fluid.default_main_program())
    # Print x and throws paddle.fluid.core.EnforceNotMet exception
    # Example printed message for x:
    #
    # Variable: fill_constant_0.tmp_0
    #   - lod: {}
    #   - place: CPUPlace()
    #   - shape: [2, 3]
    #   - layout: NCHW
    #   - dtype: float
    #   - data: [2 2 2 2 2 2]
except fluid.core.EnforceNotMet as e:
    print("Assert Exception Example")