o
    bi0                     @   s\   d dl mZ d dlmZ d dlmZ edG dd dZddd	Zed
dgdd ZdS )    )tree)keras_export)	auto_namezkeras.KerasTensorc                   @   s2  e Zd ZdZ					dtddZedd	 Zejd
d	 Zedd Zejdd Zedd Z	e	jdd Z	edd Z
e
jdd Z
edd Zejdd Zedd Zejdd Zedd Zdd ZduddZd d! Zd"d# Zd$d% Zd&d' Zdvd(d)Zd*d+ Zd,d- Zd.d/ Zd0d1 Zd2d3 Zd4d5 Zd6d7 Zd8d9 Zd:d; Zd<d= Zd>d? Zd@dA Z dBdC Z!dDdE Z"dFdG Z#dHdI Z$dJdK Z%dLdM Z&dNdO Z'dPdQ Z(dRdS Z)dTdU Z*dVdW Z+dXdY Z,dZd[ Z-d\d] Z.d^d_ Z/d`da Z0dbdc Z1ddde Z2dfdg Z3dhdi Z4djdk Z5dldm Z6dndo Z7dpdq Z8dudrdsZ9dS )wKerasTensora  Symbolic tensor -- encapsulates a shape and a dtype.

    You can use `KerasTensor` instances to build computation
    graphs of Keras operations, such as `keras.Function`
    objects or Functional `keras.models.Model` objects.

    Example:

    >>> x = keras.KerasTensor(shape=(3, 4), dtype="float32")
    >>> x.shape
    (3, 4)
    >>> x.dtype
    float32

    Calling a Keras operation (including a layer or a model)
    on a `KerasTensor` instance will return another `KerasTensor`
    instance with the appropriate shape and dtype. This is
    called a "symbolic call" (since there is no actual data
    involved). The computation of the correct output shape and
    dtype is called "static shape inference".
    float32FTNc                 K   s   ddl m} |dd }	|dd }
|r tdd|  ||| _||| _	t
|| _t
|| _| jr@| jr@td|	d urHt|	nd | _|
d urT||
nd | _|p^t| jj| _|| _d S )Nr   )backendragged_rankrow_splits_dtypezUnexpected keyword arguments: z, zIKerasTensor cannot have `sparse=True` and `ragged=True` at the same time.)	keras.srcr   pop	TypeErrorjoinkeysstandardize_shape_shapestandardize_dtype_dtypebool_sparse_ragged
ValueErrorint_ragged_rank_row_splits_dtyper   	__class____name__namerecord_history)selfshapedtypesparseraggedr   r   kwargsr   r   r	    r$   Y/home/ubuntu/.local/lib/python3.10/site-packages/keras/src/backend/common/keras_tensor.py__init__   s.   




zKerasTensor.__init__c                 C      | j S N)r   r   r$   r$   r%   r   E      zKerasTensor.shapec                 C      t d)NzlThe `shape` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.AttributeErrorr   valuer$   r$   r%   r   I      c                 C   r'   r(   )r   r)   r$   r$   r%   r    P   r*   zKerasTensor.dtypec                 C   r+   )NzlThe `dtype` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.r,   r.   r$   r$   r%   r    T   r0   c                 C   r'   r(   )r   r)   r$   r$   r%   r!   [   r*   zKerasTensor.sparsec                 C   r+   )NzmThe `sparse` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.r,   r.   r$   r$   r%   r!   _   r0   c                 C   r'   r(   )r   r)   r$   r$   r%   r   f   r*   zKerasTensor.ragged_rankc                 C   r+   )NzrThe `ragged_rank` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.r,   r.   r$   r$   r%   r   j   r0   c                 C   r'   r(   )r   r)   r$   r$   r%   r	   q   r*   zKerasTensor.row_splits_dtypec                 C   r+   )NzwThe `row_splits_dtype` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.r,   r.   r$   r$   r%   r	   u   r0   c                 C   r'   r(   )r   r)   r$   r$   r%   r"   |   r*   zKerasTensor.raggedc                 C   r+   )NzmThe `ragged` attribute of KerasTensor is immutable. One should create a new instance of KerasTensor for this.r,   r.   r$   r$   r%   r"      r0   c                 C   s
   t | jS r(   )lenr   r)   r$   r$   r%   ndim   s   
