
    Vpf&6                        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	j&                  Z' e%e          Z( e%e	j)                  Z* e%ed          Z+ e%ed          Z, e%e          Z- e%e          Z. e%e          Z/ e%e          Z0 e%e          Z1 e%e          Z2d"dZ3 e3ej4        e	j5                   Z6 e3ej7        d          Z8d Z9 e3ej7        de9          Z:d Z; e;            Z;d Z< e<            Z<d Z=d Z> e>            Z>d#dZ?d$dZ@d ZAd  ZBd! ZCdS )%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
          Z/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/example_libraries/stax.pyinit_funzDense.<locals>.init_fun6   sf    ss#wj0L\#FB6"{201166"wj3I3IqA!Q    c                 <    | \  }}t          j        ||          |z   S N)jnpdot)paramsinputskwargsr   r   s        r!   	apply_funzDense.<locals>.apply_fun;   s"    DAq761!!r#    )r    r   r   r"   r+   s   ```  r!   Denser-   4   sD                 
" " " 
9	r#   VALIDgư>c           
          	
  \  	dt                    z  
p
p5t                              d                              d                     	f
d} 
fd}||fS )z<Layer construction function for a general convolution layer.   IOc                    
	 t                    		fdD             }t          j        |          }fdD             }t          j        |           \  }} 
||           ||          }}|||ffS )Nc                     g | ]:}|d k    rn/|dk    r                     d                   nt                    ;S r3   r2   Cindexnext.0cfilter_shape_iterr   lhs_specout_chans     r!   
<listcomp>z1GeneralConv.<locals>.init_fun.<locals>.<listcomp>K   ^     ? ? ?01 !"SHH89SKs 3 344*++? ? ?r#   c                 $    g | ]}|d k    rndS r7   r1   r,   r<   r=   r@   s     r!   rA   z1GeneralConv.<locals>.init_fun.<locals>.<listcomp>P   %    @@@!a3hh((A@@@r#   )iterr   conv_general_shape_tupler   r   r   r   kernel_shaper   
bias_shaper   r   r   r   r>   r   r   dimension_numbersfilter_shaper?   r@   out_specpaddingrhs_specstridess    `       @r!   r"   zGeneralConv.<locals>.init_funI   s    \**? ? ? ? ? ? ?5=? ? ?L /\7G5FH HL@@@@x@@@J\#FB6"l##VVB
%;%;qA!Qr#   c           	      J    | \  }}t          j        ||          |z   S N)rL   )r   conv_general_dilated)	r(   r)   r*   r   r   rL   onerO   rQ   s	        r!   r+   zGeneralConv.<locals>.apply_funT   sB    DAq#FAwc6GI I IKLM Mr#   lenr   r9   )rL   r@   rM   rQ   rO   r   r   r"   r+   r?   rU   rN   rP   s   ```````  @@@@r!   GeneralConvrX   A   s     "3(Hhs<   #Ns'L]8>>##6#6s8K8KLL&
  
  
  
  
  
  
  
  
  
  
  
  
  
 M M M M M M M M 
9	r#   )NHWCHWIOrY   c           
          
  \  
dt                    z  }p|p5t                              d                              d                     
f
d} fd}	||	fS )zGLayer construction function for a general transposed-convolution layer.r0   r2   r3   c                    
	 t                    		fdD             }t          j        |          }fdD             }t          j        |           \  }} 
||           ||          }}|||ffS )Nc                     g | ]:}|d k    rn/|dk    r                     d                   nt                    ;S r6   r8   r;   s     r!   rA   z:GeneralConvTranspose.<locals>.init_fun.<locals>.<listcomp>f   rB   r#   c                 $    g | ]}|d k    rndS rD   r,   rE   s     r!   rA   z:GeneralConvTranspose.<locals>.init_fun.<locals>.<listcomp>k   rF   r#   )rG   r   conv_transpose_shape_tupler   r   rI   s    `       @r!   r"   z&GeneralConvTranspose.<locals>.init_fund   s    \**? ? ? ? ? ? ?5=? ? ?L 1\7G5FH HL@@@@x@@@J\#FB6"l##VVB
%;%;qA!Qr#   c                 F    | \  }}t          j        ||          |z   S rS   )r   conv_transpose)r(   r)   r*   r   r   rL   rO   rQ   s        r!   r+   z'GeneralConvTranspose.<locals>.apply_funo   s>    DAqfa'0AC C CEFG Gr#   rV   )rL   r@   rM   rQ   rO   r   r   rU   r"   r+   r?   rN   rP   s   ```````   @@@r!   GeneralConvTransposerb   \   s     "3(Hhs<   #Ns'L]8>>##6#6s8K8KLL&
  
  
  
  
  
  
  
  
  
  
  
  
  
 G G G G G G G 
