jiahuimbzuai/CoQuIR huggingface dataset:
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    with tf.Graph().as_default():
        indices = tf.placeholder(
            shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
        )
        values = tf.placeholder(
            shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
        )
        oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

        if default_value == None:
            output = tf.sparse_to_dense(indices, oshape, values)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values], ["indices:0", "values:0"], output.name
            )
        else:
            dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
            output = tf.sparse_to_dense(indices, oshape, values, dv)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values, default_value],
                ["indices:0", "values:0", "default_value:0"],
                output.name,
            )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    with tf.Graph().as_default():
        indices = tf.placeholder(
            shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
        )
        values = tf.placeholder(
            shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
        )
        oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

        if default_value == None:
            output = tf.sparse.to_dense(tf.SparseTensor(indices, values, oshape))
            compare_tf_with_tvm(
                [sparse_indices, sparse_values], ["indices:0", "values:0"], output.name
            )
        else:
            dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
            output = tf.sparse.to_dense(tf.SparseTensor(indices, values, oshape), default_value=dv)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values, default_value],
                ["indices:0", "values:0", "default_value:0"],
                output.name,
            )
//  [python]
def build_decoder(logit, seq_len):
	# ctc decoding
	decoded, _ = tf.nn.ctc_beam_search_decoder(tf.transpose(logit, [1, 0, 2]), seq_len, merge_repeated=False)
	# to dense tensor
	top = decoded[0]
	y = tf.sparse_to_dense(top.indices, top.dense_shape, top.values) + 1 # skip <EOS>

	return y, decoded
//  [python]
def build_decoder(logit, seq_len):
	# ctc decoding
	decoded, _ = tf.nn.ctc_beam_search_decoder(tf.transpose(logit, [1, 0, 2]), seq_len, merge_repeated=False)
	# to dense tensor
	top = decoded[0]
	y = tf.sparse.to_dense(top.indices, top.dense_shape, top.values) + 1 # skip <EOS>

	return y, decoded
//  [python]
def convert_labeled_classes_to_k_hot(groundtruth_labeled_classes,
                                     num_classes,
                                     map_empty_to_ones=False):
  """Returns k-hot encoding of the labeled classes.

  If map_empty_to_ones is enabled and the input labeled_classes is empty,
  this function assumes all classes are exhaustively labeled, thus returning
  an all-one encoding.

  Args:
    groundtruth_labeled_classes: a Tensor holding a sparse representation of
      labeled classes.
    num_classes: an integer representing the number of classes
    map_empty_to_ones: boolean (default: False).  Set this to be True to default
    to an all-ones result if given an empty `groundtruth_labeled_classes`.
  Returns:
    A k-hot (and 0-indexed) tensor representation of
    `groundtruth_labeled_classes`.
  """

  # If the input labeled_classes is empty, it assumes all classes are
  # exhaustively labeled, thus returning an all-one encoding.
  def true_fn():
    return tf.sparse_to_dense(
        groundtruth_labeled_classes - _LABEL_OFFSET, [num_classes],
        tf.constant(1, dtype=tf.float32),
        validate_indices=False)

  def false_fn():
    return tf.ones(num_classes, dtype=tf.float32)

  if map_empty_to_ones:
    return tf.cond(tf.size(groundtruth_labeled_classes) > 0, true_fn, false_fn)
  return true_fn()
//  [python]
def convert_labeled_classes_to_k_hot(groundtruth_labeled_classes,
                                     num_classes,
                                     map_empty_to_ones=False):
  """Returns k-hot encoding of the labeled classes.

  If map_empty_to_ones is enabled and the input labeled_classes is empty,
  this function assumes all classes are exhaustively labeled, thus returning
  an all-one encoding.

  Args:
    groundtruth_labeled_classes: a Tensor holding a sparse representation of
      labeled classes.
    num_classes: an integer representing the number of classes
    map_empty_to_ones: boolean (default: False).  Set this to be True to default
    to an all-ones result if given an empty `groundtruth_labeled_classes`.
  Returns:
    A k-hot (and 0-indexed) tensor representation of
    `groundtruth_labeled_classes`.
  """

  # If the input labeled_classes is empty, it assumes all classes are
  # exhaustively labeled, thus returning an all-one encoding.
  def true_fn():
    return tf.sparse.to_dense(
        groundtruth_labeled_classes - _LABEL_OFFSET, [num_classes],
        tf.constant(1, dtype=tf.float32),
        validate_indices=False)

  def false_fn():
    return tf.ones(num_classes, dtype=tf.float32)

  if map_empty_to_ones:
    return tf.cond(tf.size(groundtruth_labeled_classes) > 0, true_fn, false_fn)
  return true_fn()
//  [python]
def remove_from_sparse(sp_tensor, remove_indices):
  """Remove indices from SparseTensor if present."""
  # 1. create 1d index maps
  scaling_vector = tf.cumprod(
      tf.cast(tf.shape(sp_tensor), sp_tensor.indices.dtype))
  a1s = tf.linalg.matvec(sp_tensor.indices, scaling_vector)
  b1s = tf.linalg.matvec(remove_indices, scaling_vector)

  # 2. get relevant indices of sp_a
  int_idx = tf.setdiff1d(a1s, b1s)[1]
  to_retain = tf.sparse_to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(a1s),
      default_value=0.0,
      sparse_values=1.0) > 0.5
  return tf.sparse.retain(sp_tensor, to_retain)