zKerasTensor.ndimc                 C      ddl m} ||| S Nr   ops)r
   r6   Reshape)r   newshaper6   r$   r$   r%   reshape      zKerasTensor.reshapec                 C   r3   r4   )r
   r6   Squeeze)r   axisr6   r$   r$   r%   squeeze   r:   zKerasTensor.squeezec                 C   r+   )NzA KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to an int.r   r)   r$   r$   r%   __int__      zKerasTensor.__int__c                 C   r+   )NzA KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a float.r>   r)   r$   r$   r%   	__float__   r@   zKerasTensor.__float__c                 C   r+   )NzA KerasTensor is symbolic: it's a placeholder for a shape an a dtype. It doesn't have any actual numerical value. You cannot convert it to a NumPy array.r>   r)   r$   r$   r%   	__array__   r@   zKerasTensor.__array__c                 C   r+   )Na%  A KerasTensor cannot be used as input to a JAX function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.ops`). You are likely doing something like:

```
x = Input(...)
...
jax_fn(x)  # Invalid.
```

What you should do instead is wrap `jax_fn` in a layer:

```
class MyLayer(Layer):
    def call(self, x):
        return jax_fn(x)

x = MyLayer()(x)
```
r>   r)   r$   r$   r%   __jax_array__   r@   zKerasTensor.__jax_array__c                 C   r+   )Na)  A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.ops`). You are likely doing something like:

```
x = Input(...)
...
tf_fn(x)  # Invalid.
```

What you should do instead is wrap `tf_fn` in a layer:

```
class MyLayer(Layer):
    def call(self, x):
        return tf_fn(x)

x = MyLayer()(x)
```
r>   )r   r    r   r$   r$   r%   __tf_tensor__   r@   zKerasTensor.__tf_tensor__c                 C   s.   d| j  d| j d| j d| j d| j dS )Nz<KerasTensor shape=z, dtype=z	, sparse=z	, ragged=z, name=>)r   r    r!   r"   r   r)   r$   r$   r%   __repr__   s   zKerasTensor.__repr__c                 C   r+   )Nz7Iterating over a symbolic KerasTensor is not supported.)NotImplementedErrorr)   r$   r$   r%   __iter__   r@   zKerasTensor.__iter__c                 C   r+   )Nz3A symbolic KerasTensor cannot be used as a boolean.)r   r)   r$   r$   r%   __bool__   s   zKerasTensor.__bool__c                 C      ddl m} | | |S r4   r
   r6   Addsymbolic_callr   otherr6   r$   r$   r%   __add__      zKerasTensor.__add__c                 C      ddl m} | || S r4   rK   rN   r$   r$   r%   __radd__   rQ   zKerasTensor.__radd__c                 C   rJ   r4   r
   r6   SubtractrM   rN   r$   r$   r%   __sub__   rQ   zKerasTensor.__sub__c                 C   rR   r4   rT   rN   r$   r$   r%   __rsub__   rQ   zKerasTensor.__rsub__c                 C   rJ   r4   r
   r6   MultiplyrM   rN   r$   r$   r%   __mul__   rQ   zKerasTensor.__mul__c                 C   rR   r4   rX   rN   r$   r$   r%   __rmul__   rQ   zKerasTensor.__rmul__c                 C   rJ   r4   r
   r6   MatmulrM   rN   r$   r$   r%   
