code: def _custom_sparse_to_dense_fn():
      # TODO(fardin): ask petastorm team about codecs for sparse and dense vectors and see if that is
      # a better solution
      def custom_sparse_to_dense(custom_sparse_vec, dense_shape):
          # original sparse vector:   v = {1:2.0, 3:.4.5, 5:7.1}
          # custom sparse vector:     v = [3, 1, 3, 5, 2.0, 4.5, 7.1]
          # dense vector:             v = [0, 2.0, 0, 4.5, 0, 7.1]
  
          # Get the first element from custom_sparse_vec. This element is the size of
          # non-zero elements in the original sparse vector.
          sparse_vector_size = tf.cast(
              tf.gather(custom_sparse_vec, 0, axis=0), tf.int32
          )
          sparse_vector_size = tf.reshape(sparse_vector_size, [1])
  
          # get the first sparse_vector_size elements of the custom_sparse_vec which are the
          # indices
          indices_1d = tf.cast(
              tf.slice(
                  custom_sparse_vec,
                  begin=tf.constant([1]),
                  size=sparse_vector_size,
              ),
              tf.int64,
          )
          indices_reshaped = tf.reshape(
              indices_1d,
              tf.concat([sparse_vector_size, tf.constant([1])], 0),
          )
          # have to pad the indices to match the expected format by the SparseTensor
          indices = tf.pad(
              indices_reshaped, [[0, 0], [1, 0]], "CONSTANT"
          )
  
          # get the second sparse_vector_size elements of the custom_sparse_vec which are
          # the values
          begin_index = sparse_vector_size + tf.constant(1)
          values = tf.slice(
              custom_sparse_vec,
              begin=begin_index,
              size=sparse_vector_size,
          )
  
          # construct a sparse vector with the indices and values
          dense_shape = [1, dense_shape]
          sparse_tensor = tf.sparse.SparseTensor(
              indices=indices, values=values, dense_shape=dense_shape
          )
          # convert the sparse vector into a dense vector
          return tf.sparse.to_dense(sparse_tensor)
  
      return custom_sparse_to_dense
  
imports: import io
  from packaging import version
  import h5py
  import numpy
  import tensorflow
  from horovod.runner.common.util import codec
  from horovod.spark.common import constants, params
  from horovod.spark.keras import optimizer, remote
label_desc: Use image node_id 4 for calling a global function with example usage: _custom_sparse_to_dense_fn() and returns: custom_sparse_to_dense, tf