a
    /Sic                    @   sR  d Z ddlZddlZddl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
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 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& ddl'm(Z( ddl'm)Z) ddl*m+Z+ dd Z,dd  Z-d!d" Z.e+d#G d$d# d#ej/Z0e+d%g d&G d'd( d(ej/Z1e+d%gd&G d)d% d%ej/Z2e1j d* e2_ d+d, Z3G d-d. d.e4Z5e+d/g d&G d0d/ d/ej6e5d1Z7e78  e"9d/e7 e+d/gd&G d2d3 d3e7Z:G d4d5 d5e:e j;Z<d6d7 Z=e>d8\Z?Z@ZAd9d: ZBd;d< ZCd=d> ZDd?d@ ZEG dAdB dBZFeGe<e<jH e+dCgd&dldDdCZIe+dEgd&e(dFdGdHdE ZJdmdIdJZKe+dKgd&dndLdKZLe+dMgd&dodNdMZMe+dOgd&dpdPdOZNe+dQgd&dqdRdQZOe+dSdTgd&drdVdTZPe+dWgd&e%jQe(dFdXdsdYdWZRe+dZd[gd&d\d[ ZSe+d]gd&e%jQe(dFd^d_d] ZTe+d`dagd&dbda ZUe+dcgd&e%jQe(dFdddedc ZVe+dfgd&e%jQdgdf ZWe+dhgd&e%jQdtdidhZXe+djgd&e%jQdudkdjZYeGeFeFjH dS )vzVariable class.    N)attr_value_pb2)variable_pb2)pywrap_tensorflow)context)dtypes)indexed_slices)ops)tensor_shape)	array_ops)control_flow_ops)gen_array_ops)gen_math_ops)gen_state_ops)math_ops)	state_ops)
tf_logging)base)core)_pywrap_utils)compat)object_identity)tf_should_use)traceback_utils)
deprecated)deprecated_args)	tf_exportc                 K   s   ~t dd S Nz#variable_scope needs to be importedNotImplementedError_kwds r"   [/var/www/html/django/DPS/env/lib/python3.9/site-packages/tensorflow/python/ops/variables.pydefault_variable_creator2   s    r$   c                 K   s   ~t dd S r   r   r   r"   r"   r#   default_variable_creator_v27   s    r%   c                    s    fdd}|S )z"To avoid capturing loop variables.c                     s    fi | S Nr"   kwargscaptured_gettercaptured_previousr"   r#   getter?   s    z_make_getter.<locals>.getterr"   )r*   r+   r,   r"   r)   r#   _make_getter<   s    r-   VariableSynchronizationc                   @   s    e Zd ZdZdZdZdZdZdS )r.   aT  Indicates when a distributed variable will be synced.

  * `AUTO`: Indicates that the synchronization will be determined by the current
    `DistributionStrategy` (eg. With `MirroredStrategy` this would be
    `ON_WRITE`).
  * `NONE`: Indicates that there will only be one copy of the variable, so
    there is no need to sync.
  * `ON_WRITE`: Indicates that the variable will be updated across devices
    every time it is written.
  * `ON_READ`: Indicates that the variable will be aggregated across devices
    when it is read (eg. when checkpointing or when evaluating an op that uses
    the variable).

    Example:
  >>> temp_grad=[tf.Variable([0.], trainable=False,
  ...                      synchronization=tf.VariableSynchronization.ON_READ,
  ...                      aggregation=tf.VariableAggregation.MEAN
  ...                      )]
  r            N)__name__
__module____qualname____doc__AUTONONEZON_WRITEON_READr"   r"   r"   r#   r.   E   s
   VariableAggregation)v1c                   @   s0   e Zd ZdZdZdZdZdZdd Zdd	 Z	d
S )VariableAggregationV2a@  Indicates how a distributed variable will be aggregated.

  `tf.distribute.Strategy` distributes a model by making multiple copies
  (called "replicas") acting data-parallel on different elements of the input
  batch. When performing some variable-update operation, say
  `var.assign_add(x)`, in a model, we need to resolve how to combine the
  different values for `x` computed in the different replicas.

  * `NONE`: This is the default, giving an error if you use a
    variable-update operation with multiple replicas.
  * `SUM`: Add the updates across replicas.
  * `MEAN`: Take the arithmetic mean ("average") of the updates across replicas.
  * `ONLY_FIRST_REPLICA`: This is for when every replica is performing the same
    update, but we only want to perform the update once. Used, e.g., for the
    global step counter.
  r   r/   r0   r1   c                 C   s
   t | jS r&   hashvalueselfr"   r"   r#   __hash__x   s    zVariableAggregationV2.__hash__c                 C   s2   | |u rdS t |tr*t| jt|jkS dS d S )NTF)
isinstancer9   intr>   r@   otherr"   r"   r#   __eq__{   s
    
zVariableAggregationV2.__eq__N)
r2   r3   r4   r5   r7   SUMMEANONLY_FIRST_REPLICArA   rF   r"   r"   r"   r#   r;   a   s   r;   c                   @   s(   e Zd ZdZdZdZdZdZdd ZdS )r9   r   r/   r0   r1   c                 C   s
   t | jS r&   r<   r?   r"   r"   r#   rA      s    zVariableAggregation.__hash__N)	r2   r3   r4   r7   rG   rH   rI   ZONLY_FIRST_TOWERrA   r"   r"   r"   r#   r9      s   zC* `ONLY_FIRST_TOWER`: Deprecated alias for `ONLY_FIRST_REPLICA`.
  c                 C   s   |du rt j}n>t|t tfsNzt|}W n" tyL   td||Y n0 | du r^tj} n0zt| } W n" ty   td| |Y n0 |du r| tjk}| ||fS )zEGiven user-provided variable properties, sets defaults and validates.Nz6Invalid variable aggregation mode: {} for variable: {}z:Invalid variable synchronization mode: {} for variable: {})	r9   r7   rB   r;   
ValueErrorformatr.   r6   r8   )synchronizationaggregation	trainablenamer"   r"   r#   .validate_synchronization_aggregation_trainable   s4    


rP   c                       sx   e Zd ZdZddddddddddddejejdfddZdddddddddejejdfddZ	e
j fdd	Z  ZS )
VariableMetaclassz@Metaclass to allow construction of tf.Variable to be overridden.NTc                 C   sZ   dd }t  jD ]\}}t||}q|du r4tj}||||||||||	|
|||||dS )6Call on Variable class. Useful to force the signature.c                  [   s   t di | S N)N)r$   r'   r"   r"   r#   <lambda>       z5VariableMetaclass._variable_v1_call.<locals>.<lambda>N)initial_valuerN   collectionsvalidate_shapecaching_devicerO   variable_defdtypeexpected_shapeimport_scope
constraintuse_resourcerL   rM   shaper   get_default_graph_variable_creator_stackr-   r9   r7   )clsrV   rN   rW   rX   rY   rO   rZ   r[   r\   r]   r^   r_   rL   rM   r`   previous_getterr    r,   r"   r"   r#   _variable_v1_call   s,    z#VariableMetaclass._variable_v1_callc                 C   sT   dd }t  jD ]\}}t||}q|du r4tj}||||||||||	|
||dS )rR   c                  [   s   t di | S rS   )r%   )kwsr"   r"   r#   rT      rU   z5VariableMetaclass._variable_v2_call.<locals>.<lambda>N)rV   rN   rX   rY   rO   rZ   r[   r]   r^   rL   rM   r`   ra   )rd   rV   rN   rX   rY   rO   rZ   r[   r]   r^   rL   rM   r`   re   r    r,   r"   r"   r#   _variable_v2_call   s&    z#VariableMetaclass._variable_v2_callc                    sJ   | t u r| j|i |S | tu r0| j|i |S tt| j|i |S d S r&   )
VariableV1rf   Variablerh   superrQ   __call__)rd   argsr(   	__class__r"   r#   rl     s
    zVariableMetaclass.__call__)r2   r3   r4   r5   r.   r6   r9   r7   rf   rh   r   filter_tracebackrl   __classcell__r"   r"   rn   r#   rQ      s@   
