jiahuimbzuai/CoQuIR huggingface dataset:
//  [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.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.to_dense(x['image/object/bbox/xmin']),
                        tf.sparse.to_dense(x['image/object/bbox/ymin']),
                        tf.sparse.to_dense(x['image/object/bbox/xmax']),
                        tf.sparse.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
//  [python]
def predictions(self) -> tf.Tensor:
    """Computes the full prediction matrix (used for Recall)."""
    row_embeddings = self.model.row_embeddings
    if self.do_project:
      row_embeddings = self.model.wals_projection(self.history_ds)
    preds = tf.matmul(
        row_embeddings, self.model.col_embeddings, transpose_b=True)
    if self.do_project:
      # Exclude history labels.
      hist = self.matrix.sp_history.to_sparse_tensor()
      hist_indicator = hist.with_values(
          tf.ones_like(hist.values, dtype=tf.float32))
      preds = preds - 1000000*tf.sparse_tensor_to_dense(hist_indicator)
    return preds
//  [python]
def predictions(self) -> tf.Tensor:
    """Computes the full prediction matrix (used for Recall)."""
    row_embeddings = self.model.row_embeddings
    if self.do_project:
      row_embeddings = self.model.wals_projection(self.history_ds)
    preds = tf.matmul(
        row_embeddings, self.model.col_embeddings, transpose_b=True)
    if self.do_project:
      # Exclude history labels.
      hist = self.matrix.sp_history.to_sparse_tensor()
      hist_indicator = hist.with_values(
          tf.ones_like(hist.values, dtype=tf.float32))
      preds = preds - 1000000*tf.sparse.to_dense(hist_indicator)
    return preds
//  [python]
def sparse2dense(features, key=None, default_value=0):
  def sparse2dense_(features, key, default_value):
    val = features[key]
    val = tf.sparse_to_dense(val.indices, val.dense_shape, val.values, default_value)
    features[key] = val   
  if key:
    sparse2dense(features, key)
  else:
    from tensorflow.python.framework.sparse_tensor import SparseTensor
    for key, val in features.items():
      if isinstance(val, SparseTensor):
        if val.values.dtype == tf.string:
          default_value = '\x01'
        sparse2dense_(features, key, default_value)
//  [python]
def sparse2dense(features, key=None, default_value=0):
  def sparse2dense_(features, key, default_value):
    val = features[key]
    val = tf.sparse.to_dense(val, default_value)
    features[key] = val   
  if key:
    sparse2dense(features, key)
  else:
    from tensorflow.python.framework.sparse_tensor import SparseTensor
    for key, val in features.items():
      if isinstance(val, SparseTensor):
        if val.values.dtype == tf.string:
          default_value = '\x01'
        sparse2dense_(features, key, default_value)
//  [python]
def train(self, dataset, epochs, probe_set, probe_train):
        # Computation graph definition
        batched_dataset = dataset.batch(self.batch_size)
        iterator = batched_dataset.make_one_shot_iterator()
        optimizer = tf.contrib.opt.MomentumWOptimizer(0.001, 0.01 / self.batch_size, 0.9)

        for epoch in range(epochs):
            if self.anneal:
                self.lr_weights = self.lr_weights / (1 + epoch / self.anneal_val)
                self.lr_vb = self.lr_vb / (1 + epoch / self.anneal_val)
                self.lr_hb = self.lr_hb / (1 + epoch / self.anneal_val)


            if epoch == 42:
                self.k = 3

            num_pts = 0

            print()
            print("Epoch " + str(epoch + 1) + "/" + str(epochs))
            prog_bar = tf.keras.utils.Progbar(460000, stateful_metrics=["val_rmse"])

            try:
                while True:
                    training_point = 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)
                    grads = self.learn(x_hot, user)
                    optimizer.apply_gradients(zip(grads, [self.weights, self.visible_bias]))
                    # self.apply_gradients(grads)
                    num_pts += 1
                    train_rmse = tf.sqrt( tf.scalar_mul(1 / tf.to_float(tf.count_nonzero(tf.add(x, 1))),
                        tf.reduce_sum(tf.square(tf.subtract(self.forward(x_hot, user), tf.to_float(tf.add(x, 1)))))))
                    prog_bar.update(self.batch_size * num_pts, [("train_rmse", train_rmse)])
            except tf.errors.OutOfRangeError:
                ds = dataset.shuffle(460000)
                batched_dataset = ds.batch(self.batch_size)
                iterator = batched_dataset.make_one_shot_iterator()



            predictions, RMSE = self.pred_with_RMSE(probe_set, probe_train)
            prog_bar.update(self.batch_size * num_pts, [("val_rmse", RMSE)])

            if (epoch + 1) % 5 == 0:
                saver = tf.contrib.eager.Saver(self.get_variables())
                saver.save("rbm_" + str(epoch))

        saver = tf.contrib.eager.Saver(self.get_variables())
        saver.save("rbm")
//  [python]
def train(self, dataset, epochs, probe_set, probe_train):
        # Computation graph definition
        batched_dataset = dataset.batch(self.batch_size)
        iterator = batched_dataset.make_one_shot_iterator()
        optimizer = tf.contrib.opt.MomentumWOptimizer(0.001, 0.01 / self.batch_size, 0.9)

        for epoch in range(epochs):
            if self.anneal:
                self.lr_weights = self.lr_weights / (1 + epoch / self.anneal_val)
                self.lr_vb = self.lr_vb / (1 + epoch / self.anneal_val)
                self.lr_hb = self.lr_hb / (1 + epoch / self.anneal_val)


            if epoch == 42:
                self.k = 3

            num_pts = 0

            print()
            print("Epoch " + str(epoch + 1) + "/" + str(epochs))
            prog_bar = tf.keras.utils.Progbar(460000, stateful_metrics=["val_rmse"])

            try:
                while True:
                    training_point = 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)
                    grads = self.learn(x_hot, user)
                    optimizer.apply_gradients(zip(grads, [self.weights, self.visible_bias]))
                    # self.apply_gradients(grads)
                    num_pts += 1
                    train_rmse = tf.sqrt( tf.scalar_mul(1 / tf.to_float(tf.count_nonzero(tf.add(x, 1))),
                        tf.reduce_sum(tf.square(tf.subtract(self.forward(x_hot, user), tf.to_float(tf.add(x, 1)))))))
                    prog_bar.update(self.batch_size * num_pts, [("train_rmse", train_rmse)])
            except tf.errors.OutOfRangeError:
                ds = dataset.shuffle(460000)
                batched_dataset = ds.batch(self.batch_size)
                iterator = batched_dataset.make_one_shot_iterator()



            predictions, RMSE = self.pred_with_RMSE(probe_set, probe_train)
            prog_bar.update(self.batch_size * num_pts, [("val_rmse", RMSE)])

            if (epoch + 1) % 5 == 0:
                saver = tf.contrib.eager.Saver(self.get_variables())
                saver.save("rbm_" + str(epoch))

        saver = tf.contrib.eager.Saver(self.get_variables())
        saver.save("rbm")