9	r#   )NHCHIOrc   )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 | |          ndS Nr,   r,   )r   shape	beta_initcenters     r!   <lambda>zBatchNorm.<locals>.<lambda>|   s    6"I))C"7"7"7r r#   c                 $    r | |          ndS rh   r,   )r   ri   
gamma_initscales     r!   rl   zBatchNorm.<locals>.<lambda>}   s    U#J::c5#9#9#9 r#   c                     t          	fdt          |          D                       }t          j        |           \  }} ||           ||          }}|||ffS )Nc              3   *   K   | ]\  }}|v	|V  d S r%   r,   )r<   idaxiss      r!   	<genexpr>z.BatchNorm.<locals>.init_fun.<locals>.<genexpr>   s+      HH1!4--!----HHr#   )tuple	enumerater   r   )
r   r   ri   r   r   betagamma
_beta_init_gamma_initrt   s
          r!   r"   zBatchNorm.<locals>.init_fun   sp    HHHH	+ 6 6HHH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   B   K   | ]}|v rd nt          d           V  d S r%   )slice)r<   rr   rt   s     r!   ru   z/BatchNorm.<locals>.apply_fun.<locals>.<genexpr>   s5      NNaqDyytteDkkNNNNNNr#   )epsilon)rv   ranger&   ndimr   )r(   xr*   rx   ry   edzrt   rk   r   ro   s          r!   r+   zBatchNorm.<locals>.apply_fun   s    KD% 
NNNN5!;M;MNNN	N	NBAtW---A8%8b	AR 88"a$r(l""U2Y]"Hr#   )r&   isscalar)
rt   r   rk   ro   rj   rn   r"   r+   rz   r{   s
   ``````  @@r!   	BatchNormr   y   s     JIIII*JJJJJ+L&&	0$D$& & & & & & &
	 	 	 	 	 	 	 	 