*
#rQ   rj   c                   @   s  e Zd ZdZeddddddddddddejejdfddZ	dd	 Z
d
d Zdd Zdd Zedd Zedd Zedd ZdxddZedddd Zedd Zedd Zdyd d!Zdzd"d#Zd{d$d%Zd|d&d'Zd}d(d)Zd~d*d+Zdd,d-Zdd.d/Zdd0d1Zdd2d3Z dd4d5Z!dd6d7Z"dd8d9Z#dd:d;Z$dd<d=Z%dd>d?Z&edd@dAdB Z'eddCddDdEZ(e)ddFdGZ*e+dHdI Z,e+dJdK Z-dLdM Z.dNdO Z/dPdQ Z0dRdS Z1dTZ2edUdV Z3edWdX Z4edYdZ Z5ed[d\ Z6ed]d^ Z7ed_d` Z8edadb Z9edcdd Z:dedf Z;dgdh Z<ddidjZ=e)ddkdlZ>dmdn Z?dodp Z@eddqdrds ZAdtdu ZBG dvdw dwZCdS )rj   a  See the [variable guide](https://tensorflow.org/guide/variable).

  A variable maintains shared, persistent state manipulated by a program.

  The `Variable()` constructor requires an initial value for the variable, which
  can be a `Tensor` of any type and shape. This initial value defines the type
  and shape of the variable. After construction, the type and shape of the
  variable are fixed. The value can be changed using one of the assign methods.

  >>> v = tf.Variable(1.)
  >>> v.assign(2.)
  <tf.Variable ... shape=() dtype=float32, numpy=2.0>
  >>> v.assign_add(0.5)
  <tf.Variable ... shape=() dtype=float32, numpy=2.5>

  The `shape` argument to `Variable`'s constructor allows you to construct a
  variable with a less defined shape than its `initial_value`:

  >>> v = tf.Variable(1., shape=tf.TensorShape(None))
  >>> v.assign([[1.]])
  <tf.Variable ... shape=<unknown> dtype=float32, numpy=array([[1.]], ...)>

  Just like any `Tensor`, variables created with `Variable()` can be used as
  inputs to operations. Additionally, all the operators overloaded for the
  `Tensor` class are carried over to variables.

  >>> w = tf.Variable([[1.], [2.]])
  >>> x = tf.constant([[3., 4.]])
  >>> tf.matmul(w, x)
  <tf.Tensor:... shape=(2, 2), ... numpy=
    array([[3., 4.],
           [6., 8.]], dtype=float32)>
  >>> tf.sigmoid(w + x)
  <tf.Tensor:... shape=(2, 2), ...>

  When building a machine learning model it is often convenient to distinguish
  between variables holding trainable model parameters and other variables such
  as a `step` variable used to count training steps. To make this easier, the
  variable constructor supports a `trainable=<bool>`
  parameter. `tf.GradientTape` watches trainable variables by default:

  >>> with tf.GradientTape(persistent=True) as tape:
  ...   trainable = tf.Variable(1.)
  ...   non_trainable = tf.Variable(2., trainable=False)
  ...   x1 = trainable * 2.
  ...   x2 = non_trainable * 3.
  >>> tape.gradient(x1, trainable)
  <tf.Tensor:... shape=(), dtype=float32, numpy=2.0>
  >>> assert tape.gradient(x2, non_trainable) is None  # Unwatched

  Variables are automatically tracked when assigned to attributes of types
  inheriting from `tf.Module`.

  >>> m = tf.Module()
  >>> m.v = tf.Variable([1.])
  >>> m.trainable_variables
  (<tf.Variable ... shape=(1,) ... numpy=array([1.], dtype=float32)>,)

  This tracking then allows saving variable values to
  [training checkpoints](https://www.tensorflow.org/guide/checkpoint), or to
  [SavedModels](https://www.tensorflow.org/guide/saved_model) which include
  serialized TensorFlow graphs.

  Variables are often captured and manipulated by `tf.function`s. This works the
  same way the un-decorated function would have:

  >>> v = tf.Variable(0.)
  >>> read_and_decrement = tf.function(lambda: v.assign_sub(0.1))
  >>> read_and_decrement()
  <tf.Tensor: shape=(), dtype=float32, numpy=-0.1>
  >>> read_and_decrement()
  <tf.Tensor: shape=(), dtype=float32, numpy=-0.2>

  Variables created inside a `tf.function` must be owned outside the function
  and be created only once:

  >>> class M(tf.Module):
  ...   @tf.function
  ...   def __call__(self, x):
  ...     if not hasattr(self, "v"):  # Or set self.v to None in __init__
  ...       self.v = tf.Variable(x)
  ...     return self.v * x
  >>> m = M()
  >>> m(2.)
  <tf.Tensor: shape=(), dtype=float32, numpy=4.0>
  >>> m(3.)
  <tf.Tensor: shape=(), dtype=float32, numpy=6.0>
  >>> m.v
  <tf.Variable ... shape=() dtype=float32, numpy=2.0>

  See the `tf.function` documentation for details.
  NzA variable's value can be manually cached by calling tf.Variable.read_value() under a tf.device scope. The caching_device argument does not work properly.rY   Tc                 C   s   t dS )a  Creates a new variable with value `initial_value`.

    Args:
      initial_value: A `Tensor`, or Python object convertible to a `Tensor`,
        which is the initial value for the Variable. The initial value must have
        a shape specified unless `validate_shape` is set to False. Can also be a
        callable with no argument that returns the initial value when called. In
        that case, `dtype` must be specified. (Note that initializer functions
        from init_ops.py must first be bound to a shape before being used here.)
      trainable: If `True`, GradientTapes automatically watch uses of this
        variable. Defaults to `True`, unless `synchronization` is set to
        `ON_READ`, in which case it defaults to `False`.
      validate_shape: If `False`, allows the variable to be initialized with a
        value of unknown shape. If `True`, the default, the shape of
        `initial_value` must be known.
      caching_device: Note: This argument is only valid when using a v1-style
        `Session`. Optional device string describing where the Variable should
        be cached for reading. Defaults to the Variable's device. If not `None`,
        caches on another device. Typical use is to cache on the device where
        the Ops using the Variable reside, to deduplicate copying through
        `Switch` and other conditional statements.
      name: Optional name for the variable. Defaults to `'Variable'` and gets
        uniquified automatically.
      variable_def: `VariableDef` protocol buffer. If not `None`, recreates the
        Variable object with its contents, referencing the variable's nodes in
        the graph, which must already exist. The graph is not changed.
        `variable_def` and the other arguments are mutually exclusive.
      dtype: If set, initial_value will be converted to the given type. If
        `None`, either the datatype will be kept (if `initial_value` is a
        Tensor), or `convert_to_tensor` will decide.
      import_scope: Optional `string`. Name scope to add to the `Variable.` Only
        used when initializing from protocol buffer.
      constraint: An optional projection function to be applied to the variable
        after being updated by an `Optimizer` (e.g. used to implement norm
        constraints or value constraints for layer weights). The function must
        take as input the unprojected Tensor representing the value of the
        variable and return the Tensor for the projected value (which must have
        the same shape). Constraints are not safe to use when doing asynchronous
        distributed training.
      synchronization: Indicates when a distributed a variable will be
        aggregated. Accepted values are constants defined in the class
        `tf.VariableSynchronization`. By default the synchronization is set to
        `AUTO` and the current `DistributionStrategy` chooses when to
        synchronize.
      aggregation: Indicates how a distributed variable will be aggregated.
        Accepted values are constants defined in the class
        `tf.VariableAggregation`.
      shape: (optional) The shape of this variable. If None, the shape of
        `initial_value` will be used. When setting this argument to
        `tf.TensorShape(None)` (representing an unspecified shape), the variable
        can be assigned with values of different shapes.

    Raises:
      ValueError: If both `variable_def` and initial_value are specified.
      ValueError: If the initial value is not specified, or does not have a
        shape and `validate_shape` is `True`.
    Nr   )r@   rV   rN   rX   rY   rO   rZ   r[   r]   r^   rL   rM   r`   r"   r"   r#   __init__o  s    JzVariable.__init__c                 C   s   t d S r&   r   r?   r"   r"   r#   __repr__  s    zVariable.__repr__c                 C   s   t dS )  Returns the last snapshot of this variable.

    You usually do not need to call this method as all ops that need the value
    of the variable call it automatically through a `convert_to_tensor()` call.

    Returns a `Tensor` which holds the value of the variable.  You can not
    assign a new value to this tensor as it is not a reference to the variable.

    To avoid copies, if the consumer of the returned value is on the same device
    as the variable, this actually returns the live value of the variable, not
    a copy.  Updates to the variable are seen by the consumer.  If the consumer
    is on a different device it will get a copy of the variable.

    Returns:
      A `Tensor` containing the value of the variable.
    Nr   r?   r"   r"   r#   r>     s    zVariable.valuec                 C   s   t dS )Returns the value of this variable, read in the current context.

    Can be different from value() if it's on another device, with control
    dependencies, etc.

    Returns:
      A `Tensor` containing the value of the variable.
    Nr   r?   r"   r"   r#   
read_value  s    	zVariable.read_valuec                 C   s   t dS zxOverrides the shape for this variable.

    Args:
      shape: the `TensorShape` representing the overridden shape.
    Nr   r@   r`   r"   r"   r#   	set_shape  s    zVariable.set_shapec                 C   s   t d S r&   r   r?   r"   r"   r#   rN     s    zVariable.trainablec                 C   s   t d S r&   r   r?   r"   r"   r#   rL     s    zVariable.synchronizationc                 C   s   t d S r&   r   r?   r"   r"   r#   rM     s    zVariable.aggregationc                 C   s   t dS )  In a session, computes and returns the value of this variable.

    This is not a graph construction method, it does not add ops to the graph.

    This convenience method requires a session where the graph
    containing this variable has been launched. If no session is
    passed, the default session is used.  See `tf.compat.v1.Session` for more
    information on launching a graph and on sessions.

    ```python
    v = tf.Variable([1, 2])
    init = tf.compat.v1.global_variables_initializer()

    with tf.compat.v1.Session() as sess:
        sess.run(init)
        # Usage passing the session explicitly.
        print(v.eval(sess))
        # Usage with the default session.  The 'with' block
        # above makes 'sess' the default session.
        print(v.eval())
    ```

    Args:
      session: The session to use to evaluate this variable. If none, the
        default session is used.

    Returns:
      A numpy `ndarray` with a copy of the value of this variable.
    Nr   r@   sessionr"   r"   r#   eval  s    zVariable.evalz{Use Variable.read_value. Variables in 2.X are initialized automatically both in eager and graph (inside tf.defun) contexts.c                    sH   t  , tt  j fddW  d   S 1 s:0    Y  dS )a  Returns the value of the initialized variable.

    You should use this instead of the variable itself to initialize another
    variable with a value that depends on the value of this variable.

    ```python
    # Initialize 'v' with a random tensor.
    v = tf.Variable(tf.random.truncated_normal([10, 40]))
    # Use `initialized_value` to guarantee that `v` has been
    # initialized before its value is used to initialize `w`.
    # The random values are picked only once.
    w = tf.Variable(v.initialized_value() * 2.0)
    ```

    Returns:
      A `Tensor` holding the value of this variable after its initializer
      has run.
    c                      s    j S r&   )rV   r"   r?   r"   r#   rT   )  rU   z,Variable.initialized_value.<locals>.<lambda>N)r   
init_scoper   condis_variable_initializedrv   r?   r"   r?   r#   initialized_value  s
    


zVariable.initialized_valuec                 C   s   t dS )L  Returns the Tensor used as the initial value for the variable.

    Note that this is different from `initialized_value()` which runs
    the op that initializes the variable before returning its value.
    This method returns the tensor that is used by the op that initializes
    the variable.

    Returns:
      A `Tensor`.
    Nr   r?   r"   r"   r#   rV   +  s    zVariable.initial_valuec                 C   s   t dS )Returns the constraint function associated with this variable.

    Returns:
      The constraint function that was passed to the variable constructor.
      Can be `None` if no constraint was passed.
    Nr   r?   r"   r"   r#   r^   9  s    zVariable.constraintFc                 C   s   t dS )a?  Assigns a new value to the variable.

    This is essentially a shortcut for `assign(self, value)`.

    Args:
      value: A `Tensor`. The new value for this variable.
      use_locking: If `True`, use locking during the assignment.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      The updated variable. If `read_value` is false, instead returns None in
      Eager mode and the assign op in graph mode.
    Nr   )r@   r>   use_lockingrO   rv   r"   r"   r#   assignC  s    zVariable.assignc                 C   s   t dS )a?  Adds a value to this variable.

     This is essentially a shortcut for `assign_add(self, delta)`.

    Args:
      delta: A `Tensor`. The value to add to this variable.
      use_locking: If `True`, use locking during the operation.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      The updated variable. If `read_value` is false, instead returns None in
      Eager mode and the assign op in graph mode.
    Nr   r@   deltar   rO   rv   r"   r"   r#   
assign_addU  s    zVariable.assign_addc                 C   s   t dS )aL  Subtracts a value from this variable.

    This is essentially a shortcut for `assign_sub(self, delta)`.

    Args:
      delta: A `Tensor`. The value to subtract from this variable.
      use_locking: If `True`, use locking during the operation.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      The updated variable. If `read_value` is false, instead returns None in
      Eager mode and the assign op in graph mode.
    Nr   r   r"   r"   r#   