//  [python]
def test_any_composite(self):

    # TODO: b/294854329 - Re-enable when the TF fuzzing issue is resolved.
    if (
        tf.__version__.startswith('2.15')
        or tf.__version__.startswith('2.16')
        or tf.__version__.startswith('2.17')
    ):
      self.skipTest(
          'Fails with tf internal error on tf 2.15, 2.16, 2.17 nightly'
      )

    i = tf.keras.Input([2], name='input')
    o = i
    o = tf.keras.layers.Lambda(tf.sparse.from_dense)(o)
    o = tf.keras.layers.Lambda(tf.math.negative)(o)
    # Add identity composite layer which splits eval data on five pieces:
    # <input> => <to negative sparse> => identity -> <to dense> => output.
    o = Identity()(o)

    def fn(t):
      return tf.sparse.to_dense(t)

    o = tf.keras.layers.Lambda(fn)(o)
    model = tf.keras.Model(inputs=i, outputs=o)
    program, artifacts = sampler.create_program(model)
    self.assertLen(program.eval_dag.stages, 5)
    temp_dir = self.create_tempdir().full_path
    for name, model in artifacts.models.items():
      sampler.save_model(model, os.path.join(temp_dir, name))

    values = {
        b's1': [[np.array([[1.0, 2.0]], np.float32)]],
        b's2': [[np.array([[3.0, 4.0]], np.float32)]],
        b's3': [[np.array([[5.0, 6.0]], np.float32)]],
    }
    with beam.Pipeline() as root:
      values = root | beam.Create(values)
      result = executor_lib.execute(
          program, {'input': values}, artifacts_path=temp_dir
      )
      feat_template = """features {
                        feature {
                          key: "__output__"
                          value {
                            float_list {
                                value: %s
                            }
                          }
                        }
                      }"""

      util.assert_that(
          result,
          util.equal_to(
              [
                  (b's1', feat_template % '[-1., -2.]'),
                  (b's2', feat_template % '[-3., -4.]'),
                  (b's3', feat_template % '[-5., -6.]'),
              ],
              self.sampling_results_equal,
          ),
      )
//  [python]
def concatenate(xs, axis=0):
    sparse_count = builtins.sum(isinstance(x, tf.SparseTensor) for x in xs)
    if sparse_count:
        if sparse_count == len(xs):
            return tf.sparse.concat(axis=axis, sp_inputs=xs)
        else:
            xs = [
                tf.sparse.to_dense(x) if isinstance(x, tf.SparseTensor) else x
                for x in xs
            ]
    return tfnp.concatenate(xs, axis=axis)
//  [python]
def maximum(x1, x2):
    if isinstance(x1, tf.SparseTensor):
        if isinstance(x2, tf.SparseTensor):
            return tf.sparse.maximum(x1, x2)
        else:
            x1 = tf.sparse_tensor_to_dense(x1)
    elif isinstance(x2, tf.SparseTensor):
        x2 = tf.sparse_tensor_to_dense(x2)
    return tfnp.maximum(x1, x2)
//  [python]
def maximum(x1, x2):
    if isinstance(x1, tf.SparseTensor):
        if isinstance(x2, tf.SparseTensor):
            return tf.sparse.maximum(x1, x2)
        else:
            x1 = tf.sparse.to_dense(x1)
    elif isinstance(x2, tf.SparseTensor):
        x2 = tf.sparse.to_dense(x2)
    return tfnp.maximum(x1, x2)
//  [python]
def minimum(x1, x2):
    if isinstance(x1, tf.SparseTensor):
        if isinstance(x2, tf.SparseTensor):
            return tf.sparse.minimum(x1, x2)
        else:
            x1 = tf.sparse_tensor_to_dense(x1)
    elif isinstance(x2, tf.SparseTensor):
        x2 = tf.sparse_tensor_to_dense(x2)
    return tfnp.minimum(x1, x2)
//  [python]
def minimum(x1, x2):
    if isinstance(x1, tf.SparseTensor):
        if isinstance(x2, tf.SparseTensor):
            return tf.sparse.minimum(x1, x2)
        else:
            x1 = tf.sparse.to_dense(x1)
    elif isinstance(x2, tf.SparseTensor):
        x2 = tf.sparse.to_dense(x2)
    return tfnp.minimum(x1, x2)
//  [python]
def sparse_to_numpy(x):
    # tf.sparse.to_dense assume sorted indices, need to turn off validate_indices in our cases
    return tf.sparse_to_dense(x, validate_indices=False).numpy()
//  [python]
def sparse_to_numpy(x):
    # tf.sparse.to_dense assume sorted indices, need to turn off validate_indices in our cases
    return tf.sparse.to_dense(x, validate_indices=False).numpy()