//  [python]
def remove_from_sparse(sp_tensor, remove_indices):
  """Remove indices from SparseTensor if present."""
  # 1. create 1d index maps
  scaling_vector = tf.cumprod(
      tf.cast(tf.shape(sp_tensor), sp_tensor.indices.dtype))
  a1s = tf.linalg.matvec(sp_tensor.indices, scaling_vector)
  b1s = tf.linalg.matvec(remove_indices, scaling_vector)

  # 2. get relevant indices of sp_a
  int_idx = tf.setdiff1d(a1s, b1s)[1]
  to_retain = tf.sparse.to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(a1s),
      default_value=0.0,
      sparse_values=1.0) > 0.5
  return tf.sparse.retain(sp_tensor, to_retain)
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"):
        with tf.Graph().as_default():
            indices = tf.placeholder(
                shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
            )
            values = tf.placeholder(
                shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
            )
            oshape = tf.constant(
                output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype)
            )

            if default_value == None:
                output = tf.sparse_to_dense(indices, oshape, values)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output],
                )
            else:
                dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
                output = tf.sparse_to_dense(indices, oshape, values, dv)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv],
                    [output],
                )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"):
        with tf.Graph().as_default():
            indices = tf.placeholder(
                shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
            )
            values = tf.placeholder(
                shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
            )
            oshape = tf.constant(
                output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype)
            )

            if default_value == None:
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape))
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output],
                )
            else:
                dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape), default_value=dv)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv],
                    [output],
                )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    with tf.Graph().as_default():
        indices = tf.placeholder(
            shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
        )
        values = tf.placeholder(
            shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
        )
        oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

        # Output shape depends on a dynamic input, use VM.
        if default_value == None:
            output = tf.sparse_to_dense(indices, oshape, values)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values], ["indices:0", "values:0"], output.name, mode="vm"
            )
        else:
            dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
            output = tf.sparse_to_dense(indices, oshape, values, dv)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values, default_value],
                ["indices:0", "values:0", "default_value:0"],
                output.name,
                mode="vm",
            )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    with tf.Graph().as_default():
        indices = tf.placeholder(
            shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
        )
        values = tf.placeholder(
            shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
        )
        oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

        # Output shape depends on a dynamic input, use VM.
        if default_value == None:
            output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape))
            compare_tf_with_tvm(
                [sparse_indices, sparse_values], ["indices:0", "values:0"], output.name, mode="vm"
            )
        else:
            dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
            output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape), dv)
            compare_tf_with_tvm(
                [sparse_indices, sparse_values, default_value],
                ["indices:0", "values:0", "default_value:0"],
                output.name,
                mode="vm",
            )
//  [python]
def one_hot_encoding(labels, num_classes, scope=None):
  """Transform numeric labels into onehot_labels.

  Args:
    labels: [batch_size] target labels.
    num_classes: total number of classes.
    scope: Optional scope for name_scope.
  Returns:
    one hot encoding of the labels.
  """
  with tf.name_scope(scope, 'OneHotEncoding', [labels]):
    batch_size = labels.get_shape()[0]
    indices = tf.expand_dims(tf.range(0, batch_size), 1)
    labels = tf.cast(tf.expand_dims(labels, 1), indices.dtype)
    concated = tf.concat([indices, labels], 1)
    onehot_labels = tf.sparse_to_dense(
        concated, tf.pack([batch_size, num_classes]), 1.0, 0.0)
    onehot_labels.set_shape([batch_size, num_classes])
    return onehot_labels
//  [python]
def one_hot_encoding(labels, num_classes, scope=None):
  """Transform numeric labels into onehot_labels.

  Args:
    labels: [batch_size] target labels.
    num_classes: total number of classes.
    scope: Optional scope for name_scope.
  Returns:
    one hot encoding of the labels.
  """
  with tf.name_scope(scope, 'OneHotEncoding', [labels]):
    batch_size = labels.get_shape()[0]
    indices = tf.expand_dims(tf.range(0, batch_size), 1)
    labels = tf.cast(tf.expand_dims(labels, 1), indices.dtype)
    concated = tf.concat([indices, labels], 1)
    onehot_labels = tf.sparse.to_dense(
        tf.sparse.SparseTensor(concated, tf.ones([batch_size]), [batch_size, num_classes]), 0.0)
    onehot_labels.set_shape([batch_size, num_classes])
    return onehot_labels
