class_docstr: File writer for TFRecord dataset.
  
  TFRecordWriter serializes tensors in tf.Example schema and write them
  into files in TFRecord format. The written file can be loaded with
  tf.data.TFRecordDataset.
class_docstr_tok: ["File","writer","for","TFRecord","dataset",".","TFRecordWriter","serializes","tensors","in","tf.Example","schema","and","write","them","into","files","in","TFRecord","format",".","The","written","file","can","be","loaded","with","tf.data.TFRecordDataset","."]
code: def _add_tensor_to_features(
      self, generator, feature_name, tensor, features
  ):
      spec = generator.spec
      if isinstance(spec, tf.TensorSpec):
          self._add_dense_tensor_to_features(
              feature_name, tensor.numpy(), tensor.dtype, features
          )
      elif isinstance(spec, tf.SparseTensorSpec):
          if (
              issubclass(
                  generator.get_generator_cls(),
                  VarLenTensorGeneratorBase,
              )
              and spec.shape.rank == 1
          ):
              self._add_dense_tensor_to_features(
                  feature_name,
                  tf.sparse.to_dense(tensor).numpy(),
                  tensor.dtype,
                  features,
              )
          elif (
              issubclass(
                  generator.get_generator_cls(),
                  SparseTensorGeneratorBase,
              )
              and spec.shape.is_fully_defined()
          ):
              self._add_sparse_tensor_to_features(
                  feature_name, tensor, features
              )
          else:
              raise ValueError(
                  "SparseTensorSpec shape must be either a 1D varlen tensor from VarLenTensorGenerator "
                  f"or fully defined sparse tensor from SparseTensorGenerator. Found {spec}"
              )
      else:
          raise TypeError(
              f"Spec {spec} is not supported in TFRecordWriter"
          )
  
file_path: io/tests/test_atds_avro/utils/tf_record_writer.py
imports: import os
  from tests.test_atds_avro.utils.generator.tensor_generator import TensorGeneratorBase
  from tests.test_atds_avro.utils.generator.sparse_tensor_generator import SparseTensorGeneratorBase
  from tests.test_atds_avro.utils.generator.varlen_tensor_generator import VarLenTensorGeneratorBase
  import numpy
  import tensorflow
  from tests.test_atds_avro.utils.file_writer import FileWriter
label_desc: Use image node_id 8 for calling the TFRecordWriter obj's underlying member method code with example usage: obj._add_tensor_to_features(generator, feature_name, tensor, features) without return types