assign_subg  s    zVariable.assign_subc                 C   s   t dS )ah  Subtracts `tf.IndexedSlices` from this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be subtracted from this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r@   sparse_deltar   rO   r"   r"   r#   scatter_suby  s    zVariable.scatter_subc                 C   s   t dS )aZ  Adds `tf.IndexedSlices` to this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be added to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_add  s    zVariable.scatter_addc                 C   s   t dS )a  Updates this variable with the max of `tf.IndexedSlices` and itself.

    Args:
      sparse_delta: `tf.IndexedSlices` to use as an argument of max with this
        variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_max  s    zVariable.scatter_maxc                 C   s   t dS )a  Updates this variable with the min of `tf.IndexedSlices` and itself.

    Args:
      sparse_delta: `tf.IndexedSlices` to use as an argument of min with this
        variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_min  s    zVariable.scatter_minc                 C   s   t dS )a^  Multiply this variable by `tf.IndexedSlices`.

    Args:
      sparse_delta: `tf.IndexedSlices` to multiply this variable by.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_mul  s    zVariable.scatter_mulc                 C   s   t dS )aZ  Divide this variable by `tf.IndexedSlices`.

    Args:
      sparse_delta: `tf.IndexedSlices` to divide this variable by.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_div  s    zVariable.scatter_divc                 C   s   t dS )a`  Assigns `tf.IndexedSlices` to this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be assigned to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   scatter_update  s    zVariable.scatter_updatec                 C   s   t dS )a  Assigns `tf.IndexedSlices` to this variable batch-wise.

    Analogous to `batch_gather`. This assumes that this variable and the
    sparse_delta IndexedSlices have a series of leading dimensions that are the
    same for all of them, and the updates are performed on the last dimension of
    indices. In other words, the dimensions should be the following:

    `num_prefix_dims = sparse_delta.indices.ndims - 1`
    `batch_dim = num_prefix_dims + 1`
    `sparse_delta.updates.shape = sparse_delta.indices.shape + var.shape[
         batch_dim:]`

    where

    `sparse_delta.updates.shape[:num_prefix_dims]`
    `== sparse_delta.indices.shape[:num_prefix_dims]`
    `== var.shape[:num_prefix_dims]`

    And the operation performed can be expressed as:

    `var[i_1, ..., i_n,
         sparse_delta.indices[i_1, ..., i_n, j]] = sparse_delta.updates[
            i_1, ..., i_n, j]`

    When sparse_delta.indices is a 1D tensor, this operation is equivalent to
    `scatter_update`.

    To avoid this operation one can looping over the first `ndims` of the
    variable and using `scatter_update` on the subtensors that result of slicing
    the first dimension. This is a valid option for `ndims = 1`, but less
    efficient than this implementation.

    Args:
      sparse_delta: `tf.IndexedSlices` to be assigned to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      The updated variable.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    Nr   r   r"   r"   r#   batch_scatter_update  s    ,zVariable.batch_scatter_updatec                 C   s   t dS )aF  Applies sparse subtraction to individual values or slices in a Variable.

    Assuming the variable has rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into self.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of self.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, self.shape[K], ..., self.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        v = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        v.scatter_nd_sub(indices, updates)
        print(v)
    ```

    After the update `v` would look like this:

        [1, -9, 3, -6, -4, 6, 7, -4]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      The updated variable.
    Nr   r@   indicesupdatesrO   r"   r"   r#   scatter_nd_sub  s    ,zVariable.scatter_nd_subc                 C   s   t dS )a?  Applies sparse addition to individual values or slices in a Variable.

    The Variable has rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into self.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of self.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, self.shape[K], ..., self.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        v = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        v.scatter_nd_add(indices, updates)
        print(v)
    ```

    The resulting update to v would look like this:

        [1, 13, 3, 14, 14, 6, 7, 20]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      The updated variable.
    Nr   r   r"   r"   r#   scatter_nd_addG  s    ,zVariable.scatter_nd_addc                 C   s   t dS )aC  Applies sparse assignment to individual values or slices in a Variable.

    The Variable has rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into self.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of self.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, self.shape[K], ..., self.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        v = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        v.scatter_nd_update(indices, updates)
        print(v)
    ```

    The resulting update to v would look like this:

        [1, 11, 3, 10, 9, 6, 7, 12]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      The updated variable.
    Nr   r   r"   r"   r#   scatter_nd_updateu  s    ,zVariable.scatter_nd_updatec                 C   s   t dS )a  Gather slices from params axis axis according to indices.

    This function supports a subset of tf.gather, see tf.gather for details on
    usage.

    Args:
      indices: The index `Tensor`.  Must be one of the following types: `int32`,
        `int64`. Must be in range `[0, params.shape[axis])`.
      name: A name for the operation (optional).

    Returns:
      A `Tensor`. Has the same type as `params`.
    NAttributeErrorr@   r   rO   r"   r"   r#   sparse_read  s    zVariable.sparse_readc                 C   s   t dS )aV  Gather slices from `params` into a Tensor with shape specified by `indices`.

    See tf.gather_nd for details.

    Args:
      indices: A `Tensor`. Must be one of the following types: `int32`, `int64`.
        Index tensor.
      name: A name for the operation (optional).

    Returns:
      A `Tensor`. Has the same type as `params`.
    Nr   r   r"   r"   r#   	gather_nd  s    zVariable.gather_ndPrefer Dataset.range instead.c                 C   s   t dS )  Increments this variable until it reaches `limit`.

    When that Op is run it tries to increment the variable by `1`. If
    incrementing the variable would bring it above `limit` then the Op raises
    the exception `OutOfRangeError`.

    If no error is raised, the Op outputs the value of the variable before
    the increment.

    This is essentially a shortcut for `count_up_to(self, limit)`.

    Args:
      limit: value at which incrementing the variable raises an error.

    Returns:
      A `Tensor` that will hold the variable value before the increment. If no
      other Op modifies this variable, the values produced will all be
      distinct.
    Nr   r@   limitr"   r"   r#   count_up_to  s    zVariable.count_up_toz<Prefer Variable.assign which has equivalent behavior in 2.X.c                 C   sN   t  r| | n6|pt }|du r0td|| j| jjd |i dS )a5  Load new value into this variable.

    Writes new value to variable's memory. Doesn't add ops to the graph.

    This convenience method requires a session where the graph
    containing this variable has been launched. If no session is
    passed, the default session is used.  See `tf.compat.v1.Session` for more
    information on launching a graph and on sessions.

    ```python
    v = tf.Variable([1, 2])
    init = tf.compat.v1.global_variables_initializer()

    with tf.compat.v1.Session() as sess:
        sess.run(init)
        # Usage passing the session explicitly.
        v.load([2, 3], sess)
        print(v.eval(sess)) # prints [2 3]
        # Usage with the default session.  The 'with' block
        # above makes 'sess' the default session.
        v.load([3, 4], sess)
        print(v.eval()) # prints [3 4]
    ```

    Args:
        value: New variable value
        session: The session to use to evaluate this variable. If none, the
          default session is used.

    Raises:
        ValueError: Session is not passed and no default session
    NzSEither session argument should be provided or default session should be establishedr/   )	r   executing_eagerlyr   r   get_default_sessionrJ   runinitializerinputs)r@   r>   r|   r"   r"   r#   load  s    #zVariable.loadc                 C   sN   |}|r6| | js6td|j d| jj d|  d|rB|  S |  S dS )7Utility function for converting a Variable to a Tensor.z0Incompatible type conversion requested to type 'z' for variable of type 'z' (Variable: z).Nis_compatible_withr[   rJ   rO   _refr>   vr[   rO   as_refr    r"   r"   r#   _TensorConversionFunction  s    
z"Variable._TensorConversionFunctionc                 C   s*   t jjD ]}| | qt| dtj dS )z%Register overloads for all operators.__getitem__N)r   TensorOVERLOADABLE_OPERATORS_OverloadOperatorsetattrr
   _SliceHelperVar)rd   operatorr"   r"   r#   _OverloadAllOperators  s    zVariable._OverloadAllOperatorsc                    sH   |dks|dkrdS t tj|  fdd}t|  t| || dS )zDefer an operator overload to `ops.Tensor`.

    We pull the operator out of ops.Tensor dynamically to avoid ordering issues.

    Args:
      operator: string. The operator name.
    rF   __ne__Nc                    s    |   g|R i |S r&   )r>   )arm   r(   Ztensor_operr"   r#   _run_op0  s    z+Variable._OverloadOperator.<locals>._run_op)getattrr   r   	functoolsupdate_wrapperr   )rd   r   r   r"   r   r#   r     s    zVariable._OverloadOperatorc                 C   s.   t jjr"t  r"td|  dnt| S d S )NzKVariable is unhashable. Instead, use variable.ref() as the key. (Variable: ))r   r   _USE_EQUALITY#executing_eagerly_outside_functions	TypeErroridr?   r"   r"   r#   rA   7  s    zVariable.__hash__c                 C   s,   t jjr t  r tj| |ddS | |u S dS z1Compares two variables element-wise for equality.F)incompatible_shape_errorN)r   r   r   r   r   equalrD   r"   r"   r#   rF   @  s    zVariable.__eq__c                 C   s,   t jjr t  r tj| |ddS | |uS dS r   )r   r   r   r   r   	not_equalrD   r"   r"   r#   r   I  s    zVariable.__ne__c                 C   s   t |  S )z@When executing eagerly, iterates over the value of the variable.)iterrv   r?   r"   r"   r#   __iter__Q  s    zVariable.__iter__d   c                 C   s   t dS )The name of this variable.Nr   r?   r"   r"   r#   rO   ^  s    zVariable.namec                 C   s   | j d| j d S )zThe shared name of the variable.

      Unlike name(), shared_name doesn't have ":0" suffix. It is user-specified
      name with name scope prefix.

    Returns:
      variable name.
    N:)rO   indexr?   r"   r"   r#   _shared_namec  s    
zVariable._shared_namec                 C   s   t dS ),The initializer operation for this variable.Nr   r?   r"   r"   r#   r   o  s    zVariable.initializerc                 C   s   t dS )The device of this variable.Nr   r?   r"   r"   r#   devicet  s    zVariable.devicec                 C   s   t dS )The `DType` of this variable.Nr   r?   r"   r"   r#   r[   y  s    zVariable.dtypec                 C   s   t dS )!The `Operation` of this variable.Nr   r?   r"   r"   r#   op~  s    zVariable.opc                 C   s   t dS )The `Graph` of this variable.Nr   r?   r"   r"   r#   graph  s    zVariable.graphc                 C   s   t dS )MThe `TensorShape` of this variable.

    Returns:
      A `TensorShape`.
    Nr   r?   r"   r"   r#   r`     s    zVariable.shapec                 C   s   | j S )zAlias of `Variable.shape`.)r`   r?   r"   r"   r#   	get_shape  s    zVariable.get_shapec                 C   s
   t j| iS )zAFor implementing `Trackable`. This object is saveable on its own.)	trackableVARIABLE_VALUE_KEYr?   r"   r"   r#    _gather_saveables_for_checkpoint  s    z)Variable._gather_saveables_for_checkpointc                 C   s   t dS )  Converts a `Variable` to a `VariableDef` protocol buffer.

    Args:
      export_scope: Optional `string`. Name scope to remove.

    Returns:
      A `VariableDef` protocol buffer, or `None` if the `Variable` is not
      in the specified name scope.
    Nr   )r@   export_scoper"   r"   r#   to_proto  s    
zVariable.to_protoc                 C   s   t | |dS )z8Returns a `Variable` object created from `variable_def`.rZ   r]   )RefVariabler   r"   r"   r#   
from_proto  s    zVariable.from_protoc                 C   s
   || _ dS )zrSets the slice info for this `Variable`.

    Args:
      save_slice_info: A `Variable.SaveSliceInfo` object.
    N_save_slice_info)r@   save_slice_infor"   r"   r#   _set_save_slice_info  s    zVariable._set_save_slice_infoc                 C   s   | j S r&   r   r?   r"   r"   r#   _get_save_slice_info  s    zVariable._get_save_slice_infozUse ref() instead.c                 C   s   |   S r&   )refr?   r"   r"   r#   experimental_ref  s    zVariable.experimental_refc                 C   s
   t | S )a  Returns a hashable reference object to this Variable.

    The primary use case for this API is to put variables in a set/dictionary.
    We can't put variables in a set/dictionary as `variable.__hash__()` is no
    longer available starting Tensorflow 2.0.

    The following will raise an exception starting 2.0

    >>> x = tf.Variable(5)
    >>> y = tf.Variable(10)
    >>> z = tf.Variable(10)
    >>> variable_set = {x, y, z}
    Traceback (most recent call last):
      ...
    TypeError: Variable is unhashable. Instead, use tensor.ref() as the key.
    >>> variable_dict = {x: 'five', y: 'ten'}
    Traceback (most recent call last):
      ...
    TypeError: Variable is unhashable. Instead, use tensor.ref() as the key.

    Instead, we can use `variable.ref()`.

    >>> variable_set = {x.ref(), y.ref(), z.ref()}
    >>> x.ref() in variable_set
    True
    >>> variable_dict = {x.ref(): 'five', y.ref(): 'ten', z.ref(): 'ten'}
    >>> variable_dict[y.ref()]
    'ten'

    Also, the reference object provides `.deref()` function that returns the
    original Variable.

    >>> x = tf.Variable(5)
    >>> x.ref().deref()
    <tf.Variable 'Variable:0' shape=() dtype=int32, numpy=5>
    )r   	Referencer?   r"   r"   r#   r     s    'zVariable.refc                   @   s0   e Zd ZdZd	ddZedd Zd
ddZdS )zVariable.SaveSliceInfoa!  Information on how to save this Variable as a slice.

    Provides internal support for saving variables as slices of a larger
    variable.  This API is not public and is subject to change.

    Available properties:

    * full_name
    * full_shape
    * var_offset
    * var_shape
    Nc                 C   sz   |r^t |tjsJ tj|j|d| _dd |jD | _dd |jD | _dd |jD | _n|| _|| _|| _|| _dS )a  Create a `SaveSliceInfo`.

      Args:
        full_name: Name of the full variable of which this `Variable` is a
          slice.
        full_shape: Shape of the full variable, as a list of int.
        var_offset: Offset of this `Variable` into the full variable, as a list
          of int.
        var_shape: Shape of this `Variable`, as a list of int.
        save_slice_info_def: `SaveSliceInfoDef` protocol buffer. If not `None`,
          recreates the SaveSliceInfo object its contents. `save_slice_info_def`
          and other arguments are mutually exclusive.
        import_scope: Optional `string`. Name scope to add. Only used when
          initializing from protocol buffer.
      r]   c                 S   s   g | ]}|qS r"   r"   .0ir"   r"   r#   