//  [python]
def _count_matrix_input(self, filenames, submatrix_rows, submatrix_cols):
    """Creates ops that read submatrix shards from disk."""
    random.shuffle(filenames)
    filename_queue = tf.train.string_input_producer(filenames)
    reader = tf.WholeFileReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
            'global_row': tf.FixedLenFeature([submatrix_rows], dtype=tf.int64),
            'global_col': tf.FixedLenFeature([submatrix_cols], dtype=tf.int64),
            'sparse_local_row': tf.VarLenFeature(dtype=tf.int64),
            'sparse_local_col': tf.VarLenFeature(dtype=tf.int64),
            'sparse_value': tf.VarLenFeature(dtype=tf.float32)
        })

    global_row = features['global_row']
    global_col = features['global_col']

    sparse_local_row = features['sparse_local_row'].values
    sparse_local_col = features['sparse_local_col'].values
    sparse_count = features['sparse_value'].values

    sparse_indices = tf.concat(
        axis=1, values=[tf.expand_dims(sparse_local_row, 1),
                        tf.expand_dims(sparse_local_col, 1)])

    count = tf.sparse_to_dense(sparse_indices, [submatrix_rows, submatrix_cols],
                               sparse_count)

    return global_row, global_col, count
//  [python]
def _count_matrix_input(self, filenames, submatrix_rows, submatrix_cols):
    """Creates ops that read submatrix shards from disk."""
    random.shuffle(filenames)
    filename_queue = tf.train.string_input_producer(filenames)
    reader = tf.WholeFileReader()
    _, serialized_example = reader.read(filename_queue)
    features = tf.parse_single_example(
        serialized_example,
        features={
            'global_row': tf.FixedLenFeature([submatrix_rows], dtype=tf.int64),
            'global_col': tf.FixedLenFeature([submatrix_cols], dtype=tf.int64),
            'sparse_local_row': tf.VarLenFeature(dtype=tf.int64),
            'sparse_local_col': tf.VarLenFeature(dtype=tf.int64),
            'sparse_value': tf.VarLenFeature(dtype=tf.float32)
        })

    global_row = features['global_row']
    global_col = features['global_col']

    sparse_local_row = features['sparse_local_row'].values
    sparse_local_col = features['sparse_local_col'].values
    sparse_count = features['sparse_value'].values

    sparse_indices = tf.concat(
        axis=1, values=[tf.expand_dims(sparse_local_row, 1),
                        tf.expand_dims(sparse_local_col, 1)])

    count = tf.sparse.to_dense(sparse_indices, sparse_count, [submatrix_rows, submatrix_cols])

    return global_row, global_col, count
//  [python]
def sp_sp_matmul(sp_a, sp_b):
  """Element-wise multiply two SparseTensors of same shape."""
  sp_a = tf.sparse.reorder(sp_a)
  sp_b = tf.sparse.reorder(sp_b)

  # 1. create 1d index maps
  scaling_vector = tf.cumprod(tf.cast(tf.shape(sp_b), sp_b.indices.dtype))
  a1s = tf.linalg.matvec(sp_a.indices, scaling_vector)
  b1s = tf.linalg.matvec(sp_b.indices, scaling_vector)

  # 2. get relevant indices of sp_a
  int_idx = tf.setdiff1d(a1s, b1s)[1]
  to_retain = tf.sparse_to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(a1s),
      default_value=0.0,
      sparse_values=1.0) < 0.5
  rsp_a = tf.sparse.retain(sp_a, to_retain)
  rsp_a = tf.sparse.reorder(rsp_a)

  # 3. get relevant indices of sp_b
  int_idx = tf.setdiff1d(b1s, a1s)[1]
  to_retain = tf.sparse_to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(b1s),
      default_value=0.0,
      sparse_values=1.0) < 0.5
  rsp_b = tf.sparse.retain(sp_b, to_retain)
  rsp_b = tf.sparse.reorder(rsp_b)

  # 4. create output matrix
  return tf.SparseTensor(
      indices=rsp_b.indices,
      values=rsp_a.values * rsp_b.values,
      dense_shape=sp_b.dense_shape)
//  [python]
def sp_sp_matmul(sp_a, sp_b):
  """Element-wise multiply two SparseTensors of same shape."""
  sp_a = tf.sparse.reorder(sp_a)
  sp_b = tf.sparse.reorder(sp_b)

  # 1. create 1d index maps
  scaling_vector = tf.cumprod(tf.cast(tf.shape(sp_b), sp_b.indices.dtype))
  a1s = tf.linalg.matvec(sp_a.indices, scaling_vector)
  b1s = tf.linalg.matvec(sp_b.indices, scaling_vector)

  # 2. get relevant indices of sp_a
  int_idx = tf.setdiff1d(a1s, b1s)[1]
  to_retain = tf.sparse.to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(a1s),
      default_value=0.0,
      sparse_values=1.0) < 0.5
  rsp_a = tf.sparse.retain(sp_a, to_retain)
  rsp_a = tf.sparse.reorder(rsp_a)

  # 3. get relevant indices of sp_b
  int_idx = tf.setdiff1d(b1s, a1s)[1]
  to_retain = tf.sparse.to_dense(
      sparse_indices=int_idx,
      output_shape=tf.shape(b1s),
      default_value=0.0,
      sparse_values=1.0) < 0.5
  rsp_b = tf.sparse.retain(sp_b, to_retain)
  rsp_b = tf.sparse.reorder(rsp_b)

  # 4. create output matrix
  return tf.SparseTensor(
      indices=rsp_b.indices,
      values=rsp_a.values * rsp_b.values,
      dense_shape=sp_b.dense_shape)
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"):
        with tf.Graph().as_default():
            indices = tf.placeholder(
                shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
            )
            values = tf.placeholder(
                shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
            )
            oshape = tf.constant(
                output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype)
            )

            if default_value is None:
                output = tf.sparse_to_dense(indices, oshape, values)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output],
                )
            else:
                dv_placeholder = tf.placeholder(
                    shape=(), dtype=str(default_value.dtype), name="default_value"
                )
                output = tf.sparse_to_dense(indices, oshape, values, dv_placeholder)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv_placeholder],
                    [output],
                )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse("1.14.0"):
        with tf.Graph().as_default():
            indices = tf.placeholder(
                shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices"
            )
            values = tf.placeholder(
                shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values"
            )
            oshape = tf.constant(
                output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype)
            )

            if default_value is None:
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape))
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output],
                )
            else:
                dv_placeholder = tf.placeholder(
                    shape=(), dtype=str(default_value.dtype), name="default_value"
                )
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape), default_value=dv_placeholder)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv_placeholder],
                    [output],
                )
