B
    Ð»ˆd^  ã               @   sH   d 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
S )z)Utility to manipulate resource variables.é    )Úcomposite_tensor)Úops)Ú_pywrap_utils)Únestc             C   s   dd„ }t  || ¡S )aé  Converts `ResourceVariable`s in `values` to `Tensor`s.

  If an object is a `CompositeTensor` and overrides its
  `_convert_variables_to_tensors` method, its `ResourceVariable` components
  will also be converted to `Tensor`s. Objects other than `ResourceVariable`s
  in `values` will be returned unchanged.

  Args:
    values: A nested structure of `ResourceVariable`s, or any other objects.

  Returns:
    A new structure with `ResourceVariable`s in `values` converted to `Tensor`s.
  c             S   s2   t  | ¡rt | ¡S t| tjƒr*t | ¡S | S d S )N)r   ÚIsResourceVariabler   Úconvert_to_tensorÚ
isinstancer   ÚCompositeTensorÚconvert_variables_to_tensors)Úx© r   úW/var/www/html/venv/lib/python3.7/site-packages/tensorflow/python/util/variable_utils.pyÚ$_convert_resource_variable_to_tensor%   s
    


zJconvert_variables_to_tensors.<locals>._convert_resource_variable_to_tensor)r   Úmap_structure)Úvaluesr   r   r   r   r
      s    r
   c             C   s   dd„ }t  || ¡S )a  Replaces `ResourceVariable`s in `values` with tf.nest atoms.

  This function is mostly for backward compatibility. Historically,
  `ResourceVariable`s are treated as tf.nest atoms. This is no
  longer the case after `ResourceVariable` becoming `CompositeTensor`.
  Unfortunately, tf.nest doesn't allow customization of what objects
  are treated as atoms. Calling this function to manually convert
  `ResourceVariable`s to atoms to avoid breaking tf.assert_same_structure
  with inputs of a `ResourceVariable` and an atom, like a `Tensor`.

  The specific implementation uses 0 as the tf.nest atom, but other tf.nest
  atoms could also serve the purpose. Note, the `TypeSpec` of None is not a
  tf.nest atom.

  Objects other than `ResourceVariable`s in `values` will be returned unchanged.

  Note: this function does not look into `CompositeTensor`s. Replacing
  `ResourceVariable`s in a `CompositeTensor` with atoms will change the
  `TypeSpec` of the `CompositeTensor`, which violates the semantics of
  `CompositeTensor` and tf.nest. So `ResourceVariable`s in `CompositeTensor`s
  will be returned as they are.

  Args:
    values: A nested structure of `ResourceVariable`s, or any other objects.

  Returns:
    A new structure with `ResourceVariable`s in `values` converted to atoms.
  c             S   s   t  | ¡rdS | S d S )Nr   )r   r   )r   r   r   r   Ú$_replace_resource_variable_with_atomM   s    
zJreplace_variables_with_atoms.<locals>._replace_resource_variable_with_atom)r   r   )r   r   r   r   r   Úreplace_variables_with_atoms0   s    r   N)	Ú__doc__Útensorflow.python.frameworkr   r   Útensorflow.python.utilr   r   r
   r   r   r   r   r   Ú<module>   s   