Serializes a `tf.SparseTensor` into `nested_proto`.

  Args:
    sparse_tensor: An instance of `tf.SparseTensor`.
    nested_proto: A `module_pb2.NestedData` instance to be filled from
      `sparse_tensor`.
    process_leafs: A function to be applied to the leaf valued of the nested
      structure.
    already_processed: Set of already processed objects (used to avoid
      infinite recursion).

def _to_proto_sparse_tensor(sparse_tensor, nested_proto,
                            process_leafs, already_processed):
  """Serializes a `tf.SparseTensor` into `nested_proto`.

  Args:
    sparse_tensor: An instance of `tf.SparseTensor`.
    nested_proto: A `module_pb2.NestedData` instance to be filled from
      `sparse_tensor`.
    process_leafs: A function to be applied to the leaf valued of the nested
      structure.
    already_processed: Set of already processed objects (used to avoid
      infinite recursion).
  """
  already_processed.add(id(sparse_tensor))
  nested_proto.named_tuple.name = _SPARSE_TENSOR_NAME
  for str_key in _SPARSE_TENSOR_FIELD:
    tensor = getattr(sparse_tensor, str_key)
    nested_proto.named_tuple.map[str_key].value = process_leafs(tensor)