code: def retrieve_seq_length_op3(data, pad_val=0):
      """An op to compute the length of a sequence, the data shape can be [batch_size, n_step(max)] or
      [batch_size, n_step(max), n_features].
  
      If the data has type of tf.string and pad_val is assigned as empty string (''), this op will compute the
      length of the string sequence.
  
      Parameters
      -----------
      data : tensor
          [batch_size, n_step(max)] or [batch_size, n_step(max), n_features] with zero padding on the right hand side.
      pad_val:
          By default 0. If the data is tf.string, please assign this as empty string ('')
  
      Examples
      -----------
      >>> data = [[[1],[2],[0],[0],[0]],
      >>>         [[1],[2],[3],[0],[0]],
      >>>         [[1],[2],[6],[1],[0]]]
      >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
      >>> length = tl.layers.retrieve_seq_length_op3(data)
      tensor([2, 3, 4])
      >>> data = [[[1,2],[2,2],[1,2],[1,2],[0,0]],
      >>>         [[2,3],[2,4],[3,2],[0,0],[0,0]],
      >>>         [[3,3],[2,2],[5,3],[1,2],[0,0]]]
      >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
      >>> length = tl.layers.retrieve_seq_length_op3(data)
      tensor([4, 3, 4])
      >>> data = [[1,2,0,0,0],
      >>>         [1,2,3,0,0],
      >>>         [1,2,6,1,0]]
      >>> data = tf.convert_to_tensor(data, dtype=tf.float32)
      >>> length = tl.layers.retrieve_seq_length_op3(data)
      tensor([2, 3, 4])
      >>> data = [['hello','world','','',''],
      >>>         ['hello','world','tensorlayer','',''],
      >>>         ['hello','world','tensorlayer','2.0','']]
      >>> data = tf.convert_to_tensor(data, dtype=tf.string)
      >>> length = tl.layers.retrieve_seq_length_op3(data, pad_val='')
      tensor([2, 3, 4])
  
      """
      data_shape_size = data.get_shape().ndims
      if data_shape_size == 3:
          return tf.reduce_sum(
              input_tensor=tf.cast(
                  tf.reduce_any(
                      input_tensor=tf.not_equal(data, pad_val), axis=2
                  ),
                  dtype=tf.int32,
              ),
              axis=1,
          )
      elif data_shape_size == 2:
          return tf.reduce_sum(
              input_tensor=tf.cast(
                  tf.not_equal(data, pad_val), dtype=tf.int32
              ),
              axis=1,
          )
      elif data_shape_size == 1:
          raise ValueError(
              "retrieve_seq_length_op3: data has wrong shape! Shape got ",
              data.get_shape().as_list(),
          )
      else:
          raise ValueError(
              "retrieve_seq_length_op3: handling data with num of dims %s hasn't been implemented!"
              % (data_shape_size)
          )
  
imports: import numpy
  import tensorflow
  import tensorlayer
  from tensorlayer import logging
  from tensorlayer.decorators import deprecated_alias
  from tensorlayer.layers.core import Layer
label_desc: Use image node_id 3 for calling a global function with example usage: retrieve_seq_length_op3(data, pad_val) and returns: tf, tf