//  [python]
def single_tower(net, gpu_indx, dataset_loader, out_charset, optimizer, name,
                 is_train):
    """ Single model tower.
    """
    # Get batch
    with tf.device('/gpu:%d' % gpu_indx), \
            tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE), \
            tf.name_scope(name):

        batch = dataset_loader.get_batch()

        # Tensorboard image
        tf.summary.image('image', batch.image, max_outputs=10)

        # Inference
        logits, sequence_length = net.get_logits(
            batch.image, is_train, label=batch.label if is_train else None)

        tvars, loss, extra_update_ops = net.get_loss(
            logits,
            batch.label,
            sequence_length=sequence_length,
            label_length=batch.length,
            label_maxlen=dataset_loader.label_maxlen)

        grads = optimizer.compute_gradients(
                loss, tvars,
                colocate_gradients_with_ops=True) \
            if optimizer is not None \
            else None

        prediction, log_prob = net.get_prediction(logits, sequence_length)

        error = get_error(prediction,
                          batch.label,
                          batch.length,
                          out_charset,
                          name='err',
                          max_text_summary=10)

        prediction = tf.sparse_to_dense(sparse_indices=prediction.indices,
                                        sparse_values=prediction.values,
                                        output_shape=prediction.dense_shape,
                                        default_value=len(out_charset))

        result = TowerResult(tvars, loss, grads, extra_update_ops, error,
                             prediction, batch.text, batch.filename,
                             batch.dataset_name)

    return result
//  [python]
def single_tower(net, gpu_indx, dataset_loader, out_charset, optimizer, name,
                 is_train):
    """ Single model tower.
    """
    # Get batch
    with tf.device('/gpu:%d' % gpu_indx), \
            tf.variable_scope(tf.get_variable_scope(), reuse=tf.AUTO_REUSE), \
            tf.name_scope(name):

        batch = dataset_loader.get_batch()

        # Tensorboard image
        tf.summary.image('image', batch.image, max_outputs=10)

        # Inference
        logits, sequence_length = net.get_logits(
            batch.image, is_train, label=batch.label if is_train else None)

        tvars, loss, extra_update_ops = net.get_loss(
            logits,
            batch.label,
            sequence_length=sequence_length,
            label_length=batch.length,
            label_maxlen=dataset_loader.label_maxlen)

        grads = optimizer.compute_gradients(
                loss, tvars,
                colocate_gradients_with_ops=True) \
            if optimizer is not None \
            else None

        prediction, log_prob = net.get_prediction(logits, sequence_length)

        error = get_error(prediction,
                          batch.label,
                          batch.length,
                          out_charset,
                          name='err',
                          max_text_summary=10)

        prediction = tf.sparse.to_dense(sparse_indices=prediction.indices,
                                        sparse_values=prediction.values,
                                        output_shape=prediction.dense_shape,
                                        default_value=len(out_charset))

        result = TowerResult(tvars, loss, grads, extra_update_ops, error,
                             prediction, batch.text, batch.filename,
                             batch.dataset_name)

    return result
//  [python]
def nms_return_masks_no_score_filter(self, X):
    config = self.config
    prob, box = X # [K], [K,4]
    output_shape = tf.shape(prob)
    # NMS
    selection = tf.image.non_max_suppression(
        box, prob, max_output_size=config.result_per_im,
        iou_threshold=config.fastrcnn_nms_iou_thres)
    sorted_selection = -tf.nn.top_k(-selection, k=tf.size(selection))[0]

    mask = tf.sparse_to_dense(
        sparse_indices=sorted_selection, output_shape=output_shape,
        sparse_values=True, default_value=False)

    return mask
//  [python]
def nms_return_masks_no_score_filter(self, X):
    config = self.config
    prob, box = X # [K], [K,4]
    output_shape = tf.shape(prob)
    # NMS
    selection = tf.image.non_max_suppression(
        box, prob, max_output_size=config.result_per_im,
        iou_threshold=config.fastrcnn_nms_iou_thres)
    sorted_selection = -tf.nn.top_k(-selection, k=tf.size(selection))[0]

    mask = tf.sparse.to_dense(
        sparse_indices=sorted_selection, output_shape=output_shape,
        sparse_values=True, default_value=False)

    return mask
