
    Vpf,                    :   d Z ddlmZ ddlmZm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ZddlmZ ddlZddlmZ ddlmZ ddlZddlZddlZddlZ ej        d	d
d          ZdZ dZ!dZ"g dZ#dZ$dZ%d"dZ& G d d          Z' G d d          Z(	 d#d$d!Z)dS )%zDefinitions of two versions of MNIST (model and training code ).

One definition uses pure JAX (for those who prefer an example with fewer
moving parts, at the expense of code size), and another using Flax.

See README.md for how these are used.
    )annotations)CallableSequenceN)Any)flags)linen)pyplot	mock_dataFzUse fake data, for testing.      )   r      )i     r   
   g?gMbP?split
tfds.Split
batch_sizeintc                X   t           j        rt          j                            |          5  	 t          j        d|           }na# t          $ rT}t          j        dt          |                    }|r)d|
                    d           d}t          |          ||d}~ww xY w	 ddd           n# 1 swxY w Y   nt          j        d|           }d	 }|                    |          }|                                                    d
                              |d          }|S )a  Loads either training or test MNIST data.

  Args:
    split: either tfds.Split.TRAIN or tfds.Split.TEST.

  Returns:
    an iterator with pairs (images, labels). The images have shape
    (B, 28, 28, 1) and the labels have shape (B, 10), where B is the batch_size.
  )num_examplesmnist)r   z,metadata files were not found in (.+/)mnist/zTFDS mock_data is missing the mnist metadata files. Run the `saved_model_main.py` binary and see where TFDS downloads the mnist data set (typically ~/tensorflow_datasets/mnist). Copy the `mnist` directory to r   z and re-run the testNc                    t          j        | d         t           j                  dz  }t          j        | d         d          }||fS )Nimageg     o@labelr   )tfcastfloat32one_hot)xr   r   s      j/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/experimental/jax2tf/examples/mnist_lib.py_prepare_examplez$load_mnist.<locals>._prepare_exampleW   s=    GAgJ
++e3EJqz2&&E5>    i  T)drop_remainder)
_MOCK_DATAvaluetfdstestingr
   load	Exceptionresearchstrgroup
