I recently encountered a situation where the result of a computation was of type NAN. I got the following example after a step-by-step debug:
using Zygote
using OMEinsum
using LinearAlgebra
function f(T)
@ein M[ b, e, c, d ] := ( T[ a, b, u, f ] * T[ a, c, u, h ] ) * ( T[ l, e, f, m ] * T[ l, d, h, m ] )
d = size( T, 1 )
M = reshape( M, ( d^2, d^2 ) )
M = ( M + M' ) / 2
U, S,_ = svd( M )
@show S
@ein Coef[] := U[ i, i]
return Coef[]
end
function test()
x0=0.5
T1 = [0.9988894589661916 2.4141401009672692e-8 -9.21073527124551e-18; 2.414140092879679e-8 1.0113486732236356e-13 -3.254292770737505e-24; -2.4058145031123452e-19 2.4387329449624486e-24 1.002665275075667e-13;;; 2.2369956667600393e-8 0.01177751073439025 0.01177685069366975; 0.011777510734396866 5.692640873553409e-10 2.8460655019662047e-10; 0.011776850693676363 2.846065512560657e-10 -1.9093252656828917e-14;;; 8.318087750543311e-19 -0.011777510733978307 -0.011776850693259231; 0.011777510733971711 9.483301236635998e-19 -2.846256431019167e-10; 0.01177685069325263 2.846256439393433e-10 -1.1084366881805116e-19;;;; 2.2369956656976694e-8 0.011777510734401848 -0.011776850693658138; 0.011777510734408457 5.692640873559015e-10 -2.846065504135412e-10; -0.011776850693664753 -2.8460655126146057e-10 -1.909302979277114e-14;;; 3.130556155863574e-7 -4.861245076481914e-7 -3.8609241904943518e-19; -4.861245076487369e-7 0.0002777279464551575 2.735873352730558e-16; -3.85943379257726e-19 2.7370274942050075e-16 -0.000277696818233557;;; -1.4626012574453234e-21 4.863882701148023e-7 -2.637401479213294e-10; -4.863882701147824e-7 -1.557657977123615e-16 -0.000277712381910125; 2.6374014790165455e-10 0.00027771238191012494 1.5574236952443137e-16;;;; -9.693798775119823e-18 -0.011777510733989914 0.011776850693247628; 0.011777510733983297 9.48331430058368e-19 2.846256428844345e-10; -0.011776850693241015 -2.846256439333898e-10 1.0067199819365289e-19;;; -6.894583731466444e-21 4.863882701145528e-7 2.637401484443907e-10; -4.863882701147811e-7 -1.5594644388542407e-16 0.00027771238191012494; -2.637401486727373e-10 -0.000277712381910125 1.5592068003439878e-16;;; 3.130556136009777e-7 -4.866520174529157e-7 -1.3689486660478041e-19; -4.866520174525782e-7 -0.00027772794648245336 -2.734954860940402e-16; -3.8511372770911903e-19 -2.7374346680089633e-16 0.0002776968182138848]
T2 = [0.9988894593101187 2.2939233802901028e-8 -9.693961756608732e-18; 2.293923381308961e-8 9.987325892856526e-14 2.2543638292181353e-24; 8.316496036966675e-19 -2.9423333181213116e-24 9.934646468007591e-14;;; -5.406181172607965e-10 0.01177717890675867 -0.011777178906347725; 0.011777178897777966 5.409024037548475e-10 -2.7044258700019684e-10; -0.011777178897367017 -2.704425865497314e-10 -1.7230186046708423e-14;;; -2.0216711167529834e-19 -0.011777178897358671 0.011777178896947715; 0.011777178906339374 5.778757151237687e-20 2.704598168745984e-10; -0.011777178905928417 -2.704598170281596e-10 9.577391798957861e-20;;;; -5.406181981389911e-10 0.011777178906765277 0.011777178906341113; 0.011777178897784579 5.409024037551513e-10 2.704425867714562e-10; 0.01177717889736042 2.7044258656919056e-10 -1.7230395024849602e-14;;; 1.0040895017914501e-13 -1.2767290128062186e-11 9.48493733078004e-19; -1.276729011831356e-11 0.00027771229659764916 -1.559763444041006e-16; 9.484927452570197e-19 -1.557336558187481e-16 -0.0002777122965779568;;; -2.225974728205143e-22 6.393253237300295e-12 -6.374037831597291e-12; -6.393253246930552e-12 2.1176987405798918e-13 0.0002777122965779157; 6.374037831690397e-12 -0.00027771229657791565 -2.1176976089970796e-13;;;; 8.767751521417996e-18 -0.01177717889736528 -0.0117771788969411; 0.011777178906345992 5.778772549179366e-20 -2.7045981710303523e-10; 0.01177717890592182 2.7045981700839744e-10 -1.5242568034864584e-19;;; -9.109651133721204e-21 6.393252389487262e-12 6.37403678243116e-12; -6.3932521876009254e-12 2.1176995330461678e-13 -0.00027771229657791576; -6.374036994040965e-12 0.0002777122965779157 -2.1177010845828176e-13;;; 1.004089309946328e-13 -1.92155038291017e-14 1.0067294462752412e-19; -1.92153018792467e-14 -0.0002777122965778749 1.559594509160548e-16; -1.1084349947529846e-19 1.557346227870929e-16 0.000277712296558182]
# @show f(T1)
@show gradient(f,T1)[1]
# @show f(T2)
@show gradient(f,T2)[1]
end
test()
The output is:
S = [0.9966733066799104, 0.000553884336002314, 0.0005538843359628737, 3.078119489781907e-7, 9.517013661198881e-14, 8.233208632182651e-20, 3.152181524404255e-20, 2.21461089491521e-20, 3.244767819833425e-21]
(gradient(f, T1))[1] = [0.5930936292861588 7624.065189342431 1923.250385191218; -0.008112132803168928 230688.20549618005 -1.0052478316457001e6; 0.023404018301786112 -1.0023995820129112e6 229874.507193745;;; 10.553837932255085 -8.356714691702897e6 -8.366949201460425e6; 1197.341668354167 -98466.40538184528 -104976.6490006776; 1211.9274984744588 -97231.75609906396 -106199.55496457085;;; -10.554681766014166 8.346222273536322e6 8.345128778829346e6; 8082.5080766834435 -106516.22428477029 -96925.69189064114; 8067.390969392194 -107669.12179149309 -95761.76577622129;;;; 7.781678518198429 8.334538750588702e6 -8.366146786695122e6; -12726.386662478204 -57205.113172444835 62112.68877471877; 12706.25462049855 58335.116218700015 -60976.290836456545;;; -2107.7418497581 3400.377447668149 807.9788817186554; 162.73594169763243 255832.81028894515 365745.04965628346; -57.28095383751074 366084.9237695921 256456.64622777177;;; 2048.776221317851 -3289.9263969847116 -694.2366713737454; -144.70786066088436 366071.46541845205 256132.3438346258; 74.40043057047265 256152.1051356172
365763.55382267665;;;; -7.769985794854151 -8.344364602271023e6 8.370074122969149e6; 3446.234865028609 -61114.81018706875 58203.677269715816; -3426.620530473898 60021.65478200047 -59289.78009630316;;; 2057.1865482553208 -3517.7005815112448 -695.9658722909976; -149.42500941330974 -365977.8189552523 -256186.14348111482; 44.380215997682626 -256095.37475010278 -365843.398443275;;; -1998.2209269612583 3398.7281161764204 580.0189728918299; 131.3971439572214 -256442.73089178288 -365903.2879046907; -61.49905283810004 -365922.9529728504 -255833.7777953479]
S = [0.9966733077096799, 0.00055388416539868, 0.00055388416539868, 3.0781175853684797e-7, 9.517001850668072e-14, 1.691437704129239e-19, 1.361519953025284e-19, 5.109999329154659e-21, 1.6347206747950515e-21]
(gradient(f, T2))[1] = [NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN;;; NaN NaN NaN; NaN NaN NaN; NaN NaN NaN]
My judgment may be that the SVD decomposition get singular value degenerate, but it may not be. If so, how do I fix SVD? I don’t understand the underlying SVD implementation very well.