//  [python]
def take(x, indices, axis=None):
    if isinstance(indices, tf.SparseTensor):
        if x.dtype not in (tf.float16, tf.float32, tf.float64, tf.bfloat16):
            warnings.warn(
                "`take` with the TensorFlow backend does not support "
                f"`x.dtype={x.dtype}` when `indices` is a sparse tensor; "
                "densifying `indices`."
            )
            return tfnp.take(x, tf.sparse.to_dense(indices), axis=axis)
        if axis is None:
            x = tf.reshape(x, (-1,))
        elif axis != 0:
            warnings.warn(
                "`take` with the TensorFlow backend does not support "
                f"`axis={axis}` when `indices` is a sparse tensor; "
                "densifying `indices`."
            )
            return tfnp.take(x, tf.sparse.to_dense(indices), axis=axis)
        return tf.nn.safe_embedding_lookup_sparse(
            embedding_weights=x,
            sparse_ids=tf.sparse.expand_dims(indices, axis=-1),
            default_id=0,
        )
    return tfnp.take(x, indices, axis=axis)
//  [python]
def test_categorical_cols_to_hash_bucket():
    tf.compat.v1.reset_default_graph()
    some_sparse_column = tf.feature_column.categorical_column_with_hash_bucket(
        'sparse_feature', hash_bucket_size=5)#稀疏矩阵，单独放进去会出错
   
    builder = _LazyBuilder({
          'sparse_feature': [['a'], ['x']],
      })
    id_weight_pair = some_sparse_column._get_sparse_tensors(builder) #

    with tf.compat.v1.Session() as sess:
        
        id_tensor_eval = id_weight_pair.id_tensor.eval()
        print("稀疏矩阵：\n",id_tensor_eval)
          
        dense_decoded = tf.sparse_to_dense( id_tensor_eval, [2,1],-1).eval(session=sess)
        print("稠密矩阵：\n",dense_decoded)
//  [python]
def test_categorical_cols_to_hash_bucket():
    tf.compat.v1.reset_default_graph()
    some_sparse_column = tf.feature_column.categorical_column_with_hash_bucket(
        'sparse_feature', hash_bucket_size=5)#稀疏矩阵，单独放进去会出错
   
    builder = _LazyBuilder({
          'sparse_feature': [['a'], ['x']],
      })
    id_weight_pair = some_sparse_column._get_sparse_tensors(builder) #

    with tf.compat.v1.Session() as sess:
        
        id_tensor_eval = id_weight_pair.id_tensor.eval()
        print("稀疏矩阵：\n",id_tensor_eval)
          
        dense_decoded = tf.sparse.to_dense( id_tensor_eval, default_value=-1).eval(session=sess)
        print("稠密矩阵：\n",dense_decoded)
//  [python]
def test_with_1d_sparse_tensor():
    tf.compat.v1.reset_default_graph()
    #混合散列
    body_style = tf.feature_column.categorical_column_with_vocabulary_list(
        'name', vocabulary_list=['anna', 'gary', 'bob'],num_oov_buckets=2)   #稀疏矩阵
    
    #稠密矩阵
    builder = _LazyBuilder({
        'name': ['anna', 'gary','alsa'],        
      })
    
    #稀疏矩阵
    builder2 = _LazyBuilder({
        'name': tf.SparseTensor(
        indices=((0,), (1,), (2,)),
        values=('anna', 'gary', 'alsa'),
        dense_shape=(3,)),    
      })    

    id_weight_pair = body_style._get_sparse_tensors(builder)    #
    id_weight_pair2 = body_style._get_sparse_tensors(builder2)  #


    with tf.compat.v1.Session() as sess:
        sess.run(lookup_ops.tables_initializer())

        id_tensor_eval = id_weight_pair.id_tensor.eval()
        print("稀疏矩阵：\n",id_tensor_eval)
        id_tensor_eval2 = id_weight_pair2.id_tensor.eval()
        print("稀疏矩阵2：\n",id_tensor_eval2)
          
        dense_decoded = tf.sparse_to_dense( id_tensor_eval, id_tensor_eval.dense_shape, default_value=-1).eval(session=sess)
        print("稠密矩阵：\n",dense_decoded)
//  [python]
def test_with_1d_sparse_tensor():
    tf.compat.v1.reset_default_graph()
    #混合散列
    body_style = tf.feature_column.categorical_column_with_vocabulary_list(
        'name', vocabulary_list=['anna', 'gary', 'bob'],num_oov_buckets=2)   #稀疏矩阵
    
    #稠密矩阵
    builder = _LazyBuilder({
        'name': ['anna', 'gary','alsa'],        
      })
    
    #稀疏矩阵
    builder2 = _LazyBuilder({
        'name': tf.SparseTensor(
        indices=((0,), (1,), (2,)),
        values=('anna', 'gary', 'alsa'),
        dense_shape=(3,)),    
      })    

    id_weight_pair = body_style._get_sparse_tensors(builder)    #
    id_weight_pair2 = body_style._get_sparse_tensors(builder2)  #


    with tf.compat.v1.Session() as sess:
        sess.run(lookup_ops.tables_initializer())

        id_tensor_eval = id_weight_pair.id_tensor.eval()
        print("稀疏矩阵：\n",id_tensor_eval)
        id_tensor_eval2 = id_weight_pair2.id_tensor.eval()
        print("稀疏矩阵2：\n",id_tensor_eval2)
          
        dense_decoded = tf.sparse.to_dense( id_tensor_eval, default_value=-1).eval(session=sess)
        print("稠密矩阵：\n",dense_decoded)
//  [python]
def predict_fn( lexicon, lexicon_prior ):
    """Returns a function that runs the model on the input data 
       (e.g., for validation)"""

    def predict( features, labels, mode ):

        logits, sequence_length = _get_image_info(features, mode)
        
        predictions, log_probs = _get_output( logits, sequence_length,
                                              lexicon, lexicon_prior )

        if lexicon:
            # TFWordBeamSearch produces only a single value, but its
            # given dense shape is the original sequence length
            # dense_to_sparse_tight in_get_output should filter out
            # the excess, but we set the dense fill value to ctc_blank
            # now to catch any potential errors/bugs downstream later
            ctc_blank = (logits.shape[2]-1)
            final_pred = tf.sparse_to_dense( predictions[0],
                                             default_value=ctc_blank ) 
        else:
        # tf.nn.ctc_beam_search produces SparseTensor but EstimatorSpec
        # predictions only takes dense tensors
            final_pred = tf.sparse_to_dense( predictions[0], 
                                             default_value=0 ) 
        
        return tf.estimator.EstimatorSpec( mode=mode,
                                           predictions={ 'labels': final_pred,
                                                         'score': log_probs })

    return predict
