
    uki
6                     P   d 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
mZmZmZmZmZmZmZmZmZ ddlmZmZmZmZ eZeZeZ e        e       fdZddd ed	      fd
Z ej<                  ed      Zddd ed	      fdZ  ej<                  e d      Z! ej<                  e d      Z"ddddeefdZ#d Z$ e$ejJ                        Z& e$e
      Z' e$ejP                        Z) e$ed      Z* e$ed      Z+ e$e      Z, e$e      Z- e$e      Z. e$e      Z/ e$e      Z0 e$e      Z1d!dZ2 e2ejf                  ejh                         Z5 e2ejl                  d      Z7d Z8 e2ejl                  de8      Z9d Z: e:       Z:d Z; e;       Z;d Z<d Z= e=       Z=d"dZ>d#dZ?d Z@d ZAd  ZBy)$a  Stax is a small but flexible neural net specification library from scratch.

You likely do not mean to import this module! Stax is intended as an example
library only. There are a number of other much more fully-featured neural
network libraries for JAX, including `Flax`_ from Google, and `Haiku`_ from
DeepMind.

.. _Haiku: https://github.com/deepmind/dm-haiku
.. _Flax: https://github.com/google/flax
    N)lax)random)
relulog_softmaxsoftmaxsoftplussigmoidelu
leaky_reluselugelustandardize)glorot_normalnormaloneszerosc                 $      fd}d }||fS )z?Layer constructor function for a dense (fully-connected) layer.c                     |d d 	fz   }t        j                  |       \  }} ||d   	f       |	f      }}|||ffS )N)r   split)
rnginput_shapeoutput_shapek1k2WbW_initb_initout_dims
          U/home/cdr/jupyterlab/.venv/lib/python3.12/site-packages/jax/example_libraries/stax.pyinit_funzDense.<locals>.init_fun6   sY    s#wj0L\\#FB"{2016"wj3IqA!Q    c                 >    | \  }}t        j                  ||      |z   S N)jnpdot)paramsinputskwargsr   r   s        r!   	apply_funzDense.<locals>.apply_fun;   s!    DAq7761!!r#    )r    r   r   r"   r+   s   ```  r!   Denser-   4   s     
" 
9	r#   VALIDgư>c           
          	
  \  	dt              z  
xs 
xs* t        j                  d      j                  d             	f
d} 
fd}||fS )z<Layer construction function for a general convolution layer.   IOc                 \  
 t              }D cg c]-  }|dk(  rn#|dk(  r|j                  d         n
t        |      / }}t        j                  ||      }D cg c]  }|dk(  rnd }}t        j                  |       \  }} ||       ||      }
}	||	|
ffS c c}w c c}w Nr3   r2   Cr1   )iterindexnextr   conv_general_shape_tupler   r   r   r   filter_shape_iterckernel_shaper   
bias_shaper   r   r   r   r   r   dimension_numbersfilter_shapelhs_specout_chanout_specpaddingrhs_specstridess              r!   r"   zGeneralConv.<locals>.init_funI   s    \* 6>?01 !"SH89SKs 34*+, ?L ? //\7G5FHL7?@!a3h(A-@J@\\#FB"l#VB
%;qA!Q?
 A   2B$"B)c           	      L    | \  }}t        j                  ||      |z   S N)r@   )r   conv_general_dilated)	r(   r)   r*   r   r   r@   onerE   rG   s	        r!   r+   zGeneralConv.<locals>.apply_funT   s9    DAq##FAwc6GIKLM Mr#   lenr   r8   )r@   rC   rA   rG   rE   r   r   r"   r+   rB   rL   rD   rF   s   ```````  @@@@r!   GeneralConvrO   A   sj     "3(Hhs<  #Ns'L]8>>##6s8KL&
  
 M 
9	r#   )NHWCHWIOrP   c           
          
  \  
dt              z  }xs |xs* t        j                  d      j                  d             
f
d} fd}	||	fS )zGLayer construction function for a general transposed-convolution layer.r0   r2   r3   c                 \  
 t              }D cg c]-  }|dk(  rn#|dk(  r|j                  d         n
t        |      / }}t        j                  ||      }D cg c]  }|dk(  rnd }}t        j                  |       \  }} ||       ||      }
}	||	|
ffS c c}w c c}w r5   )r7   r8   r9   r   conv_transpose_shape_tupler   r   r;   s              r!   r"   z&GeneralConvTranspose.<locals>.init_fund   s    \* 6>?01 !"SH89SKs 34*+, ?L ? 11\7G5FHL7?@!a3h(A-@J@\\#FB"l#VB
%;qA!Q?
 ArH   c                 H    | \  }}t        j                  ||      |z   S rJ   )r   conv_transpose)r(   r)   r*   r   r   r@   rE   rG   s        r!   r+   z'GeneralConvTranspose.<locals>.apply_funo   s5    DAqfa'0ACEFG Gr#   rM   )r@   rC   rA   rG   rE   r   r   rL   r"   r+   rB   rD   rF   s   ```````   @@@r!   GeneralConvTransposerW   \   sj     "3(Hhs<  #Ns'L]8>>##6s8KL&
  
 G 