__matmul__  rQ   zKerasTensor.__matmul__c                 C   rR   r4   r\   rN   r$   r$   r%   __rmatmul__	  rQ   zKerasTensor.__rmatmul__c                 C   rJ   r4   r
   r6   DividerM   rN   r$   r$   r%   __div__  rQ   zKerasTensor.__div__c                 C   rR   r4   r`   rN   r$   r$   r%   __rdiv__  rQ   zKerasTensor.__rdiv__c                 C   rJ   r4   r
   r6   
TrueDividerM   rN   r$   r$   r%   __truediv__  rQ   zKerasTensor.__truediv__c                 C   rR   r4   rd   rN   r$   r$   r%   __rtruediv__  rQ   zKerasTensor.__rtruediv__c                 C      ddl m} | | S r4   )r
   r6   NegativerM   r   r6   r$   r$   r%   __neg__"  r:   zKerasTensor.__neg__c                 C   rh   r4   )r
   r6   AbsoluterM   rj   r$   r$   r%   __abs__'  r:   zKerasTensor.__abs__c                 C   rJ   r4   r
   r6   PowerrM   rN   r$   r$   r%   __pow__,  rQ   zKerasTensor.__pow__c                 C   rR   r4   rn   rN   r$   r$   r%   __rpow__1  rQ   zKerasTensor.__rpow__c                 C   rJ   r4   r
   r6   FloorDividerM   rN   r$   r$   r%   __floordiv__6  rQ   zKerasTensor.__floordiv__c                 C   rR   r4   rr   rN   r$   r$   r%   __rfloordiv__;  rQ   zKerasTensor.__rfloordiv__c                 C   rJ   r4   r
   r6   ModrM   rN   r$   r$   r%   __mod__@  rQ   zKerasTensor.__mod__c                 C   rR   r4   rv   rN   r$   r$   r%   __rmod__E  rQ   zKerasTensor.__rmod__c                 C   rJ   r4   )r
   r6   LessrM   rN   r$   r$   r%   __lt__J  rQ   zKerasTensor.__lt__c                 C   rJ   r4   )r
   r6   	LessEqualrM   rN   r$   r$   r%   __le__O  rQ   zKerasTensor.__le__c                 C   rJ   r4   )r
   r6   GreaterrM   rN   r$   r$   r%   __gt__T  rQ   zKerasTensor.__gt__c                 C   rJ   r4   )r
   r6   GreaterEqualrM   rN   r$   r$   r%   __ge__Y  rQ   zKerasTensor.__ge__c                 C   rJ   r4   )r
   r6   NotEqualrM   rN   r$   r$   r%   __ne__^  rQ   zKerasTensor.__ne__c                 C   rJ   r4   r
   r6   
LogicalAndrM   rN   r$   r$   r%   __and__c  rQ   zKerasTensor.__and__c                 C   rR   r4   r   rN   r$   r$   r%   __rand__h  rQ   zKerasTensor.__rand__c                 C   rJ   r4   r
   r6   	LogicalOrrM   rN   r$   r$   r%   __or__m  rQ   zKerasTensor.__or__c                 C   rR   r4   r   rN   r$   r$   r%   __ror__r  rQ   zKerasTensor.__ror__c                 C   rh   r4   )r
   r6   
LogicalNotrM   rj   r$   r$   r%   
__invert__w  r:   zKerasTensor.__invert__c                 C   rJ   r4   r
   r6   
LogicalXorrM   rN   r$   r$   r%   __xor__|  rQ   zKerasTensor.__xor__c                 C   rR   r4   r   rN   r$   r$   r%   __rxor__  rQ   zKerasTensor.__rxor__c                 C   rJ   r4   )r
   r6   GetItemrM   )r   keyr6   r$   r$   r%   __getitem__  rQ   zKerasTensor.__getitem__c                 C   s&   ddl m} |p	d}|j|d| S )Nr   r5   )decimals)r
   r6   RoundrM   )r   ndigitsr6   r   r$   r$   r%   	__round__  s   zKerasTensor.__round__)r   FFTNr(   NN):r   
__module____qualname____doc__r&   propertyr   setterr    r!   r   r	   r"   r2   r9   r=   r?   rA   rB   rC   rD   rF   rH   rI   rP   rS   rV   rW   rZ   r[   r^   r_   rb   rc   rf   rg   rk   rm   rp   rq   rt   ru   rx   ry   r{   r}   r   r   r   r   r   r   r   r   r   r   r   r   r$   r$   r$   r%   r      s    
'














r   Nc                 C   s8   | pd} |pi }t | |fD ]
}t|tr dS qdS )Nr$   TF)r   flatten
isinstancer   )argsr#   xr$   r$   r%   any_symbolic_tensors  s   
r   zkeras.utils.is_keras_tensorzkeras.backend.is_keras_tensorc                 C   s
   t | tS )a|  Returns whether `x` is a Keras tensor.

    A "Keras tensor" is a *symbolic tensor*, such as a tensor
    that was created via `Input()`. A "symbolic tensor"
    can be understood as a placeholder -- it does not
    contain any actual numerical data, only a shape and dtype.
    It can be used for building Functional models, but it
    cannot be used in actual computations.
    )r   r   )r   r$   r$   r%   is_keras_tensor  s   
r   r   )	r
   r   keras.src.api_exportr   keras.src.utils.namingr   r   r   r   r$   r$   r$   r%   <module>   s       

	