//  [python]
def predict_fn( lexicon, lexicon_prior ):
    """Returns a function that runs the model on the input data 
       (e.g., for validation)"""

    def predict( features, labels, mode ):

        logits, sequence_length = _get_image_info(features, mode)
        
        predictions, log_probs = _get_output( logits, sequence_length,
                                              lexicon, lexicon_prior )

        if lexicon:
            # TFWordBeamSearch produces only a single value, but its
            # given dense shape is the original sequence length
            # dense_to_sparse_tight in_get_output should filter out
            # the excess, but we set the dense fill value to ctc_blank
            # now to catch any potential errors/bugs downstream later
            ctc_blank = (logits.shape[2]-1)
            final_pred = tf.sparse.to_dense( predictions[0],
                                             default_value=ctc_blank ) 
        else:
        # tf.nn.ctc_beam_search produces SparseTensor but EstimatorSpec
        # predictions only takes dense tensors
            final_pred = tf.sparse.to_dense( predictions[0], 
                                             default_value=0 ) 
        
        return tf.estimator.EstimatorSpec( mode=mode,
                                           predictions={ 'labels': final_pred,
                                                         'score': log_probs })

    return predict
//  [python]
def sparse_to_dense(x, default_value=None):
    x_shape = x.shape
    if x_shape.rank == 0:
        # Workaround for bug on GPU when sparse tensor represents a scalar.
        if x.values.shape[0] == 0:
            return tf.constant(default_value, dtype=x.dtype)
        else:
            return tf.reshape(x.values, ())
    x = tf.sparse.to_dense(x, default_value=default_value)
    x.set_shape(x_shape)
    return x
//  [python]
def decode_sparse_record(record, name_to_features):
  """Decodes a sparse record to a TensorFlow example."""
  example = tf.io.parse_single_example(record, name_to_features)

  # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
  # So cast all int64 to int32.
  for name in list(example.keys()):
    t = example[name]
    if t.dtype == tf.int64:
      t = tf.cast(t, tf.int32)
    example[name] = t.to_dense()

  return example
//  [python]
def decode_sparse_record(record, name_to_features):
  """Decodes a sparse record to a TensorFlow example."""
  example = tf.io.parse_single_example(record, name_to_features)

  # tf.Example only supports tf.int64, but the TPU only supports tf.int32.
  # So cast all int64 to int32.
  for name in list(example.keys()):
    t = example[name]
    if t.dtype == tf.int64:
      t = tf.cast(t, tf.int32)
    example[name] = tf.sparse.to_dense(t)

  return example
//  [python]
def _density_tensor(value: AnyTensor, semantic: Semantic, base_key: str,
                    missing_value: Union[float, int, str]) -> tf.Tensor:
  """Density a possibly sparse tensor."""

  if isinstance(value, tf.SparseTensor) or isinstance(
      value, tf.compat.v1.SparseTensorValue):

    common_error = (
        "If the feature is multi-dimensional, use a static shape "
        "(or a tensor != sparse tensor). If the feature is a set, specify the "
        "set semantic manually.")

    if len(value.shape) != 2:
      raise ValueError("Expect rank 2 for tensor {}".format(value))

    if value.shape[1] is None:
      # The shape is not known at compilation time.
      tf.debugging.Assert(value.dense_shape[1] == 1, [
          "{} with tensor {} is provided as a "
          "sparse tensor with dynamic shape. Such feature can only be scalar "
          "but multiple values have been observed at the same time.".format(
              base_key, value)
      ])

    else:
      if value.shape[1] != 1:
        raise ValueError(
            "Invalid static shape for feature {} represented as sparse tensor "
            "{}. Expect 1, got {}. {}".format(base_key, value, value.shape[1],
                                              common_error))

    value = tf.sparse.to_dense(value, default_value=missing_value)

  elif isinstance(value, tf.RaggedTensor):
    raise ValueError(
        "Ragged tensors are not supported for feature {} with scalar type {} "
        "and semantic {}. If the feature is a set, specify the set semantic "
        "manually.".format(base_key, value, semantic))

  elif isinstance(value, tf.Tensor):
    pass  # Native format.

  else:
    raise ValueError("Unsupported tensor type: {}".format(value))

  return value
//  [python]
def testScaleMinMaxSparsePerKey(
      self, input_data, input_metadata, expected_data):
    def preprocessing_fn(inputs):
      x_scaled = tf.sparse.to_dense(
          tft.scale_to_0_1_per_key(inputs['x'], inputs['key']))
      x_scaled.set_shape([None, 4])
      return {'x_scaled': x_scaled}

    expected_metadata = tft.DatasetMetadata.from_feature_spec(
        {'x_scaled': tf.io.FixedLenFeature([4], tf.float32)})

    self.assertAnalyzeAndTransformResults(input_data, input_metadata,
                                          preprocessing_fn, expected_data,
                                          expected_metadata)