9	r#   c                       d } fd}||fS )z?Layer that applies a scalar function elementwise on its inputs.c                 
    |dfS rh   r,   r   r   s     r!   rl   zelementwise.<locals>.<lambda>       {B&7 r#   c                      |fi S r%   r,   )r(   r)   r*   fun
fun_kwargss      r!   rl   zelementwise.<locals>.<lambda>   s    ss6/H/HZ/H/H r#   r,   )r   r   r"   r+   s   ``  r!   elementwiser      s(    77(HHHHH)	9	r#   r   )rt   c                      d fd	}|S )Nr.   c                     pdt                     z  r            nddt                     dz   f}n*                    d                              d          f}t          |          D ]2} d|         dz    |d         z    d|         dz   |d         z   3 fd}	
 fd}||fS )	z0Layer construction function for a pooling layer.r0   Nr   r1   Nr7   c                     t          j        |          }dt                    z  }t          j        ||||          }|dfS )Nr0   r,   )r   padtype_to_padsrW   reduce_window_shape_tuple)r   r   padding_valsr   	out_shaperO   rQ   window_shapes        r!   r"   z6_pooling_layer.<locals>.PoolingLayer.<locals>.init_fun   s[    (l)0'; ;lC%%%d/\7L$F Fi]r#   c                 X    t          j        |
	          }r ||          n|S r%   )r   reduce_window)r(   r)   r*   outinit_valrO   reducerrescalespecrQ   r   s       r!   r+   z7_pooling_layer.<locals>.PoolingLayer.<locals>.apply_fun   s@    fh%w0 0c+2;WWS&$''';r#   )rW   r9   sorted)r   rQ   rO   r   non_spatial_axesrr   r"   r+   r   r   r   rescalers   ````    @r!   PoolingLayerz$_pooling_layer.<locals>.PoolingLayer   s<   1L 1 11G:BLhh|Wg666G|C--11C$**S//9$%% 1 1!"1"%,|ABB/??ld"WQRR[0gg      < < < < < < < < < < < Yr#   )Nr.   Nr,   )r   r   r   r   s   ``` r!   _pooling_layerr      s5           4 
r#           c                       fd}|S )Nc                    |dj         dz
  fn*|                    d          |                    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   r7   c              3   :   K   | ]}|vj         |         V  d S r%   ri   )r<   rr   r)   r   s     r!   ru   z=_normalize_by_window_size.<locals>.rescale.<locals>.<genexpr>   sA       8 8&666 !,q/66668 8r#   )dtyper   )r   r9   rv   r   r&   r   r   r   r   addr   expand_dims)outputsr)   r   spatial_shaperU   window_sizesrr   r   dimsrO   rQ   s    `     @r!   r   z*_normalize_by_window_size.<locals>.rescale   s    |FK!O+C$**S//9 8 8 8 8 8#(#5#58 8 8 8 8M (=
5
5
5C$S"cgtWgNNL$%% 6 6_\155ll\!!r#   r,   )r   rQ   rO   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                 j    |d         t          j        t          j        |dd          d          f}|dfS )Nr   r1   r,   )	functoolsreduceopmul)r   r   r   s      r!   r"   zFlatten.<locals>.init_fun   s4    q>9#3BFKOQ#O#OOLr#   c                 F    t          j        ||j        d         df          S )Nr   r   )r&   reshaperi   r(   r)   r*   s      r!   r+   zFlatten.<locals>.apply_fun   s    ;vQ4555r#   r,   r"   r+   s     r!   Flattenr      s+      6 6 6	9	r#   c                      d } d }| |fS )z2Layer construction function for an identity layer.c                 
    |dfS rh   r,   r   s     r!   rl   zIdentity.<locals>.<lambda>   r   r#   c                     |S r%   r,   r   s      r!   rl   zIdentity.<locals>.<lambda>   s    v r#   r,   r   s     r!   Identityr      s    77(55)	9	r#   c                        fd} fd}||fS )z0Layer construction function for a fan-out layer.c                     |gz  dfS rh   r,   )r   r   nums     r!   rl   zFanOut.<locals>.<lambda>   s    }s':B&? r#   c                     |gz  S r%   r,   )r(   r)   r*   r   s      r!   rl   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!   rl   zFanInSum.<locals>.<lambda>   s    {1~r&: r#   c                      t          |          S r%   )sumr   s      r!   rl   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   | ]}|         V  d S r%   r,   )r<   ri   axs     r!   ru   z0FanInConcat.<locals>.init_fun.<locals>.<genexpr>   s'      99EeBi999999r#   r1   r,   )rW   r   )r   r   concat_sizer   r   rt   s       @r!   r"   zFanInConcat.<locals>.init_fun   su    	KN##	#B9999[99999KAss#{n4{1~bdee7LLIb=r#   c                 .    t          j        |          S r%   )r&   concatenate)r(   r)   r*   rt   s      r!   r+   zFanInConcat.<locals>.apply_fun   s    ?64(((r#   r,   )rt   r"   r+   s   `  r!   FanInConcatr      s@        
) ) ) ) )	9	r#   trainc                       d } fd}||fS )z@Layer construction function for a dropout layer with given rate.c                 
    |dfS rh   r,   r   s     r!   r"   zDropout.<locals>.init_fun  s    ?r#   c                     |                     dd           }|d}t          |          dk    r4t          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`).r   r   )get
ValueErrorr   	bernoulliri   r&   where)r(   r)   r*   r   msgkeepmoderates         r!   r+   zDropout.<locals>.apply_fun	  sl    
**UD
!
!C
{6c sOOwc466dYtVd]A...mr#   r,   )r   r   r"   r+   s   ``  r!   Dropoutr     s>            
9	r#   c                  \    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 ]=}t          j        |           \  } } |||          \  }}|                    |           >||fS r%   )r   r   append)r   r   r(   r"   	layer_rngparam	init_funss         r!   r"   zserial.<locals>.init_fun(  s`    F  |C((nc9#8I{;;k5mmEr#   c                     |                     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/  s{    
**UD
!
!C),6<W%%%g>ODz6488 5 5UCs5&44c4V44ffMr#   rW   r   layersr"   r+   r   r   r   s      @@@r!   serialr     sd     KK'v,)Z          
9	r#   c                  ^    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                 r    t          j        |           }t          d t          ||          D              S )Nc                 .    g | ]\  }}} |||          S r,   r,   )r<   initr   ri   s       r!   rA   z.parallel.<locals>.init_fun.<locals>.<listcomp>K  s7     7 7 7&6dCc5!! 7 7 7r#   )r   r   r   )r   r   r   r   r   s      r!   r"   zparallel.<locals>.init_funI  sI    <W%%D 7 7	4557 7 7 8 8r#   c                                          dd           }|t          j        |          ndz  }fdt          | ||          D             S )Nr   r%   c                 2    g | ]\  }}}} |||fd |iS )r   r,   )r<   fpr   rr*   s        r!   rA   z/parallel.<locals>.apply_fun.<locals>.<listcomp>P  s:    ]]]Aq!AAa$$$V$$]]]r#   r   )r(   r)   r*   r   r   r   r   s     `  r!   r+   zparallel.<locals>.apply_funM  s_    
**UD
!
!C),6<W%%%g>OD]]]]s:vvW[7\7\]]]]r#   r   r   s      @@@r!   parallelr   8  so     KK'v,)Z8 8 8 8 8 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77r#   c                 @      |j                   d         | |fi |S )Nr1   r   )r(   r)   r*   r   s      r!   r+   z"shape_dependent.<locals>.apply_funb  s.    &::fl##A&vv@@@@@r#   r,   )r   r"   r+   s   `  r!   shape_dependentr   T  sE    8 8 8 8 8A A A A A	9	r#   r%   )r   )r   )D__doc__r   operatorr   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-   rX   partialConvrb   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 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 B B B B B B B B B B B B 

 (-//&&(( 
 
 
 
 gdvd||   0 y&>?? "&wt &t   0 $)#$8:OPP!	!"6":< < d4t$   0  
 {38{4k#'[2...

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