<listcomp>
  rU   z3Variable.SaveSliceInfo.__init__.<locals>.<listcomp>c                 S   s   g | ]}|qS r"   r"   r   r"   r"   r#   r     rU   c                 S   s   g | ]}|qS r"   r"   r   r"   r"   r#   r     rU   N)	rB   r   SaveSliceInfoDefr   prepend_name_scope	full_name
full_shape
var_offset	var_shape)r@   r   r   r   r   save_slice_info_defr]   r"   r"   r#   rr     s    zVariable.SaveSliceInfo.__init__c                 C   s@   d dd | jD d }d dd t| j| jD }|| S )z)Computes the spec string used for saving. c                 s   s   | ]}d | V  qdS )z%dNr"   )r   dr"   r"   r#   	<genexpr>  rU   z.Variable.SaveSliceInfo.spec.<locals>.<genexpr>r   c                 s   s   | ]\}}d ||f V  qdS )z%d,%dNr"   )r   osr"   r"   r#   r     s   )joinr   zipr   r   )r@   full_shape_strZsl_specr"   r"   r#   spec  s
    
zVariable.SaveSliceInfo.specc                 C   s   |du s| j |rxt }t| j ||_ | jD ]}|j| q2| jD ]}|j| qJ| j	D ]}|j	| qb|S dS dS )zReturns a SaveSliceInfoDef() proto.

      Args:
        export_scope: Optional `string`. Name scope to remove.

      Returns:
        A `SaveSliceInfoDef` protocol buffer, or None if the `Variable` is not
        in the specified name scope.
      N)
r   
startswithr   r   r   strip_name_scoper   appendr   r   )r@   r   r   r   r"   r"   r#   r     s    



zVariable.SaveSliceInfo.to_proto)NNNNNN)N)r2   r3   r4   r5   rr   propertyr   r   r"   r"   r"   r#   SaveSliceInfo  s         
#
r  )N)FNT)FNT)FNT)FN)FN)FN)FN)FN)FN)FN)FN)N)N)N)N)N)N)NNF)N)N)Dr2   r3   r4   r5   r   r.   r6   r9   r7   rr   rs   r>   rv   ry   r  rN   rL   rM   r}   r   r   rV   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   r   staticmethodr   classmethodr   r   rA   rF   r   r   __array_priority__rO   r   r   r   r[   r   r   r`   r   r   r   r   r   r   r   r   r  r"   r"   r"   r#   rj     s   ]H



 


	










.
.
.
.


,
	
		









))	metaclassc                   @   sB   e Zd ZdZddddddddddddejejdfddZe	j
Z
dS )ri   a  See the [Variables Guide](https://tensorflow.org/guide/variables).

  A variable maintains state in the graph across calls to `run()`. You add a
  variable to the graph by constructing an instance of the class `Variable`.

  The `Variable()` constructor requires an initial value for the variable,
  which can be a `Tensor` of any type and shape. The initial value defines the
  type and shape of the variable. After construction, the type and shape of
  the variable are fixed. The value can be changed using one of the assign
  methods.

  If you want to change the shape of a variable later you have to use an
  `assign` Op with `validate_shape=False`.

  Just like any `Tensor`, variables created with `Variable()` can be used as
  inputs for other Ops in the graph. Additionally, all the operators
  overloaded for the `Tensor` class are carried over to variables, so you can
  also add nodes to the graph by just doing arithmetic on variables.

  ```python
  import tensorflow as tf

  # Create a variable.
  w = tf.Variable(<initial-value>, name=<optional-name>)

  # Use the variable in the graph like any Tensor.
  y = tf.matmul(w, ...another variable or tensor...)

  # The overloaded operators are available too.
  z = tf.sigmoid(w + y)

  # Assign a new value to the variable with `assign()` or a related method.
  w.assign(w + 1.0)
  w.assign_add(1.0)
  ```

  When you launch the graph, variables have to be explicitly initialized before
  you can run Ops that use their value. You can initialize a variable by
  running its *initializer op*, restoring the variable from a save file, or
  simply running an `assign` Op that assigns a value to the variable. In fact,
  the variable *initializer op* is just an `assign` Op that assigns the
  variable's initial value to the variable itself.

  ```python
  # Launch the graph in a session.
  with tf.compat.v1.Session() as sess:
      # Run the variable initializer.
      sess.run(w.initializer)
      # ...you now can run ops that use the value of 'w'...
  ```

  The most common initialization pattern is to use the convenience function
  `global_variables_initializer()` to add an Op to the graph that initializes
  all the variables. You then run that Op after launching the graph.

  ```python
  # Add an Op to initialize global variables.
  init_op = tf.compat.v1.global_variables_initializer()

  # Launch the graph in a session.
  with tf.compat.v1.Session() as sess:
      # Run the Op that initializes global variables.
      sess.run(init_op)
      # ...you can now run any Op that uses variable values...
  ```

  If you need to create a variable with an initial value dependent on another
  variable, use the other variable's `initialized_value()`. This ensures that
  variables are initialized in the right order.

  All variables are automatically collected in the graph where they are
  created. By default, the constructor adds the new variable to the graph
  collection `GraphKeys.GLOBAL_VARIABLES`. The convenience function
  `global_variables()` returns the contents of that collection.

  When building a machine learning model it is often convenient to distinguish
  between variables holding the trainable model parameters and other variables
  such as a `global step` variable used to count training steps. To make this
  easier, the variable constructor supports a `trainable=<bool>` parameter. If
  `True`, the new variable is also added to the graph collection
  `GraphKeys.TRAINABLE_VARIABLES`. The convenience function
  `trainable_variables()` returns the contents of this collection. The
  various `Optimizer` classes use this collection as the default list of
  variables to optimize.

  WARNING: tf.Variable objects by default have a non-intuitive memory model. A
  Variable is represented internally as a mutable Tensor which can
  non-deterministically alias other Tensors in a graph. The set of operations
  which consume a Variable and can lead to aliasing is undetermined and can
  change across TensorFlow versions. Avoid writing code which relies on the
  value of a Variable either changing or not changing as other operations
  happen. For example, using Variable objects or simple functions thereof as
  predicates in a `tf.cond` is dangerous and error-prone:

  ```
  v = tf.Variable(True)
  tf.cond(v, lambda: v.assign(False), my_false_fn)  # Note: this is broken.
  ```

  Here, adding `use_resource=True` when constructing the variable will
  fix any nondeterminism issues:
  ```
  v = tf.Variable(True, use_resource=True)
  tf.cond(v, lambda: v.assign(False), my_false_fn)
  ```

  To use the replacement for variables which does
  not have these issues:

  * Add `use_resource=True` when constructing `tf.Variable`;
  * Call `tf.compat.v1.get_variable_scope().set_use_resource(True)` inside a
    `tf.compat.v1.variable_scope` before the `tf.compat.v1.get_variable()` call.
  NTc                 C   s   dS )a  Creates a new variable with value `initial_value`.

    The new variable is added to the graph collections listed in `collections`,
    which defaults to `[GraphKeys.GLOBAL_VARIABLES]`.

    If `trainable` is `True` the variable is also added to the graph collection
    `GraphKeys.TRAINABLE_VARIABLES`.

    This constructor creates both a `variable` Op and an `assign` Op to set the
    variable to its initial value.

    Args:
      initial_value: A `Tensor`, or Python object convertible to a `Tensor`,
        which is the initial value for the Variable. The initial value must have
        a shape specified unless `validate_shape` is set to False. Can also be a
        callable with no argument that returns the initial value when called. In
        that case, `dtype` must be specified. (Note that initializer functions
        from init_ops.py must first be bound to a shape before being used here.)
      trainable: If `True`, also adds the variable to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES`. This collection is used as the default
        list of variables to use by the `Optimizer` classes. Defaults to `True`,
        unless `synchronization` is set to `ON_READ`, in which case it defaults
        to `False`.
      collections: List of graph collections keys. The new variable is added to
        these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.
      validate_shape: If `False`, allows the variable to be initialized with a
        value of unknown shape. If `True`, the default, the shape of
        `initial_value` must be known.
      caching_device: Optional device string describing where the Variable
        should be cached for reading.  Defaults to the Variable's device. If not
        `None`, caches on another device.  Typical use is to cache on the device
        where the Ops using the Variable reside, to deduplicate copying through
        `Switch` and other conditional statements.
      name: Optional name for the variable. Defaults to `'Variable'` and gets
        uniquified automatically.
      variable_def: `VariableDef` protocol buffer. If not `None`, recreates the
        Variable object with its contents, referencing the variable's nodes in
        the graph, which must already exist. The graph is not changed.
        `variable_def` and the other arguments are mutually exclusive.
      dtype: If set, initial_value will be converted to the given type. If
        `None`, either the datatype will be kept (if `initial_value` is a
        Tensor), or `convert_to_tensor` will decide.
      expected_shape: A TensorShape. If set, initial_value is expected to have
        this shape.
      import_scope: Optional `string`. Name scope to add to the `Variable.` Only
        used when initializing from protocol buffer.
      constraint: An optional projection function to be applied to the variable
        after being updated by an `Optimizer` (e.g. used to implement norm
        constraints or value constraints for layer weights). The function must
        take as input the unprojected Tensor representing the value of the
        variable and return the Tensor for the projected value (which must have
        the same shape). Constraints are not safe to use when doing asynchronous
        distributed training.
      use_resource: whether to use resource variables.
      synchronization: Indicates when a distributed a variable will be
        aggregated. Accepted values are constants defined in the class
        `tf.VariableSynchronization`. By default the synchronization is set to
        `AUTO` and the current `DistributionStrategy` chooses when to
        synchronize.
      aggregation: Indicates how a distributed variable will be aggregated.
        Accepted values are constants defined in the class
        `tf.VariableAggregation`.
      shape: (optional) The shape of this variable. If None, the shape of
        `initial_value` will be used. When setting this argument to
        `tf.TensorShape(None)` (representing an unspecified shape), the variable
        can be assigned with values of different shapes.

    Raises:
      ValueError: If both `variable_def` and initial_value are specified.
      ValueError: If the initial value is not specified, or does not have a
        shape and `validate_shape` is `True`.
      RuntimeError: If eager execution is enabled.
    Nr"   )r@   rV   rN   rW   rX   rY   rO   rZ   r[   r\   r]   r^   r_   rL   rM   r`   r"   r"   r#   rr     s    zVariableV1.__init__)r2   r3   r4   r5   r.   r6   r9   r7   rr   rj   r  r"   r"   r"   r#   ri   8  s$   t
[ri   c                   @   s  e Zd ZdZdkddZdd Zdldd	Zdmd
dZdd Zdd Z	dd Z
dd Zdd Zedd Zedd Zedd ZdnddZedd Zed d! Zdod#d$Zdpd%d&Zdqd'd(Zdrd)d*Zdsd+d,Zdtd-d.Zdud/d0Zdvd1d2Zdwd3d4Zdxd5d6Zdyd7d8Zdzd9d:Zd{d;d<Z d|d=d>Z!d}d?d@Z"d~dAdBZ#dCdD Z$e%ddEdFdG Z&e'ddHdIZ(dJZ)edKdL Z*edMdN Z+edOdP Z,edQdR Z-edSdT Z.edUdV Z/edWdX Z0edYdZ Z1dd[d\Z2d]d^ Z3d_d` Z4dadb Z5dcdd Z6dedf Z7dgdh Z8didj Z9dS )r   z&Ref-based implementation of variables.NTc                 C   sL   d| _ |r&|rtd| j||
d n"| j||||||||	||||d dS )a  Creates a new variable with value `initial_value`.

    The new variable is added to the graph collections listed in `collections`,
    which defaults to `[GraphKeys.GLOBAL_VARIABLES]`.

    If `trainable` is `True` the variable is also added to the graph collection
    `GraphKeys.TRAINABLE_VARIABLES`.

    This constructor creates both a `variable` Op and an `assign` Op to set the
    variable to its initial value.

    Args:
      initial_value: A `Tensor`, or Python object convertible to a `Tensor`,
        which is the initial value for the Variable. The initial value must have
        a shape specified unless `validate_shape` is set to False. Can also be a
        callable with no argument that returns the initial value when called. In
        that case, `dtype` must be specified. (Note that initializer functions
        from init_ops.py must first be bound to a shape before being used here.)
      trainable: If `True`, also adds the variable to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES`. This collection is used as the default
        list of variables to use by the `Optimizer` classes. Defaults to `True`,
        unless `synchronization` is set to `ON_READ`, in which case it defaults
        to `False`.
      collections: List of graph collections keys. The new variable is added to
        these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.
      validate_shape: If `False`, allows the variable to be initialized with a
        value of unknown shape. If `True`, the default, the shape of
        `initial_value` must be known.
      caching_device: Optional device string describing where the Variable
        should be cached for reading.  Defaults to the Variable's device. If not
        `None`, caches on another device.  Typical use is to cache on the device
        where the Ops using the Variable reside, to deduplicate copying through
        `Switch` and other conditional statements.
      name: Optional name for the variable. Defaults to `'Variable'` and gets
        uniquified automatically.
      variable_def: `VariableDef` protocol buffer. If not `None`, recreates the
        Variable object with its contents, referencing the variable's nodes in
        the graph, which must already exist. The graph is not changed.
        `variable_def` and the other arguments are mutually exclusive.
      dtype: If set, initial_value will be converted to the given type. If
        `None`, either the datatype will be kept (if `initial_value` is a
        Tensor), or `convert_to_tensor` will decide.
      expected_shape: A TensorShape. If set, initial_value is expected to have
        this shape.
      import_scope: Optional `string`. Name scope to add to the `Variable.` Only
        used when initializing from protocol buffer.
      constraint: An optional projection function to be applied to the variable
        after being updated by an `Optimizer` (e.g. used to implement norm
        constraints or value constraints for layer weights). The function must
        take as input the unprojected Tensor representing the value of the
        variable and return the Tensor for the projected value (which must have
        the same shape). Constraints are not safe to use when doing asynchronous
        distributed training.
      synchronization: Indicates when a distributed a variable will be
        aggregated. Accepted values are constants defined in the class
        `tf.VariableSynchronization`. By default the synchronization is set to
        `AUTO` and the current `DistributionStrategy` chooses when to
        synchronize.
      aggregation: Indicates how a distributed variable will be aggregated.
        Accepted values are constants defined in the class
        `tf.VariableAggregation`.
      shape: (optional) The shape of this variable. If None, the shape of
        `initial_value` will be used. When setting this argument to
        `tf.TensorShape(None)` (representing an unspecified shape), the variable
        can be assigned with values of different shapes.

    Raises:
      ValueError: If both `variable_def` and initial_value are specified.
      ValueError: If the initial value is not specified, or does not have a
        shape and `validate_shape` is `True`.
      RuntimeError: If eager execution is enabled.
    Tz6variable_def and initial_value are mutually exclusive.r   )rV   rN   rW   rX   rY   rO   r[   r\   r^   rL   rM   r`   N)_in_graph_moderJ   _init_from_proto_init_from_args)r@   rV   rN   rW   rX   rY   rO   rZ   r[   r\   r]   r^   rL   rM   r`   r"   r"   r#   rr     s&    XzRefVariable.__init__c                 C   sR   t  r6| js6d| j|  | jjtj|  ddf S d| j|  | jjf S d S )Nz.<tf.Variable '%s' shape=%s dtype=%s, numpy=%s>T)is_reprz$<tf.Variable '%s' shape=%s dtype=%s>)	r   r   r
  rO   r   r[   r   
numpy_textrv   r?   r"   r"   r#   rs   }  s    zRefVariable.__repr__c                 C   s  |}|du rt dt|}|du r.tjjg}t|tttfsRt d|t	|f |	durjt|	sjt dt
 j| _t|tjr|   |jj| _|j}t|
|||\}
}}|
| _|| _|| _|rtjj|vrt|tjjg }t  t rtdt|d|rg n|g}|rTt|}tjtjj t!"d| gdd	}t
 #d
|i td t$dp | }t|tjr|   |jj| _|j}tj%|d|d| _&|du r|r| j&' nt() }W d   n1 s0    Y  W d   n1 s0    Y  t*j+|| j&j,j-|d| _.W d   n1 sH0    Y  njtj%|d|d| _&| j&j/0 durt d| |du r|r| j&' nt() }t*j+|| j&j,j-|d| _.| j.j1| _2|r| j&' }|3 st d| j& t*j4| j.t5|| j&|dj/| _6|durXt$|" t7j8| j.dd| _9W d   n1 sL0    Y  nBt:| j.j/" t7j8| j.dd| _9W d   n1 s0    Y  W d   n1 s0    Y  t;||  W d   n1 s0    Y  || _<d| _=|	| _>dS )a>  Creates a new variable from arguments.

    Args:
      initial_value: A `Tensor`, or Python object convertible to a `Tensor`,
        which is the initial value for the Variable. The initial value must have
        a shape specified unless `validate_shape` is set to False. Can also be a
        callable with no argument that returns the initial value when called.
        (Note that initializer functions from init_ops.py must first be bound to
        a shape before being used here.)
      trainable: If `True`, also adds the variable to the graph collection
        `GraphKeys.TRAINABLE_VARIABLES`. This collection is used as the default
        list of variables to use by the `Optimizer` classes. Defaults to `True`,
        unless `synchronization` is set to `ON_READ`, in which case it defaults
        to `False`.
      collections: List of graph collections keys. The new variable is added to
        these collections. Defaults to `[GraphKeys.GLOBAL_VARIABLES]`.
      validate_shape: If `False`, allows the variable to be initialized with a
        value of unknown shape. If `True`, the default, the shape of
        `initial_value` must be known.
      caching_device: Optional device string or function describing where the
        Variable should be cached for reading.  Defaults to the Variable's
        device.  If not `None`, caches on another device.  Typical use is to
        cache on the device where the Ops using the Variable reside, to
        deduplicate copying through `Switch` and other conditional statements.
      name: Optional name for the variable. Defaults to `'Variable'` and gets
        uniquified automatically.
      dtype: If set, initial_value will be converted to the given type. If None,
        either the datatype will be kept (if initial_value is a Tensor) or
        float32 will be used (if it is a Python object convertible to a Tensor).
      expected_shape: Deprecated. Ignored.
      constraint: An optional projection function to be applied to the variable
        after being updated by an `Optimizer` (e.g. used to implement norm
        constraints or value constraints for layer weights). The function must
        take as input the unprojected Tensor representing the value of the
        variable and return the Tensor for the projected value (which must have
        the same shape). Constraints are not safe to use when doing asynchronous
        distributed training.
      synchronization: Indicates when a distributed a variable will be
        aggregated. Accepted values are constants defined in the class
        `tf.VariableSynchronization`. By default the synchronization is set to
        `AUTO` and the current `DistributionStrategy` chooses when to
        synchronize.
      aggregation: Indicates how a distributed variable will be aggregated.
        Accepted values are constants defined in the class
        `tf.VariableAggregation`.
      shape: (optional) The shape of this variable. If None, the shape of
        `initial_value` will be used. When setting this argument to
        `tf.TensorShape(None)` (representing an unspecified shape), the variable
        can be assigned with values of different shapes.

    Raises:
      ValueError: If the initial value is not specified, or does not have a
        shape and `validate_shape` is `True`.
      RuntimeError: If lifted into the eager context.
    Nz initial_value must be specified.z]collections argument to Variable constructor must be a list, tuple, or set. Got %s of type %sz-The `constraint` argument must be a callable.zReference variables are not supported when eager execution is enabled. Please run `tf.compat.v1.enable_resource_variables()` to switch to resource variables.rj   zloc:@%s)r   )list_classInitializerrV   )rO   r[   rO   zInitializer for variable %s is from inside a control-flow construct, such as a loop or conditional. When creating a variable inside a loop or conditional, use a lambda as the initializer.z-initial_value must have a shape specified: %s)rX   read)?rJ   callabler   	GraphKeysGLOBAL_VARIABLESrB   r  tuplesettyperb   
_graph_keyr   CheckpointInitialValue_maybe_initialize_trackablecheckpoint_positionrestore_uid_update_uidwrapped_valuerP   _synchronization_aggregation
_trainableTRAINABLE_VARIABLESr~   r   r   RuntimeError
name_scopename_from_scope_namer   	AttrValue	ListValuer   as_bytes_attr_scoper   convert_to_tensor_initial_valuer   r	   unknown_shaper   variable_op_v2r[   
base_dtype	_variabler   _get_control_flow_contextrO   _nameis_fully_definedr   -_try_guard_against_uninitialized_dependencies_initializer_opr
   identity	_snapshotcolocate_withadd_to_collections_caching_devicer   _constraint)r@   rV   rN   rW   rX   rY   rO   r[   r\   r^   rL   rM   r`   r    init_from_fnZ	true_nameattrZinitial_value_shaper"   r"   r#   r    s    D