//  [python]
def get_metric_ops(
      self, features_dict: types.TensorTypeMaybeDict,
      predictions_dict: types.TensorTypeMaybeDict,
      labels_dict: types.TensorTypeMaybeDict
  ) -> Dict[str, Tuple[types.TensorType, types.TensorType]]:

    squeezed_weights = None
    if self._example_weight_key:
      squeezed_weights = _flatten_to_one_dim(
          features_dict[self._example_weight_key])

    labels = labels_dict
    if self._labels_key:
      labels = labels_dict[self._labels_key]
    if isinstance(labels, tf.SparseTensor):
      if labels.dtype == tf.string:
        labels = tf.sparse_to_dense(labels, default_value='')
      else:
        labels = tf.sparse_to_dense(labels)

    # Expand dims if necessary.
    labels = tf.case(
        [(tf.equal(tf.rank(labels), 1), lambda: tf.expand_dims(labels, -1))],
        default=lambda: labels)

    scores = _get_target_tensor(predictions_dict, self._probabilities_keys)
    classes = _get_target_tensor(predictions_dict, self._classes_keys)
    if (classes is None and
        prediction_keys.PredictionKeys.ALL_CLASSES in self._classes_keys):
      # If no classes were found and ALL_CLASSES is used in the classes_keys
      # then default to using class IDs as the classes. The intended use case is
      # keras model_to_estimator which does not output any classes.
      classes = tf.as_string(_class_ids(scores))  # pytype: disable=wrong-arg-types

    # To support canned Estimators which right now only expose the argmax class
    # id, if labels are ints then then the classes are likely class_ids in
    # string form, so we can automatically expand the classes to the full set
    # for matching the labels (see b/113170729).
    if labels.dtype == tf.int64:
      classes = tf.case(
          # Match only when classes has a single item (i.e. argmax).
          [(tf.equal(tf.shape(input=classes)[-1],
                     1), lambda: tf.as_string(_class_ids(scores)))],  # pytype: disable=wrong-arg-types
          default=lambda: classes)

    if classes is None:
      raise ValueError('Could not determine classes for use with '
                       'PrecisionRecallAtK. The classes_key ({}) could'
                       'not be found in predictions dict ({}) and the metric '
                       'was not configured to use ALL_CLASSES.'.format(
                           self._classes_keys, predictions_dict))
    labels = _cast_or_convert(labels, classes.dtype)

    if self._metric_name == metric_keys.PRECISION_AT_K:
      metric_ops = metrics.precision_at_k(  # pytype: disable=wrong-arg-types
          classes, scores, labels, self._cutoffs, squeezed_weights)
    else:
      metric_ops = metrics.recall_at_k(  # pytype: disable=wrong-arg-types
          classes, scores, labels, self._cutoffs, squeezed_weights)

    return {self._metric_key(self._metric_name): metric_ops}
//  [python]
def get_metric_ops(
      self, features_dict: types.TensorTypeMaybeDict,
      predictions_dict: types.TensorTypeMaybeDict,
      labels_dict: types.TensorTypeMaybeDict
  ) -> Dict[str, Tuple[types.TensorType, types.TensorType]]:

    squeezed_weights = None
    if self._example_weight_key:
      squeezed_weights = _flatten_to_one_dim(
          features_dict[self._example_weight_key])

    labels = labels_dict
    if self._labels_key:
      labels = labels_dict[self._labels_key]
    if isinstance(labels, tf.SparseTensor):
      if labels.dtype == tf.string:
        labels = tf.sparse.to_dense(labels, default_value='')
      else:
        labels = tf.sparse.to_dense(labels)

    # Expand dims if necessary.
    labels = tf.case(
        [(tf.equal(tf.rank(labels), 1), lambda: tf.expand_dims(labels, -1))],
        default=lambda: labels)

    scores = _get_target_tensor(predictions_dict, self._probabilities_keys)
    classes = _get_target_tensor(predictions_dict, self._classes_keys)
    if (classes is None and
        prediction_keys.PredictionKeys.ALL_CLASSES in self._classes_keys):
      # If no classes were found and ALL_CLASSES is used in the classes_keys
      # then default to using class IDs as the classes. The intended use case is
      # keras model_to_estimator which does not output any classes.
      classes = tf.as_string(_class_ids(scores))  # pytype: disable=wrong-arg-types

    # To support canned Estimators which right now only expose the argmax class
    # id, if labels are ints then then the classes are likely class_ids in
    # string form, so we can automatically expand the classes to the full set
    # for matching the labels (see b/113170729).
    if labels.dtype == tf.int64:
      classes = tf.case(
          # Match only when classes has a single item (i.e. argmax).
          [(tf.equal(tf.shape(input=classes)[-1],
                     1), lambda: tf.as_string(_class_ids(scores)))],  # pytype: disable=wrong-arg-types
          default=lambda: classes)

    if classes is None:
      raise ValueError('Could not determine classes for use with '
                       'PrecisionRecallAtK. The classes_key ({}) could'
                       'not be found in predictions dict ({}) and the metric '
                       'was not configured to use ALL_CLASSES.'.format(
                           self._classes_keys, predictions_dict))
    labels = _cast_or_convert(labels, classes.dtype)

    if self._metric_name == metric_keys.PRECISION_AT_K:
      metric_ops = metrics.precision_at_k(  # pytype: disable=wrong-arg-types
          classes, scores, labels, self._cutoffs, squeezed_weights)
    else:
      metric_ops = metrics.recall_at_k(  # pytype: disable=wrong-arg-types
          classes, scores, labels, self._cutoffs, squeezed_weights)

    return {self._metric_key(self._metric_name): metric_ops}
//  [python]
def call(self, input_context: Dict[str, tf.Tensor]) -> tf.Tensor:
    """Call function of the feature group encoder layer.

    Takes in input context tensor dictionary, generates embedding for features
    in the group, concatenate them up and encode them according to the
    specified encoder type.

    Feature embedding layers will have mask_zero enabled, and generated feature
    embeddings will be masked if any of the feature value is 0. In most
    cases, the mask of all features in the same group should be the same. This
    function treats the input config generically.

    Args:
      input_context: A dictionary mapping input context feature name to tenors.

    Returns:
      Feature group encoding.
    """
    embedding_feature_shape = []
    embeddings = []
    masks = []
    for feature_name, embedding_layer in sorted(
        self._embedding_layer_dict.items()):
      input_feature = input_context[feature_name]
      if isinstance(input_feature, tf.SparseTensor):
        input_feature = tf.sparse.to_dense(input_feature)
      if not embedding_feature_shape:
        embedding_feature_shape = input_feature.shape.as_list()
      assert input_feature.shape.as_list() == embedding_feature_shape

      embeddings.append(embedding_layer(input_feature))
      masks.append(embedding_layer.compute_mask(input_feature))
    # Collect nonembedding feature values.
    for feature_name in self._nonembedding_feature_names:
      input_feature = input_context[feature_name]
      if isinstance(input_feature, tf.SparseTensor):
        input_feature = tf.sparse.to_dense(input_feature)
      embeddings.append(tf.expand_dims(input_feature, -1))
    embedding = tf.concat(embeddings, -1)

    # Set mask_shape with batch dim = 1. Compatible to unknown batch size.
    mask_shape = [1] + embedding_feature_shape[1:]
    mask = tf.ones(shape=mask_shape)
    for m in masks:
      mask = mask * tf.cast(m, 'float32')
    mask = tf.expand_dims(mask, -1)
    embedding = embedding * mask

    if self._feature_group.encoder_type == input_config_pb2.EncoderType.BOW:
      weighted_summed_context_embedding = tf.keras.backend.sum(
          embedding, axis=1)
      total_weights = tf.ones_like(
          weighted_summed_context_embedding) * tf.keras.backend.sum(
              mask, axis=1)
      embedding = safe_div(weighted_summed_context_embedding, total_weights)
    elif self._feature_group.encoder_type == input_config_pb2.EncoderType.CNN:
      for conv1d_layer in self._conv1d_layers:
        embedding = conv1d_layer(embedding)
      embedding = tf.math.reduce_max(embedding, axis=1)
    elif self._feature_group.encoder_type == input_config_pb2.EncoderType.LSTM:
      embedding = self._lstm_layer(embedding)
    else:
      raise ValueError('Unsupported encoder type %s.' %
                       self._feature_group.encoder_type)

    return embedding