9	r#   )NHCHIOrX   )r   r1      gh㈵>Tc                     	 fdfd	t        j                         r fn  	 fd} fd}||fS )z<Layer construction function for a batch normalization layer.c                     r	 | |      S dS Nr,   r,   )r   shape	beta_initcenters     r!   <lambda>zBatchNorm.<locals>.<lambda>|   s    6)C"7 r r#   c                     r	 | |      S dS r]   r,   )r   r^   
gamma_initscales     r!   ra   zBatchNorm.<locals>.<lambda>}   s    U:c5#9  r#   c                     t        	fdt        |      D              }t        j                  |       \  }} ||       ||      }}|||ffS )Nc              3   2   K   | ]  \  }}|vs|  y wr%   r,   ).0idaxiss      r!   	<genexpr>z.BatchNorm.<locals>.init_fun.<locals>.<genexpr>   s     H1!4-!Hs   )tuple	enumerater   r   )
r   r   r^   r   r   betagamma
_beta_init_gamma_initrj   s
          r!   r"   zBatchNorm.<locals>.init_fun   sP    H	+ 6HHE\\#FBR'R)?%Du%%r#   c                     | \  }}t        fdt        t        j                  |            D              }t	        |	      }r
r||   |z  ||   z   S r|||   z   S 
r||   |z  S |S )Nc              3   @   K   | ]  }|v rd n
t        d         y wr%   )slice)rg   rh   rj   s     r!   rk   z/BatchNorm.<locals>.apply_fun.<locals>.<genexpr>   s      NaqDyteDk1Ns   )epsilon)rl   ranger&   ndimr   )r(   xr*   rn   ro   edzrj   r`   ru   rd   s          r!   r+   zBatchNorm.<locals>.apply_fun   sy    KD% 
N5!;MN	NBAtW-A%b	AR 88a$r(l"U2Y]"Hr#   )r&   isscalar)
rj   ru   r`   rd   r_   rc   r"   r+   rp   rq   s
   ``````  @@r!   	BatchNormr|   y   s;     J*J+LL&$D$&
	 
9	r#   c                       d } fd}||fS )z?Layer that applies a scalar function elementwise on its inputs.c                 
    |dfS r]   r,   r   r   s     r!   ra   zelementwise.<locals>.<lambda>       {B&7 r#   c                      |fi S r%   r,   )r(   r)   r*   fun
fun_kwargss      r!   ra   zelementwise.<locals>.<lambda>   s    s6/HZ/H r#   r,   )r   r   r"   r+   s   ``  r!   elementwiser      s    7(H)	9	r#   r   )rj   c                      d fd	}|S )Nc                 <    xs dt               z  r
        nddt               dz   f}n"j                  d      j                  d      f}t        |      D ]  } d| dz    |d z    d| dz   |d z      fd}	
 fd}||fS )	z0Layer construction function for a pooling layer.r0   Nr   r1   Nr6   c                     t        j                  |      }dt              z  }t        j                  ||||      }|dfS )Nr0   r,   )r   padtype_to_padsrN   reduce_window_shape_tuple)r   r   padding_valsr   	out_shaperE   rG   window_shapes        r!   r"   z6_pooling_layer.<locals>.PoolingLayer.<locals>.init_fun   sS    ((l)0';lC%%d//\7L$Fi]r#   c                 T    t        j                  |
	      }r
 ||      S |S r%   )r   reduce_window)r(   r)   r*   outinit_valrE   reducerrescalespecrG   r   s       r!   r+   z7_pooling_layer.<locals>.PoolingLayer.<locals>.apply_fun   s6    fh%w0c+2WS&$';;r#   )rN   r8   sorted)r   rG   rE   r   non_spatial_axesrh   r"   r+   r   r   r   rescalers   ````    @r!   PoolingLayerz$_pooling_layer.<locals>.PoolingLayer   s    1L 11G:Bh|Wg6G|C-11C$**S/9$% 1!"1%,|AB/??ld"WQR[0g1< < Yr#   )Nr.   Nr,   )r   r   r   r   s   ``` r!   _pooling_layerr      s    4 