B*




	
4R,zRefVariable._init_from_argsc                 C   s   t |tjsJ t }|tj|j|d| _| jj	| _
|tj|j|d| _t|dr||jr||tj|j|d| _nd| _t|j|j|j|j\}}}|| _|| _|| _|tj|j|d| _|drtj|j|d| _nd| _d| _d| _dS )a  Recreates the Variable object from a `VariableDef` protocol buffer.

    Args:
      variable_def: `VariableDef` protocol buffer, describing a variable whose
        nodes already exists in the graph.
      import_scope: Optional `string`. Name scope to add.
    r   initial_value_nameNr   )r   r]   ) rB   r   VariableDefr   rb   as_graph_elementr   variable_namer1  rO   r3  initializer_namer6  hasattrr?  r-  rP   rL   rM   rN   r!  r"  r#  snapshot_namer8  HasFieldrj   r  r   r   r;  r<  )r@   rZ   r]   grL   rM   rN   r"   r"   r#   r  E  sV    



zRefVariable._init_from_protoc                 C   s   | j S )z1Conversion function for Graph.as_graph_element().r1  r?   r"   r"   r#   _as_graph_elementr  s    zRefVariable._as_graph_elementc                 C   s   | j S )rt   )r8  r?   r"   r"   r#   r>   v  s    zRefVariable.valuec                 C   s   t j| jddS )ru   r  r  )r
   r7  r1  r?   r"   r"   r#   rv     s    	zRefVariable.read_valuec                 C   s   | j S )a  Returns a reference to this variable.

    You usually do not need to call this method as all ops that need a reference
    to the variable call it automatically.

    Returns is a `Tensor` which holds a reference to the variable.  You can
    assign a new value to the variable by passing the tensor to an assign op.
    See `tf.Variable.value` if you want to get the value of the
    variable.

    Returns:
      A `Tensor` that is a reference to the variable.
    rH  r?   r"   r"   r#   r     s    zRefVariable._refc                 C   s    |   | |  | dS rw   )r   ry   r>   rx   r"   r"   r#   ry     s    zRefVariable.set_shapec                 C   s   | j S r&   )r#  r?   r"   r"   r#   rN     s    zRefVariable.trainablec                 C   s   | j S r&   )r!  r?   r"   r"   r#   rL     s    zRefVariable.synchronizationc                 C   s   | j S r&   )r"  r?   r"   r"   r#   rM     s    zRefVariable.aggregationc                 C   s   | j j|dS )rz   )r|   )r1  r}   r{   r"   r"   r#   r}     s    zRefVariable.evalc                 C   s   | j S )r   )r-  r?   r"   r"   r#   rV     s    zRefVariable.initial_valuec                 C   s   | j S )r   )r<  r?   r"   r"   r#   r^     s    zRefVariable.constraintFc                 C   s"   t j| j|||d}|r|S |jS )a(  Assigns a new value to the variable.

    This is essentially a shortcut for `assign(self, value)`.

    Args:
      value: A `Tensor`. The new value for this variable.
      use_locking: If `True`, use locking during the assignment.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the assignment has completed.
    r   rO   )r   r   r1  r   )r@   r>   r   rO   rv   r   r"   r"   r#   r     s    
