
    ni                     v    S r SSKJr  SSKJr  SSKJr   SSKJs  Jr   " S S\5      rg! \	 a    SSK
r Nf = f)DNormalize tensors based on streaming estimates of mean and variance.    )absolute_import)division)print_functionNc                   F    \ rS rSrSrSS jrS rS rS rS r	S r
S	 rS
rg)StreamingNormalize   r   c                    X l         X0l        X@l        XPl        [        R
                  " U5         [        R                  " SS5      U l        [        R                  " [        R                  " U5      S5      U l	        [        R                  " [        R                  " U5      S5      U l
        SSS5        g! , (       d  f       g= f)a  Normalize tensors based on streaming estimates of mean and variance.

Centering the value, scaling it by the standard deviation, and clipping
outlier values are optional.

Args:
  template: Example tensor providing shape and dtype of the vaule to track.
  center: Python boolean indicating whether to subtract mean from values.
  scale: Python boolean indicating whether to scale values by stddev.
  clip: If and when to clip normalized values.
  name: Parent scope of operations provided by this class.
r   FN)_center_scale_clip_nametf
name_scopeVariable_count
zeros_like_mean_var_sum)selftemplatecenterscaleclipnames         \/home/james-whalen/.local/lib/python3.13/site-packages/pybullet_envs/agents/ppo/normalize.py__init__StreamingNormalize.__init__   sx     LKJJ	t	KK5)dk;;r}}X6>djkk"--"95Adm 
		s   A=B55