//  [python]
def build_feature_output_dict(self):
    """For exporting: get output feature nodes."""
    outputs = {}
    for feature_name in self._feature_dict:
      out_name = 'feature_' + feature_name
      feature_value = self._feature_dict[feature_name]
      if isinstance(feature_value, tf.SparseTensor):
        sparse_values = feature_value.values
        if sparse_values.dtype != tf.string:
          sparse_values = tf.as_string(sparse_values)
        feature_value = tf.sparse_to_dense(feature_value.indices,
                                           feature_value.dense_shape,
                                           sparse_values, '')
      elif feature_value.dtype != tf.string:
        feature_value = tf.as_string(feature_value)
      feature_value = tf.reduce_join(feature_value, axis=-1, separator=',')
      outputs[out_name] = feature_value
    return outputs
//  [python]
def build_feature_output_dict(self):
    """For exporting: get output feature nodes."""
    outputs = {}
    for feature_name in self._feature_dict:
      out_name = 'feature_' + feature_name
      feature_value = self._feature_dict[feature_name]
      if isinstance(feature_value, tf.SparseTensor):
        sparse_values = feature_value.values
        if sparse_values.dtype != tf.string:
          sparse_values = tf.as_string(sparse_values)
        feature_value = tf.sparse.to_dense(feature_value, sparse_values, '')
      elif feature_value.dtype != tf.string:
        feature_value = tf.as_string(feature_value)
      feature_value = tf.reduce_join(feature_value, axis=-1, separator=',')
      outputs[out_name] = feature_value
    return outputs
//  [python]
def parse_sequence(self, sequence):
        """
        split the sequence and convert to dense tensor
        """
        split_sequence = tf.string_split(sequence, delimiter="\t")
        split_sequence = tf.sparse_to_dense(sparse_indices=split_sequence.indices,
                                            output_shape=[self.feat_config["batch_size"],
                                                          self.feat_config["max_length"]],
                                            sparse_values=split_sequence.values, default_value="0")

        return split_sequence
//  [python]
def parse_sequence(self, sequence):
        """
        split the sequence and convert to dense tensor
        """
        split_sequence = tf.string_split(sequence, delimiter="\t")
        split_sequence = tf.sparse.to_dense(sparse_indices=split_sequence.indices,
                                            output_shape=[self.feat_config["batch_size"],
                                                          self.feat_config["max_length"]],
                                            sparse_values=split_sequence.values, default_value="0")

        return split_sequence
//  [python]
def _single_token_mask(inputs, tgt_len, num_predict):
  """Sample individual tokens as prediction targets."""
  all_indices = tf.range(tgt_len, dtype=tf.int64)
  non_func_mask = tf.logical_and(
      tf.not_equal(inputs, SEP_ID), tf.not_equal(inputs, CLS_ID))
  non_func_indices = tf.boolean_mask(all_indices, non_func_mask)

  masked_pos = tf.random.shuffle(non_func_indices)
  masked_pos = tf.sort(masked_pos[:num_predict])
  target_mask = tf.sparse_to_dense(
      sparse_indices=masked_pos,
      output_shape=[tgt_len],
      sparse_values=1.0,
      default_value=0.0)

  is_masked = tf.cast(target_mask, tf.bool)

  return is_masked, target_mask
//  [python]
def _single_token_mask(inputs, tgt_len, num_predict):
  """Sample individual tokens as prediction targets."""
  all_indices = tf.range(tgt_len, dtype=tf.int64)
  non_func_mask = tf.logical_and(
      tf.not_equal(inputs, SEP_ID), tf.not_equal(inputs, CLS_ID))
  non_func_indices = tf.boolean_mask(all_indices, non_func_mask)

  masked_pos = tf.random.shuffle(non_func_indices)
  masked_pos = tf.sort(masked_pos[:num_predict])
  target_mask = tf.sparse.to_dense(
      sparse_indices=masked_pos,
      output_shape=[tgt_len],
      sparse_values=1.0,
      default_value=0.0)

  is_masked = tf.cast(target_mask, tf.bool)

  return is_masked, target_mask
//  [python]
def grad_cam(img, imgs0, end_points, sess, predicted_class, layer_name, nb_classes, eval_image_size):
  # Conv layer tensor [?,10,10,2048]
  conv_layer = end_points[layer_name]
  # [1000]-D tensor with target class index set to 1 and rest as 0
  one_hot = tf.sparse_to_dense(predicted_class, [nb_classes], 1.0)
  signal = tf.multiply(end_points[_logits_name], one_hot)
  loss = tf.reduce_mean(signal)

  grads = tf.gradients(loss, conv_layer)[0]
  # Normalizing the gradients
  norm_grads = tf.divide(grads, tf.sqrt(tf.reduce_mean(tf.square(grads))) + tf.constant(1e-5))

  output, grads_val = sess.run([conv_layer, norm_grads], feed_dict={imgs0: img})
  output = output[0]           # [10,10,2048]
  grads_val = grads_val[0]	 # [10,10,2048]

  weights = np.mean(grads_val, axis = (0, 1)) 			# [2048]
  cam = np.ones(output.shape[0 : 2], dtype = np.float32)	# [10,10]

  # Taking a weighted average
  for i, w in enumerate(weights):
    cam += w * output[:, :, i]

  # Passing through ReLU
  cam = np.maximum(cam, 0)
  cam = cam / np.max(cam)
  cam3 = cv2.resize(cam, (eval_image_size,eval_image_size))

  return cam3