zRefVariable.assignc                 C   s"   t j| j|||d}|r|S |jS )a&  Adds a value to this variable.

     This is essentially a shortcut for `assign_add(self, delta)`.

    Args:
      delta: A `Tensor`. The value to add to this variable.
      use_locking: If `True`, use locking during the operation.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the addition has completed.
    rJ  )r   r   r1  r   r@   r   r   rO   rv   r   r"   r"   r#   r     s    
zRefVariable.assign_addc                 C   s"   t j| j|||d}|r|S |jS )a6  Subtracts a value from this variable.

    This is essentially a shortcut for `assign_sub(self, delta)`.

    Args:
      delta: A `Tensor`. The value to subtract from this variable.
      use_locking: If `True`, use locking during the operation.
      name: The name of the operation to be created
      read_value: if True, will return something which evaluates to the new
        value of the variable; if False will return the assign op.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the subtraction has completed.
    rJ  )r   r   r1  r   rK  r"   r"   r#   r     s    
zRefVariable.assign_subc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Subtracts `tf.IndexedSlices` from this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be subtracted from this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered subtraction has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    %sparse_delta is not IndexedSlices: %srJ  )	rB   r   IndexedSlicesr   r   r   r1  r   valuesr   r"   r"   r#   r   3  s    zRefVariable.scatter_subc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Adds `tf.IndexedSlices` to this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be added to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered addition has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r   K  s    zRefVariable.scatter_addc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Updates this variable with the max of `tf.IndexedSlices` and itself.

    Args:
      sparse_delta: `tf.IndexedSlices` to use as an argument of max with this
        variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered maximization has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r   c  s    zRefVariable.scatter_maxc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Updates this variable with the min of `tf.IndexedSlices` and itself.

    Args:
      sparse_delta: `tf.IndexedSlices` to use as an argument of min with this
        variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered minimization has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r   |  s    zRefVariable.scatter_minc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Multiply this variable by `tf.IndexedSlices`.

    Args:
      sparse_delta: `tf.IndexedSlices` to multiply this variable by.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered multiplication has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r     s    zRefVariable.scatter_mulc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Divide this variable by `tf.IndexedSlices`.

    Args:
      sparse_delta: `tf.IndexedSlices` to divide this variable by.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered division has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r     s    zRefVariable.scatter_divc                 C   s2   t |tjstd| tj| j|j|j||dS )a  Assigns `tf.IndexedSlices` to this variable.

    Args:
      sparse_delta: `tf.IndexedSlices` to be assigned to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered assignment has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rL  rJ  )	rB   r   rM  r   r   r   r1  r   rN  r   r"   r"   r#   r     s    zRefVariable.scatter_updatec                 C   s   t j| |j|j||dS )at  Assigns `tf.IndexedSlices` to this variable batch-wise.

    Analogous to `batch_gather`. This assumes that this variable and the
    sparse_delta IndexedSlices have a series of leading dimensions that are the
    same for all of them, and the updates are performed on the last dimension of
    indices. In other words, the dimensions should be the following:

    `num_prefix_dims = sparse_delta.indices.ndims - 1`
    `batch_dim = num_prefix_dims + 1`
    `sparse_delta.updates.shape = sparse_delta.indices.shape + var.shape[
         batch_dim:]`

    where

    `sparse_delta.updates.shape[:num_prefix_dims]`
    `== sparse_delta.indices.shape[:num_prefix_dims]`
    `== var.shape[:num_prefix_dims]`

    And the operation performed can be expressed as:

    `var[i_1, ..., i_n,
         sparse_delta.indices[i_1, ..., i_n, j]] = sparse_delta.updates[
            i_1, ..., i_n, j]`

    When sparse_delta.indices is a 1D tensor, this operation is equivalent to
    `scatter_update`.

    To avoid this operation one can looping over the first `ndims` of the
    variable and using `scatter_update` on the subtensors that result of slicing
    the first dimension. This is a valid option for `ndims = 1`, but less
    efficient than this implementation.

    Args:
      sparse_delta: `tf.IndexedSlices` to be assigned to this variable.
      use_locking: If `True`, use locking during the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered assignment has completed.

    Raises:
      TypeError: if `sparse_delta` is not an `IndexedSlices`.
    rJ  )r   r   r   rN  r   r"   r"   r#   r     s    -z RefVariable.batch_scatter_updatec                 C   s   t j| j||d|dS )a  Applies sparse subtraction to individual values or slices in a Variable.

    `ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into `ref`.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of `ref`.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        op = ref.scatter_nd_sub(indices, updates)
        with tf.compat.v1.Session() as sess:
          print sess.run(op)
    ```

    The resulting update to ref would look like this:

        [1, -9, 3, -6, -6, 6, 7, -4]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered subtraction has completed.
    TrJ  )r   r   r1  r   r"   r"   r#   r   	  s    .zRefVariable.scatter_nd_subc                 C   s   t j| j||d|dS )a  Applies sparse addition to individual values or slices in a Variable.

    `ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into `ref`.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of `ref`.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        add = ref.scatter_nd_add(indices, updates)
        with tf.compat.v1.Session() as sess:
          print sess.run(add)
    ```

    The resulting update to ref would look like this:

        [1, 13, 3, 14, 14, 6, 7, 20]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered addition has completed.
    TrJ  )r   r   r1  r   r"   r"   r#   r   B	  s    .zRefVariable.scatter_nd_addc                 C   s   t j| j||d|dS )a  Applies sparse assignment to individual values or slices in a Variable.

    `ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into `ref`.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of `ref`.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
    ```

    For example, say we want to add 4 scattered elements to a rank-1 tensor to
    8 elements. In Python, that update would look like this:

    ```python
        ref = tf.Variable([1, 2, 3, 4, 5, 6, 7, 8])
        indices = tf.constant([[4], [3], [1] ,[7]])
        updates = tf.constant([9, 10, 11, 12])
        op = ref.scatter_nd_update(indices, updates)
        with tf.compat.v1.Session() as sess:
          print sess.run(op)
    ```

    The resulting update to ref would look like this:

        [1, 11, 3, 10, 9, 6, 7, 12]

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered assignment has completed.
    TrJ  )r   r   r1  r   r"   r"   r#   r   s	  s    .zRefVariable.scatter_nd_updatec                 C   s   t j| j||d|dS )a  Updates this variable with the max of `tf.IndexedSlices` and itself.

    `ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into `ref`.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of `ref`.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
    ```

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered addition has completed.
    TrJ  )r   scatter_nd_maxr1  r   r"   r"   r#   rO  	  s    zRefVariable.scatter_nd_maxc                 C   s   t j| j||d|dS )a  Updates this variable with the min of `tf.IndexedSlices` and itself.

    `ref` is a `Tensor` with rank `P` and `indices` is a `Tensor` of rank `Q`.

    `indices` must be integer tensor, containing indices into `ref`.
    It must be shape `[d_0, ..., d_{Q-2}, K]` where `0 < K <= P`.

    The innermost dimension of `indices` (with length `K`) corresponds to
    indices into elements (if `K = P`) or slices (if `K < P`) along the `K`th
    dimension of `ref`.

    `updates` is `Tensor` of rank `Q-1+P-K` with shape:

    ```
    [d_0, ..., d_{Q-2}, ref.shape[K], ..., ref.shape[P-1]].
    ```

    See `tf.scatter_nd` for more details about how to make updates to
    slices.

    Args:
      indices: The indices to be used in the operation.
      updates: The values to be used in the operation.
      name: the name of the operation.

    Returns:
      A `Tensor` that will hold the new value of this variable after
      the scattered addition has completed.
    TrJ  )r   scatter_nd_minr1  r   r"   r"   r#   rP  	  s    zRefVariable.scatter_nd_minc                 C   s$   t j|  |||||||||	|
dS )N)r   beginendstridesr>   rO   
begin_maskend_maskellipsis_masknew_axis_maskshrink_axis_mask)r   strided_slice_assignr   )r@   rQ  rR  rS  r>   rO   rT  rU  rV  rW  rX  r"   r"   r#   _strided_slice_assign	  s    z!RefVariable._strided_slice_assignr   c                 C   s   t j| j|dS )r   )r   )r   r   r1  r   r"   r"   r#   r   	  s    zRefVariable.count_up_toc                 C   sB   |}|r*| | js*td|j| jjf |r6|  S |  S dS )r   MIncompatible type conversion requested to type '%s' for variable of type '%s'Nr   r   r"   r"   r#   r   
  s    z%RefVariable._TensorConversionFunctionr   c                 C   s   | j S )r   r3  r?   r"   r"   r#   rO   $
  s    zRefVariable.namec                 C   s   | j S )r   )r6  r?   r"   r"   r#   r   )
  s    zRefVariable.initializerc                 C   s   | j jS )r   )r1  r   r?   r"   r"   r#   r   .
  s    zRefVariable.devicec                 C   s   | j jS )r   )r1  r[   r?   r"   r"   r#   r[   3
  s    zRefVariable.dtypec                 C   s   | j jS )r   )r1  r   r?   r"   r"   r#   r   8
  s    zRefVariable.opc                 C   s   | j jS )r   )r1  r   r?   r"   r"   r#   r   =
  s    zRefVariable.graphc                 C   s   dS zBThe `tf.distribute.Strategy` that this variable was created under.Nr"   r?   r"   r"   r#   _distribute_strategyB
  s    z RefVariable._distribute_strategyc                 C   s
   | j  S )r   )r1  r   r?   r"   r"   r#   r`   G
  s    zRefVariable.shapec                 C   s   |du s| j j|rt }t| j j||_| jdurLt| jj||_	| j
|_
| jj|_| jj|_t| jj||_t| jj||_| jr|j| jj|d |S dS dS )r   N)r   )r1  rO   r  r   r@  r   r  rB  r-  r?  rN   rL   r>   rM   r   rC  r8  rE  r   r   	MergeFromr   )r@   r   var_defr"   r"   r#   r   P
  s0    






