B
    ٻd$                 @   s   d Z ddl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dZG dd dejZdS )z0The implementation of `tf.data.Dataset.rebatch`.    N)dataset_ops)nest)dtypes)ops)tensor_shape)tensor_util)gen_experimental_dataset_opsFc             C   s   t | |||S )N)RebatchDataset)input_datasetZ
batch_sizedrop_remaindername r   W/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/data/ops/rebatch_op.pyrebatch   s    r   c                   s>   e Zd ZdZd fdd	Zdd Zdd	 Zed
d Z  Z	S )r	   ad  A `Dataset` that rebatches elements from its input into new batch sizes.

  `RebatchDataset(input_dataset, batch_sizes)` is functionally equivalent to
  `input_dataset.unbatch().batch(N)`, where the value of N cycles through the
  `batch_sizes` input list. The elements produced by this dataset have the same
  rank as the elements of the input dataset.
  FNc                s   || _ tj|tjdd| _tj|tjdd| _|| _| 	  t
 fddt|| _t|}tj|jf||d| j}tt| || dS )z"See `Dataset.rebatch` for details.batch_sizes)Zdtyper   r   c                s   |    S )N)Z_unbatchZ_batch)ts)new_batch_dimr   r   <lambda>9       z)RebatchDataset.__init__.<locals>.<lambda>)r   r   N)_input_datasetr   Zconvert_to_tensorr   Zint64_batch_sizesbool_drop_remainder_name_compute_static_batch_dimr   Zmap_structurer   get_structure_element_specZnormalize_to_denseged_opsZrebatch_dataset_v2Z_variant_tensorZ_flat_structuresuperr	   __init__)selfr
   r   r   r   Zvariant_tensor)	__class__)r   r   r   )   s"    


zRebatchDataset.__init__c             C   s   t | j}|dkrdS t|tjrxt|jdkrTt||d krN|d }qxdS n$t|jdkrxt	dt|j d| 
|rdS |S )a0  Computes the static batch dimension of a dataset if it can be determined.

    Given the RebatchDataset parameters, determines the batch dimension of this
    dataset statically. Returns None if this cannot be determined or is
    variable.

    Returns:
      An integer representing the batch dimension of the dataset. If it cannot
      be determined statically, returns None.

    Raises:
      ValueError: The batch_sizes parameter is malformed, input_dataset is
      not batched, or input_dataset batch sizes are incompatible with each
      other.
    N   r   ziInvalid `batch_sizes`. Expected `batch_sizes` to be a scalar or a vector. Received `batch_sizes` of rank .)r   constant_valuer   
isinstancenpZndarraylenshapeall
ValueError_may_form_partial_batches)r    r   r   r   r   r   I   s    

z(RebatchDataset._compute_static_batch_dimc                s   t | jrdS dd   fddtt| jD }dd |D }|sNdS t	|}t
||d kstd	|d  d
| d|d | dkS )z6Returns whether this dataset may form partial batches.Fc             S   s`   y|   }W n tk
r    d S X t|tjs2d S |jd kr@d S t|dk rTtd|jd j	S )Nr"   zInvalid `batch_sizes`. Expected dataset with rank of >= 1 but found a dataset with scalar elements. Fix the issue by adding the `batch` transformation to the dataset.r   )
Z_to_legacy_output_shapesNotImplementedErrorr%   r   ZTensorShapeZrankr'   r*   Zdimsvalue)Z	type_specr(   r   r   r   get_batch_dims   s    
z?RebatchDataset._may_form_partial_batches.<locals>.get_batch_dimc                s   g | ]} |qS r   r   ).0r   )r.   r   r   
<listcomp>   s   z<RebatchDataset._may_form_partial_batches.<locals>.<listcomp>c             S   s   g | ]}|d k	r|qS )Nr   )r/   dr   r   r   r0      s    Tr   z?Invalid `input_dataset.` The batch dimension of component 0 is z., while the batch dimension of component i is r#   )r   r$   r   r   flattenr   r   r   r&   Zasarrayr)   r*   )r    Zdesired_batch_sizeZinput_batch_dimsZknown_input_batch_dimsr   )r.   r   r+   n   s    

z(RebatchDataset._may_form_partial_batchesc             C   s   | j S )N)r   )r    r   r   r   element_spec   s    zRebatchDataset.element_spec)FN)
__name__
__module____qualname____doc__r   r   r+   propertyr3   __classcell__r   r   )r!   r   r	       s    %'r	   )FN)r7   numpyr&   Ztensorflow.python.data.opsr   Ztensorflow.python.data.utilr   Ztensorflow.python.frameworkr   r   r   r   Ztensorflow.python.opsr   r   r   ZUnaryDatasetr	   r   r   r   r   <module>   s   