ValueErrormapcacheshufflebatch)r   r   dsemmsgr!   s          r    
load_mnistr7   <   s     )			Z		8	8  Ywe,,, 	 	 	IEs1vvNN 	S 34''!**S S S# 3Q
&'	 	               
7%	(	(	(B  
 
vv"	xxzz$%%j%FF"	)s5   B4AB4
B$ABB$$B44B8;B8c                      e Zd ZdZdZeddd            Zed             Zedd
            Zed             Z	edd            Z
dS )PureJaxMNISTzAn MNIST model written using pure JAX.

  There is an option for the model to skip the classifier layer, for
  demonstrating reuse of the classifier-less model into a larger model.
  See README.md.
  mnist_pure_jaxTparamsSequence[tuple[Any, Any]]c                `   |                     |j        d         df          }| dd         D ]1\  }}t          j        ||          |z   }t          j        |          }2|s|S | d         \  }}t          j        ||          |z   }|t
          j        j                            |dd          z
  S )a_  The prediction function.

    Args:
      params: a list with pairs of weights and biases for each layer.
      inputs: the batch of images (B, 28, 28, 1)
      with_classifier: whether to include the classifier layer.

    Returns:
      either the predictions (B, 10) if with_classifier=True, or the
      final set of logits of shape (B, 512).
    r   Nr   T)axiskeepdims)	reshapeshapejnpdottanhjaxscipyspecial	logsumexp)	r;   inputswith_classifierr   wbfinal_wfinal_blogitss	            r    predictzPureJaxMNIST.predictl   s     	Q,--Ass  1
'!Q--!
a
(1++aa hbzGWWQ  7*FCI%//1t 0 % % % %r"   c                    t                               | |d          }t          j        t          j        ||z  d                     S NTrK   r   r?   )r9   rQ   rC   meansumr;   rJ   labelspredictionss       r    losszPureJaxMNIST.loss   sC    &&vvt&LLKHSW[61:::;;;;r"   rQ   r   c                     t           j         fd            fdt          j        |          D             }t	          j        t	          j        |                    S )Nc                    t          j        |d          }t          j         |           d          }t          j        ||k              S )Nr   rU   )rC   argmaxrV   )rJ   rY   target_classpredicted_classr;   rQ   s       r    
_per_batchz)PureJaxMNIST.accuracy.<locals>._per_batch   sL    ZQ///l
7766#:#:CCCoXo5666r"   c                .    g | ]\  }} ||          S  rc   ).0rJ   rY   ra   s      r    
<listcomp>z)PureJaxMNIST.accuracy.<locals>.<listcomp>   s6       %3VVjj    r"   )rF   jitr&   as_numpyrC   rV   stack)rQ   r;   datasetbatchedra   s   ``  @r    accuracyzPureJaxMNIST.accuracy   s     	W7 7 7 7 7 W7
   7;}W7M7M  G 8CIg&&'''r"   c                     t          j        t          j                  | ||          }d t	          | |          D             S )Nc                T    g | ]%\  \  }}\  }}|t           |z  z
  |t           |z  z
  f&S rc   )	step_size)rd   rL   rM   dwdbs        r    re   z'PureJaxMNIST.update.<locals>.<listcomp>   sK     8 8 8 AR RY^!34 8 8 8r"   )rF   gradr9   r[   zip)r;   rJ   rY   gradss       r    updatezPureJaxMNIST.update   sM    'CH\&''??E8 8$'$6$68 8 8 8r"   c                X   t           j                            d          fdt          t          dd         t          dd                   D             }t          |          D ]"}t          j                    }t          j        |           D ].\  }} t          j	        t          j                  |||          }/t          j                    |z
  }	t                              t          j        ||           }
t                              t          j        ||          }t          j        dt          j        ||	           t          j        dt          j        d|
z             t          j        d	t          j        d|z             $t#          j        t          j        |
          |fS )ag  Trains a pure JAX MNIST predictor.

    Returns:
      a tuple with two elements:
        - a predictor function with signature "(Params, ImagesBatch) ->
        Predictions".
          If `with_classifier=False` then the output of the predictor function
          is the last layer of logits.
        - the parameters "Params" for the predictor function
    r   c                    g | ]X\  }}t           t          j                            ||f          z  t           t          j                            |f          z  fYS rc   )param_scalerF   randomnormal)rd   r5   nrngs      r    re   z&PureJaxMNIST.train.<locals>.<listcomp>   so     C C C!Q SZ..sQF;;;SZ..sQD999; C C Cr"   Nr>   r   %s: Epoch %d in %0.2f sec!%s: Training set accuracy %0.2f%%      Y@%s: Test set accuracy %0.2f%%rT   )rF   rx   PRNGKeyrr   layer_sizesrangetimer&   rg   rf   r9   rt   rk   rQ   logginginfoname	functoolspartial)train_dstest_ds
num_epochsrK   r;   epoch
start_timerJ   rY   
epoch_time	train_acctest_accr{   s               @r    trainzPureJaxMNIST.train   s    *

Q

CC C C C{3B3/QRRAAC C CF z"" $ $9;;j M(33 F F.&&-,--fffEE9;;+j''(<fhOOi&&|';VWMMhl.0A5  l68I)#% % %l2L4E(?$ $ $ $ O= = =>DF Fr"   NT)r;   r<   )rQ   r   )__name__
__module____qualname____doc__r   staticmethodrQ   r[   rk   rt   r   rc   r"   r    r9   r9   b   s          
$% % % % <%0 < < << ( ( ( <( 8 8 <8
 F F F <F F Fr"   r9   c                      e Zd ZdZdZ G d dej                  Z e            Zedd            Z	ed             Z
ed             Zedd	            Zd
S )	FlaxMNISTzAn MNIST model using Flax.
mnist_flaxc                  4    e Zd ZdZej        dd            ZdS )FlaxMNIST.ModulezA simple CNN model for MNIST.

    There is an option for the model to skip the classifier layer, for
    demonstrating reuse of the classifier-less model into a larger model.
    See README.md.
    Tc                B    t          j        dd          |          }t          j        |          }t          j        |dd          } t          j        dd          |          }t          j        |          }t          j        |dd          }|                    |j        d         df          } t          j        d	
          |          }t          j        |          }|s|S  t          j        d
          |          }t          j        |          }|S )N    )   r   )featureskernel_size)   r   )window_shapestrides@   r   r>      )r   r   )nnConvreluavg_poolrA   rB   Denselog_softmax)selfr   rK   s      r    __call__zFlaxMNIST.Module.__call__   s   
2"'26
2
2
21
5
5a
'!**a
+aff
=
=
=a
2"'26
2
2
21
5
5a
'!**a
+aff
=
=
=a
))QWQZ$
%
%a
 "(C
 
 
 
#
#a
'!**a 
"(B



"
"a
.

ahr"   Nr   )r   r   r   r   r   compactr   rc   r"   r    Moduler      s@          Z   Z  r"   r   Tc                J    t           j                            d| i||          S )Nr;   rT   )r   modelapply)r;   rJ   rK   s      r    rQ   zFlaxMNIST.predict   s1    ?  (F!3!'1@ ! B B Br"   c                    t                               | |d          }t          j        t          j        ||z  d                     S rS   )r   rQ   rC   rV   rW   rX   s       r    r[   zFlaxMNIST.loss   sE     ##FFD#IIKHSW[61:::;;;;r"   c                     t          j        t          j                  |||          }|                     ||          \  }}t          j        ||          }||fS N)rF   rq   r   r[   rt   optaxapply_updates)txr;   	opt_staterJ   rY   rq   updatess          r    rt   zFlaxMNIST.update   sV    #38IN##FFF;;D433GY 11F9r"   c           	        t           j                            d          }d}t          j        dt
          z   t          j                  }t          j        	                    ||          d         }t          j        t          |          }|	                    |          }	t          |          D ])}
t          j                    }t          j        |           D ]5\  }} t          j        t          j        d          |||	||          \  }}	6t          j                    |z
  }t&                              t          j        ||           }t&                              t          j        ||          }t-          j        dt          j        |
|           t-          j        dt          j        d	|z             t-          j        d
t          j        d	|z             +t3          j        t          j        |          }||fS )ai  Trains a pure JAX MNIST predictor.

    Returns:
      a tuple with two elements:
        - a predictor function with signature "(Params, ImagesBatch) ->
          Predictions".
          If `with_classifier=False` then the output of the predictor function
          is the last layer of logits.
        - the parameters "Params" for the predictor function
    r   g?)r   r;   )learning_ratemomentum)static_argnumsr|   r}   r~   r   rT   )rF   rx   r   rC   onesinput_shaper   r   r   initr   sgdrn   r   r   r&   rg   rf   rt   r9   rk   rQ   r   r   r   r   r   )r   r   r   rK   r{   momentum_mass
init_shaper;   r   r   r   r   rJ   rY   r   r   r   
predict_fns                     r    r   zFlaxMNIST.train   s    *

Q

CM$,ck::J_!!#z228<F	]	C	C	CBIz"" $ $9;;j M(33 F F.&&6CGI$4346 6 668&)6<fF F		 9;;+j''	(96(02 2i&&y'8&'.0 0hl.	  l6	)#% % %l2IN(?$ $ $ $ "9#43BD D DJr"   Nr   )r   r   r   r   r   r   r   r   r   rQ   r[   rt   r   rc   r"   r    r   r      s        ""	$    ry   4 &((%B B B <B
 < < <<
   < )  )  )  <)  )  ) r"   r   nr_rowsnr_colstitler,   inference_fnCallable | Nonec                   ||z  }t          j        d|          }t          t          j        |                     d                              \  \  }}|r ||          }	t          |d|                   D ]\  }
}|                    |||
dz             }|rdt          j	        |	|
                    d}nd}|dt          j	        ||
                    z  }|
                    |           t          j        t          j        |d	          d
z                      t          j                  d           t          j                     dS )a  Plots a grid of images with their predictions.

  Params:
    ds: a tensorflow dataset from where to pick the images and labels.
    nr_rows, nr_cols: the size of the grid to plot
    title: the title of the plot
    inference_fn: if None then print the existing label, else use this function
      on the batch of images to produce a batch of inference results, which
      get printed.
    inference_batch_size: the size of the batch of images passed to
    `inference_fn`.
  )g       @g      @)figsizenumr   Nzinfer: 
 zlabel: )r   r      nearest)interpolation)pltfigurelistr&   rg   take	enumerateadd_subplotnpr^   	set_titleimshowrA   astypeuint8show)r3   r   r   r   r   countfigimagesrY   inferred_labelsir   digitdigit_titles                 r    plot_imagesr   "  sU   " G
%
8///#4=445566 +"l6**OF6E6N++ 
 
haOOGWa!e44E ?bi(:;;???kkk3RYvay11333K	OOK   J	z%""S(00::     (*****r"   )r   r   r   r   r   )r   r   r   r   r   r,   r   r   )*r   
__future__r   collections.abcr   r   r   r   r*   r   typingr   abslr   flaxr   r   rF   	jax.numpynumpyrC   
matplotlibr	   r   r   r   
tensorflowr   tensorflow_datasetsr&   DEFINE_booleanr$   train_batch_sizetest_batch_sizer   r   rw   rn   r7   r9   r   r   rc   r"   r    <module>r      s    # " " " " " . . . . . . . .      				                     



       $ $ $ $ $ $          " " " "!U!+u"?A A
   !!!	# # # #L\F \F \F \F \F \F \F \F~^  ^  ^  ^  ^  ^  ^  ^ J 15	" " " " " " "r"   