zRefVariable.to_protoc                 C   s   t t jdd | | S )NzVariable += will be deprecated. Use variable.assign_add if you want assignment to the variable value or 'x = x + y' if you want a new python Tensor object.r/   logginglog_first_nWARNrD   r"   r"   r#   __iadd__p
  s
    zRefVariable.__iadd__c                 C   s   t t jdd | | S )NzVariable -= will be deprecated. Use variable.assign_sub if you want assignment to the variable value or 'x = x - y' if you want a new python Tensor object.r/   ra  rD   r"   r"   r#   __isub__w
  s
    zRefVariable.__isub__c                 C   s   t t jdd | | S )NzVariable *= will be deprecated. Use `var.assign(var * other)` if you want assignment to the variable value or `x = x * y` if you want a new python Tensor object.r/   ra  rD   r"   r"   r#   __imul__~
  s    zRefVariable.__imul__c                 C   s   t t jdd | | S NzVariable /= will be deprecated. Use `var.assign(var / other)` if you want assignment to the variable value or `x = x / y` if you want a new python Tensor object.r/   ra  rD   r"   r"   r#   __idiv__
  s    zRefVariable.__idiv__c                 C   s   t t jdd | | S rh  ra  rD   r"   r"   r#   __itruediv__
  s    zRefVariable.__itruediv__c                 C   s   t t jdd | | S rh  ra  rD   r"   r"   r#   __irealdiv__
  s    zRefVariable.__irealdiv__c                 C   s   t t jdd | | S )NzVariable **= will be deprecated. Use `var.assign(var ** other)` if you want assignment to the variable value or `x = x ** y` if you want a new python Tensor object.r/   ra  rD   r"   r"   r#   __ipow__
  s    zRefVariable.__ipow__)NNNTNNNNNNNNNN)NNNTNNNNNNNN)N)N)FNT)FNT)FNT)FN)FN)FN)FN)FN)FN)FN)FN)N)N)N)N)N)NNF)N):r2   r3   r4   r5   rr   rs   r  r  rI  r>   rv   r   ry   r  rN   rL   rM   r}   rV   r^   r   r   r   r   r   r   r   r   r   r   r   r   r   r   rO  rP  rZ  r   r   r  r   r  rO   r   r   r[   r   r   r^  r`   r   re  rf  rg  ri  rj  rk  rl  r"   r"   r"   r#   r     s                 
o
            
 @
-	



 

	










4
1
1
1
!
!









 r   c                 C   s8   t |tjstd| t|ji dr*|S t| |i dS )a  Attempt to guard against dependencies on uninitialized variables.

  Replace references to variables in `initial_value` with references to the
  variable's initialized values. The initialized values are essentially
  conditional TensorFlow graphs that return a variable's value if it is
  initialized or its `initial_value` if it hasn't been initialized. This
  replacement is done on a best effort basis:

  - If the `initial_value` graph contains cycles, we don't do any
    replacements for that graph.
  - If the variables that `initial_value` depends on are not present in the
    `GLOBAL_VARIABLES` or `LOCAL_VARIABLES` we don't replace them.

  In these cases, it is up to the caller to ensure that the `initial_value`
  graph uses initialized variables or that they guard access to variables
  using their `initialized_value` method.

  Args:
    name: Variable name.
    initial_value: `Tensor`. The initial value.

  Returns:
    A `Tensor` suitable to initialize a variable.
  Raises:
    TypeError: If `initial_value` is not a `Tensor`.
  z&initial_value needs to be a Tensor: %s)state)op_cache)rB   r   r   r   
_has_cycler   _safe_initial_value_from_tensor)rO   rV   r"   r"   r#   r5  
  s
    r5  r1   c                 C   sn   | | jt}|tkrdS |tkr&dS t|| j< tdd | jD | jD ]}t	||rJ dS qJt|| j< dS )z5Detect cycles in the dependencies of `initial_value`.TFc                 s   s   | ]}|j V  qd S r&   r   r   r"   r"   r#   r   
  rU   z_has_cycle.<locals>.<genexpr>)
getrO   _UNKNOWN_STARTED	_FINISHED	itertoolschainr   control_inputsro  )r   rm  Zop_stater   r"   r"   r#   ro  
  s    


ro  c                 C   s<   |j }||j}|du r0t| ||}|||j< |j|j S )aW  Replace dependencies on variables with their initialized values.

  Args:
    name: Variable name.
    tensor: A `Tensor`. The tensor to replace.
    op_cache: A dict mapping operation names to `Operation`s. Used to memoize
      the results so as to avoid creating redundant operations.

  Returns:
    A `Tensor` compatible with `tensor`. Any inputs that lead to variable
    values will be replaced with a corresponding graph that uses the
    variable's initialized values. This is done on a best-effort basis. If no
    modifications need to be made then `tensor` will be returned unchanged.
  N)r   rr  rO   _safe_initial_value_from_opoutputsvalue_index)rO   tensorrn  r   new_opr"   r"   r#   rp  
  s    
rp  c                 C   s   |j j}|dv r|S |dv r6t|}|du r0|S |jS d}g }|jD ]&}t| ||}|| |ph||k}qD|r|}	|	dkrd}	|j jd |  }
|
dd}
|jj	|	||j
|
|j jd	S |S )
aV  Replace dependencies on variables with their initialized values.

  Args:
    name: Variable name.
    op: An `Operation`. The operation to replace.
    op_cache: A dict mapping operation names to `Operation`s. Used to memoize
      the results so as to avoid creating redundant operations.

  Returns:
    An `Operation` compatible with `op`. Any inputs that lead to variable
    values will be replaced with a corresponding graph that uses the
    variable's initialized values. This is done on a best-effort basis. If no
    modifications need to be made then `op` will be returned unchanged.
  )IsVariableInitializedVarIsInitializedOpReadVariableOpIf)rj   
VariableV2VarHandleOpNF	RefSwitchSwitchr    r   )rO   attrs)node_defr   $_find_initialized_value_for_variabler   rp  r  rO   replacer   	create_op_output_typesr>  )rO   r   rn  op_typer   modifiedZnew_op_inputsop_inputZnew_op_inputZnew_op_typeZnew_op_namer"   r"   r#   ry  
  s4    

ry  c                 C   sv   z\| j j| j jd g}tjjtjjfD ]2}| j|D ] }|j|v r6|     W S q6q&W n t	yp   Y dS 0 dS )a-  Find the initialized value for a variable op.

  To do so, lookup the variable op in the variables collection.

  Args:
    variable_op: A variable `Operation`.

  Returns:
    A `Tensor` representing the initialized value for the variable or `None`
    if the initialized value could not be found.
  z:0N)
r  rO   r   r  r  LOCAL_VARIABLESr   get_collectionr   r   )variable_op	var_namescollection_namevarr"   r"   r#   r  (  s    
r  c                   @   s   e Zd ZdZdd Zdd Zdd Zdd	 Zd
d Zdd Z	e
d)ddZedd Zedd Zedd Zedd Zdd Zdd Zdd Zd d! Zd*d#d$Zd+d%d&Zd,d'd(ZdS )-PartitionedVariablea  A container for partitioned `Variable` objects.

  @compatibility(eager) `tf.PartitionedVariable` is not compatible with
  eager execution.  Use `tf.Variable` instead which is compatible
  with both eager execution and graph construction.  See [the
  TensorFlow Eager Execution
  guide](https://www.tensorflow.org/guide/eager#variables_and_optimizers)
  for details on how variables work in eager execution.
  @end_compatibility
  c                 C   s  t |ttfstd| t |ttfs4td| tdd |D sRtd| |s^td|D ]v}tdd |D stdd	d
 |D  t|t|krtd||f | j|krbtd|t	| jgf qbt
|dd d| _|| _|| _|| _|| _d| _dS )aS  Creates a new partitioned variable wrapper.

    Variables passed via the variable_list must contain a save_slice_info
    field.  Concatenation and iteration is in lexicographic order according
    to the var_offset property of the save_slice_info.

    Args:
      name: String. Overall name of the variables.
      shape: List of integers.  Overall shape of the variables.
      dtype: Type of the variables.
      variable_list: List of `Variable` that comprise this partitioned variable.
      partitions: List of integers.  Number of partitions for each dimension.

    Raises:
      TypeError: If `variable_list` is not a list of `Variable` objects, or
        `partitions` is not a list.
      ValueError: If `variable_list` is empty, or the `Variable` shape
        information does not match `shape`, or `partitions` has invalid values.
    z(variable_list is not a list or tuple: %sz%partitions is not a list or tuple: %sc                 s   s   | ]}|d kV  qdS r/   Nr"   r   pr"   r"   r#   r   g  rU   z/PartitionedVariable.__init__.<locals>.<genexpr>z%partition values must be positive: %szvariable_list may not be emptyc                 s   s   | ]}|  d uV  qd S r&   )r   r   r   r"   r"   r#   r   n  rU   z7All variables must have a save_slice_info available: %sc                 S   s   g | ]
}|j qS r"   r  r  r"   r"   r#   r   q  rU   z0PartitionedVariable.__init__.<locals>.<listcomp>z(len(shape) != len(partitions): %s vs. %szIAll variables' full shapes must match shape: %s; but full shapes were: %sc                 S   s
   |   jS r&   )r   r   )r   r"   r"   r#   rT   z  rU   z.PartitionedVariable.__init__.<locals>.<lambda>)keyN)rB   r  r  r   allrJ   lenr   r   strsorted_variable_listr3  _shape_dtype_partitions
_as_tensor)r@   rO   r`   r[   variable_list
partitionsr   r"   r"   r#   rr   N  sB    zPartitionedVariable.__init__c                 C   s
   t | jS )zDReturn an iterable for accessing the underlying partition Variables.)r   r  r?   r"   r"   r#   r     s    zPartitionedVariable.__iter__c                 C   s*   t |  }|dkr td| t | jS )Nr/   z-Cannot get a length for %d > 1 partition axes)r  _partition_axesrJ   r  )r@   Znum_partition_axesr"   r"   r#   __len__  s    zPartitionedVariable.__len__c                 C   s2   t dd | jD rdgS dd t| jD S d S )Nc                 s   s   | ]}|d kV  qdS r  r"   r  r"   r"   r#   r     rU   z6PartitionedVariable._partition_axes.<locals>.<genexpr>r   c                 S   s   g | ]\}}|d kr|qS )r/   r"   )r   r   r  r"   r"   r#   r     rU   z7PartitionedVariable._partition_axes.<locals>.<listcomp>)r  r  	enumerater?   r"   r"   r#   r    s    z#PartitionedVariable._partition_axesc                 C   s   t | jdkrNtd& tj| jd | jdW  d   S 1 sD0    Y  |  }t |dkrrtdt	| |d }t| jd  t
| j|}W d   n1 s0    Y  td  tj|| jdW  d   S 1  s0    Y  dS )a]  Returns the overall concatenated value as a `Tensor`.

    This is different from using the partitioned variable directly as a tensor
    (through tensor conversion and `as_tensor`) in that it creates a new set of
    operations that keeps the control dependencies from its scope.

    Returns:
      `Tensor` containing the concatenated value.
    r/   Nr   r  zcCannot concatenate along more than one dimension: %s.  Multi-axis partition concat is not supportedz/ConcatPartitions/)r  r  r   r&  r
   r7  r3  r  r   r  concat)r@   partition_axespartition_ixconcatenatedr"   r"   r#   _concat  s    
4,zPartitionedVariable._concatc                 C   s6   t d |  W  d   S 1 s(0    Y  dS )a"  Returns the overall concatenated value as a `Tensor`.

    The returned tensor will not inherit the control dependencies from the scope
    where the value is used, which is similar to getting the value of
    `Variable`.

    Returns:
      `Tensor` containing the concatenated value.
    N)r   control_dependenciesr  r?   r"   r"   r#   	as_tensor  s    
zPartitionedVariable.as_tensorNFc                 C   sH   |}|d ur.| | js.td|j| jjf |r<tdn|  S d S )Nr[  z>PartitionedVariable doesn't support being used as a reference.)r   r[   rJ   rO   r   r  r   r"   r"   r#   r     s    z-PartitionedVariable._TensorConversionFunctionc                 C   s   | j S r&   r\  r?   r"   r"   r#   rO     s    zPartitionedVariable.namec                 C   s   | j S r&   )r  r?   r"   r"   r#   r[     s    zPartitionedVariable.dtypec                 C   s   |   S r&   )r   r?   r"   r"   r#   r`     s    zPartitionedVariable.shapec                 C   s   dS r]  r"   r?   r"   r"   r#   r^    s    z(PartitionedVariable._distribute_strategyc                 C   s   | j S r&   )r  r?   r"   r"   r#   r     s    zPartitionedVariable.get_shapec                 C   s   | j S r&   )r  r?   r"   r"   r#   _get_variable_list  s    z&PartitionedVariable._get_variable_listc                 C   s   | j S r&   )r  r?   r"   r"   r#   _get_partitions  s    z#PartitionedVariable._get_partitionsc                    s   |   }t|dkr$tdt| t|trJt|t| jksDJ |nFt|trddd |D n,|d fdd| jD }tj	||d fddt
| jD }|S )	Nr/   zpCannot do assign action along more than one dimension: %s.  Multi-axis partition assign action is not supported c                 S   s   g | ]}|qS r"   r"   )r   Zvar_partr"   r"   r#   r     rU   z8PartitionedVariable._apply_assign_fn.<locals>.<listcomp>r   c                    s   g | ]}t |j  qS r"   )r	   dimension_valuer`   )r   r  )r  r"   r#   r     s   )axisc                    s   g | ]\}} || qS r"   r"   )r   idxr  )	assign_fn
value_listr"   r#   r     s   )r  r  r   r  rB   r  r  r  r
   splitr  )r@   r  r>   r  Zsize_splits_listop_listr"   )r  r  r  r#   _apply_assign_fn  s*    


