B
    ٻd                 @   s   d Z ddlmZ ddlmZ ddlmZ ddlmZ ddlmZ ddl	m
Z
 ddlmZ dd	lmZ ed
eddG dd dZdS )z$Python wrappers for tf.data writers.    )dataset_ops)convert)dtypes)ops)tensor_spec)gen_experimental_dataset_ops)deprecation)	tf_exportz data.experimental.TFRecordWriterNzTo write TFRecords to disk, use `tf.io.TFRecordWriter`. To save and load the contents of a dataset, use `tf.data.experimental.save` and `tf.data.experimental.load`c               @   s"   e Zd ZdZdddZdd ZdS )TFRecordWritera  Writes a dataset to a TFRecord file.

  The elements of the dataset must be scalar strings. To serialize dataset
  elements as strings, you can use the `tf.io.serialize_tensor` function.

  ```python
  dataset = tf.data.Dataset.range(3)
  dataset = dataset.map(tf.io.serialize_tensor)
  writer = tf.data.experimental.TFRecordWriter("/path/to/file.tfrecord")
  writer.write(dataset)
  ```

  To read back the elements, use `TFRecordDataset`.

  ```python
  dataset = tf.data.TFRecordDataset("/path/to/file.tfrecord")
  dataset = dataset.map(lambda x: tf.io.parse_tensor(x, tf.int64))
  ```

  To shard a `dataset` across multiple TFRecord files:

  ```python
  dataset = ... # dataset to be written

  def reduce_func(key, dataset):
    filename = tf.strings.join([PATH_PREFIX, tf.strings.as_string(key)])
    writer = tf.data.experimental.TFRecordWriter(filename)
    writer.write(dataset.map(lambda _, x: x))
    return tf.data.Dataset.from_tensors(filename)

  dataset = dataset.enumerate()
  dataset = dataset.apply(tf.data.experimental.group_by_window(
    lambda i, _: i % NUM_SHARDS, reduce_func, tf.int64.max
  ))

  # Iterate through the dataset to trigger data writing.
  for _ in dataset:
    pass
  ```
  Nc             C   s.   t j|tjdd| _tjd|dtjd| _dS )a^  Initializes a `TFRecordWriter`.

    Args:
      filename: a string path indicating where to write the TFRecord data.
      compression_type: (Optional.) a string indicating what type of compression
        to use when writing the file. See `tf.io.TFRecordCompressionType` for
        what types of compression are available. Defaults to `None`.
    filename)namecompression_type )argument_defaultZargument_dtypeN)r   Zconvert_to_tensorr   string	_filenamer   Zoptional_param_to_tensor_compression_type)selfr   r    r   a/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/data/experimental/ops/writers.py__init__I   s    	zTFRecordWriter.__init__c             C   sx   t |tjs tdt| dt|tg t	j
s\tdt| dt| d| }t|j| j| jS )a  Writes a dataset to a TFRecord file.

    An operation that writes the content of the specified dataset to the file
    specified in the constructor.

    If the file exists, it will be overwritten.

    Args:
      dataset: a `tf.data.Dataset` whose elements are to be written to a file

    Returns:
      In graph mode, this returns an operation which when executed performs the
      write. In eager mode, the write is performed by the method itself and
      there is no return value.

    Raises
      TypeError: if `dataset` is not a `tf.data.Dataset`.
      TypeError: if the elements produced by the dataset are not scalar strings.
    z?Invalid `dataset.` Expected a `tf.data.Dataset` object but got .zInvalid `dataset`. Expected a`dataset` that produces scalar `tf.string` elements, but got a dataset which produces elements with shapes z and types )
isinstancer   Z	DatasetV2	TypeErrortypeZget_structureZis_compatible_withr   Z
TensorSpecr   r   Zget_legacy_output_shapesZget_legacy_output_typesZ_apply_debug_optionsr   Zdataset_to_tf_recordZ_variant_tensorr   r   )r   Zdatasetr   r   r   writeZ   s    
 zTFRecordWriter.write)N)__name__
__module____qualname____doc__r   r   r   r   r   r   r
      s   -
r
   )r   Ztensorflow.python.data.opsr   Ztensorflow.python.data.utilr   Ztensorflow.python.frameworkr   r   r   Ztensorflow.python.opsr   Ztensorflow.python.utilr   Z tensorflow.python.util.tf_exportr	   
deprecatedr
   r   r   r   r   <module>   s   