Cc                   ^  [         R                  " T R                  S-   5         UR                  R                  T R
                  R                  R                  :H  nU(       a  US   nT R                  (       a  UT R
                  S   -  nT R                  (       a3  U[         R                  " T R                  S:  U 4S jU 4S j5      S   -  nT R                  (       a-  [         R                  " UT R                  * T R                  5      nU(       a  US   n[         R                  " US5      sSSS5        $ ! , (       d  f       g= f)	zNormalize a single or batch tensor.

Applies the activated transformations in the constructor using current
estimates of mean and variance.

Args:
  value: Batch or single value tensor.

Returns:
  Normalized batch or single value tensor.
z
/transformN.   c                  *   > T R                  5       S-   $ )Ng:0yE>)_stdr   s   r   <lambda>.StreamingNormalize.transform.<locals>.<lambda>I   s    TYY[4%7    c                  D   > [         R                  " T R                  5      $ N)r   	ones_liker   r$   s   r   r%   r&   I   s    dmmA\r'   Nr   value)r   r   r   shapendimsr   r   r   condr   r   clip_by_valuecheck_numerics)r   r+   no_batch_dims   `  r   	transformStreamingNormalize.transform3   s     
tzzL0	1[[&&$***:*:*@*@@l	i 	I&&	KK!O79\^^bd 	d	  TZZ@	aug.! 
2	1	1s   DD::
Ec                 Z  ^^ [         R                  " U R                  S-   5         TR                  R                  U R
                  R                  R                  :X  a  TS   m[         R                  " T5      S   n[         R                  " U R                  R                  U5      /5         [         R                  " U R                  [         R                  5      n[         R                  " TU R
                  S   -
  S5      nU R
                  XC-  -   m[         R                  " U R                  S:  U4S jU4S j5      mTU R
                  S   -
  TTS   -
  -  nU R                  [         R                  " US5      -   nSSS5        [         R                  " TW/5         U R
                  R                  T5      U R                  R                  U5      4nSSS5        [         R                  " W5         TR                  R                  S:X  a  [         R                  " T5      mU R!                  S[         R                  " T5      5      sSSS5        sSSS5        $ ! , (       d  f       N= f! , (       d  f       N= f! , (       d  f       O= f SSS5        g! , (       d  f       g= f)	zrUpdate the mean and variance estimates.

Args:
  value: Batch or single value tensor.

Returns:
  Summary tensor.
z/updater    r   r!   c                     > T $ r)    )new_means   r   r%   +StreamingNormalize.update.<locals>.<lambda>c   s    Hr'   c                     > T S   $ )Nr   r6   )r+   s   r   r%   r8   c   s	    eAhr'   Nr+   )r   r   r   r,   r-   r   control_dependenciesr   
assign_addcastfloat32
reduce_sumr.   r   assignreduce_mean_summary)	r   r+   countstep
mean_delta	var_deltanew_var_sumupdater7   s	    `      @r   rG   StreamingNormalize.updateQ   s    
tzzI-	.			djj..44	4i hhuoa e""DKK$:$:5$A#BCwwt{{BJJ/]]54::i+@#@!D
::
 11774;;?,<>NOTZZ	22ux	?R7RS	mmbmmIq&AA D ""Hk#:;""8,dmm.B.B;.OO <""6*;;!..'%}}WbnnU&;< +* 
/	.
 DC <;*** 
/	.	.sV   BJ-CI J!7I0J6AJ	J
I-	)J0
I>	:J
J	J
J*c           
         [         R                  " U R                  S-   5         [         R                  " U R                  R                  S5      U R                  R                  [         R                  " U R                  5      5      U R                  R                  [         R                  " U R                  5      5      5      sSSS5        $ ! , (       d  f       g= f)zgReset the estimates of mean and variance.

Resets the full state of this class.

Returns:
  Operation.
z/resetr   N)	r   r   r   groupr   r?   r   r   r   r$   s    r   resetStreamingNormalize.resetm   s     
tzzH,	-XXdkk((+TZZ->->r}}TZZ?X-Ymm**2==+GHJ 
.	-	-s   BC
Cc                 t  ^  [         R                  " T R                  S-   5         [         R                  " T R                  S:  U 4S j[
        5      n[         R                  " T R                  S:  U 4S j[
        5      n[         R                  R                  X/5      sSSS5        $ ! , (       d  f       g= f)zKSummary string of mean and standard deviation.

Returns:
  Summary tensor.
z/summaryr   c                  <   > T R                  ST R                  5      $ )Nmean)rA   r   r$   s   r   r%   ,StreamingNormalize.summary.<locals>.<lambda>   s    dmmFDJJ6Wr'   r!   c                  D   > T R                  ST R                  5       5      $ )Nstddev)rA   r#   r$   s   r   r%   rP      s    T]]8TYY[5Yr'   N)r   r   r   r.   r   strsummarymerge)r   mean_summarystd_summarys   `  r   rT   StreamingNormalize.summaryy   ss     
tzzJ.	/WWT[[1_.WY\]lGGDKK!O-Y[^_kZZ|9: 
0	/	/s   A:B))
B7c                    ^  [         R                  " T R                  S:  U 4S jU 4S j5      n[         R                  " US-   5      $ )zComputes the current estimate of the standard deviation.

Note that the standard deviation is not defined until at least two samples
were seen.

Returns:
  Tensor of current variance.
r!   c                     > T R                   [        R                  " T R                  S-
  [        R                  5      -  $ )Nr!   )r   r   r<   r   r=   r$   s   r   r%   )StreamingNormalize._std.<locals>.<lambda>   s+    aA% 1%r'   c                  \   > [         R                  " T R                  5      [        S5      -  $ )Nnan)r   r*   r   floatr$   s   r   r%   r[      s    bll4==.IERWL.Xr'   g-C6?)r   r.   r   sqrt)r   variances   ` r   r#   StreamingNormalize._std   s;     wwt{{Q )%&XZH
 778d?##r'   c                     UR                   R                  S:X  a  [        R                  R	                  X5      $ [        R                  R                  X5      $ )zCreate a scalar or histogram summary matching the rank of the tensor.

Args:
  name: Name for the summary.
  tensor: Tensor to summarize.

Returns:
  Summary tensor.
r   )r,   r-   r   rT   scalar	histogram)r   r   tensors      r   rA   StreamingNormalize._summary   s@     ||QZZt,,ZZ!!$//r'   )r   r   r   r   r   r   r   N)TT
   	normalize)__name__
__module____qualname____firstlineno____doc__r   r2   rG   rK   rT   r#   rA   __static_attributes__r6   r'   r   r   r      s*    LB,/<=8
J	;$ 0r'   r   )rm   
__future__r   r   r   tensorflow.compat.v1compatv1r   	Exception
tensorflowobjectr   r6   r'   r   <module>rv      sA    K &  %##
G0 G0	  s   	+ 
88