B
    ٻdD                 @   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	 Z
d
d Zdd Zdd Zdd Zdd Zdd ZdS )z/Python dataset sparse tensor utility functions.    )nest)dtypes)ops)sparse_tensor)tensor_shape)
sparse_opsc             C   s   t dd t| D S )zChecks for sparse tensor.

  Args:
    classes: a structure of objects that identify the dataset item classes

  Returns:
    `True` if `classes` contains a sparse tensor type and `False` otherwise.
  c             s   s   | ]}|t jkV  qd S )N)r   SparseTensor).0c r   T/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/data/util/sparse.py	<genexpr>!   s    zany_sparse.<locals>.<genexpr>)anyr   flatten)classesr   r   r   
any_sparse   s    	r   c          	   C   s,   t | dd tt | t |D }|S )a  Converts sparse tensor shapes to their physical shapes.

  Args:
    shapes: a structure of shapes to convert.
    classes: a structure of objects that identify the dataset item classes

  Returns:
    a structure matching the nested structure of `shapes`, containing
    `tensor_shape.unknown_shape()` at positions where `classes` contains
    `tf.sparse.SparseTensor` and matching contents of `shapes` otherwise
  c             S   s&   g | ]\}}|t jkrt n|qS r   )r   r   r   Zunknown_shape)r	   shaper
   r   r   r   
<listcomp>1   s   z#as_dense_shapes.<locals>.<listcomp>)r   pack_sequence_aszipr   )shapesr   retr   r   r   as_dense_shapes$   s    r   c          	   C   s,   t | dd tt | t |D }|S )a  Converts sparse tensor types to `dtypes.variant`.

  Args:
    types: a structure of types to convert.
    classes: a structure of objects that identify the dataset item classes

  Returns:
    a structure matching the nested structure of `types`, containing
    `dtypes.variant` at positions where `classes` contains
    `tf.sparse.SparseTensor` and matching contents of `types` otherwise
  c             S   s$   g | ]\}}|t jkrtjn|qS r   )r   r   r   variant)r	   tyr
   r   r   r   r   D   s   z"as_dense_types.<locals>.<listcomp>)r   r   r   r   )typesr   r   r   r   r   as_dense_types7   s    r   c             C   s<   t |dd tt | t |t |t |D }|S )a  Deserializes sparse tensors.

  Args:
    tensors: a structure of tensors to deserialize.
    types: a structure that holds information about types of `tensors`
    shapes: a structure that holds information about shapes of `tensors`
    classes: a structure of objects that identify the dataset item classes

  Returns:
    `tensors` with any serialized sparse tensors replaced by their deserialized
    version.
  c             S   s4   g | ],\}}}}|t jkr,tj|||jd n|qS ))ZdtypeZrank)r   r   r   Zdeserialize_sparseZndims)r	   tensorr   r   r
   r   r   r   r   X   s   z.deserialize_sparse_tensors.<locals>.<listcomp>)r   r   r   r   )tensorsr   r   r   r   r   r   r   deserialize_sparse_tensorsJ   s    r   c             C   s   t | dd t | D S )a/  Gets classes for a structure of tensors.

  Args:
    tensors: the tensor structure to get classes for.

  Returns:
    a structure matching the nested structure of `tensors`, containing
    `tf.sparse.SparseTensor` at positions where `tensors` contains a sparse
    tensor and `tf.Tensor` otherwise.
  c             S   s$   g | ]}t |tjrtjntjqS r   )
isinstancer   r   r   ZTensor)r	   r   r   r   r   r   m   s   zget_classes.<locals>.<listcomp>)r   r   r   )r   r   r   r   get_classesa   s    r!   c             C   s    t | dd t | D }|S )zSerializes many sparse tensors into a batch.

  Args:
    tensors: a tensor structure to serialize.

  Returns:
    `tensors` with any sparse tensors replaced by the serialized batch.
  c             S   s*   g | ]"}t |r"tj|tjd n|qS ))out_type)r   Z	is_sparser   Zserialize_many_sparser   r   )r	   r   r   r   r   r   ~   s   z1serialize_many_sparse_tensors.<locals>.<listcomp>)r   r   r   )r   r   r   r   r   serialize_many_sparse_tensorss   s    
r#   c             C   s    t | dd t | D }|S )zSerializes sparse tensors.

  Args:
    tensors: a tensor structure to serialize.

  Returns:
    `tensors` with any sparse tensors replaced by their serialized version.
  c             S   s,   g | ]$}t |tjr$tj|tjd n|qS ))r"   )r    r   r   r   Zserialize_sparser   r   )r	   r   r   r   r   r      s   z,serialize_sparse_tensors.<locals>.<listcomp>)r   r   r   )r   r   r   r   r   serialize_sparse_tensors   s    
r$   N)__doc__Ztensorflow.python.data.utilr   Ztensorflow.python.frameworkr   r   r   r   Ztensorflow.python.opsr   r   r   r   r   r!   r#   r$   r   r   r   r   <module>   s   