r#           c                       fd}|S )Nc                    |dj                   dz
  fn"|j                  d      |j                  d      ft        fdt        j                         D              }t	        j
                  |j                        }t        j                  |dt        j                  
	      }t              D ]  }t	        j                  ||      } | |z  S )Nr   r1   r   r6   c              3   D   K   | ]  }|vrj                   |     y wr%   r^   )rg   rh   r)   r   s     r!   rk   z=_normalize_by_window_size.<locals>.rescale.<locals>.<genexpr>   s*      8&66 !,,q/ 8s    )dtyper   )rw   r8   rl   rv   r&   r   r   r   r   addr   expand_dims)outputsr)   r   spatial_shaperL   window_sizesrh   r   dimsrE   rG   s    `     @r!   r   z*_normalize_by_window_size.<locals>.rescale   s    |FKK!O+C$**S/9 8#(#58 8M ((=
5C$$S"cggtWgNL$% 6__\15l6 \!!r#   r,   )r   rG   rE   r   s   ``` r!   _normalize_by_window_sizer      s    " 
.r#   c                      d } d }| |fS )zCLayer construction function for flattening all but the leading dim.c                 D    |d   t        j                  |dd        f}|dfS )Nr   r1   r,   )mathprod)r   r   r   s      r!   r"   zFlatten.<locals>.init_fun   s*    q>499[_#==Lr#   c                 L    t        j                  ||j                  d   df      S )Nr   r   )r&   reshaper^   r(   r)   r*   s      r!   r+   zFlatten.<locals>.apply_fun   s     ;;vQ455r#   r,   r"   r+   s     r!   Flattenr      s    6	9	r#   c                      d } d }| |fS )z2Layer construction function for an identity layer.c                 
    |dfS r]   r,   r   s     r!   ra   zIdentity.<locals>.<lambda>   r   r#   c                     |S r%   r,   r   s      r!   ra   zIdentity.<locals>.<lambda>   s    v r#   r,   r   s     r!   Identityr      s    7(5)	9	r#   c                        fd} fd}||fS )z0Layer construction function for a fan-out layer.c                     |gz  dfS r]   r,   )r   r   nums     r!   ra   zFanOut.<locals>.<lambda>   s    }s':B&? r#   c                     |gz  S r%   r,   )r(   r)   r*   r   s      r!   ra   zFanOut.<locals>.<lambda>   s    x#~ r#   r,   )r   r"   r+   s   `  r!   FanOutr      s    ?(=)	9	r#   c                      d } d }| |fS )z3Layer construction function for a fan-in sum layer.c                     |d   dfS )Nr   r,   r,   r   s     r!   ra   zFanInSum.<locals>.<lambda>   s    {1~r&: r#   c                     t        |      S r%   )sumr   s      r!   ra   zFanInSum.<locals>.<lambda>   s
    s6{ r#   r,   r   s     r!   FanInSumr      s    :(:)	9	r#   c                        fd} fd}||fS )z=Layer construction function for a fan-in concatenation layer.c                     t        |d         z  t        fd|D              }|d   d  |fz   |d   dz   d  z   }|dfS )Nr   c              3   (   K   | ]	  }|     y wr%   r,   )rg   r^   axs     r!   rk   z0FanInConcat.<locals>.init_fun.<locals>.<genexpr>   s     9EeBi9s   r1   r,   )rN   r   )r   r   concat_sizer   r   rj   s       @r!   r"   zFanInConcat.<locals>.init_fun   s[    	KN#	#B9[99KAs#{n4{1~bde7LLIb=r#   c                 0    t        j                  |      S r%   )r&   concatenate)r(   r)   r*   rj   s      r!   r+   zFanInConcat.<locals>.apply_fun   s    ??64((r#   r,   )rj   r"   r+   s   `  r!   FanInConcatr      s    
)	9	r#   c                       d } fd}||fS )z@Layer construction function for a dropout layer with given rate.c                 
    |dfS r]   r,   r   s     r!   r"   zDropout.<locals>.init_fun  s    ?r#   c                     |j                  dd       }|d}t        |      dk(  r;t        j                  ||j                        }t        j                  ||z  d      S |S )Nr   zDropout layer requires apply_fun to be called with a PRNG key argument. That is, instead of `apply_fun(params, inputs)`, call it like `apply_fun(params, inputs, rng)` where `rng` is a PRNG key (e.g. from `jax.random.key`).trainr   )get
