B M>d&@sddlZddlZddlZddlZddlmZeej r>dndZGddde Z Gddde Z Gdd d e ZGd d d e ZGd d d e ZGddde ZGdddejjZdS)NcudacpucsNeZdZdfdd ZddZddZd d Zd d Zd dZddZ Z S) MatReaderTFcs>tt|||_||_||_||_d|_d|_| dS)N) superr__init__to_torchto_cudato_float file_pathdataold_mat _load_file)selfr rrr ) __class__D/work/home/acrzcyisbk/Ocean_AI_model/02_training/utils/darcy_loss.pyr'szMatReader.__init__cCs@ytj|j|_d|_Wn t|j|_d|_YnXdS)NTF)scipyioloadmatr r r h5pyFile)rrrrr 4s  zMatReader._load_filecCs||_|dS)N)r r )rr rrr load_file<szMatReader.load_filecCsl|j|}|js8|d}tj|tt|jdddd}|jrJ|tj }|j rht |}|j rh|}|S)Nr)axes)r r np transposerangelenshaper astypefloat32rtorch from_numpyrr)rfieldxrrr read_field@s    zMatReader.read_fieldcCs ||_dS)N)r)rrrrrset_cudaRszMatReader.set_cudacCs ||_dS)N)r)rrrrr set_torchUszMatReader.set_torchcCs ||_dS)N)r )rr rrr set_floatXszMatReader.set_float)TFT) __name__ __module__ __qualname__rr rr&r'r(r) __classcell__rr)rrr&s rcs@eZdZd fdd ZddZdddZd d Zd d ZZS)UnitGaussianNormalizerh㈵>cs4tt|t|d|_t|d|_||_dS)Nr)rr.rr"meanstdeps)rr%r2)rrrr]szUnitGaussianNormalizer.__init__cCs||j|j|j}|S)N)r0r1r2float)rr%rrrencodeeszUnitGaussianNormalizer.encodeNcCs|dkr|j|j}|j}nxt|jjt|djkrP|j||j}|j|}t|jjt|djkr|jdd|f|j}|jdd|f}|||}|S)Nr)r1r2r0rrr3)rr% sample_idxr1r0rrrdecodeis   zUnitGaussianNormalizer.decodecCs|j|_|j|_dS)N)r0rr1)rrrrrys zUnitGaussianNormalizer.cudacCs|j|_|j|_dS)N)r0rr1)rrrrr}s zUnitGaussianNormalizer.cpu)r/)N) r*r+r,rr4r6rrr-rr)rrr.\s  r.cs@eZdZd fdd ZddZdddZd d Zd d ZZS)GaussianNormalizerh㈵>cs0tt|t||_t||_||_dS)N)rr7rr"r0r1r2)rr%r2)rrrrs  zGaussianNormalizer.__init__cCs||j|j|j}|S)N)r0r1r2)rr%rrrr4szGaussianNormalizer.encodeNcCs||j|j|j}|S)N)r1r2r0)rr%r5rrrr6szGaussianNormalizer.decodecCs|j|_|j|_dS)N)r0rr1)rrrrrs zGaussianNormalizer.cudacCs|j|_|j|_dS)N)r0rr1)rrrrrs zGaussianNormalizer.cpu)r8)N) r*r+r,rr4r6rrr-rr)rrr7s  r7cs.eZdZd fdd ZddZddZZS) RangeNormalizer?csbtt|t|ddd}t|ddd}|||||_|j |||_dS)Nrr) rr9rr"minviewmaxab)rr%lowhighZmyminZmymax)rrrrs zRangeNormalizer.__init__cCs6|}||dd}|j||j}||}|S)Nrr)sizer=r?r@)rr%srrrr4s  zRangeNormalizer.encodecCs6|}||dd}||j|j}||}|S)Nrr)rCr=r@r?)rr%rDrrrr6s  zRangeNormalizer.decode)r:r;)r*r+r,rr4r6r-rr)rrr9sr9cs6eZdZd fdd ZddZddZd d ZZS) LpLossTcs>tt||dkr|dks"t||_||_||_||_dS)Nr)rrErAssertionErrordp reduction size_average)rrHrIrKrJ)rrrrs zLpLoss.__init__cCsx|d}d|dd}||j|jt||d||d|jd}|jrt|jrjt|St |S|S)Nrg?rr) rCrHrIr"normr=rJrKr0sum)rr%y num_exampleshZ all_normsrrrabss 4  z LpLoss.abscCsx|d}t||d||d|jd}t||d|jd}|jrp|jrbt||St||S||S)Nrrr) rCr"rLreshaperIrJrKr0rM)rr%rNrO diff_normsy_normsrrrrels $z LpLoss.relcCs |||S)N)rU)rr%rNrrr__call__szLpLoss.__call__)rFrFTT)r*r+r,rrQrUrVr-rr)rrrEs rEcs0eZdZd fdd Zdd Zd d d ZZS)HsLossrFrNFTcsbtt||dkr|dks"t||_||_||_||_||_||_ |dkrXdg|}||_ dS)Nrr) rrWrrGrHrIkbalancedrJrKr?)rrHrIrXr?grouprKrJ)rrrrs zHsLoss.__init__cCsx|d}t||d||d|jd}t||d|jd}|jrp|jrbt||St||S||S)Nrrr) rCr"rLrRrIrJrKr0rM)rr%rNrOrSrTrrrrUs $z HsLoss.relc Cs|d}|d}|j}|j}|j}||jd||d}||jd||d}ttjd|dddtj| ddddfd |d d|}ttjd|dddtj| ddddfd d| |d} t | d||d |j }t |  d||d |j } tjj|ddgd}tjj|ddgd}|dkrd} |dkrv| |dd|d| d7} |dkr| |dd|dd|d| d| d7} t| } ||| || } n|||} |dkr |dt|d| d} | ||| || 7} |dkrt|dt|dd|d| d| d} | ||| || 7} | |d} | S) NrrFrr)startendstep)dimF)rCrXrYr?r=rr"catarangerRrepeatrQtodevicefftfftnsqrtrU) rr%rNr?nxnyrXrYZk_xZk_yweightlossrrrrVs<  BB    4    2 zHsLoss.__call__)rFrFrNFTT)N)r*r+r,rrUrVr-rr)rrrWs rWcs&eZdZdfdd ZddZZS)DenseNetNFc stt|t|d|_|jdks*tt|_xlt |jD]^}|j t ||||d||jdkr@|r|j t ||d|j |q@W|dk r|j |dS)Nr) rrlrrZn_layersrGnn ModuleListlayersrappendLinear BatchNorm1d)rro nonlinearityZout_nonlinearity normalizej)rrrr's  zDenseNet.__init__cCs$xt|jD]\}}||}q W|S)N) enumeratero)rr%_lrrrforward<s zDenseNet.forward)NF)r*r+r,rryr-rr)rrrl&srl)r"numpyrscipy.iorrtorch.nnrmrdr is_availableobjectrr.r7r9rErWModulerlrrrrs 6&/C