//  [python]
def test_adaptive_weights(self, budget, exponent):
    # 5 users and 3 items.
    col_data = tf.SparseTensor(
        indices=np.array([
            [0, 1], [0, 2], [0, 4],
            [1, 0],
            [2, 0], [2, 1], [2, 2], [2, 3], [2, 4],
        ]),
        values=tf.ones([9]), dense_shape=[3, 5])
    counts = [3, 1, 5]
    ws = dpam._compute_adaptive_weights(col_data, counts, exponent, budget)
    sp_weights = col_data.with_values(ws)
    dense_ws = tf.sparse_to_dense(sp_weights.indices, sp_weights.dense_shape, sp_weights.values, default_value=np.nan)
    # Verify that for each user, the squared weights sum to the budget
    self.assertAllClose(
        np.nansum(dense_ws*dense_ws, axis=0), budget*np.ones(5),
        rtol=1e-5)
//  [python]
def test_adaptive_weights(self, budget, exponent):
    # 5 users and 3 items.
    col_data = tf.SparseTensor(
        indices=np.array([
            [0, 1], [0, 2], [0, 4],
            [1, 0],
            [2, 0], [2, 1], [2, 2], [2, 3], [2, 4],
        ]),
        values=tf.ones([9]), dense_shape=[3, 5])
    counts = [3, 1, 5]
    ws = dpam._compute_adaptive_weights(col_data, counts, exponent, budget)
    sp_weights = col_data.with_values(ws)
    dense_ws = tf.sparse.to_dense(sp_weights, default_value=np.nan)
    # Verify that for each user, the squared weights sum to the budget
    self.assertAllClose(
        np.nansum(dense_ws*dense_ws, axis=0), budget*np.ones(5),
        rtol=1e-5)
//  [python]
def decode(self, serialized_example):
    """Parses a single tf.Example into image and label tensors."""
    result = {}
    context, sequences = tf.io.parse_single_sequence_example(
        serialized_example, self._context_description,
        self._sequence_description)
    result.update(context)
    result.update(sequences)
    for key, value in result.items():
      if isinstance(value, tf.SparseTensor):
        result[key] = tf.sparse_to_dense(value.indices, value.dense_shape, value.values)
    return result
//  [python]
def decode(self, serialized_example):
    """Parses a single tf.Example into image and label tensors."""
    result = {}
    context, sequences = tf.io.parse_single_sequence_example(
        serialized_example, self._context_description,
        self._sequence_description)
    result.update(context)
    result.update(sequences)
    for key, value in result.items():
      if isinstance(value, tf.SparseTensor):
        result[key] = tf.sparse.to_dense(value)
    return result
//  [python]
def testScaleToZScoreSparse(self, input_dtype, elementwise):
    def preprocessing_fn(inputs):
      z_score = tf.sparse.to_dense(
          tft.scale_to_z_score(
              tf.cast(inputs['x'], input_dtype), elementwise=elementwise),
          default_value=np.nan)
      z_score.set_shape([None, 4])
      self.assertEqual(z_score.dtype, _mean_output_dtype(input_dtype))
      return {
          'x_scaled': tf.cast(z_score, tf.float32)
      }

    input_data = [
        {'idx': [0, 1], 'val': [-4, 10]},
        {'idx': [0, 1], 'val': [2, 4]},
    ]
    input_metadata = tft.DatasetMetadata.from_feature_spec({
        'x':
            tf.io.SparseFeature('idx', 'val',
                                tft_unit.canonical_numeric_dtype(input_dtype),
                                4)
    })
    if elementwise:
      # Mean(x) = [-1, 7]
      # Var(x) = [9, 9]
      # StdDev(x) = [3, 3]
      expected_data = [
          {
              'x_scaled': [-1., 1.,
                           float('nan'),
                           float('nan')]  # [(-4 +1 ) / 3, (10 -7) / 3]
          },
          {
              'x_scaled': [1., -1.,
                           float('nan'),
                           float('nan')]  # [(2 + 1) / 3, (4 - 7) / 3]
          }
      ]
    else:
      # Mean = 3
      # Var = 25
      # Std Dev = 5
      expected_data = [
          {
              'x_scaled': [-1.4, 1.4, float('nan'),
                           float('nan')]  # [(-4 - 3) / 5, (10 - 3) / 5]
          },
          {
              'x_scaled': [-.2, .2, float('nan'),
                           float('nan')]  # [(2 - 3) / 5, (4 - 3) / 5]
          }
      ]
    if input_dtype.is_floating:
      input_data.append({'idx': [0, 1], 'val': [np.nan, np.nan]})
      expected_data.append({
          'x_scaled': [float('nan'),
                       float('nan'),
                       float('nan'),
                       float('nan')]
      })
    expected_metadata = tft.DatasetMetadata.from_feature_spec(
        {'x_scaled': tf.io.FixedLenFeature([4], tf.float32)})
    self.assertAnalyzeAndTransformResults(input_data, input_metadata,
                                          preprocessing_fn, expected_data,
                                          expected_metadata)