z$PartitionedVariable._apply_assign_fnTc                    s2    fdd}|  ||}r$|S dd |D S )Nc                    s   | j | dS N)r   rO   rv   )r   r  Zr_valuerO   rv   r   r"   r#   rT     s   z,PartitionedVariable.assign.<locals>.<lambda>c                 S   s   g | ]
}|j qS r"   rq  r   r   r"   r"   r#   r     rU   z.PartitionedVariable.assign.<locals>.<listcomp>r  r@   r>   r   rO   rv   r  Zassign_listr"   r  r#   r      s
    zPartitionedVariable.assignc                    s2    fdd}|  ||}r$|S dd |D S )Nc                    s   | j | dS r  )r   r  r  r"   r#   rT   	  s   z0PartitionedVariable.assign_add.<locals>.<lambda>c                 S   s   g | ]
}|j qS r"   rq  r  r"   r"   r#   r     rU   z2PartitionedVariable.assign_add.<locals>.<listcomp>r  r  r"   r  r#   r     s
    zPartitionedVariable.assign_addc                    s2    fdd}|  ||}r$|S dd |D S )Nc                    s   | j | dS r  )r   r  r  r"   r#   rT     s   z0PartitionedVariable.assign_sub.<locals>.<lambda>c                 S   s   g | ]
}|j qS r"   rq  r  r"   r"   r#   r     rU   z2PartitionedVariable.assign_sub.<locals>.<listcomp>r  r  r"   r  r#   r     s
    zPartitionedVariable.assign_sub)NNF)FNT)FNT)FNT)r2   r3   r4   r5   rr   r   r  r  r  r  r  r   r  rO   r[   r`   r^  r   r  r  r  r   r   r   r"   r"   r"   r#   r  B  s0   5





r  global_variablesc                 C   s   t t jj| S )a  Returns global variables.

  Global variables are variables that are shared across machines in a
  distributed environment. The `Variable()` constructor or `get_variable()`
  automatically adds new variables to the graph collection
  `GraphKeys.GLOBAL_VARIABLES`.
  This convenience function returns the contents of that collection.

  An alternative to global variables are local variables. See
  `tf.compat.v1.local_variables`

  @compatibility(TF2)
  Not compatible with eager execution and `tf.function`. In particular, Graph
  collections are deprecated in TF2. Instead please create a
  [tf.Module](https://www.tensorflow.org/guide/intro_to_modules)
  container for all your model state, including variables.
  You can then list all the variables in your `tf.Module` through the
  `variables` attribute.
  @end_compatibility

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of `Variable` objects.
  )r   r  r  r  scoper"   r"   r#   r    s     all_variablesz
2017-03-02z'Please use tf.global_variables instead.c                   C   s   t  S )z,Use `tf.compat.v1.global_variables` instead.)r  r"   r"   r"   r#   r  B  s    c                 C   s    t t jj| t t jj|  S )a  Returns all variables and `SaveableObject`s that must be checkpointed.

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of `Variable` and `SaveableObject` to be checkpointed
  )r   r  r  r  SAVEABLE_OBJECTSr  r"   r"   r#   _all_saveable_objectsI  s    r  local_variablesc                 C   s   t t jj| S )a  Returns local variables.

  Local variables - per process variables, usually not saved/restored to
  checkpoint and used for temporary or intermediate values.
  For example, they can be used as counters for metrics computation or
  number of epochs this machine has read data.
  The `tf.contrib.framework.local_variable()` function automatically adds the
  new variable to `GraphKeys.LOCAL_VARIABLES`.
  This convenience function returns the contents of that collection.

  An alternative to local variables are global variables. See
  `tf.compat.v1.global_variables`

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of local `Variable` objects.
  )r   r  r  r  r  r"   r"   r#   r  [  s    model_variablesc                 C   s   t t jj| S )a  Returns all variables in the MODEL_VARIABLES collection.

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of local Variable objects.
  )r   r  r  MODEL_VARIABLESr  r"   r"   r#   r  w  s    trainable_variablesc                 C   s   t t jj| S )a  Returns all variables created with `trainable=True`.

  When passed `trainable=True`, the `Variable()` constructor automatically
  adds new variables to the graph collection
  `GraphKeys.TRAINABLE_VARIABLES`. This convenience function returns the
  contents of that collection.

  @compatibility(TF2)
  Not compatible with eager execution and `tf.function`. In particular, Graph
  collections are deprecated in TF2. Instead please create a `tf.Module`
  container for all your model state, including variables.
  You can then list all the trainable variables in your `tf.Module` through the
  `trainable_variables` attribute.
  @end_compatibility

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of Variable objects.
  )r   r  r  r$  r  r"   r"   r#   r    s    moving_average_variablesc                 C   s   t t jj| S )a  Returns all variables that maintain their moving averages.

  If an `ExponentialMovingAverage` object is created and the `apply()`
  method is called on a list of variables, these variables will
  be added to the `GraphKeys.MOVING_AVERAGE_VARIABLES` collection.
  This convenience function returns the contents of that collection.

  Args:
    scope: (Optional.) A string. If supplied, the resulting list is filtered to
      include only items whose `name` attribute matches `scope` using
      `re.match`. Items without a `name` attribute are never returned if a scope
      is supplied. The choice of `re.match` means that a `scope` without special
      tokens filters by prefix.

  Returns:
    A list of Variable objects.
  )r   r  r  MOVING_AVERAGE_VARIABLESr  r"   r"   r#   r    s    zinitializers.variablesvariables_initializerinitc                 C   s2   | r&t  s&tjdd | D d|iS tj|dS )a]  Returns an Op that initializes a list of variables.

  After you launch the graph in a session, you can run the returned Op to
  initialize all the variables in `var_list`. This Op runs all the
  initializers of the variables in `var_list` in parallel.

  Calling `initialize_variables()` is equivalent to passing the list of
  initializers to `Group()`.

  If `var_list` is empty, however, the function still returns an Op that can
  be run. That Op just has no effect.

  @compatibility(TF2)
  In TF2, variables are initialized immediately when they are created. There is
  no longer a need to run variable initializers before using them.
  @end_compatibility

  Args:
    var_list: List of `Variable` objects to initialize.
    name: Optional name for the returned operation.

  Returns:
    An Op that run the initializers of all the specified variables.
  c                 S   s   g | ]
}|j qS r"   )r   r  r"   r"   r#   r     rU   z)variables_initializer.<locals>.<listcomp>rO   r  )r   r   r   groupno_opvar_listrO   r"   r"   r#   r    s    initialize_variablesz'Use `tf.variables_initializer` instead.c                 C   s   t | |dS )z)See `tf.compat.v1.variables_initializer`.r  )r  r  r"   r"   r#   r    s    zinitializers.global_variablesglobal_variables_initializerc                   C   s   t  rtjddS tt S )a  Returns an Op that initializes global variables.

  This is just a shortcut for `variables_initializer(global_variables())`

  @compatibility(TF2)
  In TF2, variables are initialized immediately when they are created. There is
  no longer a need to run variable initializers before using them.
  @end_compatibility

  Returns:
    An Op that initializes global variables in the graph.
  r  r  )r   r   r   r  r  r  r"   r"   r"   r#   r    s    initialize_all_variablesz.Use `tf.global_variables_initializer` instead.c                   C   s   t  S )z0See `tf.compat.v1.global_variables_initializer`.)r  r"   r"   r"   r#   r    s    zinitializers.local_variableslocal_variables_initializerc                   C   s   t  rtjddS tt S )a  Returns an Op that initializes all local variables.

  This is just a shortcut for `variables_initializer(local_variables())`

  @compatibility(TF2)
  In TF2, variables are initialized immediately when they are created. There is
  no longer a need to run variable initializers before using them.
  @end_compatibility

  Returns:
    An Op that initializes all local variables in the graph.
  r  r  )r   r   r   r  r  r  r"   r"   r"   r#   r    s    initialize_local_variablesz-Use `tf.local_variables_initializer` instead.c                   C   s   t  S )z/See `tf.compat.v1.local_variables_initializer`.)r  r"   r"   r"   r#   r    s    r   c                 C   s
   t | S )zTests if a variable has been initialized.

  Args:
    variable: A `Variable`.

  Returns:
    Returns a scalar boolean Tensor, `True` if the variable has been
    initialized, `False` otherwise.
  r   r   )variabler"   r"   r#   r     s    assert_variables_initializedc              	   C   s   | du rt  t  } | sHg } t  D ]}|jdv r(| |jd  q(| sPdS g }| D ]D}t|j	$ |t
j|dd W d   qX1 s0    Y  qXt|dkr|d S t
|S dS )a  Returns an Op to check if variables are initialized.

  NOTE: This function is obsolete and will be removed in 6 months.  Please
  change your implementation to use `report_uninitialized_variables()`.

  When run, the returned Op will raise the exception `FailedPreconditionError`
  if any of the variables has not yet been initialized.

  Note: This function is implemented by trying to fetch the values of the
  variables. If one of the variables is not initialized a message may be
  logged by the C++ runtime. This is expected.

  Args:
    var_list: List of `Variable` objects to check. Defaults to the value of
      `global_variables().`

  Returns:
    An Op, or None if there are no variables.
  Nrj   r  ZAutoReloadVariabler   F)optimizer/   )r  r  r   rb   get_operationsr  r  rz  r9  r   r
   rank_internalr  stack)r  r   ranksr  r"   r"   r#   r  (  s     
4report_uninitialized_variablesc              	   C   s@  | du rHt  t  } | sHg } t  D ]}|jdv r(| |jd  q(t| | rfdd | D }t	j
dd}t| | stjg tjdW  d   W  d   S tt|}td	d | D }t||W  d   W  d   S W d   n1 s0    Y  W d   n1 s20    Y  dS )
a  Adds ops to list the names of uninitialized variables.

  When run, it returns a 1-D tensor containing the names of uninitialized
  variables if there are any, or an empty array if there are none.

  Args:
    var_list: List of `Variable` objects to check. Defaults to the value of
      `global_variables() + local_variables()`
    name: Optional name of the `Operation`.

  Returns:
    A 1-D tensor containing names of the uninitialized variables, or an empty
    1-D tensor if there are no variables or no uninitialized variables.
  Nr  r   c                 S   s   g | ]}t |qS r"   r  r  r"   r"   r#   r   p  rU   z2report_uninitialized_variables.<locals>.<listcomp>Z.TF_DEVICE_FOR_UNINITIALIZED_VARIABLE_REPORTINGz/cpu:0)r[   c                 S   s   g | ]}|j jqS r"   )r   rO   )r   r   r"   r"   r#   r   }  rU   )r  r  r   rb   r  r  r  rz  r&  osenvironrr  r   r
   constantr   stringr   logical_notr  boolean_mask)r  rO   r   Z	init_varsZlocal_deviceZvariables_maskZvariable_names_tensorr"   r"   r#   r  S  s*    
,)N)N)N)N)N)N)r  )r  )N)Nr  )Zr5   enumr   rv  r  tensorflow.core.frameworkr   r   tensorflow.pythonr   tensorflow.python.eagerr   tensorflow.python.frameworkr   r   r   r	   tensorflow.python.opsr
   r   r   r   r   r   r   tensorflow.python.platformr   rb  tensorflow.python.trackabler   r   tensorflow.python.typesr   tensorflow.python.utilr   r   r   r   r   "tensorflow.python.util.deprecationr   r    tensorflow.python.util.tf_exportr   r$   r%   r-   Enumr.   r;   r9   rP   r  rQ   	Trackablerj   r   RegisterTyperi   r   r   r5  rangers  rt  ru  ro  rp  ry  r  r  #register_tensor_conversion_functionr   r  r  r  r  r  r  r  r  should_use_resultr  r  r  r  r  r   r  r  r"   r"   r"   r#   <module>   s   	
"
Y
        +
 S         %$3 Z
"












)
  .