ValueErrorr   	bernoullir^   r&   where)r(   r)   r*   r   msgkeepmoderates         r!   r+   zDropout.<locals>.apply_fun	  sf    
**UD
!C
{6c sOwc46dYYtVd]A..mr#   r,   )r   r   r"   r+   s   ``  r!   Dropoutr     s     
9	r#   c                  R    t        |       t        |  \  fd}fd}||fS )a
  Combinator for composing layers in serial.

  Args:
    *layers: a sequence of layers, each an (init_fun, apply_fun) pair.

  Returns:
    A new layer, meaning an (init_fun, apply_fun) pair, representing the serial
    composition of the given sequence of layers.
  c                     g }D ]7  }t        j                  |       \  } } |||      \  }}|j                  |       9 ||fS r%   )r   r   append)r   r   r(   r"   	layer_rngparam	init_funss         r!   r"   zserial.<locals>.init_fun(  sS    F ||C(nc9#I{;k5mmE r#   c                     |j                  dd       }|t        j                  |      ndz  }t        | |      D ]  \  }}} |||fd|i|} |S Nr   r%   popr   r   zip)	r(   r)   r*   r   rngsr   r   
apply_funsnlayerss	          r!   r+   zserial.<locals>.apply_fun/  sg    
**UD
!C),6<<W%g>ODz648 5UC5&4c4V4f5Mr#   rN   r   layersr"   r+   r   r   r   s      @@@r!   serialr     s3     K'v,)Z 
9	r#   c                  T    t        |       t        |  \  fd}fd}||fS )a  Combinator for composing layers in parallel.

  The layer resulting from this combinator is often used with the FanOut and
  FanInSum layers.

  Args:
    *layers: a sequence of layers, each an (init_fun, apply_fun) pair.

  Returns:
    A new layer, meaning an (init_fun, apply_fun) pair, representing the
    parallel composition of the given sequence of layers. In particular, the
    returned layer takes a sequence of inputs and returns a sequence of outputs
    with the same length as the argument `layers`.
  c                     t        j                  |       }t        t        ||      D  cg c]  \  }} } || |       c}} } S c c}} }w r%   )r   r   r   )r   r   r   initr^   r   r   s        r!   r"   zparallel.<locals>.init_funI  sS    <<W%D	457 7&6dCc5! 7 8 8 7s   Ac                     |j                  dd       }|t        j                  |
      nd
z  }t        	| ||      D cg c]  \  }}}} |||fd|i| c}}}}S c c}}}}w r   r   )r(   r)   r*   r   r   fprx   rr   r   s            r!   r+   zparallel.<locals>.apply_funM  sj    
**UD
!C),6<<W%g>OD7::vvW[7\]]Aq!Aa$$V$]]]s   A"
r   r   s      @@@r!   parallelr   8  s4     K'v,)Z8^ 
9	r#   c                        fd} fd}||fS )a  Combinator to delay layer constructor pair until input shapes are known.

  Args:
    make_layer: a one-argument function that takes an input shape as an argument
      (a tuple of positive integers) and returns an (init_fun, apply_fun) pair.

  Returns:
    A new layer, meaning an (init_fun, apply_fun) pair, representing the same
    layer as returned by `make_layer` but with its construction delayed until
    input shapes are known.
  c                 (      |      d   | |      S )Nr   r,   )r   r   
make_layers     r!   r"   z!shape_dependent.<locals>.init_fun`  s    %:k"1%c;77r#   c                 >      |j                         d   | |fi |S )Nr1   r   )r(   r)   r*   r   s      r!   r+   z"shape_dependent.<locals>.apply_funb  s%    &:fll#A&vv@@@r#   r,   )r   r"   r+   s   `  r!   shape_dependentr   T  s    8A	9	r#   r%   )r   )r   )C__doc__	functoolsr   jaxr   r   	jax.numpynumpyr&   jax.nnr   r   r   r   r	   r
   r   r   r   r   jax.nn.initializersr   r   r   r   glorotrandn
logsoftmaxr-   rO   partialConvrW   Conv1DTransposeConvTransposer|   r   tanhTanhReluexpExp
LogSoftmaxSoftmaxSoftplusSigmoidElu	LeakyReluSeluGelur   maxinfMaxPoolr   SumPoolr   AvgPoolr   r   r   r   r   r   r   r   r   r,   r#   r!   <module>r     s  	     9 9 9 B B 

 (/&( 
 gdd|0 y&>? "&wt &t0 $)##$8:OP!	!!"6":< d4t$0
 3884#''2.

gB
'x 
g
#
#	448 377(
+
"
%" "&?
@ )
 :
 :	.88r#   