//  [python]
def grad_cam(img, imgs0, end_points, sess, predicted_class, layer_name, nb_classes, eval_image_size):
  # Conv layer tensor [?,10,10,2048]
  conv_layer = end_points[layer_name]
  # [1000]-D tensor with target class index set to 1 and rest as 0
  one_hot = tf.sparse.to_dense(predicted_class, [nb_classes], 1.0)
  signal = tf.multiply(end_points[_logits_name], one_hot)
  loss = tf.reduce_mean(signal)

  grads = tf.gradients(loss, conv_layer)[0]
  # Normalizing the gradients
  norm_grads = tf.divide(grads, tf.sqrt(tf.reduce_mean(tf.square(grads))) + tf.constant(1e-5))

  output, grads_val = sess.run([conv_layer, norm_grads], feed_dict={imgs0: img})
  output = output[0]           # [10,10,2048]
  grads_val = grads_val[0]	 # [10,10,2048]

  weights = np.mean(grads_val, axis = (0, 1)) 			# [2048]
  cam = np.ones(output.shape[0 : 2], dtype = np.float32)	# [10,10]

  # Taking a weighted average
  for i, w in enumerate(weights):
    cam += w * output[:, :, i]

  # Passing through ReLU
  cam = np.maximum(cam, 0)
  cam = cam / np.max(cam)
  cam3 = cv2.resize(cam, (eval_image_size,eval_image_size))

  return cam3
//  [python]
def todense(self, a):
        if self.issparse(a):
            return tf.sparse_to_dense(tf.sparse.reorder(a))
        else:
            return a
//  [python]
def todense(self, a):
        if self.issparse(a):
            return tf.sparse.to_dense(tf.sparse.reorder(a))
        else:
            return a
//  [python]
def parse_feature_sequence(self, sequence):
        split_sequence = tf.string_split(sequence, delimiter="\t")
        split_sequence1 = tf.sparse_to_dense(sparse_indices=split_sequence.indices,
                                            output_shape=[self.feat_config["batch_size"],
                                                          self.feat_config["max_length"]],
                                            sparse_values=split_sequence.values,
                                            default_value="".join(["0" for i in range(self.feat_config["n_seq_feat"])]))

        split_sequence1 = tf.reshape(split_sequence1, shape=[self.feat_config["batch_size"]*self.feat_config["max_length"]])

        split_sequence2 = tf.string_split(split_sequence1, delimiter="")
        split_sequence2 = tf.sparse_to_dense(sparse_indices=split_sequence2.indices,
                                             output_shape=[self.feat_config["batch_size"]*self.feat_config["max_length"],
                                                           self.feat_config["n_seq_feat"]],
                                             sparse_values=split_sequence2.values,
                                             default_value="0")

        split_sequence_final = tf.reshape(split_sequence2, shape=[-1, self.feat_config["max_length"], self.feat_config["n_seq_feat"]])

        return split_sequence_final
//  [python]
def parse_feature_sequence(self, sequence):
        split_sequence = tf.string_split(sequence, delimiter="\t")
        split_sequence1 = tf.sparse.to_dense(sparse_indices=split_sequence.indices,
                                            output_shape=[self.feat_config["batch_size"],
                                                          self.feat_config["max_length"]],
                                            sparse_values=split_sequence.values,
                                            default_value="".join(["0" for i in range(self.feat_config["n_seq_feat"])]))

        split_sequence1 = tf.reshape(split_sequence1, shape=[self.feat_config["batch_size"]*self.feat_config["max_length"]])

        split_sequence2 = tf.string_split(split_sequence1, delimiter="")
        split_sequence2 = tf.sparse.to_dense(sparse_indices=split_sequence2.indices,
                                             output_shape=[self.feat_config["batch_size"]*self.feat_config["max_length"],
                                                           self.feat_config["n_seq_feat"]],
                                             sparse_values=split_sequence2.values,
                                             default_value="0")

        split_sequence_final = tf.reshape(split_sequence2, shape=[-1, self.feat_config["max_length"], self.feat_config["n_seq_feat"]])

        return split_sequence_final
//  [python]
def _test_sparse_to_dense_v2(indices, values, A_shape, dtype, default_value=None):
    with tf.Graph().as_default():
        A_sp = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=A_shape)

        result = tf.sparse_to_dense(A_sp.indices, A_sp.dense_shape, A_sp.values, default_value=default_value)

        # The output shape depends on a dynamic input, use VM.
        compare_tf_with_tvm([], [], result.name, mode="vm")