//  [python]
def asnumpy(input):
    if isinstance(input, tf.SparseTensor):
        # tf.sparse.to_dense assume sorted indices, need to turn off validate_indices in our cases
        return tf.sparse_tensor_to_dense(input, validate_indices=False).numpy()
    else:
        return input.numpy()
//  [python]
def asnumpy(input):
    if isinstance(input, tf.SparseTensor):
        # tf.sparse.to_dense assume sorted indices, need to turn off validate_indices in our cases
        return tf.sparse.to_dense(input, validate_indices=False).numpy()
    else:
        return input.numpy()
//  [python]
def _split_string_with_delimiter(self, data, delimiter):
    """
    only applicable to string columns.
    """
    data = tf.sparse.to_dense(data)
    # this method acts differently compared to tf.strings.split
    # this will split the string based on multiple delimiters while
    # the latter will split the string based on a single delimiter.
    fn = lambda data: tf.compat.v1.string_split(
        data, delimiter, result_type='RaggedTensor')
    # tf.compat.v1.string_split works on a single string. Use tf.map_fn
    # to apply the function on each element of the input data.
    data = tf.map_fn(
        fn,
        data,
        fn_output_signature=tf.RaggedTensorSpec(
            tf.TensorShape([None, None]), tf.string))
    data = data.values.to_sparse()
    # the columns of the sparse tensor are suffixed with $indices, $values
    # related to sparse tensor. Create a new sparse tensor by extracting
    # the indices, values and dense_shape from the original sparse tensor
    # to preserve the original column name.
    data = tf.sparse.SparseTensor(
        indices=data.indices, values=data.values, dense_shape=data.dense_shape)
    # for list of string, batch dimensions becomes inverted after tf.map_fn,
    #  transpose the data to get the original shape.
    if tf.shape(data)[1] == 1:
      data = tf.sparse.transpose(data)
    return data
//  [python]
def testScaleToZScoreSparsePerKey(self, input_dtype):
    # TODO(b/131852830) Add elementwise tests.
    def preprocessing_fn(inputs):
      z_score = tf.sparse_tensor_to_dense(
          tft.scale_to_z_score_per_key(
              tf.cast(inputs['x'], input_dtype),
              inputs['key'],
              elementwise=False),
          default_value=np.nan)
      z_score.set_shape([None, 4])
      self.assertEqual(z_score.dtype, _mean_output_dtype(input_dtype))
      return {
          'x_scaled': tf.cast(z_score, tf.float32)
      }

    input_data = [
        {'idx': [0, 1], 'val': [-4, 10], 'key_idx': [0, 1], 'key': ['a', 'a']},
        {'idx': [0, 1], 'val': [2, 1], 'key_idx': [0, 1], 'key': ['a', 'b']},
        {'idx': [0, 1], 'val': [-1, 4], 'key_idx': [0, 1], 'key': ['b', 'a']},
    ]
    input_metadata = tft.DatasetMetadata.from_feature_spec({
        'key':
            tf.io.SparseFeature('key_idx', 'key', tf.string, 4),
        'x':
            tf.io.SparseFeature('idx', 'val',
                                tft_unit.canonical_numeric_dtype(input_dtype),
                                4)
    })
    # 'a':
    # Mean = 3
    # Var = 25
    # Std Dev = 5
    # 'b':
    # Mean = 0
    # Var = 1
    # Std Dev = 1
    expected_data = [
        {
            'x_scaled': [-1.4, 1.4, float('nan'),
                         float('nan')]  # [(-4 - 3) / 5, (10 - 3) / 5]
        },
        {
            'x_scaled': [-.2, 1., float('nan'),
                         float('nan')]  # [(2 - 3) / 5, (1 - 0) / 1]
        },
        {
            'x_scaled': [-1., .2,
                         float('nan'),
                         float('nan')]  # [(-1 - 0) / 1, (4 - 3) / 5]
        }
    ]
    if input_dtype.is_floating:
      input_data.append({
          'idx': [0, 1],
          'val': [np.nan, np.nan],
          'key_idx': [0, 1],
          'key': ['a', 'b']
      })
      expected_data.append({
          'x_scaled': [float('nan'),
                       float('nan'),
                       float('nan'),
                       float('nan')]
      })
    expected_metadata = tft.DatasetMetadata.from_feature_spec(
        {'x_scaled': tf.io.FixedLenFeature([4], tf.float32)})
    self.assertAnalyzeAndTransformResults(input_data, input_metadata,
                                          preprocessing_fn, expected_data,
                                          expected_metadata)
//  [python]
def testScaleToZScoreSparsePerKey(self, input_dtype):
    # TODO(b/131852830) Add elementwise tests.
    def preprocessing_fn(inputs):
      z_score = tf.sparse.to_dense(
          tft.scale_to_z_score_per_key(
              tf.cast(inputs['x'], input_dtype),
              inputs['key'],
              elementwise=False),
          default_value=np.nan)
      z_score.set_shape([None, 4])
      self.assertEqual(z_score.dtype, _mean_output_dtype(input_dtype))
      return {
          'x_scaled': tf.cast(z_score, tf.float32)
      }

    input_data = [
        {'idx': [0, 1], 'val': [-4, 10], 'key_idx': [0, 1], 'key': ['a', 'a']},
        {'idx': [0, 1], 'val': [2, 1], 'key_idx': [0, 1], 'key': ['a', 'b']},
        {'idx': [0, 1], 'val': [-1, 4], 'key_idx': [0, 1], 'key': ['b', 'a']},
    ]
    input_metadata = tft.DatasetMetadata.from_feature_spec({
        'key':
            tf.io.SparseFeature('key_idx', 'key', tf.string, 4),
        'x':
            tf.io.SparseFeature('idx', 'val',
                                tft_unit.canonical_numeric_dtype(input_dtype),
                                4)
    })
    # 'a':
    # Mean = 3
    # Var = 25
    # Std Dev = 5
    # 'b':
    # Mean = 0
    # Var = 1
    # Std Dev = 1
    expected_data = [
        {
            'x_scaled': [-1.4, 1.4, float('nan'),
                         float('nan')]  # [(-4 - 3) / 5, (10 - 3) / 5]
        },
        {
            'x_scaled': [-.2, 1., float('nan'),
                         float('nan')]  # [(2 - 3) / 5, (1 - 0) / 1]
        },
        {
            'x_scaled': [-1., .2,
                         float('nan'),
                         float('nan')]  # [(-1 - 0) / 1, (4 - 3) / 5]
        }
    ]
    if input_dtype.is_floating:
      input_data.append({
          'idx': [0, 1],
          'val': [np.nan, np.nan],
          'key_idx': [0, 1],
          'key': ['a', 'b']
      })
      expected_data.append({
          'x_scaled': [float('nan'),
                       float('nan'),
                       float('nan'),
                       float('nan')]
      })
    expected_metadata = tft.DatasetMetadata.from_feature_spec(
        {'x_scaled': tf.io.FixedLenFeature([4], tf.float32)})
    self.assertAnalyzeAndTransformResults(input_data, input_metadata,
                                          preprocessing_fn, expected_data,
                                          expected_metadata)