//  [python]
def _test_sparse_to_dense_v2(indices, values, A_shape, dtype, default_value=None):
    with tf.Graph().as_default():
        A_sp = tf.sparse.SparseTensor(indices=indices, values=values, dense_shape=A_shape)

        result = tf.sparse.to_dense(A_sp, default_value=default_value)

        # The output shape depends on a dynamic input, use VM.
        compare_tf_with_tvm([], [], result.name, mode="vm")
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse('1.14.0'):
        with tf.Graph().as_default():
            indices = tf.placeholder(shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices")
            values = tf.placeholder(shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values")
            oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

            if default_value == None:
                output = tf.sparse_to_dense(indices, oshape, values)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output]
                )
            else:
                dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
                output = tf.sparse_to_dense(indices, oshape, values, dv)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv],
                    [output]
                )
//  [python]
def _test_sparse_to_dense(sparse_indices, sparse_values, default_value, output_shape):
    # tflite 1.13 convert method does not accept empty shapes
    if package_version.parse(tf.VERSION) >= package_version.parse('1.14.0'):
        with tf.Graph().as_default():
            indices = tf.placeholder(shape=sparse_indices.shape, dtype=str(sparse_indices.dtype), name="indices")
            values = tf.placeholder(shape=sparse_values.shape, dtype=str(sparse_values.dtype), name="values")
            oshape = tf.constant(output_shape, shape=output_shape.shape, dtype=str(output_shape.dtype))

            if default_value == None:
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape))
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values],
                    ["indices", "values"],
                    [indices, values],
                    [output]
                )
            else:
                dv = tf.placeholder(shape=(), dtype=str(default_value.dtype), name="default_value")
                output = tf.sparse.to_dense(tf.sparse.SparseTensor(indices, values, oshape), default_value=dv)
                compare_tflite_with_tvm(
                    [sparse_indices, sparse_values, default_value],
                    ["indices", "values", "default_value"],
                    [indices, values, dv],
                    [output]
                )
//  [python]
def _decode_tf_example(self, encoded_inputs: tf.Tensor):
    sequence_description = {
        # Each image is a string encoding JPEG.
        video_input.IMAGE_KEY:
            tf.io.FixedLenSequenceFeature((), tf.string),
    }
    if self._output_audio:
      sequence_description[self._params.task.validation_data.audio_feature] = (
          tf.io.VarLenFeature(dtype=tf.float32))
    _, decoded_tensors = tf.io.parse_single_sequence_example(
        encoded_inputs, {}, sequence_description)
    for key, value in decoded_tensors.items():
      if isinstance(value, tf.SparseTensor):
        decoded_tensors[key] = tf.sparse_to_dense(value.indices, value.dense_shape, value.values)
    return decoded_tensors
//  [python]
def _decode_tf_example(self, encoded_inputs: tf.Tensor):
    sequence_description = {
        # Each image is a string encoding JPEG.
        video_input.IMAGE_KEY:
            tf.io.FixedLenSequenceFeature((), tf.string),
    }
    if self._output_audio:
      sequence_description[self._params.task.validation_data.audio_feature] = (
          tf.io.VarLenFeature(dtype=tf.float32))
    _, decoded_tensors = tf.io.parse_single_sequence_example(
        encoded_inputs, {}, sequence_description)
    for key, value in decoded_tensors.items():
      if isinstance(value, tf.SparseTensor):
        decoded_tensors[key] = tf.sparse.to_dense(value)
    return decoded_tensors
//  [python]
def pred_with_RMSE(self, test_set, pred_set):
        test_set = test_set.repeat(1)
        test_set = test_set.batch(self.batch_size)
        test_iterator = test_set.make_one_shot_iterator()
        pred_set = pred_set.batch(self.batch_size)
        pred_iterator = pred_set.make_one_shot_iterator()

        training_point = pred_iterator.get_next()
        x = tf.sparse_to_dense(training_point[1], [self.batch_size, self.num_rat], default_value = -1)
        user = training_point[0]
        x_hot = tf.one_hot(x, self.num_rat, axis=1)
        batch_count = 0
        curr_preds = self.forward(x_hot, user, False)


        batch_count = 0
        total_predictions = []
        actual = []
        try:
            while True:
                batch_count += 1

                row_batch = test_iterator.get_next()
                test_preds = tf.gather_nd(curr_preds, row_batch[1].indices)

                total_predictions = tf.concat([total_predictions, test_preds], 0)
                actual = tf.concat([actual, row_batch[1].values], 0)
                batch_count += self.batch_size
                training_point = pred_iterator.get_next()
                x = tf.sparse_to_dense(training_point[1], [self.batch_size, self.num_rat], default_value = -1)
                user = training_point[0]
                x_hot = tf.one_hot(x, self.num_rat, axis=1)
                curr_preds = self.forward(x_hot, user, False)

        except tf.errors.OutOfRangeError:
            pass

        total_error = tf.math.sqrt(tf.losses.mean_squared_error(total_predictions, actual))

        return total_predictions, total_error