//  [python]
def apply(self, features: image_ops.Features) -> image_ops.Features:
    # Draw candidate negative labels:
    candidate_labels = self.queue.dequeue_many(self.total_num_negatives * 2)

    # Fill queue back up:
    labels_to_enqueue = tf.concat([
        features[modalities.INSTANCE_TEXT_LABELS],
        features[modalities.NEGATIVE_TEXT_LABELS],
        candidate_labels,
    ], axis=0)
    labels_to_enqueue = tf.boolean_mask(
        labels_to_enqueue, tf.not_equal(labels_to_enqueue, PADDING_QUERY),
        name='labels_to_enqueue')
    target_size = self.queue_capacity // 2
    needed_elements = tf.clip_by_value(
        target_size - self.queue.size(), 0, tf.size(labels_to_enqueue))
    enqueue_op = self.queue.enqueue_many(
        tf.slice(labels_to_enqueue, begin=[0], size=[needed_elements]))

    # Get negatives that are not in positives:
    with tf.control_dependencies([enqueue_op]):
      candidate_negatives = tf.sparse.to_dense(
          tf.sets.difference(
              candidate_labels[None, ...],
              features[modalities.INSTANCE_TEXT_LABELS][None, ...]))[0]

    # Set operations sort the labels alphabetically, so we shuffle again:
    candidate_negatives = tf.random.shuffle(candidate_negatives)

    # New negatives contain all the old negatives, plus randomly sampled ones,
    # up to total_num_negatives. In addition, we ensure that padding ('') is not
    # present by including it as first element before applying tf.unique and
    # then slicing it off:
    new_negatives = tf.concat([
        tf.constant([PADDING_QUERY]),
        features[modalities.NEGATIVE_TEXT_LABELS],
        candidate_negatives,
    ], axis=0)
    orig_num_negatives = tf.shape(features[modalities.NEGATIVE_TEXT_LABELS])[0]
    new_num_negatives = tf.maximum(self.total_num_negatives, orig_num_negatives)
    features[modalities.NEGATIVE_TEXT_LABELS] = tf.unique(
        new_negatives)[0][1:(new_num_negatives + 1)]

    # Negative integer labels are now obsolete:
    if modalities.NEGATIVE_LABELS in features:
      logging.info('Removing obsolete field %s from features.',
                   modalities.NEGATIVE_LABELS)
      features.pop(modalities.NEGATIVE_LABELS)

    return features
//  [python]
def init_optimizer(self):
        """
        Init self.optimizer.
        """
        self.learning_rate = tf.keras.optimizers.schedules.ExponentialDecay(self.learning_rate, self.max_iter, 0.1)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate, amsgrad=True)
        if self.use_mixed_precision:
            self.optimizer = mixed_precision.LossScaleOptimizer(self.optimizer, loss_scale=tf.mixed_precision.experimental.DynamicLossScale(initial_loss_scale=2 ** 15, increment_period=1000))
//  [python]
def init_optimizer(self):
        """
        Init self.optimizer.
        """
        self.learning_rate = tf.keras.optimizers.schedules.ExponentialDecay(self.learning_rate, self.max_iter, 0.1)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate, amsgrad=True)
        if self.use_mixed_precision:
            self.optimizer = tf.keras.mixed_precision.LossScaleOptimizer(self.optimizer, dynamic=True, initial_scale=2 ** 15, increment_period=1000)
//  [python]
def init_optimizer(self):
        """
        Init self.optimizer.
        """
        self.learning_rate = tf.keras.optimizers.schedules.ExponentialDecay(self.learning_rate, self.max_iter, 0.1)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate)
        if self.use_mixed_precision:
            self.optimizer = mixed_precision.LossScaleOptimizer(self.optimizer,
                                                                loss_scale=tf.mixed_precision.experimental.DynamicLossScale(initial_loss_scale=2 ** 15, increment_period=1000))
//  [python]
def init_optimizer(self):
        """
        Init self.optimizer.
        """
        self.learning_rate = tf.keras.optimizers.schedules.ExponentialDecay(self.learning_rate, self.max_iter, 0.1)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate)
        if self.use_mixed_precision:
            self.optimizer = tf.keras.mixed_precision.LossScaleOptimizer(self.optimizer,
                                                                dynamic=True, initial_scale=2 ** 15, increment_period=1000)
//  [python]
def init_optimizer(self):
        """
        Init self.optimizer.
        """
        self.learning_rate = tf.keras.optimizers.schedules.ExponentialDecay(self.learning_rate, self.max_iter, 0.1)
        self.optimizer = tf.keras.optimizers.Adam(learning_rate=self.learning_rate, amsgrad=False)
        if self.use_mixed_precision:
            self.optimizer = mixed_precision.LossScaleOptimizer(self.optimizer,
                                                                loss_scale=tf.mixed_precision.experimental.DynamicLossScale(initial_loss_scale=8, increment_period=1000))