//  [python]
def pred_with_RMSE(self, test_set, pred_set):
        test_set = test_set.repeat(1)
        test_set = test_set.batch(self.batch_size)
        test_iterator = test_set.make_one_shot_iterator()
        pred_set = pred_set.batch(self.batch_size)
        pred_iterator = pred_set.make_one_shot_iterator()

        training_point = pred_iterator.get_next()
        x = tf.sparse.to_dense(training_point[1], default_value = -1)
        user = training_point[0]
        x_hot = tf.one_hot(x, self.num_rat, axis=1)
        batch_count = 0
        curr_preds = self.forward(x_hot, user, False)


        batch_count = 0
        total_predictions = []
        actual = []
        try:
            while True:
                batch_count += 1

                row_batch = test_iterator.get_next()
                test_preds = tf.gather_nd(curr_preds, row_batch[1].indices)

                total_predictions = tf.concat([total_predictions, test_preds], 0)
                actual = tf.concat([actual, row_batch[1].values], 0)
                batch_count += self.batch_size
                training_point = pred_iterator.get_next()
                x = tf.sparse.to_dense(training_point[1], default_value = -1)
                user = training_point[0]
                x_hot = tf.one_hot(x, self.num_rat, axis=1)
                curr_preds = self.forward(x_hot, user, False)

        except tf.errors.OutOfRangeError:
            pass

        total_error = tf.math.sqrt(tf.losses.mean_squared_error(total_predictions, actual))

        return total_predictions, total_error
//  [python]
def pred_for_sub(self, test_set, pred_set, submit=True, filename="rbm.txt"):
        test_set = test_set.repeat(1)
        test_set = test_set.batch(self.batch_size)
        test_iterator = test_set.make_one_shot_iterator()
        pred_set = pred_set.batch(self.batch_size)
        pred_iterator = pred_set.make_one_shot_iterator()

        training_point = pred_iterator.get_next()
        x = tf.sparse_to_dense(training_point[1], [self.batch_size, self.num_rat], default_value = -1)
        user = training_point[0]
        x_hot = tf.one_hot(x, self.num_rat, axis=1)
        batch_count = 0
        curr_preds = self.forward(x_hot, user, False)


        batch_count = 0
        total_predictions = []
        actual = []
        try:
            while True:
                row_batch = test_iterator.get_next()
                test_preds = tf.gather_nd(curr_preds, row_batch[1].indices)

                total_predictions = tf.concat([total_predictions, test_preds], 0)
                training_point = pred_iterator.get_next()
                x = tf.sparse_to_dense(training_point[1], [self.batch_size, self.num_rat], default_value = -1)
                user = training_point[0]
                x_hot = tf.one_hot(x, self.num_rat, axis=1)
                curr_preds = self.forward(x_hot, user, False)

        except tf.errors.OutOfRangeError:
            pass

        if submit:
            submission = total_predictions.numpy()
            np.savetxt(filename, submission, delimiter="\n")

        return total_predictions
//  [python]
def pred_for_sub(self, test_set, pred_set, submit=True, filename="rbm.txt"):
        test_set = test_set.repeat(1)
        test_set = test_set.batch(self.batch_size)
        test_iterator = test_set.make_one_shot_iterator()
        pred_set = pred_set.batch(self.batch_size)
        pred_iterator = pred_set.make_one_shot_iterator()

        training_point = pred_iterator.get_next()
        x = tf.sparse.to_dense(training_point[1], default_value = -1)
        user = training_point[0]
        x_hot = tf.one_hot(x, self.num_rat, axis=1)
        batch_count = 0
        curr_preds = self.forward(x_hot, user, False)


        batch_count = 0
        total_predictions = []
        actual = []
        try:
            while True:
                row_batch = test_iterator.get_next()
                test_preds = tf.gather_nd(curr_preds, row_batch[1].indices)

                total_predictions = tf.concat([total_predictions, test_preds], 0)
                training_point = pred_iterator.get_next()
                x = tf.sparse.to_dense(training_point[1], default_value = -1)
                user = training_point[0]
                x_hot = tf.one_hot(x, self.num_rat, axis=1)
                curr_preds = self.forward(x_hot, user, False)

        except tf.errors.OutOfRangeError:
            pass

        if submit:
            submission = total_predictions.numpy()
            np.savetxt(filename, submission, delimiter="\n")

        return total_predictions
//  [python]
def parse_tfrecord(tfrecord, class_table, size):
    x = tf.io.parse_single_example(tfrecord, IMAGE_FEATURE_MAP)
    x_train = tf.image.decode_jpeg(x['image/encoded'], channels=3)
    x_train = tf.image.resize(x_train, (size, size))

    class_text = tf.sparse_tensor_to_dense(
        x['image/object/class/text'], default_value='')
    labels = tf.cast(class_table.lookup(class_text), tf.float32)
    y_train = tf.stack([tf.sparse_tensor_to_dense(x['image/object/bbox/xmin']),
                        tf.sparse_tensor_to_dense(x['image/object/bbox/ymin']),
                        tf.sparse_tensor_to_dense(x['image/object/bbox/xmax']),
                        tf.sparse_tensor_to_dense(x['image/object/bbox/ymax']),
                        labels], axis=1)

    paddings = [[0, FLAGS.yolo_max_boxes - tf.shape(y_train)[0]], [0, 0]]
    y_train = tf.pad(y_train, paddings)

    return x_train, y_train