linalg.py 190 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import numpy as np
  15. import paddle
  16. from paddle import _C_ops
  17. from paddle.base.libpaddle import DataType
  18. from paddle.common_ops_import import VarDesc
  19. from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only
  20. from ..base.data_feeder import (
  21. check_dtype,
  22. check_type,
  23. check_variable_and_dtype,
  24. convert_dtype,
  25. )
  26. from ..common_ops_import import Variable
  27. from ..framework import (
  28. LayerHelper,
  29. in_dynamic_mode,
  30. in_dynamic_or_pir_mode,
  31. in_pir_mode,
  32. )
  33. from .creation import full
  34. from .manipulation import cast
  35. from .math import _get_reduce_axis
  36. __all__ = []
  37. # Consistent with kDefaultDim from C++ Backend
  38. K_DEFAULT_DIM = 9
  39. def transpose(x, perm, name=None):
  40. """
  41. Permute the data dimensions of `input` according to `perm`.
  42. The `i`-th dimension of the returned tensor will correspond to the
  43. perm[i]-th dimension of `input`.
  44. Args:
  45. x (Tensor): The input Tensor. It is a N-D Tensor of data types bool, float16, bfloat16, float32, float64, int8, int16, int32, int64, uint8, uint16, complex64, complex128.
  46. perm (list|tuple): Permute the input according to the data of perm.
  47. name (str, optional): The name of this layer. For more information, please refer to :ref:`api_guide_Name`. Default is None.
  48. Returns:
  49. Tensor: A transposed n-D Tensor, with data type being bool, float32, float64, int32, int64.
  50. Examples:
  51. .. code-block:: text
  52. # The following codes in this code block are pseudocode, designed to show the execution logic and results of the function.
  53. x = to_tensor([[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]]
  54. [[13 14 15 16] [17 18 19 20] [21 22 23 24]]])
  55. shape(x): return [2,3,4]
  56. # Example 1
  57. perm0 = [1,0,2]
  58. y_perm0 = transpose(x, perm0) # Permute x by perm0
  59. # dim:0 of y_perm0 is dim:1 of x
  60. # dim:1 of y_perm0 is dim:0 of x
  61. # dim:2 of y_perm0 is dim:2 of x
  62. # The above two lines can also be understood as exchanging the zeroth and first dimensions of x
  63. y_perm0.data = [[[ 1 2 3 4] [13 14 15 16]]
  64. [[ 5 6 7 8] [17 18 19 20]]
  65. [[ 9 10 11 12] [21 22 23 24]]]
  66. shape(y_perm0): return [3,2,4]
  67. # Example 2
  68. perm1 = [2,1,0]
  69. y_perm1 = transpose(x, perm1) # Permute x by perm1
  70. # dim:0 of y_perm1 is dim:2 of x
  71. # dim:1 of y_perm1 is dim:1 of x
  72. # dim:2 of y_perm1 is dim:0 of x
  73. # The above two lines can also be understood as exchanging the zeroth and second dimensions of x
  74. y_perm1.data = [[[ 1 13] [ 5 17] [ 9 21]]
  75. [[ 2 14] [ 6 18] [10 22]]
  76. [[ 3 15] [ 7 19] [11 23]]
  77. [[ 4 16] [ 8 20] [12 24]]]
  78. shape(y_perm1): return [4,3,2]
  79. Examples:
  80. .. code-block:: python
  81. >>> import paddle
  82. >>> x = paddle.randn([2, 3, 4])
  83. >>> x_transposed = paddle.transpose(x, perm=[1, 0, 2])
  84. >>> print(x_transposed.shape)
  85. [3, 2, 4]
  86. """
  87. if in_dynamic_or_pir_mode():
  88. return _C_ops.transpose(x, perm)
  89. else:
  90. check_variable_and_dtype(
  91. x,
  92. 'x',
  93. [
  94. 'bool',
  95. 'float16',
  96. 'bfloat16',
  97. 'float32',
  98. 'float64',
  99. 'int8',
  100. 'uint8',
  101. 'int16',
  102. 'int32',
  103. 'int64',
  104. 'uint16',
  105. 'complex64',
  106. 'complex128',
  107. ],
  108. 'transpose',
  109. )
  110. check_type(perm, 'perm', (list, tuple), 'transpose')
  111. if isinstance(perm, tuple):
  112. perm = list(perm)
  113. if len(perm) != len(x.shape):
  114. raise ValueError(
  115. "Input(perm) is the permutation of dimensions of Input(x), "
  116. "its length should be equal to dimensions of Input(x), "
  117. f"but received dimension of Input(x) is {len(x.shape)}, "
  118. f"the length of Input(perm) is {len(perm)}."
  119. )
  120. for idx, dim in enumerate(perm):
  121. if dim >= len(x.shape):
  122. raise ValueError(
  123. "Each element in Input(perm) should be less than Input(x)'s dimension, "
  124. "but %d-th element in Input(perm) is %d which exceeds Input(x)'s "
  125. "dimension %d." % (idx, perm[idx], len(x.shape))
  126. )
  127. helper = LayerHelper('transpose', **locals())
  128. out = helper.create_variable_for_type_inference(x.dtype)
  129. x_shape = helper.create_variable_for_type_inference(x.dtype)
  130. helper.append_op(
  131. type='transpose2',
  132. inputs={'X': [x]},
  133. outputs={'Out': [out], 'XShape': [x_shape]},
  134. attrs={'axis': perm},
  135. )
  136. return out
  137. @inplace_apis_in_dygraph_only
  138. def transpose_(x, perm, name=None):
  139. r"""
  140. Inplace version of ``transpose`` API, the output Tensor will be inplaced with input ``x``.
  141. Please refer to :ref:`api_paddle_transpose`.
  142. """
  143. if in_dynamic_mode():
  144. return _C_ops.transpose_(x, perm)
  145. def matmul(x, y, transpose_x=False, transpose_y=False, name=None):
  146. """
  147. Applies matrix multiplication to two tensors. `matmul` follows
  148. the complete broadcast rules,
  149. and its behavior is consistent with `np.matmul`.
  150. Currently, the input tensors' number of dimensions can be any, `matmul` can be used to
  151. achieve the `dot`, `matmul` and `batchmatmul`.
  152. The actual behavior depends on the shapes of :math:`x`, :math:`y` and the
  153. flag values of :attr:`transpose_x`, :attr:`transpose_y`. Specifically:
  154. - If a transpose flag is specified, the last two dimensions of the tensor
  155. are transposed. If the tensor is ndim-1 of shape, the transpose is invalid. If the tensor
  156. is ndim-1 of shape :math:`[D]`, then for :math:`x` it is treated as :math:`[1, D]`, whereas
  157. for :math:`y` it is the opposite: It is treated as :math:`[D, 1]`.
  158. The multiplication behavior depends on the dimensions of `x` and `y`. Specifically:
  159. - If both tensors are 1-dimensional, the dot product result is obtained.
  160. - If both tensors are 2-dimensional, the matrix-matrix product is obtained.
  161. - If the `x` is 1-dimensional and the `y` is 2-dimensional,
  162. a `1` is prepended to its dimension in order to conduct the matrix multiply.
  163. After the matrix multiply, the prepended dimension is removed.
  164. - If the `x` is 2-dimensional and `y` is 1-dimensional,
  165. the matrix-vector product is obtained.
  166. - If both arguments are at least 1-dimensional and at least one argument
  167. is N-dimensional (where N > 2), then a batched matrix multiply is obtained.
  168. If the first argument is 1-dimensional, a 1 is prepended to its dimension
  169. in order to conduct the batched matrix multiply and removed after.
  170. If the second argument is 1-dimensional, a 1 is appended to its
  171. dimension for the purpose of the batched matrix multiple and removed after.
  172. The non-matrix (exclude the last two dimensions) dimensions are
  173. broadcasted according the broadcast rule.
  174. For example, if input is a (j, 1, n, m) tensor and the other is a (k, m, p) tensor,
  175. out will be a (j, k, n, p) tensor.
  176. Args:
  177. x (Tensor): The input tensor which is a Tensor.
  178. y (Tensor): The input tensor which is a Tensor.
  179. transpose_x (bool, optional): Whether to transpose :math:`x` before multiplication. Default is False.
  180. transpose_y (bool, optional): Whether to transpose :math:`y` before multiplication. Default is False.
  181. name (str, optional): If set None, the layer will be named automatically. For more information, please refer to :ref:`api_guide_Name`. Default is None.
  182. Returns:
  183. Tensor: The output Tensor.
  184. Examples:
  185. .. code-block:: python
  186. >>> import paddle
  187. >>> # vector * vector
  188. >>> x = paddle.rand([10])
  189. >>> y = paddle.rand([10])
  190. >>> z = paddle.matmul(x, y)
  191. >>> print(z.shape)
  192. []
  193. >>> # matrix * vector
  194. >>> x = paddle.rand([10, 5])
  195. >>> y = paddle.rand([5])
  196. >>> z = paddle.matmul(x, y)
  197. >>> print(z.shape)
  198. [10]
  199. >>> # batched matrix * broadcasted vector
  200. >>> x = paddle.rand([10, 5, 2])
  201. >>> y = paddle.rand([2])
  202. >>> z = paddle.matmul(x, y)
  203. >>> print(z.shape)
  204. [10, 5]
  205. >>> # batched matrix * batched matrix
  206. >>> x = paddle.rand([10, 5, 2])
  207. >>> y = paddle.rand([10, 2, 5])
  208. >>> z = paddle.matmul(x, y)
  209. >>> print(z.shape)
  210. [10, 5, 5]
  211. >>> # batched matrix * broadcasted matrix
  212. >>> x = paddle.rand([10, 1, 5, 2])
  213. >>> y = paddle.rand([1, 3, 2, 5])
  214. >>> z = paddle.matmul(x, y)
  215. >>> print(z.shape)
  216. [10, 3, 5, 5]
  217. """
  218. if in_dynamic_or_pir_mode():
  219. return _C_ops.matmul(x, y, transpose_x, transpose_y)
  220. else:
  221. attrs = {
  222. 'trans_x': transpose_x,
  223. 'trans_y': transpose_y,
  224. }
  225. def __check_input(x, y):
  226. var_names = {'x': x, 'y': y}
  227. for name, val in var_names.items():
  228. check_variable_and_dtype(
  229. val,
  230. name,
  231. [
  232. 'int8',
  233. 'uint16',
  234. 'float16',
  235. 'float32',
  236. 'float64',
  237. 'complex64',
  238. 'complex128',
  239. ],
  240. 'matmul',
  241. )
  242. __check_input(x, y)
  243. helper = LayerHelper('matmul_v2', **locals())
  244. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  245. helper.append_op(
  246. type='matmul_v2',
  247. inputs={'X': x, 'Y': y},
  248. outputs={'Out': out},
  249. attrs=attrs,
  250. )
  251. return out
  252. def vector_norm(x, p=2.0, axis=None, keepdim=False, name=None):
  253. """
  254. Calculate the p-order vector norm for certain dimension of Tensor `input`.
  255. Returns the vector norm (the 1-norm, the Euclidean or 2-norm, and in general the p-norm)
  256. of a given tensor.
  257. Args:
  258. x (Tensor): Tensor, data type float32, float64.
  259. p (int|float, optional): None for porder=2.0. Default None.
  260. axis (int|list, optional): None for last dimension. Default None.
  261. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
  262. name (str, optional): The default value is None. Normally there is no need for
  263. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  264. Returns:
  265. Tensor: results of vector_norm operation on the specified axis of input tensor,
  266. it's data type is the same as input's Tensor.
  267. Examples:
  268. .. code-block:: python
  269. >>> import paddle
  270. >>> import numpy as np
  271. >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12
  272. >>> print(x)
  273. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  274. [[[-12., -11., -10., -9. ],
  275. [-8. , -7. , -6. , -5. ],
  276. [-4. , -3. , -2. , -1. ]],
  277. [[ 0. , 1. , 2. , 3. ],
  278. [ 4. , 5. , 6. , 7. ],
  279. [ 8. , 9. , 10., 11.]]])
  280. >>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=2,axis=None,keepdim=False)
  281. >>> print(out_vector_norm)
  282. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  283. 34.)
  284. >>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=0,axis=[0,1],keepdim=False)
  285. >>> print(out_vector_norm)
  286. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  287. [5., 6., 6., 6.])
  288. >>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=float("inf"),axis=[1,2],keepdim=False)
  289. >>> print(out_vector_norm)
  290. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  291. [12., 11.])
  292. >>> out_vector_norm = paddle.linalg.vector_norm(x=x,p=1,axis=1,keepdim=False)
  293. >>> print(out_vector_norm)
  294. Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  295. [[24., 21., 18., 15.],
  296. [12., 15., 18., 21.]])
  297. """
  298. def zero_norm(
  299. input, porder=None, axis=axis, keepdim=False, asvector=False, name=None
  300. ):
  301. return paddle.count_nonzero(
  302. input, axis=axis, keepdim=keepdim, name=name
  303. ).astype(input.dtype)
  304. def inf_norm(
  305. input, porder=None, axis=axis, keepdim=False, asvector=False, name=None
  306. ):
  307. if in_dynamic_mode():
  308. out = _C_ops.abs(input)
  309. if porder == np.float64('inf'):
  310. return _C_ops.max(out, axis, keepdim)
  311. else:
  312. return _C_ops.min(out, axis, keepdim)
  313. else:
  314. helper = LayerHelper('inf_norm', **locals())
  315. out = helper.create_variable_for_type_inference(
  316. dtype=helper.input_dtype()
  317. )
  318. helper.append_op(
  319. type='abs', inputs={'X': input}, outputs={'Out': out}
  320. )
  321. reduce_out = helper.create_variable_for_type_inference(
  322. dtype=helper.input_dtype()
  323. )
  324. reduce_all, axis = _get_reduce_axis(axis, x)
  325. reduce_type = (
  326. 'reduce_max' if porder == np.float64('inf') else 'reduce_min'
  327. )
  328. helper.append_op(
  329. type=reduce_type,
  330. inputs={'X': out},
  331. outputs={'Out': reduce_out},
  332. attrs={
  333. 'dim': axis,
  334. 'keep_dim': keepdim,
  335. 'reduce_all': reduce_all,
  336. },
  337. )
  338. return reduce_out
  339. def vector_norm_axis_tuple(
  340. input, porder=2, axis=None, keepdim=False, asvector=False, name=None
  341. ):
  342. """
  343. NOTE:
  344. This function calculates the vector norm for dim >= 2.
  345. """
  346. if in_dynamic_or_pir_mode():
  347. abs_out = _C_ops.abs(input)
  348. pow_out = _C_ops.pow(abs_out, porder)
  349. sum_out = _C_ops.sum(pow_out, axis, None, keepdim)
  350. out = _C_ops.pow(sum_out, float(1.0 / porder))
  351. return out
  352. block = LayerHelper('norm', **locals())
  353. out = block.create_variable_for_type_inference(
  354. dtype=block.input_dtype()
  355. )
  356. abs_out = block.create_variable_for_type_inference(
  357. dtype=block.input_dtype()
  358. )
  359. block.append_op(
  360. type='abs', inputs={'X': input}, outputs={'Out': abs_out}
  361. )
  362. pow_out = block.create_variable_for_type_inference(
  363. dtype=block.input_dtype()
  364. )
  365. block.append_op(
  366. type='pow',
  367. inputs={'X': abs_out},
  368. outputs={'Out': pow_out},
  369. attrs={'factor': porder},
  370. )
  371. sum_out = block.create_variable_for_type_inference(
  372. dtype=block.input_dtype()
  373. )
  374. reduce_all, axis = _get_reduce_axis(axis, x)
  375. block.append_op(
  376. type='reduce_sum',
  377. inputs={'X': pow_out},
  378. outputs={'Out': sum_out},
  379. attrs={
  380. 'dim': axis,
  381. 'keep_dim': keepdim,
  382. 'reduce_all': reduce_all,
  383. },
  384. )
  385. block.append_op(
  386. type='pow',
  387. inputs={'X': sum_out},
  388. outputs={'Out': out},
  389. attrs={'factor': float(1.0 / porder)},
  390. )
  391. return out
  392. def vector_norm_axis_int(
  393. input, porder=2, axis=None, keepdim=False, asvector=False, name=None
  394. ):
  395. """
  396. NOTE:
  397. This function calculates the vector norm for len(axis) == 1.
  398. """
  399. if in_dynamic_or_pir_mode():
  400. if axis is None:
  401. axis = -1
  402. return _C_ops.p_norm(input, porder, axis, 1e-12, keepdim, asvector)
  403. else:
  404. if porder is not None:
  405. check_type(porder, 'porder', (float, int), 'p_norm')
  406. if axis is not None:
  407. check_type(axis, 'axis', (int), 'p_norm')
  408. check_variable_and_dtype(
  409. input,
  410. 'input',
  411. ['float16', 'uint16', 'float32', 'float64'],
  412. 'p_norm',
  413. )
  414. attrs = {
  415. 'axis': axis if axis is not None else -1,
  416. 'porder': float(porder) if porder is not None else 2.0,
  417. 'keepdim': keepdim,
  418. 'asvector': asvector,
  419. 'epsilon': 1e-12,
  420. }
  421. helper = LayerHelper('p_norm', **locals())
  422. out = helper.create_variable_for_type_inference(
  423. dtype=helper.input_dtype()
  424. )
  425. helper.append_op(
  426. type='p_norm',
  427. inputs={'X': input},
  428. outputs={'Out': out},
  429. attrs=attrs,
  430. )
  431. return out
  432. if not isinstance(p, (int, float)):
  433. raise ValueError(f"only valid p type is int and float, found {type(p)}")
  434. asvector = False
  435. if axis is None:
  436. axis = -1
  437. asvector = True
  438. if isinstance(axis, tuple):
  439. axis = list(axis)
  440. if isinstance(axis, list) and len(axis) == 1:
  441. axis = axis[0]
  442. # when len(axis) == 1, use the original op to calculate
  443. if isinstance(axis, int):
  444. return vector_norm_axis_int(
  445. x,
  446. axis=axis,
  447. porder=p,
  448. keepdim=keepdim,
  449. asvector=asvector,
  450. name=name,
  451. )
  452. # when len(axis) >= 1, calculate by combining other Python apis
  453. elif isinstance(axis, list):
  454. if p == np.inf or p == -np.inf:
  455. return inf_norm(x, porder=p, axis=axis, keepdim=keepdim, name=name)
  456. elif p == 0:
  457. return zero_norm(x, porder=p, axis=axis, keepdim=keepdim, name=name)
  458. else:
  459. return vector_norm_axis_tuple(
  460. x, porder=p, axis=axis, keepdim=keepdim, name=name
  461. )
  462. def matrix_norm(x, p='fro', axis=[-2, -1], keepdim=False, name=None):
  463. """
  464. Calculate the p-order matrix norm for certain dimension of Tensor `input`.
  465. Args:
  466. x (Tensor): Tensor, data type float32, float64.
  467. p (int|float|string, optional): Default 'fro'.
  468. axis (list, optional): The axis is a list(int)/tuple(int) with two elements. Default last two dimensions.
  469. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
  470. name (str, optional): The default value is None. Normally there is no need for
  471. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  472. Returns:
  473. Tensor: results of matrix_norm operation on the specified axis of input tensor,
  474. it's data type is the same as input's Tensor.
  475. Examples:
  476. .. code-block:: python
  477. >>> import paddle
  478. >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12
  479. >>> print(x)
  480. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  481. [[[-12., -11., -10., -9. ],
  482. [-8. , -7. , -6. , -5. ],
  483. [-4. , -3. , -2. , -1. ]],
  484. [[ 0. , 1. , 2. , 3. ],
  485. [ 4. , 5. , 6. , 7. ],
  486. [ 8. , 9. , 10., 11.]]])
  487. >>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=2,axis=[0,1],keepdim=False)
  488. >>> print(out_matrix_norm)
  489. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  490. [15.75857544, 14.97978878, 14.69693947, 14.97978973])
  491. >>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p='fro',axis=[0,1],keepdim=False)
  492. >>> print(out_matrix_norm)
  493. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  494. [17.43559647, 16.91153526, 16.73320007, 16.91153526])
  495. >>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=float('inf'),axis=[1,2],keepdim=False)
  496. >>> print(out_matrix_norm)
  497. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  498. [42., 38.])
  499. >>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p=-1,axis=[0,1],keepdim=False)
  500. >>> print(out_matrix_norm)
  501. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  502. [12., 12., 12., 12.])
  503. >>> out_matrix_norm = paddle.linalg.matrix_norm(x=x,p='nuc',axis=[0,1],keepdim=False)
  504. >>> print(out_matrix_norm)
  505. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  506. [23.21962357, 22.82873154, 22.69693947, 22.82873154])
  507. """
  508. def _backshift_permutation(dim0, dim1, dimn):
  509. """
  510. Auxiliary function for matrix_norm
  511. Computes the permutation that moves the two given dimensions to the back
  512. """
  513. ret = [i for i in range(dimn) if i != dim0 and i != dim1]
  514. ret.extend((dim0, dim1))
  515. return ret
  516. def _inverse_permutation(perm):
  517. """
  518. Given a permutation, returns its inverse. It's equivalent to argsort on an array
  519. """
  520. return [i for i, j in sorted(enumerate(perm), key=lambda ij: ij[1])]
  521. def frobenius_norm(input, dim=None, keepdim=False, name=None):
  522. """
  523. The frobenius norm OP is to calculate the frobenius norm of certain two dimensions of Tensor `input`.
  524. Args:
  525. input (Variable): Tensor, data type float32, float64.
  526. dim (list, optional): None for last two dimensions. Default None.
  527. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
  528. name (str, optional): The default value is None. Normally there is no need for
  529. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  530. """
  531. if dim is not None and not (isinstance(dim, list) and len(dim) == 2):
  532. raise ValueError(
  533. "The dim of frobenius norm op should be None or two elements list!"
  534. )
  535. if in_dynamic_or_pir_mode():
  536. if dim is None:
  537. return _C_ops.frobenius_norm(input, [], keepdim, True)
  538. return _C_ops.frobenius_norm(input, dim, keepdim, False)
  539. else:
  540. attrs = {'dim': dim, 'keep_dim': keepdim, 'reduce_all': False}
  541. if dim is None:
  542. attrs['reduce_all'] = True
  543. check_variable_and_dtype(
  544. input, 'input', ['float32', 'float64'], 'frobenius_norm'
  545. )
  546. helper = LayerHelper('frobenius_norm', **locals())
  547. out = helper.create_variable_for_type_inference(
  548. dtype=helper.input_dtype()
  549. )
  550. helper.append_op(
  551. type='frobenius_norm',
  552. inputs={'X': input},
  553. outputs={'Out': out},
  554. attrs=attrs,
  555. )
  556. return out
  557. def nuclear_norm(input, axis=axis, keepdim=False, name=None):
  558. """
  559. The nuclear norm OP is to calculate the nuclear norm of certain two dimensions of Tensor `input`.
  560. Args:
  561. input (Variable): Tensor, data type float32, float64.
  562. dim (list): Two dimensions.
  563. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
  564. name (str, optional): The default value is None. Normally there is no need for
  565. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  566. """
  567. perm = _backshift_permutation(axis[0], axis[1], len(input.shape))
  568. inv_perm = _inverse_permutation(perm)
  569. if in_dynamic_or_pir_mode():
  570. transposed = _C_ops.transpose(input, perm)
  571. u, s, vh = _C_ops.svd(transposed, False)
  572. result = _C_ops.sum(s, -1, None, keepdim)
  573. if keepdim:
  574. result = _C_ops.transpose(
  575. _C_ops.unsqueeze(result, -1), inv_perm
  576. )
  577. return result
  578. attrs = {'axis': axis, 'keepdim': keepdim}
  579. check_variable_and_dtype(
  580. input, 'input', ['float32', 'float64'], 'nuclear_norm'
  581. )
  582. block = LayerHelper('nuclear_norm', **locals())
  583. out = block.create_variable_for_type_inference(
  584. dtype=block.input_dtype()
  585. )
  586. transpose_out = block.create_variable_for_type_inference(
  587. dtype=block.input_dtype()
  588. )
  589. input_shape = block.create_variable_for_type_inference(
  590. dtype=block.input_dtype()
  591. )
  592. block.append_op(
  593. type='transpose2',
  594. inputs={'X': [input]},
  595. outputs={'Out': [transpose_out], 'XShape': [input_shape]},
  596. attrs={'axis': perm},
  597. )
  598. u = block.create_variable_for_type_inference(dtype=block.input_dtype())
  599. s = block.create_variable_for_type_inference(dtype=block.input_dtype())
  600. vt = block.create_variable_for_type_inference(dtype=block.input_dtype())
  601. block.append_op(
  602. type='svd',
  603. inputs={'X': [transpose_out]},
  604. outputs={'U': u, 'VH': vt, 'S': s},
  605. attrs={'full_matrices': False},
  606. )
  607. reduce_all, sum_axis = _get_reduce_axis(-1, s)
  608. block.append_op(
  609. type='reduce_sum',
  610. inputs={'X': s},
  611. outputs={'Out': out},
  612. attrs={
  613. 'dim': sum_axis,
  614. 'keep_dim': keepdim,
  615. 'reduce_all': reduce_all,
  616. },
  617. )
  618. if keepdim:
  619. unsqueeze_out = block.create_variable_for_type_inference(
  620. dtype=block.input_dtype()
  621. )
  622. block.append_op(
  623. type='unsqueeze2',
  624. inputs={'X': [out]},
  625. outputs={'Out': [unsqueeze_out], 'XShape': [input_shape]},
  626. attrs={'axes': [-1]},
  627. )
  628. block.append_op(
  629. type='transpose2',
  630. inputs={'X': [unsqueeze_out]},
  631. outputs={'Out': [out], 'XShape': [input_shape]},
  632. attrs={'axis': inv_perm},
  633. )
  634. return out
  635. def p_matrix_norm(input, porder=1.0, axis=axis, keepdim=False, name=None):
  636. """
  637. Calculate the p-order matrix norm for certain dimension of Tensor `input`.
  638. Args:
  639. input (Variable): Tensor, data type float32, float64.
  640. porder (int|float,str): p in ['fro', 'nuc', ±1, ±2, ±inf] Default 1.
  641. axis (list): Two dimensions.
  642. keepdim (bool, optional): Whether keep the dimensions as the `input`, Default False.
  643. name (str, optional): The default value is None. Normally there is no need for
  644. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  645. """
  646. perm = _backshift_permutation(axis[0], axis[1], len(input.shape))
  647. inv_perm = _inverse_permutation(perm)
  648. if in_dynamic_or_pir_mode():
  649. abs_ord = abs(porder)
  650. max_min = _C_ops.max if porder > 0.0 else _C_ops.min
  651. if abs_ord == 2.0:
  652. transpose_out = _C_ops.transpose(input, perm)
  653. u, s, vh = _C_ops.svd(transpose_out, False)
  654. result = max_min(s, -1, keepdim)
  655. if keepdim:
  656. result = _C_ops.transpose(
  657. _C_ops.unsqueeze(result, -1), inv_perm
  658. )
  659. return result
  660. else: # 1,-1,inf,-inf
  661. dim0, dim1 = axis
  662. if abs_ord == np.float64("inf"):
  663. dim0, dim1 = dim1, dim0
  664. if not keepdim and (dim0 < dim1):
  665. dim1 -= 1
  666. return max_min(
  667. vector_norm(input, 1.0, axis=dim0, keepdim=keepdim),
  668. dim1,
  669. keepdim,
  670. )
  671. check_variable_and_dtype(
  672. input,
  673. 'input',
  674. ['float16', 'uint16', 'float32', 'float64'],
  675. 'p_matrix_norm',
  676. )
  677. block = LayerHelper('p_matrix_norm', **locals())
  678. out = block.create_variable_for_type_inference(
  679. dtype=block.input_dtype()
  680. )
  681. abs_ord = abs(porder)
  682. if abs_ord == 2.0:
  683. transpose_out = block.create_variable_for_type_inference(
  684. dtype=block.input_dtype()
  685. )
  686. input_shape = block.create_variable_for_type_inference(
  687. dtype=block.input_dtype()
  688. )
  689. block.append_op(
  690. type='transpose2',
  691. inputs={'X': [input]},
  692. outputs={'Out': [transpose_out], 'XShape': [input_shape]},
  693. attrs={'axis': perm},
  694. )
  695. u = block.create_variable_for_type_inference(
  696. dtype=block.input_dtype()
  697. )
  698. s = block.create_variable_for_type_inference(
  699. dtype=block.input_dtype()
  700. )
  701. vt = block.create_variable_for_type_inference(
  702. dtype=block.input_dtype()
  703. )
  704. block.append_op(
  705. type='svd',
  706. inputs={'X': [transpose_out]},
  707. outputs={'U': u, 'VH': vt, 'S': s},
  708. attrs={'full_matrices': False},
  709. )
  710. reduce_type = 'reduce_max' if porder > 0 else 'reduce_min'
  711. reduce_out = block.create_variable_for_type_inference(
  712. dtype=block.input_dtype()
  713. )
  714. reduce_all, max_min_axis = _get_reduce_axis(-1, s)
  715. block.append_op(
  716. type=reduce_type,
  717. inputs={'X': s},
  718. outputs={'Out': reduce_out},
  719. attrs={
  720. 'dim': max_min_axis,
  721. 'keep_dim': keepdim,
  722. 'reduce_all': reduce_all,
  723. },
  724. )
  725. if keepdim:
  726. unsqueeze_out = block.create_variable_for_type_inference(
  727. dtype=block.input_dtype()
  728. )
  729. block.append_op(
  730. type='unsqueeze2',
  731. inputs={'X': [reduce_out]},
  732. outputs={'Out': [unsqueeze_out], 'XShape': [input_shape]},
  733. attrs={'axes': [-1]},
  734. )
  735. block.append_op(
  736. type='transpose2',
  737. inputs={'X': [unsqueeze_out]},
  738. outputs={'Out': [out], 'XShape': [input_shape]},
  739. attrs={'axis': inv_perm},
  740. )
  741. return out
  742. return reduce_out
  743. else:
  744. dim0, dim1 = axis
  745. if abs_ord == np.float64("inf"):
  746. dim0, dim1 = dim1, dim0
  747. if not keepdim and (dim0 < dim1):
  748. dim1 -= 1
  749. vector_out = block.create_variable_for_type_inference(
  750. dtype=block.input_dtype()
  751. )
  752. attrs = {
  753. 'axis': dim0,
  754. 'porder': 1,
  755. 'keepdim': keepdim,
  756. 'asvector': False,
  757. 'epsilon': 1e-12,
  758. }
  759. block.append_op(
  760. type='p_norm',
  761. inputs={'X': input},
  762. outputs={'Out': vector_out},
  763. attrs=attrs,
  764. )
  765. reduce_type = 'reduce_max' if porder > 0 else 'reduce_min'
  766. reduce_out = block.create_variable_for_type_inference(
  767. dtype=block.input_dtype()
  768. )
  769. reduce_all, max_min_axis = _get_reduce_axis(dim1, vector_out)
  770. block.append_op(
  771. type=reduce_type,
  772. inputs={'X': vector_out},
  773. outputs={'Out': reduce_out},
  774. attrs={
  775. 'dim': max_min_axis,
  776. 'keep_dim': keepdim,
  777. 'reduce_all': reduce_all,
  778. },
  779. )
  780. return reduce_out
  781. if isinstance(axis, tuple):
  782. axis = list(axis)
  783. if isinstance(axis, list) and len(axis) == 2:
  784. if p == "fro":
  785. return frobenius_norm(x, dim=axis, keepdim=keepdim, name=name)
  786. elif p == "nuc":
  787. return nuclear_norm(x, axis=axis, keepdim=keepdim, name=name)
  788. elif (
  789. p == np.inf
  790. or p == -np.inf
  791. or p == 1
  792. or p == -1
  793. or p == 2
  794. or p == -2
  795. ):
  796. return p_matrix_norm(
  797. x, porder=p, axis=axis, keepdim=keepdim, name=name
  798. )
  799. else:
  800. raise ValueError(
  801. f"just support p value 'fro','nuc',1,-1,inf,-inf,2,-2 if axis is 2D, found {p}"
  802. )
  803. else:
  804. raise ValueError(
  805. f"except axis type int or list (length of list == 2), found {len(axis)}"
  806. )
  807. def norm(x, p=None, axis=None, keepdim=False, name=None):
  808. """
  809. Returns the matrix norm (the Frobenius norm, the nuclear norm and p-norm) or vector norm (the 1-norm, the Euclidean
  810. or 2-norm, and in general the p-norm) of a given tensor.
  811. Whether the function calculates the vector norm or the matrix norm is determined as follows:
  812. - If axis is of type int, calculate the vector norm.
  813. - If axis is a two-dimensional array, calculate the matrix norm.
  814. - If axis is None, x is compressed into a one-dimensional vector and the vector norm is calculated.
  815. Paddle supports the following norms:
  816. +----------------+--------------------------------+--------------------------------+
  817. | porder | norm for matrices | norm for vectors |
  818. +================+================================+================================+
  819. | None(default) | frobenius norm | 2_norm |
  820. +----------------+--------------------------------+--------------------------------+
  821. | fro | frobenius norm | not support |
  822. +----------------+--------------------------------+--------------------------------+
  823. | nuc | nuclear norm | not support |
  824. +----------------+--------------------------------+--------------------------------+
  825. | inf | max(sum(abs(x), dim=1)) | max(abs(x)) |
  826. +----------------+--------------------------------+--------------------------------+
  827. | -inf | min(sum(abs(x), dim=1)) | min(abs(x)) |
  828. +----------------+--------------------------------+--------------------------------+
  829. | 0 | not support | sum(x != 0) |
  830. +----------------+--------------------------------+--------------------------------+
  831. | 1 | max(sum(abs(x), dim=0)) | as below |
  832. +----------------+--------------------------------+--------------------------------+
  833. | -1 | min(sum(abs(x), dim=0)) | as below |
  834. +----------------+--------------------------------+--------------------------------+
  835. | 2 |The maximum singular value | as below |
  836. | |of a matrix consisting of axis. | |
  837. +----------------+--------------------------------+--------------------------------+
  838. | -2 |The minimum singular value | as below |
  839. | |of a matrix consisting of axis. | |
  840. +----------------+--------------------------------+--------------------------------+
  841. | other int | not support | sum(abs(x)^{porder})^ |
  842. | or float | | {(1 / porder)} |
  843. +----------------+--------------------------------+--------------------------------+
  844. Args:
  845. x (Tensor): The input tensor could be N-D tensor, and the input data
  846. type could be float32 or float64.
  847. p (int|float|string, optional): Order of the norm. Supported values are `fro`, `nuc`, `0`, `±1`, `±2`,
  848. `±inf` and any real number yielding the corresponding p-norm.
  849. Default value is None.
  850. axis (int|list|tuple, optional): The axis on which to apply norm operation. If axis is int
  851. or list(int)/tuple(int) with only one element, the vector norm is computed over the axis.
  852. If `axis < 0`, the dimension to norm operation is rank(input) + axis.
  853. If axis is a list(int)/tuple(int) with two elements, the matrix norm is computed over the axis.
  854. Default value is `None`.
  855. keepdim (bool, optional): Whether to reserve the reduced dimension in the
  856. output Tensor. The result tensor will have fewer dimension
  857. than the :attr:`input` unless :attr:`keepdim` is true, default
  858. value is False.
  859. name (str, optional): The default value is None. Normally there is no need for
  860. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  861. Returns:
  862. Tensor: results of norm operation on the specified axis of input tensor,
  863. it's data type is the same as input's Tensor.
  864. Examples:
  865. .. code-block:: python
  866. >>> import paddle
  867. >>> x = paddle.arange(24, dtype="float32").reshape([2, 3, 4]) - 12
  868. >>> print(x)
  869. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  870. [[[-12., -11., -10., -9. ],
  871. [-8. , -7. , -6. , -5. ],
  872. [-4. , -3. , -2. , -1. ]],
  873. [[ 0. , 1. , 2. , 3. ],
  874. [ 4. , 5. , 6. , 7. ],
  875. [ 8. , 9. , 10., 11.]]])
  876. >>> # compute frobenius norm along last two dimensions.
  877. >>> out_fro = paddle.linalg.norm(x, p='fro', axis=[0,1])
  878. >>> print(out_fro)
  879. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  880. [17.43559647, 16.91153526, 16.73320007, 16.91153526])
  881. >>> # compute 2-order vector norm along last dimension.
  882. >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=-1)
  883. >>> print(out_pnorm)
  884. Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  885. [[21.11871147, 13.19090557, 5.47722578 ],
  886. [3.74165750 , 11.22497177, 19.13112640]])
  887. >>> # compute 2-order norm along [0,1] dimension.
  888. >>> out_pnorm = paddle.linalg.norm(x, p=2, axis=[0,1])
  889. >>> print(out_pnorm)
  890. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=True,
  891. [15.75857544, 14.97978878, 14.69693947, 14.97978973])
  892. >>> # compute inf-order norm
  893. >>> out_pnorm = paddle.linalg.norm(x, p=float("inf"))
  894. >>> print(out_pnorm)
  895. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  896. 12.)
  897. >>> out_pnorm = paddle.linalg.norm(x, p=float("inf"), axis=0)
  898. >>> print(out_pnorm)
  899. Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  900. [[12., 11., 10., 9. ],
  901. [8. , 7. , 6. , 7. ],
  902. [8. , 9. , 10., 11.]])
  903. >>> # compute -inf-order norm
  904. >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf"))
  905. >>> print(out_pnorm)
  906. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  907. 0.)
  908. >>> out_pnorm = paddle.linalg.norm(x, p=-float("inf"), axis=0)
  909. >>> print(out_pnorm)
  910. Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  911. [[0., 1., 2., 3.],
  912. [4., 5., 6., 5.],
  913. [4., 3., 2., 1.]])
  914. """
  915. if isinstance(axis, tuple):
  916. axis = list(axis)
  917. elif isinstance(axis, list) and len(axis) == 1:
  918. axis = axis[0]
  919. # calculate vector norm, where axis is None, int or list with only one integer
  920. if axis is None or (isinstance(axis, int)):
  921. # 'fro' is used to adapt previous usage
  922. if p is None or p == 'fro':
  923. p = 2.0
  924. if isinstance(p, (int, float)):
  925. return vector_norm(
  926. x,
  927. p=p,
  928. axis=axis,
  929. keepdim=keepdim,
  930. name=name,
  931. )
  932. else:
  933. raise ValueError(
  934. f"only valid p type is int or float for vector_norm, found {type(p)} and{p}"
  935. )
  936. # calculate matrix norm, where axis is list with two integers
  937. elif isinstance(axis, list) and len(axis) == 2:
  938. if p is None:
  939. p = 'fro'
  940. return matrix_norm(x=x, p=p, axis=axis, keepdim=keepdim, name=name)
  941. else:
  942. raise ValueError(
  943. f"except axis type int or list (length of list <=2), found {axis}"
  944. )
  945. def dist(x, y, p=2, name=None):
  946. r"""
  947. Returns the p-norm of (x - y). It is not a norm in a strict sense, only as a measure
  948. of distance. The shapes of x and y must be broadcastable. The definition is as follows, for
  949. details, please refer to the `Introduction to Tensor <../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor>`_:
  950. - Each input has at least one dimension.
  951. - Match the two input dimensions from back to front, the dimension sizes must either be equal, one of them is 1, or one of them does not exist.
  952. Where, z = x - y, the shapes of x and y are broadcastable, then the shape of z can be
  953. obtained as follows:
  954. 1. If the number of dimensions of x and y are not equal, prepend 1 to the dimensions of the
  955. tensor with fewer dimensions.
  956. For example, The shape of x is [8, 1, 6, 1], the shape of y is [7, 1, 5], prepend 1 to the
  957. dimension of y.
  958. x (4-D Tensor): 8 x 1 x 6 x 1
  959. y (4-D Tensor): 1 x 7 x 1 x 5
  960. 2. Determine the size of each dimension of the output z: choose the maximum value from the
  961. two input dimensions.
  962. z (4-D Tensor): 8 x 7 x 6 x 5
  963. If the number of dimensions of the two inputs are the same, the size of the output can be
  964. directly determined in step 2. When p takes different values, the norm formula is as follows:
  965. When p = 0, defining $0^0=0$, the zero-norm of z is simply the number of non-zero elements of z.
  966. .. math::
  967. ||z||_{0}=\lim_{p \\rightarrow 0}\sum_{i=1}^{m}|z_i|^{p}
  968. When p = inf, the inf-norm of z is the maximum element of the absolute value of z.
  969. .. math::
  970. ||z||_\infty=\max_i |z_i|
  971. When p = -inf, the negative-inf-norm of z is the minimum element of the absolute value of z.
  972. .. math::
  973. ||z||_{-\infty}=\min_i |z_i|
  974. Otherwise, the p-norm of z follows the formula,
  975. .. math::
  976. ||z||_{p}=(\sum_{i=1}^{m}|z_i|^p)^{\\frac{1}{p}}
  977. Args:
  978. x (Tensor): 1-D to 6-D Tensor, its data type is bfloat16, float16, float32 or float64.
  979. y (Tensor): 1-D to 6-D Tensor, its data type is bfloat16, float16, float32 or float64.
  980. p (float, optional): The norm to be computed, its data type is float32 or float64. Default: 2.
  981. name (str, optional): The default value is `None`. Normally there is no need for
  982. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  983. Returns:
  984. Tensor: Tensor that is the p-norm of (x - y).
  985. Examples:
  986. .. code-block:: python
  987. >>> import paddle
  988. >>> x = paddle.to_tensor([[3, 3],[3, 3]], dtype="float32")
  989. >>> y = paddle.to_tensor([[3, 3],[3, 1]], dtype="float32")
  990. >>> out = paddle.dist(x, y, 0)
  991. >>> print(out)
  992. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  993. 1.)
  994. >>> out = paddle.dist(x, y, 2)
  995. >>> print(out)
  996. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  997. 2.)
  998. >>> out = paddle.dist(x, y, float("inf"))
  999. >>> print(out)
  1000. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1001. 2.)
  1002. >>> out = paddle.dist(x, y, float("-inf"))
  1003. >>> print(out)
  1004. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1005. 0.)
  1006. """
  1007. if in_dynamic_or_pir_mode():
  1008. return _C_ops.dist(x, y, p)
  1009. check_variable_and_dtype(
  1010. x, 'dtype', ['bfloat16', 'float16', 'float32', 'float64'], 'dist'
  1011. )
  1012. check_variable_and_dtype(
  1013. y, 'dtype', ['bfloat16', 'float16', 'float32', 'float64'], 'dist'
  1014. )
  1015. check_type(p, 'p', (float, int), 'dist')
  1016. helper = LayerHelper("dist", **locals())
  1017. out = helper.create_variable_for_type_inference(x.dtype)
  1018. inputs = {"X": [x], "Y": [y]}
  1019. outputs = {'Out': [out]}
  1020. attrs = {"p": float(p)}
  1021. helper.append_op(
  1022. type='dist', inputs=inputs, outputs={'Out': out}, attrs=attrs
  1023. )
  1024. return out
  1025. def cond(x, p=None, name=None):
  1026. """
  1027. Computes the condition number of a matrix or batches of matrices with respect to a matrix norm ``p``.
  1028. Args:
  1029. x (Tensor): The input tensor could be tensor of shape ``(*, m, n)`` where ``*`` is zero or more batch dimensions
  1030. for ``p`` in ``(2, -2)``, or of shape ``(*, n, n)`` where every matrix is invertible for any supported ``p``.
  1031. And the input data type could be ``float32`` or ``float64``.
  1032. p (float|string, optional): Order of the norm. Supported values are `fro`, `nuc`, `1`, `-1`, `2`, `-2`,
  1033. `inf`, `-inf`. Default value is `None`, meaning that the order of the norm is `2`.
  1034. name (str, optional): The default value is `None`. Normally there is no need for
  1035. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1036. Returns:
  1037. Tensor: computing results of condition number, its data type is the same as input Tensor ``x``.
  1038. Examples:
  1039. .. code-block:: python
  1040. >>> import paddle
  1041. >>> paddle.seed(2023)
  1042. >>> x = paddle.to_tensor([[1., 0, -1], [0, 1, 0], [1, 0, 1]])
  1043. >>> # compute conditional number when p is None
  1044. >>> out = paddle.linalg.cond(x)
  1045. >>> print(out)
  1046. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1047. 1.41421378)
  1048. >>> # compute conditional number when order of the norm is 'fro'
  1049. >>> out_fro = paddle.linalg.cond(x, p='fro')
  1050. >>> print(out_fro)
  1051. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1052. 3.16227770)
  1053. >>> # compute conditional number when order of the norm is 'nuc'
  1054. >>> out_nuc = paddle.linalg.cond(x, p='nuc')
  1055. >>> print(out_nuc)
  1056. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1057. 9.24264145)
  1058. >>> # compute conditional number when order of the norm is 1
  1059. >>> out_1 = paddle.linalg.cond(x, p=1)
  1060. >>> print(out_1)
  1061. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1062. 2.)
  1063. >>> # compute conditional number when order of the norm is -1
  1064. >>> out_minus_1 = paddle.linalg.cond(x, p=-1)
  1065. >>> print(out_minus_1)
  1066. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1067. 1.)
  1068. >>> # compute conditional number when order of the norm is 2
  1069. >>> out_2 = paddle.linalg.cond(x, p=2)
  1070. >>> print(out_2)
  1071. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1072. 1.41421378)
  1073. >>> # compute conditional number when order of the norm is -1
  1074. >>> out_minus_2 = paddle.linalg.cond(x, p=-2)
  1075. >>> print(out_minus_2)
  1076. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1077. 0.70710671)
  1078. >>> # compute conditional number when order of the norm is inf
  1079. >>> out_inf = paddle.linalg.cond(x, p=float("inf"))
  1080. >>> print(out_inf)
  1081. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1082. 2.)
  1083. >>> # compute conditional number when order of the norm is -inf
  1084. >>> out_minus_inf = paddle.linalg.cond(x, p=-float("inf"))
  1085. >>> print(out_minus_inf)
  1086. Tensor(shape=[], dtype=float32, place=Place(cpu), stop_gradient=True,
  1087. 1.)
  1088. >>> a = paddle.randn([2, 4, 4])
  1089. >>> print(a)
  1090. Tensor(shape=[2, 4, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1091. [[[ 0.06132207, 1.11349595, 0.41906244, -0.24858207],
  1092. [-1.85169315, -1.50370061, 1.73954511, 0.13331604],
  1093. [ 1.66359663, -0.55764782, -0.59911072, -0.57773495],
  1094. [-1.03176904, -0.33741450, -0.29695082, -1.50258386]],
  1095. [[ 0.67233968, -1.07747352, 0.80170447, -0.06695852],
  1096. [-1.85003340, -0.23008066, 0.65083790, 0.75387722],
  1097. [ 0.61212337, -0.52664012, 0.19209868, -0.18707706],
  1098. [-0.00711021, 0.35236868, -0.40404350, 1.28656745]]])
  1099. >>> a_cond_fro = paddle.linalg.cond(a, p='fro')
  1100. >>> print(a_cond_fro)
  1101. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  1102. [6.37173700 , 35.15114594])
  1103. >>> b = paddle.randn([2, 3, 4])
  1104. >>> print(b)
  1105. Tensor(shape=[2, 3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1106. [[[ 0.03306439, 0.70149767, 0.77064633, -0.55978841],
  1107. [-0.84461296, 0.99335045, -1.23486686, 0.59551388],
  1108. [-0.63035583, -0.98797107, 0.09410731, 0.47007179]],
  1109. [[ 0.85850012, -0.98949534, -1.63086998, 1.07340240],
  1110. [-0.05492965, 1.04750168, -2.33754158, 1.16518629],
  1111. [ 0.66847134, -1.05326962, -0.05703246, -0.48190674]]])
  1112. >>> b_cond_2 = paddle.linalg.cond(b, p=2)
  1113. >>> print(b_cond_2)
  1114. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  1115. [2.86566353, 6.85834455])
  1116. """
  1117. def mat_norm(input, porder=1.0, axis=None):
  1118. """
  1119. NOTE:
  1120. Calculate the matrix norm of a square matrix or batches of square matrices,
  1121. when porder is in (1, -1, inf, -inf)
  1122. """
  1123. if in_dynamic_or_pir_mode():
  1124. abs_out = _C_ops.abs(input)
  1125. sum_out = _C_ops.sum(abs_out, axis, None, False)
  1126. if porder == 1 or porder == np.inf:
  1127. return _C_ops.max(sum_out, [-1], False)
  1128. if porder == -1 or porder == -np.inf:
  1129. return _C_ops.min(sum_out, [-1], False)
  1130. else:
  1131. block = LayerHelper('norm', **locals())
  1132. abs_out = block.create_variable_for_type_inference(
  1133. dtype=block.input_dtype()
  1134. )
  1135. sum_out = block.create_variable_for_type_inference(
  1136. dtype=block.input_dtype()
  1137. )
  1138. out = block.create_variable_for_type_inference(
  1139. dtype=block.input_dtype()
  1140. )
  1141. block.append_op(
  1142. type='abs', inputs={'X': input}, outputs={'Out': abs_out}
  1143. )
  1144. reduce_all, axis = _get_reduce_axis(axis, x)
  1145. block.append_op(
  1146. type='reduce_sum',
  1147. inputs={'X': abs_out},
  1148. outputs={'Out': sum_out},
  1149. attrs={
  1150. 'dim': axis,
  1151. 'keep_dim': False,
  1152. 'reduce_all': reduce_all,
  1153. },
  1154. )
  1155. if porder == 1 or porder == np.inf:
  1156. block.append_op(
  1157. type='reduce_max',
  1158. inputs={'X': sum_out},
  1159. outputs={'Out': out},
  1160. attrs={
  1161. 'dim': [-1],
  1162. 'keep_dim': False,
  1163. 'reduce_all': reduce_all,
  1164. },
  1165. )
  1166. if porder == -1 or porder == -np.inf:
  1167. block.append_op(
  1168. type='reduce_min',
  1169. inputs={'X': sum_out},
  1170. outputs={'Out': out},
  1171. attrs={
  1172. 'dim': [-1],
  1173. 'keep_dim': False,
  1174. 'reduce_all': reduce_all,
  1175. },
  1176. )
  1177. return out
  1178. def fro_norm(input, porder=2, axis=[-1]):
  1179. """
  1180. NOTE:
  1181. Calculate the frobenius norm of a square matrix or batches of square matrices.
  1182. """
  1183. if in_dynamic_or_pir_mode():
  1184. pow_out = _C_ops.pow(input, porder)
  1185. sum_out_1 = _C_ops.sum(pow_out, axis, None, False)
  1186. sum_out_2 = _C_ops.sum(sum_out_1, axis, None, False)
  1187. return _C_ops.pow(sum_out_2, float(1.0 / porder))
  1188. else:
  1189. block = LayerHelper('norm', **locals())
  1190. pow_out = block.create_variable_for_type_inference(
  1191. dtype=block.input_dtype()
  1192. )
  1193. sum_out_1 = block.create_variable_for_type_inference(
  1194. dtype=block.input_dtype()
  1195. )
  1196. sum_out_2 = block.create_variable_for_type_inference(
  1197. dtype=block.input_dtype()
  1198. )
  1199. out = block.create_variable_for_type_inference(
  1200. dtype=block.input_dtype()
  1201. )
  1202. block.append_op(
  1203. type='pow',
  1204. inputs={'X': input},
  1205. outputs={'Out': pow_out},
  1206. attrs={'factor': porder},
  1207. )
  1208. reduce_all, axis = _get_reduce_axis(axis, x)
  1209. block.append_op(
  1210. type='reduce_sum',
  1211. inputs={'X': pow_out},
  1212. outputs={'Out': sum_out_1},
  1213. attrs={
  1214. 'dim': axis,
  1215. 'keep_dim': False,
  1216. 'reduce_all': reduce_all,
  1217. },
  1218. )
  1219. block.append_op(
  1220. type='reduce_sum',
  1221. inputs={'X': sum_out_1},
  1222. outputs={'Out': sum_out_2},
  1223. attrs={
  1224. 'dim': axis,
  1225. 'keep_dim': False,
  1226. 'reduce_all': reduce_all,
  1227. },
  1228. )
  1229. block.append_op(
  1230. type='pow',
  1231. inputs={'X': sum_out_2},
  1232. outputs={'Out': out},
  1233. attrs={'factor': float(1.0 / porder)},
  1234. )
  1235. return out
  1236. def svd_norm(input, porder, axis=[-1]):
  1237. """
  1238. NOTE:
  1239. Calculate the matrix norm, which is related to singular values, of a matrix
  1240. or batches of matrices, including nuclear norm, 2-norm and (-2)-norm.
  1241. """
  1242. u, s, vh = svd(input, full_matrices=False)
  1243. if in_dynamic_or_pir_mode():
  1244. if porder == "nuc":
  1245. return _C_ops.sum(s, axis, None, False)
  1246. max_out = _C_ops.max(s, axis, False)
  1247. min_out = _C_ops.min(s, axis, False)
  1248. if porder == 2:
  1249. return _C_ops.divide(max_out, min_out)
  1250. if porder == -2:
  1251. return _C_ops.divide(min_out, max_out)
  1252. else:
  1253. reduce_all, axis = _get_reduce_axis(axis, x)
  1254. block = LayerHelper('norm', **locals())
  1255. out = block.create_variable_for_type_inference(
  1256. dtype=block.input_dtype()
  1257. )
  1258. if porder == "nuc":
  1259. block.append_op(
  1260. type='reduce_sum',
  1261. inputs={'X': s},
  1262. outputs={'Out': out},
  1263. attrs={
  1264. 'dim': axis,
  1265. 'keep_dim': False,
  1266. 'reduce_all': reduce_all,
  1267. },
  1268. )
  1269. return out
  1270. max_out = block.create_variable_for_type_inference(
  1271. dtype=block.input_dtype()
  1272. )
  1273. min_out = block.create_variable_for_type_inference(
  1274. dtype=block.input_dtype()
  1275. )
  1276. block.append_op(
  1277. type='reduce_max',
  1278. inputs={'X': s},
  1279. outputs={'Out': max_out},
  1280. attrs={
  1281. 'dim': axis,
  1282. 'keep_dim': False,
  1283. 'reduce_all': reduce_all,
  1284. },
  1285. )
  1286. block.append_op(
  1287. type='reduce_min',
  1288. inputs={'X': s},
  1289. outputs={'Out': min_out},
  1290. attrs={
  1291. 'dim': axis,
  1292. 'keep_dim': False,
  1293. 'reduce_all': reduce_all,
  1294. },
  1295. )
  1296. if porder == 2:
  1297. block.append_op(
  1298. type='elementwise_div',
  1299. inputs={'X': max_out, 'Y': min_out},
  1300. outputs={'Out': out},
  1301. attrs={'axis': -1},
  1302. )
  1303. return out
  1304. if porder == -2:
  1305. block.append_op(
  1306. type='elementwise_div',
  1307. inputs={'X': min_out, 'Y': max_out},
  1308. outputs={'Out': out},
  1309. attrs={'axis': -1},
  1310. )
  1311. return out
  1312. def empty_tensor(input, shape):
  1313. if in_dynamic_or_pir_mode():
  1314. return input.reshape(shape)
  1315. raise ValueError(
  1316. "only support x is nonempty tensor in static graph mode"
  1317. )
  1318. x_shape = list(x.shape)
  1319. if not len(x_shape) >= 2:
  1320. raise ValueError(
  1321. "input should be a matrix or batches of matrices, "
  1322. + f"but the dimension of received input is {len(x_shape)}"
  1323. )
  1324. if p is None:
  1325. p = 2
  1326. x_size = 0 if (0 in x_shape) else 1
  1327. if p in ("fro", "nuc", 1, -1, np.inf, -np.inf):
  1328. if x_shape[len(x_shape) - 1] == x_shape[len(x_shape) - 2]:
  1329. if x_size == 0:
  1330. return empty_tensor(x, x_shape[:-2])
  1331. x_inv = x.inverse()
  1332. if p == "fro":
  1333. return fro_norm(x) * fro_norm(x_inv)
  1334. if p == "nuc":
  1335. return svd_norm(x, p) * svd_norm(x_inv, p)
  1336. if p in (1, -1):
  1337. return mat_norm(x, porder=p, axis=[-2]) * mat_norm(
  1338. x_inv, porder=p, axis=[-2]
  1339. )
  1340. if p in (np.inf, -np.inf):
  1341. return mat_norm(x, porder=p, axis=[-1]) * mat_norm(
  1342. x_inv, porder=p, axis=[-1]
  1343. )
  1344. else:
  1345. raise ValueError(
  1346. f"only support p is {p} when input is a "
  1347. + "square matrix or batches of square matrices"
  1348. )
  1349. elif p in (2, -2):
  1350. if x_size == 0:
  1351. return empty_tensor(x, x_shape[:-2])
  1352. return svd_norm(x, porder=p)
  1353. else:
  1354. raise ValueError(
  1355. f"unsupported {p} for p, only supporting ('fro', 'nuc', "
  1356. + "1, -1, 2, -2, inf, -inf) or none"
  1357. )
  1358. def dot(x, y, name=None):
  1359. """
  1360. This operator calculates inner product for vectors.
  1361. Note:
  1362. Support 1-d and 2-d Tensor. When it is 2d, the first dimension of this matrix
  1363. is the batch dimension, which means that the vectors of multiple batches are dotted.
  1364. Parameters:
  1365. x(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128``
  1366. y(Tensor): 1-D or 2-D ``Tensor``. Its dtype should be ``float32``, ``float64``, ``int32``, ``int64``, ``complex64``, ``complex128``
  1367. name(str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name`
  1368. Returns:
  1369. Tensor: the calculated result Tensor.
  1370. Examples:
  1371. .. code-block:: python
  1372. >>> import paddle
  1373. >>> # 1-D Tensor * 1-D Tensor
  1374. >>> x = paddle.to_tensor([1, 2, 3])
  1375. >>> y = paddle.to_tensor([4, 5, 6])
  1376. >>> z = paddle.dot(x, y)
  1377. >>> print(z)
  1378. Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
  1379. 32)
  1380. >>> # 2-D Tensor * 2-D Tensor
  1381. >>> x = paddle.to_tensor([[1, 2, 3], [2, 4, 6]])
  1382. >>> y = paddle.to_tensor([[4, 5, 6], [4, 5, 6]])
  1383. >>> z = paddle.dot(x, y)
  1384. >>> print(z)
  1385. Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
  1386. [32, 64])
  1387. """
  1388. if in_dynamic_or_pir_mode():
  1389. return _C_ops.dot(x, y)
  1390. else:
  1391. op_type = 'dot'
  1392. assert x is not None, f'x cannot be None in {op_type}'
  1393. assert y is not None, f'y cannot be None in {op_type}'
  1394. check_variable_and_dtype(
  1395. x,
  1396. 'x',
  1397. [
  1398. 'float16',
  1399. 'uint16',
  1400. 'float32',
  1401. 'float64',
  1402. 'int32',
  1403. 'int64',
  1404. 'complex64',
  1405. 'complex128',
  1406. ],
  1407. op_type,
  1408. )
  1409. check_variable_and_dtype(
  1410. y,
  1411. 'y',
  1412. [
  1413. 'float16',
  1414. 'uint16',
  1415. 'float32',
  1416. 'float64',
  1417. 'int32',
  1418. 'int64',
  1419. 'complex64',
  1420. 'complex128',
  1421. ],
  1422. op_type,
  1423. )
  1424. helper = LayerHelper(op_type, **locals())
  1425. if name is None:
  1426. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  1427. else:
  1428. out = helper.create_variable(
  1429. name=name, dtype=x.dtype, persistable=False
  1430. )
  1431. helper.append_op(
  1432. type="dot", inputs={'X': x, 'Y': y}, attrs={}, outputs={"Out": out}
  1433. )
  1434. return out
  1435. def cov(x, rowvar=True, ddof=True, fweights=None, aweights=None, name=None):
  1436. """
  1437. Estimate the covariance matrix of the input variables, given data and weights.
  1438. A covariance matrix is a square matrix, indicate the covariance of each pair variables in the input matrix.
  1439. For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the covariance matrix
  1440. element Cij is the covariance of xi and xj. The element Cii is the variance of xi itself.
  1441. Parameters:
  1442. x (Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below.
  1443. rowvar (Bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True.
  1444. ddof (Bool, optional): If ddof=True will return the unbiased estimate, and ddof=False will return the simple average. Default: True.
  1445. fweights (Tensor, optional): 1-D Tensor of integer frequency weights; The number of times each observation vector should be repeated. Default: None.
  1446. aweights (Tensor, optional): 1-D Tensor of observation vector weights. How important of the observation vector, larger data means this element is more important. Default: None.
  1447. name (str, optional): Name of the output. Default is None. It's used to print debug info for developers. Details: :ref:`api_guide_Name` .
  1448. Returns:
  1449. Tensor: The covariance matrix Tensor of the variables.
  1450. Examples:
  1451. .. code-block:: python
  1452. >>> import paddle
  1453. >>> paddle.seed(2023)
  1454. >>> xt = paddle.rand((3, 4))
  1455. >>> paddle.linalg.cov(xt)
  1456. >>> print(xt)
  1457. Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1458. [[0.86583614, 0.52014720, 0.25960937, 0.90525323],
  1459. [0.42400089, 0.40641287, 0.97020894, 0.74437362],
  1460. [0.51785129, 0.73292869, 0.97786582, 0.04315904]])
  1461. """
  1462. op_type = 'cov'
  1463. if len(x.shape) > 2 or len(x.shape) < 1:
  1464. raise ValueError(
  1465. "Input(x) only support N-D (1<=N<=2) tensor in cov, but received "
  1466. "length of Input(input) is %s." % len(x.shape)
  1467. )
  1468. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'cov')
  1469. nx = x
  1470. if len(x.shape) == 1:
  1471. nx = x.reshape((1, -1))
  1472. if not rowvar and nx.shape[0] != 1:
  1473. nx = nx.t()
  1474. w = None
  1475. observation_num = nx.shape[1]
  1476. if fweights is not None:
  1477. w = fweights.astype(nx.dtype)
  1478. if len(w.shape) > 1:
  1479. raise ValueError(
  1480. "Input(fweights) only support N-D (N<=1) tensor in cov, but received "
  1481. "shape of Input(input) is %s." % len(fweights.shape)
  1482. )
  1483. if fweights.shape[0] != observation_num:
  1484. raise ValueError(
  1485. f"The number of Input(fweights) should equal to x's dim[1]: {observation_num}, but received "
  1486. f"size of Input(fweights) is {fweights.shape[0]}."
  1487. )
  1488. if fweights.min() < 0:
  1489. raise ValueError(
  1490. "The value of Input(fweights) cannot be negative, but received "
  1491. f"min of Input(fweights) is {fweights.min()}."
  1492. )
  1493. if not paddle.all(
  1494. fweights
  1495. == paddle.round(fweights.astype('float64')).astype(fweights.dtype)
  1496. ):
  1497. raise ValueError("Input(fweights) must be integer ")
  1498. if aweights is not None:
  1499. aw = aweights.astype(nx.dtype)
  1500. if len(aw.shape) > 1:
  1501. raise ValueError(
  1502. "Input(aweights) only support N-D (N<=1) tensor in cov, but received "
  1503. "length of Input(input) is %s." % len(aweights.shape)
  1504. )
  1505. check_variable_and_dtype(
  1506. aweights, 'dtype', ['float32', 'float64'], 'cov'
  1507. )
  1508. if aweights.shape[0] != observation_num:
  1509. raise ValueError(
  1510. f"The number of Input(aweights) should equal to x's dim[1]: {observation_num}, but received "
  1511. f"size of Input(aweights) is {aweights.shape[0]}."
  1512. )
  1513. if aweights.min() < 0:
  1514. raise ValueError(
  1515. "The value of Input(aweights) cannot be negative, but received "
  1516. f"min of Input(aweights) is {aweights.min()}."
  1517. )
  1518. if w is not None:
  1519. w = w * aw
  1520. else:
  1521. w = aw
  1522. w_sum = paddle.to_tensor(observation_num, dtype=nx.dtype)
  1523. if fweights is not None or aweights is not None:
  1524. w_sum = w.sum()
  1525. if w_sum.item() == 0:
  1526. raise ValueError("The sum of weights is zero, can't be normalized.")
  1527. if w is not None:
  1528. nx_w = nx * w
  1529. avg = (nx_w).sum(axis=1) / w_sum
  1530. else:
  1531. avg = nx.sum(axis=1) / w_sum
  1532. nx_w = nx
  1533. if w is not None and aweights is not None and ddof:
  1534. norm_factor = w_sum - (w * aweights.astype(w.dtype)).sum() / w_sum
  1535. else:
  1536. norm_factor = w_sum - ddof
  1537. norm_factor = paddle.clip(norm_factor, min=0)
  1538. nx = nx - avg.unsqueeze(1)
  1539. xxt = paddle.mm(nx, nx_w.t().conj())
  1540. cov = paddle.divide(xxt, norm_factor).squeeze()
  1541. return cov
  1542. def t(input, name=None):
  1543. """
  1544. Transpose <=2-D tensor.
  1545. 0-D and 1-D tensors are returned as it is and 2-D tensor is equal to
  1546. the paddle.transpose function which perm dimensions set 0 and 1.
  1547. Args:
  1548. input (Tensor): The input Tensor. It is a N-D (N<=2) Tensor of data types float32, float64, int32, int64.
  1549. name (str, optional): The default value is None. Normally there is no need for
  1550. user to set this property. For more information, please refer to :ref:`api_guide_Name` .
  1551. Returns:
  1552. Tensor: A transposed n-D Tensor, with data type being float16, float32, float64, int32, int64.
  1553. Examples:
  1554. .. code-block:: python
  1555. :name: code-example
  1556. >>> import paddle
  1557. >>> # Example 1 (0-D tensor)
  1558. >>> x = paddle.to_tensor([0.79])
  1559. >>> out = paddle.t(x)
  1560. >>> print(out)
  1561. Tensor(shape=[1], dtype=float32, place=Place(cpu), stop_gradient=True,
  1562. [0.79000002])
  1563. >>> # Example 2 (1-D tensor)
  1564. >>> x = paddle.to_tensor([0.79, 0.84, 0.32])
  1565. >>> out2 = paddle.t(x)
  1566. >>> print(out2)
  1567. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  1568. [0.79000002, 0.83999997, 0.31999999])
  1569. >>> print(paddle.t(x).shape)
  1570. [3]
  1571. >>> # Example 3 (2-D tensor)
  1572. >>> x = paddle.to_tensor([[0.79, 0.84, 0.32],
  1573. ... [0.64, 0.14, 0.57]])
  1574. >>> print(x.shape)
  1575. [2, 3]
  1576. >>> out3 = paddle.t(x)
  1577. >>> print(out3)
  1578. Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  1579. [[0.79000002, 0.63999999],
  1580. [0.83999997, 0.14000000],
  1581. [0.31999999, 0.56999999]])
  1582. >>> print(paddle.t(x).shape)
  1583. [3, 2]
  1584. """
  1585. if len(input.shape) > 2:
  1586. raise ValueError(
  1587. "Input(input) only support N-D (N<=2) tensor, but received "
  1588. "length of Input(input) is %s. Perhaps you can use paddle."
  1589. "tensor.transpose() instead." % len(input.shape)
  1590. )
  1591. if in_dynamic_or_pir_mode():
  1592. if len(input.shape) <= 1:
  1593. return input
  1594. # 2-D tensor
  1595. perm = [1, 0]
  1596. out = _C_ops.transpose(input, perm)
  1597. return out
  1598. else:
  1599. check_variable_and_dtype(
  1600. input,
  1601. 'input',
  1602. ['float16', 'float32', 'float64', 'int32', 'int64', 'uint16'],
  1603. 'transpose',
  1604. )
  1605. helper = LayerHelper('t', **locals())
  1606. out = helper.create_variable_for_type_inference(input.dtype)
  1607. input_shape = helper.create_variable_for_type_inference(input.dtype)
  1608. if len(input.shape) <= 1:
  1609. out = input
  1610. else:
  1611. helper.append_op(
  1612. type='transpose2',
  1613. inputs={'X': [input]},
  1614. outputs={'Out': [out], 'XShape': [input_shape]},
  1615. attrs={'axis': [1, 0]},
  1616. )
  1617. return out
  1618. @inplace_apis_in_dygraph_only
  1619. def t_(input, name=None):
  1620. r"""
  1621. Inplace version of ``t`` API, the output Tensor will be inplaced with input ``input``.
  1622. Please refer to :ref:`api_paddle_t`.
  1623. """
  1624. if len(input.shape) > 2:
  1625. raise ValueError(
  1626. "Input(input) only support N-D (N<=2) tensor, but received "
  1627. "length of Input(input) is %s. Perhaps you can use paddle."
  1628. "tensor.transpose() instead." % len(input.shape)
  1629. )
  1630. if in_dynamic_mode():
  1631. if len(input.shape) <= 1:
  1632. return input
  1633. # 2-D tensor
  1634. perm = [1, 0]
  1635. out = _C_ops.transpose_(input, perm)
  1636. return out
  1637. def cross(x, y, axis=9, name=None):
  1638. """
  1639. Computes the cross product between two tensors along an axis.
  1640. Inputs must have the same shape, and the length of their axes should be equal to 3.
  1641. If `axis` is not given, it defaults to the first axis found with the length 3.
  1642. Args:
  1643. x (Tensor): The first input tensor, the data type is float16, float32, float64, int32, int64, complex64, complex128.
  1644. y (Tensor): The second input tensor, the data type is float16, float32, float64, int32, int64, complex64, complex128.
  1645. axis (int, optional): The axis along which to compute the cross product. It defaults to be 9 which indicates using the first axis found with the length 3.
  1646. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  1647. Returns:
  1648. Tensor. A Tensor with same data type as `x`.
  1649. Examples:
  1650. .. code-block:: python
  1651. >>> import paddle
  1652. >>> x = paddle.to_tensor([[1.0, 1.0, 1.0],
  1653. ... [2.0, 2.0, 2.0],
  1654. ... [3.0, 3.0, 3.0]])
  1655. >>> y = paddle.to_tensor([[1.0, 1.0, 1.0],
  1656. ... [1.0, 1.0, 1.0],
  1657. ... [1.0, 1.0, 1.0]])
  1658. ...
  1659. >>> z1 = paddle.cross(x, y)
  1660. >>> print(z1)
  1661. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  1662. [[-1., -1., -1.],
  1663. [ 2., 2., 2.],
  1664. [-1., -1., -1.]])
  1665. >>> z2 = paddle.cross(x, y, axis=1)
  1666. >>> print(z2)
  1667. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  1668. [[0., 0., 0.],
  1669. [0., 0., 0.],
  1670. [0., 0., 0.]])
  1671. """
  1672. if in_dynamic_or_pir_mode():
  1673. axis = K_DEFAULT_DIM if axis is None else axis
  1674. return _C_ops.cross(x, y, axis)
  1675. else:
  1676. check_variable_and_dtype(
  1677. x,
  1678. 'x',
  1679. [
  1680. 'float16',
  1681. 'uint16',
  1682. 'float32',
  1683. 'float64',
  1684. "int32",
  1685. "int64",
  1686. "complex64",
  1687. "complex128",
  1688. ],
  1689. 'cross',
  1690. )
  1691. check_variable_and_dtype(
  1692. y,
  1693. 'y',
  1694. [
  1695. 'float16',
  1696. 'uint16',
  1697. 'float32',
  1698. 'float64',
  1699. "int32",
  1700. "int64",
  1701. "complex64",
  1702. "complex128",
  1703. ],
  1704. 'cross',
  1705. )
  1706. helper = LayerHelper("cross", **locals())
  1707. out = helper.create_variable_for_type_inference(x.dtype)
  1708. attrs = {}
  1709. attrs['dim'] = axis
  1710. helper.append_op(
  1711. type='cross',
  1712. inputs={'X': x, 'Y': y},
  1713. outputs={'Out': out},
  1714. attrs=attrs,
  1715. )
  1716. return out
  1717. def cholesky(x, upper=False, name=None):
  1718. r"""
  1719. Computes the Cholesky decomposition of one symmetric positive-definite
  1720. matrix or batches of symmetric positive-definite matrices.
  1721. If `upper` is `True`, the decomposition has the form :math:`A = U^{T}U` ,
  1722. and the returned matrix :math:`U` is upper-triangular. Otherwise, the
  1723. decomposition has the form :math:`A = LL^{T}` , and the returned matrix
  1724. :math:`L` is lower-triangular.
  1725. Args:
  1726. x (Tensor): The input tensor. Its shape should be `[*, M, M]`,
  1727. where * is zero or more batch dimensions, and matrices on the
  1728. inner-most 2 dimensions all should be symmetric positive-definite.
  1729. Its data type should be float32 or float64.
  1730. upper (bool, optional): The flag indicating whether to return upper or lower
  1731. triangular matrices. Default: False.
  1732. name (str, optional): Name for the operation (optional, default is None).
  1733. For more information, please refer to :ref:`api_guide_Name`.
  1734. Returns:
  1735. Tensor, A Tensor with same shape and data type as `x`. It represents
  1736. triangular matrices generated by Cholesky decomposition.
  1737. Examples:
  1738. .. code-block:: python
  1739. >>> import paddle
  1740. >>> paddle.seed(2023)
  1741. >>> a = paddle.rand([3, 3], dtype="float32")
  1742. >>> a_t = paddle.transpose(a, [1, 0])
  1743. >>> x = paddle.matmul(a, a_t) + 1e-03
  1744. >>> out = paddle.linalg.cholesky(x, upper=False)
  1745. >>> print(out)
  1746. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  1747. [[1.04337072, 0. , 0. ],
  1748. [1.06467664, 0.17859250, 0. ],
  1749. [1.30602181, 0.08326444, 0.22790681]])
  1750. """
  1751. if in_dynamic_or_pir_mode():
  1752. return _C_ops.cholesky(x, upper)
  1753. else:
  1754. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'cholesky')
  1755. check_type(upper, 'upper', bool, 'cholesky')
  1756. helper = LayerHelper('cholesky', **locals())
  1757. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  1758. helper.append_op(
  1759. type='cholesky',
  1760. inputs={'X': [x]},
  1761. outputs={'Out': out},
  1762. attrs={'upper': upper},
  1763. )
  1764. return out
  1765. def matrix_rank(x, tol=None, hermitian=False, name=None):
  1766. r"""
  1767. Computes the rank of a matrix.
  1768. The rank of a matrix is the number of singular values that are greater than the specified `tol` threshold when hermitian=False,
  1769. or the number of eigenvalues in absolute value that are greater than the specified `tol` threshold when hermitian=True.
  1770. Args:
  1771. x (Tensor): The input tensor. Its shape should be `[..., m, n]`, where `...` is zero or more batch dimensions. If `x` is a batch
  1772. of matrices then the output has the same batch dimensions. The data type of `x` should be float32 or float64.
  1773. tol (float|Tensor, optional): the tolerance value. If `tol` is not specified, and `sigma` is the largest singular value
  1774. (or eigenvalues in absolute value), and `eps` is the epsilon value for the dtype of `x`, then `tol` is computed with formula
  1775. `tol=sigma * max(m,n) * eps`. Note that if `x` is a batch of matrices, `tol` is computed this way for every batch. Default: None.
  1776. hermitian (bool, optional): indicates whether `x` is Hermitian. Default: False. When hermitian=True, `x` is assumed to be Hermitian,
  1777. enabling a more efficient method for finding eigenvalues, but `x` is not checked inside the function. Instead, We just use
  1778. the lower triangular of the matrix to compute. Default: False.
  1779. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  1780. Returns:
  1781. Tensor: Rank of tensor x.
  1782. Examples:
  1783. .. code-block:: python
  1784. >>> import paddle
  1785. >>> a = paddle.eye(10)
  1786. >>> b = paddle.linalg.matrix_rank(a)
  1787. >>> print(b)
  1788. Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
  1789. 10)
  1790. >>> c = paddle.ones(shape=[3, 4, 5, 5])
  1791. >>> d = paddle.linalg.matrix_rank(c, tol=0.01, hermitian=True)
  1792. >>> print(d)
  1793. Tensor(shape=[3, 4], dtype=int32, place=Place(cpu), stop_gradient=True,
  1794. [[1, 1, 1, 1],
  1795. [1, 1, 1, 1],
  1796. [1, 1, 1, 1]])
  1797. """
  1798. if in_dynamic_or_pir_mode():
  1799. if isinstance(tol, (Variable, paddle.pir.Value)):
  1800. if tol.dtype != x.dtype:
  1801. tol_tensor = cast(tol, x.dtype)
  1802. else:
  1803. tol_tensor = tol
  1804. use_default_tol = False
  1805. return _C_ops.matrix_rank_tol(
  1806. x, tol_tensor, use_default_tol, hermitian
  1807. )
  1808. if tol is None:
  1809. tol_attr = 0.0
  1810. use_default_tol = True
  1811. else:
  1812. tol_attr = float(tol)
  1813. use_default_tol = False
  1814. return _C_ops.matrix_rank(x, tol_attr, use_default_tol, hermitian)
  1815. else:
  1816. inputs = {}
  1817. attrs = {}
  1818. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'matrix_rank')
  1819. inputs['X'] = x
  1820. if tol is None:
  1821. attrs['use_default_tol'] = True
  1822. elif isinstance(tol, Variable):
  1823. attrs['use_default_tol'] = False
  1824. if tol.dtype != x.dtype:
  1825. inputs['TolTensor'] = cast(tol, x.dtype)
  1826. else:
  1827. inputs['TolTensor'] = tol
  1828. else:
  1829. check_type(tol, 'tol', float, 'matrix_rank')
  1830. attrs['use_default_tol'] = False
  1831. attrs['tol'] = tol
  1832. check_type(hermitian, 'hermitian', bool, 'matrix_rank')
  1833. attrs['hermitian'] = hermitian
  1834. helper = LayerHelper('matrix_rank', **locals())
  1835. out = helper.create_variable_for_type_inference(dtype='int32')
  1836. helper.append_op(
  1837. type='matrix_rank', inputs=inputs, outputs={'Out': out}, attrs=attrs
  1838. )
  1839. return out
  1840. def bmm(x, y, name=None):
  1841. """
  1842. Applies batched matrix multiplication to two tensors.
  1843. Both of the two input tensors must be three-dimensional and share the same batch size.
  1844. If x is a (b, m, k) tensor, y is a (b, k, n) tensor, the output will be a (b, m, n) tensor.
  1845. Args:
  1846. x (Tensor): The input Tensor.
  1847. y (Tensor): The input Tensor.
  1848. name (str|None): A name for this layer(optional). If set None, the layer
  1849. will be named automatically. Default: None.
  1850. Returns:
  1851. Tensor: The product Tensor.
  1852. Examples:
  1853. .. code-block:: python
  1854. >>> import paddle
  1855. >>> # In imperative mode:
  1856. >>> # size x: (2, 2, 3) and y: (2, 3, 2)
  1857. >>> x = paddle.to_tensor([[[1.0, 1.0, 1.0],
  1858. ... [2.0, 2.0, 2.0]],
  1859. ... [[3.0, 3.0, 3.0],
  1860. ... [4.0, 4.0, 4.0]]])
  1861. >>> y = paddle.to_tensor([[[1.0, 1.0],[2.0, 2.0],[3.0, 3.0]],
  1862. ... [[4.0, 4.0],[5.0, 5.0],[6.0, 6.0]]])
  1863. >>> out = paddle.bmm(x, y)
  1864. >>> print(out)
  1865. Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  1866. [[[6. , 6. ],
  1867. [12., 12.]],
  1868. [[45., 45.],
  1869. [60., 60.]]])
  1870. """
  1871. if in_dynamic_or_pir_mode():
  1872. return _C_ops.bmm(x, y)
  1873. else:
  1874. x_shape = x.shape
  1875. y_shape = y.shape
  1876. if not len(x_shape) == len(y_shape) == 3:
  1877. raise ValueError(
  1878. f"x and y should be 3-dimensional. But received x's dimension: {x_shape}, y's dimension: {y_shape}"
  1879. )
  1880. if x_shape[2] != -1 and y_shape[1] != -1 and x_shape[2] != y_shape[1]:
  1881. raise ValueError(
  1882. f"x's width must be equal with y's height. But received x's shape: {x_shape}, y's shape: {y_shape}"
  1883. )
  1884. if x_shape[0] != -1 and y_shape[0] != -1 and x_shape[0] != y_shape[0]:
  1885. raise ValueError(
  1886. f"x's batch (shape[0]) must be equal with y's batch (shape[0]). But received x's shape: {x_shape}, y's shape: {y_shape}"
  1887. )
  1888. helper = LayerHelper('bmm', **locals())
  1889. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  1890. helper.append_op(
  1891. type='bmm', inputs={'X': x, 'Y': y}, outputs={'Out': out}
  1892. )
  1893. return out
  1894. def histogram(input, bins=100, min=0, max=0, name=None):
  1895. """
  1896. Computes the histogram of a tensor. The elements are sorted into equal width bins between min and max.
  1897. If min and max are both zero, the minimum and maximum values of the data are used.
  1898. Args:
  1899. input (Tensor): A Tensor(or LoDTensor) with shape :math:`[N_1, N_2,..., N_k]` . The data type of the input Tensor
  1900. should be float32, float64, int32, int64.
  1901. bins (int, optional): number of histogram bins. Default: 100.
  1902. min (int, optional): lower end of the range (inclusive). Default: 0.
  1903. max (int, optional): upper end of the range (inclusive). Default: 0.
  1904. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  1905. Returns:
  1906. Tensor: data type is int64, shape is (nbins,).
  1907. Examples:
  1908. .. code-block:: python
  1909. >>> import paddle
  1910. >>> inputs = paddle.to_tensor([1, 2, 1])
  1911. >>> result = paddle.histogram(inputs, bins=4, min=0, max=3)
  1912. >>> print(result)
  1913. Tensor(shape=[4], dtype=int64, place=Place(cpu), stop_gradient=True,
  1914. [0, 2, 1, 0])
  1915. """
  1916. if in_dynamic_or_pir_mode():
  1917. return _C_ops.histogram(input, bins, min, max)
  1918. else:
  1919. helper = LayerHelper('histogram', **locals())
  1920. check_variable_and_dtype(
  1921. input, 'X', ['int32', 'int64', 'float32', 'float64'], 'histogram'
  1922. )
  1923. out = helper.create_variable_for_type_inference(VarDesc.VarType.INT64)
  1924. helper.append_op(
  1925. type='histogram',
  1926. inputs={'X': input},
  1927. outputs={'Out': out},
  1928. attrs={'bins': bins, 'min': min, 'max': max},
  1929. )
  1930. return out
  1931. def bincount(x, weights=None, minlength=0, name=None):
  1932. """
  1933. Computes frequency of each value in the input tensor.
  1934. Args:
  1935. x (Tensor): A Tensor with non-negative integer. Should be 1-D tensor.
  1936. weights (Tensor, optional): Weight for each value in the input tensor. Should have the same shape as input. Default is None.
  1937. minlength (int, optional): Minimum number of bins. Should be non-negative integer. Default is 0.
  1938. name (str, optional): Normally there is no need for user to set this property.
  1939. For more information, please refer to :ref:`api_guide_Name`. Default is None.
  1940. Returns:
  1941. Tensor: The tensor of frequency.
  1942. Examples:
  1943. .. code-block:: python
  1944. >>> import paddle
  1945. >>> x = paddle.to_tensor([1, 2, 1, 4, 5])
  1946. >>> result1 = paddle.bincount(x)
  1947. >>> print(result1)
  1948. Tensor(shape=[6], dtype=int64, place=Place(cpu), stop_gradient=True,
  1949. [0, 2, 1, 0, 1, 1])
  1950. >>> w = paddle.to_tensor([2.1, 0.4, 0.1, 0.5, 0.5])
  1951. >>> result2 = paddle.bincount(x, weights=w)
  1952. >>> print(result2)
  1953. Tensor(shape=[6], dtype=float32, place=Place(cpu), stop_gradient=True,
  1954. [0. , 2.19999981, 0.40000001, 0. , 0.50000000, 0.50000000])
  1955. """
  1956. if x.dtype not in [
  1957. paddle.int32,
  1958. paddle.int64,
  1959. DataType.INT32,
  1960. DataType.INT64,
  1961. ]:
  1962. raise TypeError("Elements in Input(x) should all be integers")
  1963. if in_dynamic_or_pir_mode():
  1964. return _C_ops.bincount(x, weights, minlength)
  1965. else:
  1966. helper = LayerHelper('bincount', **locals())
  1967. check_variable_and_dtype(x, 'X', ['int32', 'int64'], 'bincount')
  1968. if weights is not None:
  1969. check_variable_and_dtype(
  1970. weights,
  1971. 'Weights',
  1972. ['int32', 'int64', 'float32', 'float64'],
  1973. 'bincount',
  1974. )
  1975. out = helper.create_variable_for_type_inference(dtype=weights.dtype)
  1976. else:
  1977. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  1978. helper.append_op(
  1979. type='bincount',
  1980. inputs={'X': x, 'Weights': weights},
  1981. outputs={'Out': out},
  1982. attrs={'minlength': minlength},
  1983. )
  1984. return out
  1985. def mv(x, vec, name=None):
  1986. """
  1987. Performs a matrix-vector product of the matrix x and the vector vec.
  1988. Args:
  1989. x (Tensor): A tensor with shape :math:`[M, N]` , The data type of the input Tensor x
  1990. should be one of float32, float64.
  1991. vec (Tensor): A tensor with shape :math:`[N]` , The data type of the input Tensor x
  1992. should be one of float32, float64.
  1993. name (str, optional): Normally there is no need for user to set this property.
  1994. For more information, please refer to :ref:`api_guide_Name`. Default is None.
  1995. Returns:
  1996. Tensor: The tensor which is producted by x and vec.
  1997. Examples:
  1998. .. code-block:: python
  1999. >>> # x: [M, N], vec: [N]
  2000. >>> # paddle.mv(x, vec) # out: [M]
  2001. >>> import paddle
  2002. >>> x = paddle.to_tensor([[2, 1, 3], [3, 0, 1]]).astype("float64")
  2003. >>> vec = paddle.to_tensor([3, 5, 1]).astype("float64")
  2004. >>> out = paddle.mv(x, vec)
  2005. >>> print(out)
  2006. Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2007. [14., 10.])
  2008. """
  2009. if in_dynamic_or_pir_mode():
  2010. return _C_ops.mv(x, vec)
  2011. else:
  2012. def __check_input(x, vec):
  2013. var_names = {'x': x, 'vec': vec}
  2014. for name, val in var_names.items():
  2015. check_variable_and_dtype(
  2016. val, name, ['float32', 'float64'], 'mv'
  2017. )
  2018. x_shape = list(x.shape)
  2019. vec_shape = list(vec.shape)
  2020. if len(x_shape) != 2:
  2021. raise ValueError(
  2022. f"x should be 2-dimensional. But received x's dimension: {x_shape}"
  2023. )
  2024. if len(vec_shape) != 1:
  2025. raise ValueError(
  2026. f"vec should be 1-dimensional. But received vec's dimension: {vec_shape}"
  2027. )
  2028. __check_input(x, vec)
  2029. helper = LayerHelper('mv', **locals())
  2030. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  2031. helper.append_op(
  2032. type='mv', inputs={'X': x, 'Vec': vec}, outputs={'Out': out}
  2033. )
  2034. return out
  2035. def det(x, name=None):
  2036. """
  2037. Calculates determinant value of a square matrix or batches of square matrices.
  2038. Args:
  2039. x (Tensor): the input matrix of size `(n, n)` or the
  2040. batch of matrices of size `(*, n, n)` where `*` is one or more
  2041. batch dimensions.
  2042. name (str, optional): Name of the output.It's used to print debug info for
  2043. developers. Details: :ref:`api_guide_Name`. Default is None.
  2044. Returns:
  2045. Tensor, the determinant value of a square matrix or batches of square matrices.
  2046. Examples:
  2047. .. code-block:: python
  2048. >>> import paddle
  2049. >>> paddle.seed(2023)
  2050. >>> x = paddle.randn([3,3,3])
  2051. >>> A = paddle.linalg.det(x)
  2052. >>> print(A)
  2053. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  2054. [-1.29280925, 0.77832544, 0.89754158])
  2055. """
  2056. if in_dynamic_or_pir_mode():
  2057. return _C_ops.det(x)
  2058. else:
  2059. check_dtype(x.dtype, 'Input', ['float16', 'float32', 'float64'], 'det')
  2060. input_shape = list(x.shape)
  2061. assert len(input_shape) >= 2, (
  2062. "The x must be at least 2-dimensional, "
  2063. "but received Input x's dimensional: %s.\n" % len(input_shape)
  2064. )
  2065. assert input_shape[-1] == input_shape[-2], (
  2066. "Expect squared input,"
  2067. f"but received {input_shape[-2]} by {input_shape[-1]} matrix.\n"
  2068. )
  2069. helper = LayerHelper('determinant', **locals())
  2070. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  2071. helper.append_op(
  2072. type='determinant', inputs={'Input': [x]}, outputs={'Out': [out]}
  2073. )
  2074. return out
  2075. def slogdet(x, name=None):
  2076. """
  2077. Calculates the sign and natural logarithm of the absolute value of a square matrix's or batches square matrices' determinant.
  2078. The determinant can be computed with ``sign * exp`` (logabsdet).
  2079. Supports input of float, double.
  2080. Note that for matrices that have zero determinant, this returns ``(0, -inf)``.
  2081. Args:
  2082. x (Tensor): the batch of matrices of size :math:`(*, n, n)`
  2083. where math:`*` is one or more batch dimensions.
  2084. name (str, optional): Name of the output.It's used to print debug info for
  2085. developers. Details: :ref:`api_guide_Name`. Default is None.
  2086. Returns:
  2087. y (Tensor), A tensor containing the sign of the determinant and the natural logarithm
  2088. of the absolute value of determinant, respectively.
  2089. Examples:
  2090. .. code-block:: python
  2091. >>> import paddle
  2092. >>> paddle.seed(2023)
  2093. >>> x = paddle.randn([3, 3, 3])
  2094. >>> A = paddle.linalg.slogdet(x)
  2095. >>> print(A)
  2096. Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  2097. [[-1. , 1. , 1. ],
  2098. [ 0.25681755, -0.25061053, -0.10809582]])
  2099. """
  2100. if in_dynamic_or_pir_mode():
  2101. return _C_ops.slogdet(x)
  2102. else:
  2103. check_dtype(x.dtype, 'Input', ['float32', 'float64'], 'slogdet')
  2104. input_shape = list(x.shape)
  2105. assert len(input_shape) >= 2, (
  2106. "The x must be at least 2-dimensional, "
  2107. "but received Input x's dimensional: %s.\n" % len(input_shape)
  2108. )
  2109. assert input_shape[-1] == input_shape[-2], (
  2110. "Expect squared input,"
  2111. f"but received {input_shape[-2]} by {input_shape[-1]} matrix.\n"
  2112. )
  2113. helper = LayerHelper('slogdeterminant', **locals())
  2114. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  2115. helper.append_op(
  2116. type='slogdeterminant',
  2117. inputs={'Input': [x]},
  2118. outputs={'Out': [out]},
  2119. )
  2120. return out
  2121. def svd(x, full_matrices=False, name=None):
  2122. r"""
  2123. Computes the singular value decomposition of one matrix or a batch of regular matrices.
  2124. Let :math:`X` be the input matrix or a batch of input matrices, the output should satisfies:
  2125. .. math::
  2126. X = U * diag(S) * VT
  2127. Args:
  2128. x (Tensor): The input tensor. Its shape should be `[..., N, M]`,
  2129. where `...` is zero or more batch dimensions. N and M can be arbitrary
  2130. positive number. Note that if x is singular matrices, the grad is numerical
  2131. instable. The data type of x should be float32 or float64.
  2132. full_matrices (bool, optional): A flag to control the behavior of svd.
  2133. If full_matrices = True, svd op will compute full U and V matrices,
  2134. which means shape of U is `[..., N, N]`, shape of V is `[..., M, M]`. K = min(M, N).
  2135. If full_matrices = False, svd op will use a economic method to store U and V.
  2136. which means shape of U is `[..., N, K]`, shape of V is `[..., M, K]`. K = min(M, N).
  2137. Default value is False.
  2138. name (str, optional): Name for the operation. For more information,
  2139. please refer to :ref:`api_guide_Name`. Default value is None.
  2140. Returns:
  2141. - U (Tensor), is the singular value decomposition result U.
  2142. - S (Tensor), is the singular value decomposition result S.
  2143. - VH (Tensor), VH is the conjugate transpose of V, which is the singular value decomposition result V.
  2144. Tuple of 3 tensors(U, S, VH): VH is the conjugate transpose of V. S is the singular value vectors of matrices with shape `[..., K]`
  2145. Examples:
  2146. .. code-block:: python
  2147. >>> import paddle
  2148. >>> x = paddle.to_tensor([[1.0, 2.0], [1.0, 3.0], [4.0, 6.0]]).astype('float64')
  2149. >>> x = x.reshape([3, 2])
  2150. >>> u, s, vh = paddle.linalg.svd(x)
  2151. >>> print (u)
  2152. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2153. [[-0.27364809, -0.21695147],
  2154. [-0.37892198, -0.87112408],
  2155. [-0.88404460, 0.44053933]])
  2156. >>> print (s)
  2157. Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2158. [8.14753743, 0.78589688])
  2159. >>> print (vh)
  2160. Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2161. [[-0.51411221, -0.85772294],
  2162. [ 0.85772294, -0.51411221]])
  2163. >>> # one can verify : U * S * VT == X
  2164. >>> # U * UH == I
  2165. >>> # V * VH == I
  2166. """
  2167. if in_dynamic_or_pir_mode():
  2168. return _C_ops.svd(x, full_matrices)
  2169. else:
  2170. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'svd')
  2171. check_type(full_matrices, 'full_matrices', bool, 'svd')
  2172. helper = LayerHelper('svd', **locals())
  2173. u = helper.create_variable_for_type_inference(dtype=x.dtype)
  2174. vh = helper.create_variable_for_type_inference(dtype=x.dtype)
  2175. s = helper.create_variable_for_type_inference(dtype=x.dtype)
  2176. attrs = {}
  2177. attrs['full_matrices'] = full_matrices
  2178. helper.append_op(
  2179. type='svd',
  2180. inputs={'X': [x]},
  2181. outputs={'U': u, 'VH': vh, 'S': s},
  2182. attrs=attrs,
  2183. )
  2184. return u, s, vh
  2185. def _conjugate(x):
  2186. if x.is_complex():
  2187. return x.conj()
  2188. return x
  2189. def _transpose(x):
  2190. shape = x.shape
  2191. perm = list(range(0, len(shape)))
  2192. perm = perm[:-2] + [perm[-1]] + [perm[-2]]
  2193. return paddle.transpose(x, perm)
  2194. def _transjugate(x):
  2195. return _conjugate(_transpose(x))
  2196. def _get_approximate_basis(x, q, niter=2, M=None):
  2197. niter = 2 if niter is None else niter
  2198. m, n = x.shape[-2:]
  2199. qr = paddle.linalg.qr
  2200. R = paddle.randn((n, q), dtype=x.dtype)
  2201. A_t = _transpose(x)
  2202. A_H = _conjugate(A_t)
  2203. if M is None:
  2204. Q = qr(paddle.matmul(x, R))[0]
  2205. for i in range(niter):
  2206. Q = qr(paddle.matmul(A_H, Q))[0]
  2207. Q = qr(paddle.matmul(x, Q))[0]
  2208. else:
  2209. M_H = _transjugate(M)
  2210. Q = qr(paddle.matmul(x, R) - paddle.matmul(M, R))[0]
  2211. for i in range(niter):
  2212. Q = qr(paddle.matmul(A_H, Q) - paddle.matmul(M_H, Q))[0]
  2213. Q = qr(paddle.matmul(x, Q) - paddle.matmul(M, Q))[0]
  2214. return Q
  2215. def svd_lowrank(x, q=None, niter=2, M=None, name=None):
  2216. r"""
  2217. Return the singular value decomposition (SVD) on a low-rank matrix or batches of such matrices.
  2218. If :math:`X` is the input matrix or a batch of input matrices, the output should satisfies:
  2219. .. math::
  2220. X \approx U * diag(S) * V^{T}
  2221. When :math:`M` is given, the output should satisfies:
  2222. .. math::
  2223. X - M \approx U * diag(S) * V^{T}
  2224. Args:
  2225. x (Tensor): The input tensor. Its shape should be `[..., N, M]`, where `...` is
  2226. zero or more batch dimensions. N and M can be arbitrary positive number.
  2227. The data type of ``x`` should be float32 or float64.
  2228. q (int, optional): A slightly overestimated rank of :math:`X`.
  2229. Default value is None, which means the overestimated rank is 6.
  2230. niter (int, optional): The number of iterations to perform. Default: 2.
  2231. M (Tensor, optional): The input tensor's mean. Its shape should be `[..., 1, M]`.
  2232. Default value is None.
  2233. name (str, optional): Name for the operation. For more information, please
  2234. refer to :ref:`api_guide_Name`. Default: None.
  2235. Returns:
  2236. - Tensor U, is N x q matrix.
  2237. - Tensor S, is a vector with length q.
  2238. - Tensor V, is M x q matrix.
  2239. tuple (U, S, V): which is the nearly optimal approximation of a singular value decomposition of the matrix :math:`X` or :math:`X - M`.
  2240. Examples:
  2241. .. code-block:: python
  2242. >>> import paddle
  2243. >>> paddle.seed(2024)
  2244. >>> x = paddle.randn((5, 5), dtype='float64')
  2245. >>> U, S, V = paddle.linalg.svd_lowrank(x)
  2246. >>> print(U)
  2247. Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2248. [[-0.03586982, -0.17211503, 0.31536566, -0.38225676, -0.85059629],
  2249. [-0.38386839, 0.67754925, 0.23222694, 0.51777188, -0.26749766],
  2250. [-0.85977150, -0.28442378, -0.41412094, -0.08955629, -0.01948348],
  2251. [ 0.18611503, 0.56047358, -0.67717019, -0.39286761, -0.19577062],
  2252. [ 0.27841082, -0.34099254, -0.46535957, 0.65071250, -0.40770727]])
  2253. >>> print(S)
  2254. Tensor(shape=[5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2255. [4.11253399, 3.03227120, 2.45499752, 1.25602436, 0.45825337])
  2256. >>> print(V)
  2257. Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2258. [[ 0.46401347, 0.50977695, -0.08742316, -0.11140428, -0.71046833],
  2259. [-0.48927226, -0.35047624, 0.07918771, 0.45431083, -0.65200463],
  2260. [-0.20494730, 0.67097011, -0.05427719, 0.66510472, 0.24997083],
  2261. [-0.69645001, 0.40237917, 0.09360970, -0.58032322, -0.08666357],
  2262. [ 0.13512270, 0.07199989, 0.98710572, 0.04529277, 0.01134594]])
  2263. """
  2264. if not paddle.is_tensor(x):
  2265. raise ValueError(f'Input must be tensor, but got {type(x)}')
  2266. m, n = x.shape[-2:]
  2267. if q is None:
  2268. q = min(6, m, n)
  2269. elif not (q >= 0 and q <= min(m, n)):
  2270. raise ValueError(
  2271. f'q(={q}) must be non-negative integer'
  2272. f' and not greater than min(m, n)={min(m, n)}'
  2273. )
  2274. if not (niter >= 0):
  2275. raise ValueError(f'niter(={niter}) must be non-negative integer')
  2276. if M is None:
  2277. M_t = None
  2278. else:
  2279. M = M.broadcast_to(x.shape)
  2280. M_t = _transpose(M)
  2281. A_t = _transpose(x)
  2282. if m < n or n > q:
  2283. Q = _get_approximate_basis(A_t, q, niter=niter, M=M_t)
  2284. Q_c = _conjugate(Q)
  2285. if M is None:
  2286. B_t = paddle.matmul(x, Q_c)
  2287. else:
  2288. B_t = paddle.matmul(x, Q_c) - paddle.matmul(M, Q_c)
  2289. assert B_t.shape[-2] == m, (B_t.shape, m)
  2290. assert B_t.shape[-1] == q, (B_t.shape, q)
  2291. assert B_t.shape[-1] <= B_t.shape[-2], B_t.shape
  2292. U, S, Vh = paddle.linalg.svd(B_t, full_matrices=False)
  2293. V = _transjugate(Vh)
  2294. V = Q.matmul(V)
  2295. else:
  2296. Q = _get_approximate_basis(x, q, niter=niter, M=M)
  2297. Q_c = _conjugate(Q)
  2298. if M is None:
  2299. B = paddle.matmul(A_t, Q_c)
  2300. else:
  2301. B = paddle.matmul(A_t, Q_c) - paddle.matmul(M_t, Q_c)
  2302. B_t = _transpose(B)
  2303. assert B_t.shape[-2] == q, (B_t.shape, q)
  2304. assert B_t.shape[-1] == n, (B_t.shape, n)
  2305. assert B_t.shape[-1] <= B_t.shape[-2], B_t.shape
  2306. U, S, Vh = paddle.linalg.svd(B_t, full_matrices=False)
  2307. V = _transjugate(Vh)
  2308. U = Q.matmul(U)
  2309. return U, S, V
  2310. def pca_lowrank(x, q=None, center=True, niter=2, name=None):
  2311. r"""
  2312. Performs linear Principal Component Analysis (PCA) on a low-rank matrix or batches of such matrices.
  2313. Let :math:`X` be the input matrix or a batch of input matrices, the output should satisfies:
  2314. .. math::
  2315. X = U * diag(S) * V^{T}
  2316. Args:
  2317. x (Tensor): The input tensor. Its shape should be `[..., N, M]`,
  2318. where `...` is zero or more batch dimensions. N and M can be arbitrary
  2319. positive number. The data type of x should be float32 or float64.
  2320. q (int, optional): a slightly overestimated rank of :math:`X`.
  2321. Default value is :math:`q=min(6,N,M)`.
  2322. center (bool, optional): if True, center the input tensor.
  2323. Default value is True.
  2324. niter (int, optional): number of iterations to perform. Default: 2.
  2325. name (str, optional): Name for the operation. For more information,
  2326. please refer to :ref:`api_guide_Name`. Default: None.
  2327. Returns:
  2328. - Tensor U, is N x q matrix.
  2329. - Tensor S, is a vector with length q.
  2330. - Tensor V, is M x q matrix.
  2331. tuple (U, S, V): which is the nearly optimal approximation of a singular value decomposition of a centered matrix :math:`X`.
  2332. Examples:
  2333. .. code-block:: python
  2334. >>> import paddle
  2335. >>> paddle.seed(2023)
  2336. >>> x = paddle.randn((5, 5), dtype='float64')
  2337. >>> U, S, V = paddle.linalg.pca_lowrank(x)
  2338. >>> print(U)
  2339. Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2340. [[ 0.80131563, 0.11962647, 0.27667179, -0.25891214, 0.44721360],
  2341. [-0.12642301, 0.69917551, -0.17899393, 0.51296394, 0.44721360],
  2342. [ 0.08997135, -0.69821706, -0.20059228, 0.51396579, 0.44721360],
  2343. [-0.23871837, -0.02815453, -0.59888153, -0.61932365, 0.44721360],
  2344. [-0.52614559, -0.09243040, 0.70179595, -0.14869394, 0.44721360]])
  2345. >>> print(S)
  2346. Tensor(shape=[5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2347. [2.60101614, 2.40554940, 1.49768346, 0.19064830, 0.00000000])
  2348. >>> print(V)
  2349. Tensor(shape=[5, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2350. [[ 0.58339481, -0.17143771, 0.00522143, 0.57976310, 0.54231640],
  2351. [ 0.22334335, 0.72963474, -0.30148399, -0.39388750, 0.41438019],
  2352. [ 0.05416913, 0.34666487, 0.93549758, 0.00063507, 0.04162998],
  2353. [-0.39519094, 0.53074980, -0.16687419, 0.71175586, -0.16638919],
  2354. [-0.67131070, -0.19071018, 0.07795789, -0.04615811, 0.71046714]])
  2355. """
  2356. if not paddle.is_tensor(x):
  2357. raise ValueError(f'Input must be tensor, but got {type(x)}')
  2358. (m, n) = x.shape[-2:]
  2359. if q is None:
  2360. q = min(6, m, n)
  2361. elif not (q >= 0 and q <= min(m, n)):
  2362. raise ValueError(
  2363. f'q(={q}) must be non-negative integer'
  2364. f' and not greater than min(m, n)={min(m, n)}'
  2365. )
  2366. if not (niter >= 0):
  2367. raise ValueError(f'niter(={niter}) must be non-negative integer')
  2368. if not center:
  2369. return svd_lowrank(x, q, niter=niter, M=None)
  2370. C = x.mean(axis=-2, keepdim=True)
  2371. return svd_lowrank(x - C, q, niter=niter, M=None)
  2372. def matrix_power(x, n, name=None):
  2373. r"""
  2374. Computes the n-th power of a square matrix or a batch of square matrices.
  2375. Let :math:`X` be a square matrix or a batch of square matrices, :math:`n` be
  2376. an exponent, the equation should be:
  2377. .. math::
  2378. Out = X ^ {n}
  2379. Specifically,
  2380. - If `n > 0`, it returns the matrix or a batch of matrices raised to the power of `n`.
  2381. - If `n = 0`, it returns the identity matrix or a batch of identity matrices.
  2382. - If `n < 0`, it returns the inverse of each matrix (if invertible) raised to the power of `abs(n)`.
  2383. Args:
  2384. x (Tensor): A square matrix or a batch of square matrices to be raised
  2385. to power `n`. Its shape should be `[*, M, M]`, where `*` is zero or
  2386. more batch dimensions. Its data type should be float32 or float64.
  2387. n (int): The exponent. It can be any positive, negative integer or zero.
  2388. name (str, optional): Name for the operation (optional, default is None).
  2389. For more information, please refer to :ref:`api_guide_Name`.
  2390. Returns:
  2391. - Tensor, The n-th power of the matrix (or the batch of matrices) `x`. Its
  2392. data type should be the same as that of `x`.
  2393. Examples:
  2394. .. code-block:: python
  2395. >>> import paddle
  2396. >>> x = paddle.to_tensor([[1, 2, 3],
  2397. ... [1, 4, 9],
  2398. ... [1, 8, 27]], dtype='float64')
  2399. >>> print(paddle.linalg.matrix_power(x, 2))
  2400. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2401. [[6. , 34. , 102.],
  2402. [14. , 90. , 282.],
  2403. [36. , 250., 804.]])
  2404. >>> print(paddle.linalg.matrix_power(x, 0))
  2405. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2406. [[1., 0., 0.],
  2407. [0., 1., 0.],
  2408. [0., 0., 1.]])
  2409. >>> print(paddle.linalg.matrix_power(x, -2))
  2410. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2411. [[ 12.91666667, -12.75000000, 2.83333333 ],
  2412. [-7.66666667 , 8. , -1.83333333 ],
  2413. [ 1.80555556 , -1.91666667 , 0.44444444 ]])
  2414. """
  2415. if in_dynamic_or_pir_mode():
  2416. return _C_ops.matrix_power(x, n)
  2417. else:
  2418. check_variable_and_dtype(
  2419. x, 'dtype', ['float32', 'float64'], 'matrix_power'
  2420. )
  2421. check_type(n, 'n', int, 'matrix_power')
  2422. helper = LayerHelper('matrix_power', **locals())
  2423. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  2424. helper.append_op(
  2425. type='matrix_power',
  2426. inputs={'X': x},
  2427. outputs={'Out': out},
  2428. attrs={'n': n},
  2429. )
  2430. return out
  2431. def qr(x, mode="reduced", name=None):
  2432. r"""
  2433. Computes the QR decomposition of one matrix or batches of matrices (backward is unsupported now).
  2434. Args:
  2435. x (Tensor): The input tensor. Its shape should be `[..., M, N]`,
  2436. where ... is zero or more batch dimensions. M and N can be arbitrary
  2437. positive number. The data type of x should be float32 or float64.
  2438. mode (str, optional): A flag to control the behavior of qr.
  2439. Suppose x's shape is `[..., M, N]` and denoting `K = min(M, N)`:
  2440. If mode = "reduced", qr op will return reduced Q and R matrices,
  2441. which means Q's shape is `[..., M, K]` and R's shape is `[..., K, N]`.
  2442. If mode = "complete", qr op will return complete Q and R matrices,
  2443. which means Q's shape is `[..., M, M]` and R's shape is `[..., M, N]`.
  2444. If mode = "r", qr op will only return reduced R matrix, which means
  2445. R's shape is `[..., K, N]`. Default: "reduced".
  2446. name (str, optional): Name for the operation (optional, default is None).
  2447. For more information, please refer to :ref:`api_guide_Name`.
  2448. Returns:
  2449. If mode = "reduced" or mode = "complete", qr will return a two tensor-tuple, which represents Q and R.
  2450. If mode = "r", qr will return a tensor which represents R.
  2451. Examples:
  2452. .. code-block:: python
  2453. >>> import paddle
  2454. >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
  2455. >>> q, r = paddle.linalg.qr(x)
  2456. >>> print (q)
  2457. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2458. [[-0.16903085, 0.89708523],
  2459. [-0.50709255, 0.27602622],
  2460. [-0.84515425, -0.34503278]])
  2461. >>> print (r)
  2462. Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2463. [[-5.91607978, -7.43735744],
  2464. [ 0. , 0.82807867]])
  2465. >>> # one can verify : X = Q * R ;
  2466. """
  2467. if in_dynamic_or_pir_mode():
  2468. q, r = _C_ops.qr(x, mode)
  2469. if mode == "r":
  2470. return r
  2471. else:
  2472. return q, r
  2473. else:
  2474. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'qr')
  2475. check_type(mode, 'mode', str, 'qr')
  2476. helper = LayerHelper('qr', **locals())
  2477. q = helper.create_variable_for_type_inference(dtype=x.dtype)
  2478. r = helper.create_variable_for_type_inference(dtype=x.dtype)
  2479. attrs = {}
  2480. attrs['mode'] = mode
  2481. helper.append_op(
  2482. type='qr', inputs={'X': [x]}, outputs={'Q': q, 'R': r}, attrs=attrs
  2483. )
  2484. if mode == "r":
  2485. return r
  2486. else:
  2487. return q, r
  2488. def lu(x, pivot=True, get_infos=False, name=None):
  2489. r"""
  2490. Computes the LU factorization of an N-D(N>=2) matrix x.
  2491. Returns the LU factorization(inplace x) and Pivots. low triangular matrix L and
  2492. upper triangular matrix U are combined to a single LU matrix.
  2493. Pivoting is done if pivot is set to True.
  2494. P mat can be get by pivots:
  2495. .. code-block:: text
  2496. ones = eye(rows) #eye matrix of rank rows
  2497. for i in range(cols):
  2498. swap(ones[i], ones[pivots[i]])
  2499. return ones
  2500. Args:
  2501. X (Tensor): the tensor to factor of N-dimensions(N>=2).
  2502. pivot (bool, optional): controls whether pivoting is done. Default: True.
  2503. get_infos (bool, optional): if set to True, returns an info IntTensor. Default: False.
  2504. name (str, optional): Name for the operation (optional, default is None).
  2505. For more information, please refer to :ref:`api_guide_Name`.
  2506. Returns:
  2507. factorization (Tensor), LU matrix, the factorization of input X.
  2508. pivots (IntTensor), the pivots of size(∗(N-2), min(m,n)). `pivots` stores all the
  2509. intermediate transpositions of rows. The final permutation `perm` could be
  2510. reconstructed by this, details refer to upper example.
  2511. infos (IntTensor, optional), if `get_infos` is `True`, this is a tensor of size (∗(N-2))
  2512. where non-zero values indicate whether factorization for the matrix or each minibatch
  2513. has succeeded or failed.
  2514. Examples:
  2515. .. code-block:: python
  2516. >>> import paddle
  2517. >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
  2518. >>> lu,p,info = paddle.linalg.lu(x, get_infos=True)
  2519. >>> print(lu)
  2520. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2521. [[5. , 6. ],
  2522. [0.20000000, 0.80000000],
  2523. [0.60000000, 0.50000000]])
  2524. >>> print(p)
  2525. Tensor(shape=[2], dtype=int32, place=Place(cpu), stop_gradient=True,
  2526. [3, 3])
  2527. >>> print(info)
  2528. Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True,
  2529. [0])
  2530. >>> P,L,U = paddle.linalg.lu_unpack(lu,p)
  2531. >>> print(P)
  2532. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2533. [[0., 1., 0.],
  2534. [0., 0., 1.],
  2535. [1., 0., 0.]])
  2536. >>> print(L)
  2537. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2538. [[1. , 0. ],
  2539. [0.20000000, 1. ],
  2540. [0.60000000, 0.50000000]])
  2541. >>> print(U)
  2542. Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2543. [[5. , 6. ],
  2544. [0. , 0.80000000]])
  2545. >>> # one can verify : X = P @ L @ U ;
  2546. """
  2547. if in_dynamic_or_pir_mode():
  2548. lu, p, info = _C_ops.lu(x, pivot)
  2549. else:
  2550. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'lu')
  2551. helper = LayerHelper('lu', **locals())
  2552. lu = helper.create_variable_for_type_inference(dtype=x.dtype)
  2553. p = helper.create_variable_for_type_inference(dtype='int')
  2554. info = helper.create_variable_for_type_inference(dtype='int')
  2555. attrs = {}
  2556. attrs['pivot'] = pivot
  2557. helper.append_op(
  2558. type='lu',
  2559. inputs={'X': x},
  2560. outputs={'Out': lu, 'Pivots': p, 'Infos': info},
  2561. attrs=attrs,
  2562. )
  2563. if get_infos:
  2564. return lu, p, info
  2565. else:
  2566. return lu, p
  2567. def lu_unpack(x, y, unpack_ludata=True, unpack_pivots=True, name=None):
  2568. r"""
  2569. Unpack L U and P to single matrix tensor .
  2570. unpack L and U matrix from LU, unpack permutation matrix P from Pivtos .
  2571. P mat can be get by pivots:
  2572. .. code-block:: text
  2573. ones = eye(rows) #eye matrix of rank rows
  2574. for i in range(cols):
  2575. swap(ones[i], ones[pivots[i]])
  2576. Args:
  2577. x (Tensor): The LU tensor get from paddle.linalg.lu, which is combined by L and U.
  2578. y (Tensor): Pivots get from paddle.linalg.lu.
  2579. unpack_ludata (bool, optional): whether to unpack L and U from x. Default: True.
  2580. unpack_pivots (bool, optional): whether to unpack permutation matrix P from Pivtos. Default: True.
  2581. name (str, optional): Name for the operation (optional, default is None).
  2582. For more information, please refer to :ref:`api_guide_Name`.
  2583. Returns:
  2584. P (Tensor), Permutation matrix P of lu factorization.
  2585. L (Tensor), The lower triangular matrix tensor of lu factorization.
  2586. U (Tensor), The upper triangular matrix tensor of lu factorization.
  2587. Examples:
  2588. .. code-block:: python
  2589. >>> import paddle
  2590. >>> x = paddle.to_tensor([[1.0, 2.0], [3.0, 4.0], [5.0, 6.0]]).astype('float64')
  2591. >>> lu,p,info = paddle.linalg.lu(x, get_infos=True)
  2592. >>> print(lu)
  2593. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2594. [[5. , 6. ],
  2595. [0.20000000, 0.80000000],
  2596. [0.60000000, 0.50000000]])
  2597. >>> print(p)
  2598. Tensor(shape=[2], dtype=int32, place=Place(cpu), stop_gradient=True,
  2599. [3, 3])
  2600. >>> print(info)
  2601. Tensor(shape=[1], dtype=int32, place=Place(cpu), stop_gradient=True,
  2602. [0])
  2603. >>> P,L,U = paddle.linalg.lu_unpack(lu,p)
  2604. >>> print(P)
  2605. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2606. [[0., 1., 0.],
  2607. [0., 0., 1.],
  2608. [1., 0., 0.]])
  2609. >>> print(L)
  2610. Tensor(shape=[3, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2611. [[1. , 0. ],
  2612. [0.20000000, 1. ],
  2613. [0.60000000, 0.50000000]])
  2614. >>> print(U)
  2615. Tensor(shape=[2, 2], dtype=float64, place=Place(cpu), stop_gradient=True,
  2616. [[5. , 6. ],
  2617. [0. , 0.80000000]])
  2618. >>> # one can verify : X = P @ L @ U ;
  2619. """
  2620. if x.ndim < 2:
  2621. raise ValueError(
  2622. f"The shape of x should be (*, M, N), but received ndim is [{x.ndim} < 2]"
  2623. )
  2624. if y.ndim < 1:
  2625. raise ValueError(
  2626. f"The shape of Pivots should be (*, K), but received ndim is [{y.ndim} < 1]"
  2627. )
  2628. if in_dynamic_or_pir_mode():
  2629. P, L, U = _C_ops.lu_unpack(x, y, unpack_ludata, unpack_pivots)
  2630. return P, L, U
  2631. else:
  2632. check_variable_and_dtype(
  2633. x, 'dtype', ['float32', 'float64'], 'lu_unpack'
  2634. )
  2635. helper = LayerHelper('lu_unpack', **locals())
  2636. p = helper.create_variable_for_type_inference(dtype=x.dtype)
  2637. l = helper.create_variable_for_type_inference(dtype=x.dtype)
  2638. u = helper.create_variable_for_type_inference(dtype=x.dtype)
  2639. attrs = {}
  2640. attrs['unpack_ludata'] = unpack_ludata
  2641. attrs['unpack_pivots'] = unpack_pivots
  2642. helper.append_op(
  2643. type='lu_unpack',
  2644. inputs={'X': x, 'Pivots': y},
  2645. outputs={'Pmat': p, 'L': l, 'U': u},
  2646. attrs=attrs,
  2647. )
  2648. return p, l, u
  2649. def eig(x, name=None):
  2650. """
  2651. Performs the eigenvalue decomposition of a square matrix or a batch of square matrices.
  2652. Note:
  2653. - If the matrix is a Hermitian or a real symmetric matrix, please use :ref:`api_paddle_linalg_eigh` instead, which is much faster.
  2654. - If only eigenvalues is needed, please use :ref:`api_paddle_linalg_eigvals` instead.
  2655. - If the matrix is of any shape, please use :ref:`api_paddle_linalg_svd`.
  2656. - This API is only supported on CPU device.
  2657. - The output datatype is always complex for both real and complex input.
  2658. Args:
  2659. x (Tensor): A tensor with shape math:`[*, N, N]`, The data type of the x should be one of ``float32``,
  2660. ``float64``, ``compplex64`` or ``complex128``.
  2661. name (str, optional): The default value is `None`. Normally there is no need for user to set
  2662. this property. For more information, please refer to :ref:`api_guide_Name`.
  2663. Returns:
  2664. Eigenvalues(Tensors): A tensor with shape math:`[*, N]` refers to the eigen values.
  2665. Eigenvectors(Tensors): A tensor with shape math:`[*, N, N]` refers to the eigen vectors.
  2666. Examples:
  2667. .. code-block:: python
  2668. >>> import paddle
  2669. >>> x = paddle.to_tensor([[1.6707249, 7.2249975, 6.5045543],
  2670. ... [9.956216, 8.749598, 6.066444 ],
  2671. ... [4.4251957, 1.7983172, 0.370647 ]])
  2672. >>> w, v = paddle.linalg.eig(x)
  2673. >>> print(v)
  2674. Tensor(shape=[3, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
  2675. [[ (0.5061365365982056+0j) , (0.7971761226654053+0j) ,
  2676. (0.1851806491613388+0j) ],
  2677. [ (0.8308236598968506+0j) , (-0.3463813066482544+0j) ,
  2678. (-0.6837005615234375+0j) ],
  2679. [ (0.23142573237419128+0j), (-0.49449989199638367+0j),
  2680. (0.7058765292167664+0j) ]])
  2681. >>> print(w)
  2682. Tensor(shape=[3], dtype=complex64, place=Place(cpu), stop_gradient=True,
  2683. [ (16.50470733642578+0j) , (-5.503481388092041+0j) ,
  2684. (-0.21026138961315155+0j)])
  2685. """
  2686. if in_dynamic_or_pir_mode():
  2687. return _C_ops.eig(x)
  2688. else:
  2689. check_variable_and_dtype(
  2690. x, 'X', ['float32', 'float64', 'complex64', 'complex128'], 'eig'
  2691. )
  2692. helper = LayerHelper('eig', **locals())
  2693. w = helper.create_variable_for_type_inference(x.dtype)
  2694. v = helper.create_variable_for_type_inference(x.dtype)
  2695. inputs = {'X': x}
  2696. outputs = {'Eigenvalues': w, 'Eigenvectors': v}
  2697. helper.append_op(type='eig', inputs=inputs, outputs=outputs)
  2698. return w, v
  2699. def eigvals(x, name=None):
  2700. """
  2701. Compute the eigenvalues of one or more general matrices.
  2702. Warning:
  2703. The gradient kernel of this operator does not yet developed.
  2704. If you need back propagation through this operator, please replace it with paddle.linalg.eig.
  2705. Args:
  2706. x (Tensor): A square matrix or a batch of square matrices whose eigenvalues will be computed.
  2707. Its shape should be `[*, M, M]`, where `*` is zero or more batch dimensions.
  2708. Its data type should be float32, float64, complex64, or complex128.
  2709. name (str, optional): Name for the operation (optional, default is None).
  2710. For more information, please refer to :ref:`api_guide_Name`.
  2711. Returns:
  2712. Tensor, A tensor containing the unsorted eigenvalues which has the same batch
  2713. dimensions with `x`. The eigenvalues are complex-valued even when `x` is real.
  2714. Examples:
  2715. .. code-block:: python
  2716. >>> import paddle
  2717. >>> paddle.seed(2023)
  2718. >>> x = paddle.rand(shape=[3, 3], dtype='float64')
  2719. >>> print(x)
  2720. Tensor(shape=[3, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2721. [[0.86583615, 0.52014721, 0.25960938],
  2722. [0.90525323, 0.42400090, 0.40641288],
  2723. [0.97020893, 0.74437359, 0.51785128]])
  2724. >>> print(paddle.linalg.eigvals(x))
  2725. Tensor(shape=[3], dtype=complex128, place=Place(cpu), stop_gradient=True,
  2726. [ (1.788956694280852+0j) , (0.16364484879581526+0j),
  2727. (-0.14491322408727625+0j)])
  2728. """
  2729. x_shape = list(x.shape)
  2730. if len(x_shape) < 2:
  2731. raise ValueError(
  2732. f"The dimension of Input(x) should be at least 2, but received x's dimension = {len(x_shape)}, x's shape = {x_shape}"
  2733. )
  2734. if x_shape[-1] != x_shape[-2]:
  2735. raise ValueError(
  2736. f"The last two dimensions of Input(x) should be equal, but received x's shape = {x_shape}"
  2737. )
  2738. if in_dynamic_or_pir_mode():
  2739. return _C_ops.eigvals(x)
  2740. else:
  2741. check_variable_and_dtype(
  2742. x,
  2743. 'dtype',
  2744. ['float32', 'float64', 'complex64', 'complex128'],
  2745. 'eigvals',
  2746. )
  2747. helper = LayerHelper('eigvals', **locals())
  2748. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  2749. helper.append_op(type='eigvals', inputs={'X': x}, outputs={'Out': out})
  2750. return out
  2751. def multi_dot(x, name=None):
  2752. """
  2753. Multi_dot is an operator that calculates multiple matrix multiplications.
  2754. Supports inputs of float16(only GPU support), float32 and float64 dtypes. This function does not
  2755. support batched inputs.
  2756. The input tensor in [x] must be 2-D except for the first and last can be 1-D.
  2757. If the first tensor is a 1-D vector of shape(n, ) it is treated as row vector
  2758. of shape(1, n), similarly if the last tensor is a 1D vector of shape(n, ), it
  2759. is treated as a column vector of shape(n, 1).
  2760. If the first and last tensor are 2-D matrix, then the output is also 2-D matrix,
  2761. otherwise the output is a 1-D vector.
  2762. Multi_dot will select the lowest cost multiplication order for calculation. The
  2763. cost of multiplying two matrices with shapes (a, b) and (b, c) is a * b * c.
  2764. Given matrices A, B, C with shapes (20, 5), (5, 100), (100, 10) respectively,
  2765. we can calculate the cost of different multiplication orders as follows:
  2766. - Cost((AB)C) = 20x5x100 + 20x100x10 = 30000
  2767. - Cost(A(BC)) = 5x100x10 + 20x5x10 = 6000
  2768. In this case, multiplying B and C first, then multiply A, which is 5 times faster
  2769. than sequential calculation.
  2770. Args:
  2771. x ([Tensor]): The input tensors which is a list Tensor.
  2772. name (str, optional): Name for the operation (optional, default is None).
  2773. For more information, please refer to :ref:`api_guide_Name`.
  2774. Returns:
  2775. Tensor: The output Tensor.
  2776. Examples:
  2777. .. code-block:: python
  2778. >>> import paddle
  2779. >>> # A * B
  2780. >>> A = paddle.rand([3, 4])
  2781. >>> B = paddle.rand([4, 5])
  2782. >>> out = paddle.linalg.multi_dot([A, B])
  2783. >>> print(out.shape)
  2784. [3, 5]
  2785. >>> # A * B * C
  2786. >>> A = paddle.rand([10, 5])
  2787. >>> B = paddle.rand([5, 8])
  2788. >>> C = paddle.rand([8, 7])
  2789. >>> out = paddle.linalg.multi_dot([A, B, C])
  2790. >>> print(out.shape)
  2791. [10, 7]
  2792. """
  2793. if in_dynamic_or_pir_mode():
  2794. return _C_ops.multi_dot(x)
  2795. else:
  2796. check_type(x, 'x', (list, tuple), 'multi_dot')
  2797. for id, item in enumerate(x):
  2798. check_variable_and_dtype(
  2799. item,
  2800. 'x[' + str(id) + ']',
  2801. ['float16', 'float32', 'float64', 'uint16'],
  2802. 'multi_dot',
  2803. )
  2804. if item.dtype != x[0].dtype:
  2805. raise TypeError(
  2806. "All the Tensors in the input must have the same data type."
  2807. )
  2808. helper = LayerHelper('multi_dot', **locals())
  2809. dtype = helper.input_dtype(input_param_name='x')
  2810. out = helper.create_variable_for_type_inference(dtype)
  2811. helper.append_op(
  2812. type='multi_dot', inputs={"X": x}, outputs={"Out": out}
  2813. )
  2814. return out
  2815. def eigh(x, UPLO='L', name=None):
  2816. """
  2817. Compute the eigenvalues and eigenvectors of a
  2818. complex Hermitian (conjugate symmetric) or a real symmetric matrix.
  2819. Args:
  2820. x (Tensor): A tensor with shape :math:`[*, N, N]` , The data type of the input Tensor x
  2821. should be one of float32, float64, complex64, complex128.
  2822. UPLO (str, optional): (string, default 'L'), 'L' represents the lower triangular matrix,
  2823. "'U' represents the upper triangular matrix.". Default: 'L'.
  2824. name (str, optional): The default value is None. Normally there is no need for user to set this
  2825. property. For more information, please refer to :ref:`api_guide_Name`.
  2826. Returns:
  2827. 2-element tuple containing
  2828. - out_value(Tensor): A Tensor with shape :math:`[*, N]` and data type of float32 and float64.
  2829. The eigenvalues of eigh op.
  2830. - out_vector(Tensor): A Tensor with shape :math:`[*, N, N]` and data type of float32, float64,
  2831. complex64 and complex128. The eigenvectors of eigh op.
  2832. Examples:
  2833. .. code-block:: python
  2834. >>> import paddle
  2835. >>> x = paddle.to_tensor([[1, -2j], [2j, 5]])
  2836. >>> out_value, out_vector = paddle.linalg.eigh(x, UPLO='L')
  2837. >>> print(out_value)
  2838. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  2839. [0.17157286, 5.82842731])
  2840. >>> print(out_vector)
  2841. Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True,
  2842. [[(-0.9238795042037964+0j), (-0.3826833963394165+0j)],
  2843. [ 0.3826833963394165j , -0.9238795042037964j ]])
  2844. """
  2845. if in_dynamic_mode():
  2846. return _C_ops.eigh(x, UPLO)
  2847. def __check_input(x, UPLO):
  2848. x_shape = list(x.shape)
  2849. if len(x.shape) < 2:
  2850. raise ValueError(
  2851. "Input(input) only support >=2 tensor, but received "
  2852. "length of Input(input) is %s." % len(x.shape)
  2853. )
  2854. if x_shape[-1] != x_shape[-2]:
  2855. raise ValueError(
  2856. f"The input matrix must be batches of square matrices. But received x's dimension: {x_shape}"
  2857. )
  2858. if UPLO != 'L' and UPLO != 'U':
  2859. raise ValueError(
  2860. f"UPLO must be L or U. But received UPLO is: {UPLO}"
  2861. )
  2862. if in_pir_mode():
  2863. __check_input(x, UPLO)
  2864. return _C_ops.eigh(x, UPLO)
  2865. else:
  2866. __check_input(x, UPLO)
  2867. helper = LayerHelper('eigh', **locals())
  2868. check_variable_and_dtype(
  2869. x,
  2870. 'dtype',
  2871. ['float32', 'float64', 'complex64', 'complex128'],
  2872. 'eigh',
  2873. )
  2874. out_value = helper.create_variable_for_type_inference(dtype=x.dtype)
  2875. out_vector = helper.create_variable_for_type_inference(dtype=x.dtype)
  2876. helper.append_op(
  2877. type='eigh',
  2878. inputs={'X': x},
  2879. outputs={'Eigenvalues': out_value, 'Eigenvectors': out_vector},
  2880. attrs={'UPLO': UPLO},
  2881. )
  2882. return out_value, out_vector
  2883. def pinv(x, rcond=1e-15, hermitian=False, name=None):
  2884. r"""
  2885. Calculate pseudo inverse via SVD(singular value decomposition)
  2886. of one matrix or batches of regular matrix.
  2887. .. math::
  2888. if hermitian == False:
  2889. x = u * s * vt (SVD)
  2890. out = v * 1/s * ut
  2891. else:
  2892. x = u * s * ut (eigh)
  2893. out = u * 1/s * u.conj().transpose(-2,-1)
  2894. If x is hermitian or symmetric matrix, svd will be replaced with eigh.
  2895. Args:
  2896. x (Tensor): The input tensor. Its shape should be (*, m, n)
  2897. where * is zero or more batch dimensions. m and n can be
  2898. arbitrary positive number. The data type of x should be
  2899. float32 or float64 or complex64 or complex128. When data
  2900. type is complex64 or complex128, hermitian should be set
  2901. True.
  2902. rcond (Tensor, optional): the tolerance value to determine
  2903. when is a singular value zero. Default:1e-15.
  2904. hermitian (bool, optional): indicates whether x is Hermitian
  2905. if complex or symmetric if real. Default: False.
  2906. name (str, optional): The default value is None. Normally there is no need for user to set this
  2907. property. For more information, please refer to :ref:`api_guide_Name`.
  2908. Returns:
  2909. Tensor: The tensor with same data type with x. it represents
  2910. pseudo inverse of x. Its shape should be (*, n, m).
  2911. Examples:
  2912. .. code-block:: python
  2913. >>> import paddle
  2914. >>> x = paddle.arange(15).reshape((3, 5)).astype('float64')
  2915. >>> input = paddle.to_tensor(x)
  2916. >>> out = paddle.linalg.pinv(input)
  2917. >>> print(input)
  2918. Tensor(shape=[3, 5], dtype=float64, place=Place(cpu), stop_gradient=True,
  2919. [[0. , 1. , 2. , 3. , 4. ],
  2920. [5. , 6. , 7. , 8. , 9. ],
  2921. [10., 11., 12., 13., 14.]])
  2922. >>> print(out)
  2923. Tensor(shape=[5, 3], dtype=float64, place=Place(cpu), stop_gradient=True,
  2924. [[-0.22666667, -0.06666667, 0.09333333],
  2925. [-0.12333333, -0.03333333, 0.05666667],
  2926. [-0.02000000, -0.00000000, 0.02000000],
  2927. [ 0.08333333, 0.03333333, -0.01666667],
  2928. [ 0.18666667, 0.06666667, -0.05333333]])
  2929. # one can verify : x * out * x = x ;
  2930. # or out * x * out = x ;
  2931. """
  2932. if in_dynamic_or_pir_mode():
  2933. if not hermitian:
  2934. # combine svd and matmul op
  2935. u, s, vt = _C_ops.svd(x, False)
  2936. max_singular_val = _C_ops.max(s, [-1], True)
  2937. rcond = paddle.to_tensor(rcond, dtype=x.dtype)
  2938. cutoff = rcond * max_singular_val
  2939. y = float('inf')
  2940. y = paddle.to_tensor(y, dtype=x.dtype)
  2941. singular = paddle.where(s > cutoff, 1 / s, 1 / y)
  2942. st = _C_ops.unsqueeze(singular, [-2])
  2943. dims = list(range(len(vt.shape)))
  2944. perm = dims[:-2] + [dims[-1]] + [dims[-2]]
  2945. v = _C_ops.transpose(vt, perm)
  2946. out_1 = v * st
  2947. out_2 = _C_ops.matmul(out_1, u, False, True)
  2948. return out_2
  2949. else:
  2950. # combine eigh and matmul op
  2951. s, u = _C_ops.eigh(x, 'UPLO')
  2952. s_abs = paddle.abs(s)
  2953. max_singular_val = _C_ops.max(s_abs, [-1], True)
  2954. rcond = paddle.to_tensor(rcond, dtype=s.dtype)
  2955. cutoff = rcond * max_singular_val
  2956. y = float('inf')
  2957. y = paddle.to_tensor(y, dtype=s.dtype)
  2958. singular = paddle.where(s_abs > cutoff, 1 / s, 1 / y)
  2959. st = _C_ops.unsqueeze(singular, [-2])
  2960. out_1 = u * st
  2961. u_conj = _C_ops.conj(u)
  2962. out_2 = _C_ops.matmul(out_1, u_conj, False, True)
  2963. return out_2
  2964. else:
  2965. if not hermitian:
  2966. helper = LayerHelper('pinv', **locals())
  2967. dtype = x.dtype
  2968. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'pinv')
  2969. u = helper.create_variable_for_type_inference(dtype)
  2970. s = helper.create_variable_for_type_inference(dtype)
  2971. vt = helper.create_variable_for_type_inference(dtype)
  2972. helper.append_op(
  2973. type='svd',
  2974. inputs={'X': [x]},
  2975. outputs={'U': u, 'VH': vt, 'S': s},
  2976. attrs={'full_matrices': False},
  2977. )
  2978. max_singular_val = helper.create_variable_for_type_inference(dtype)
  2979. helper.append_op(
  2980. type='reduce_max',
  2981. inputs={'X': s},
  2982. outputs={'Out': max_singular_val},
  2983. attrs={'dim': [-1], 'keep_dim': True, 'reduce_all': False},
  2984. )
  2985. rcond = full(shape=[1], fill_value=rcond, dtype=dtype)
  2986. cutoff = rcond * max_singular_val
  2987. y = float('inf')
  2988. y = full(shape=[1], fill_value=y, dtype=dtype)
  2989. singular = paddle.where(s > cutoff, 1 / s, 1 / y)
  2990. st = helper.create_variable_for_type_inference(dtype=dtype)
  2991. st_shape = helper.create_variable_for_type_inference(dtype=dtype)
  2992. helper.append_op(
  2993. type='unsqueeze2',
  2994. inputs={'X': singular},
  2995. attrs={'axes': [-2]},
  2996. outputs={'Out': st, 'XShape': st_shape},
  2997. )
  2998. dims = list(range(len(vt.shape)))
  2999. perm = dims[:-2] + [dims[-1]] + [dims[-2]]
  3000. v = helper.create_variable_for_type_inference(dtype)
  3001. v_shape = helper.create_variable_for_type_inference(dtype)
  3002. helper.append_op(
  3003. type='transpose2',
  3004. inputs={'X': [vt]},
  3005. outputs={'Out': [v], 'XShape': [v_shape]},
  3006. attrs={'axis': perm},
  3007. )
  3008. out_1 = helper.create_variable_for_type_inference(dtype)
  3009. helper.append_op(
  3010. type='elementwise_mul',
  3011. inputs={'X': v, 'Y': st},
  3012. outputs={'Out': out_1},
  3013. attrs={'axis': -1},
  3014. )
  3015. out_1 = helper.append_activation(out_1)
  3016. out_2 = helper.create_variable_for_type_inference(dtype)
  3017. helper.append_op(
  3018. type='matmul_v2',
  3019. inputs={'X': out_1, 'Y': u},
  3020. outputs={'Out': out_2},
  3021. attrs={'trans_x': False, 'trans_y': True},
  3022. )
  3023. return out_2
  3024. else:
  3025. helper = LayerHelper('pinv', **locals())
  3026. dtype = x.dtype
  3027. check_variable_and_dtype(
  3028. x,
  3029. 'dtype',
  3030. ['float32', 'float64', 'complex64', 'complex128'],
  3031. 'pinv',
  3032. )
  3033. if dtype == paddle.complex128:
  3034. s_type = 'float64'
  3035. elif dtype == paddle.complex64:
  3036. s_type = 'float32'
  3037. else:
  3038. s_type = dtype
  3039. u = helper.create_variable_for_type_inference(dtype)
  3040. s = helper.create_variable_for_type_inference(s_type)
  3041. helper.append_op(
  3042. type='eigh',
  3043. inputs={'X': x},
  3044. outputs={'Eigenvalues': s, 'Eigenvectors': u},
  3045. attrs={'UPLO': 'L'},
  3046. )
  3047. s_abs = helper.create_variable_for_type_inference(s_type)
  3048. helper.append_op(
  3049. type='abs', inputs={'X': s}, outputs={'Out': s_abs}
  3050. )
  3051. max_singular_val = helper.create_variable_for_type_inference(s_type)
  3052. helper.append_op(
  3053. type='reduce_max',
  3054. inputs={'X': s_abs},
  3055. outputs={'Out': max_singular_val},
  3056. attrs={'dim': [-1], 'keep_dim': True, 'reduce_all': False},
  3057. )
  3058. rcond = full(shape=[1], fill_value=rcond, dtype=s_type)
  3059. cutoff = rcond * max_singular_val
  3060. y = float('inf')
  3061. y = full(shape=[1], fill_value=y, dtype=s_type)
  3062. singular = paddle.where(s_abs > cutoff, 1 / s, 1 / y)
  3063. st = helper.create_variable_for_type_inference(dtype=s_type)
  3064. st_shape = helper.create_variable_for_type_inference(dtype=s_type)
  3065. helper.append_op(
  3066. type='unsqueeze2',
  3067. inputs={'X': singular},
  3068. attrs={'axes': [-2]},
  3069. outputs={'Out': st, 'XShape': st_shape},
  3070. )
  3071. out_1 = helper.create_variable_for_type_inference(dtype)
  3072. helper.append_op(
  3073. type='elementwise_mul',
  3074. inputs={'X': u, 'Y': st},
  3075. outputs={'Out': out_1},
  3076. attrs={'axis': -1},
  3077. )
  3078. out_1 = helper.append_activation(out_1)
  3079. u_conj = helper.create_variable_for_type_inference(dtype)
  3080. helper.append_op(
  3081. type='conj', inputs={'X': u}, outputs={'Out': [u_conj]}
  3082. )
  3083. out_2 = helper.create_variable_for_type_inference(dtype)
  3084. helper.append_op(
  3085. type='matmul_v2',
  3086. inputs={'X': out_1, 'Y': u_conj},
  3087. outputs={'Out': out_2},
  3088. attrs={'trans_x': False, 'trans_y': True},
  3089. )
  3090. return out_2
  3091. def solve(x, y, name=None):
  3092. r"""
  3093. Computes the solution of a square system of linear equations with a unique solution for input 'X' and 'Y'.
  3094. Let :math:`X` be a square matrix or a batch of square matrices, :math:`Y` be
  3095. a vector/matrix or a batch of vectors/matrices, the equation should be:
  3096. .. math::
  3097. Out = X^-1 * Y
  3098. Specifically, this system of linear equations has one solution if and only if input 'X' is invertible.
  3099. Args:
  3100. x (Tensor): A square matrix or a batch of square matrices. Its shape should be ``[*, M, M]``, where ``*`` is zero or
  3101. more batch dimensions. Its data type should be float32 or float64.
  3102. y (Tensor): A vector/matrix or a batch of vectors/matrices. Its shape should be ``[*, M, K]``, where ``*`` is zero or
  3103. more batch dimensions. Its data type should be float32 or float64.
  3104. name (str, optional): Name for the operation (optional, default is None).
  3105. For more information, please refer to :ref:`api_guide_Name`.
  3106. Returns:
  3107. Tensor: The solution of a square system of linear equations with a unique solution for input 'x' and 'y'.
  3108. Its data type should be the same as that of `x`.
  3109. Examples:
  3110. .. code-block:: python
  3111. >>> # a square system of linear equations:
  3112. >>> # 2*X0 + X1 = 9
  3113. >>> # X0 + 2*X1 = 8
  3114. >>> import paddle
  3115. >>> x = paddle.to_tensor([[3, 1],[1, 2]], dtype="float64")
  3116. >>> y = paddle.to_tensor([9, 8], dtype="float64")
  3117. >>> out = paddle.linalg.solve(x, y)
  3118. >>> print(out)
  3119. Tensor(shape=[2], dtype=float64, place=Place(cpu), stop_gradient=True,
  3120. [2., 3.])
  3121. """
  3122. if in_dynamic_or_pir_mode():
  3123. return _C_ops.solve(x, y)
  3124. else:
  3125. inputs = {"X": [x], "Y": [y]}
  3126. helper = LayerHelper("solve", **locals())
  3127. check_variable_and_dtype(x, 'x', ['float32', 'float64'], 'solve')
  3128. check_variable_and_dtype(y, 'y', ['float32', 'float64'], 'solve')
  3129. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  3130. helper.append_op(
  3131. type="solve", inputs={"X": x, "Y": y}, outputs={"Out": out}
  3132. )
  3133. return out
  3134. def triangular_solve(
  3135. x, y, upper=True, transpose=False, unitriangular=False, name=None
  3136. ):
  3137. r"""
  3138. Computes the solution of a system of equations with a triangular coefficient. `x` is coefficient matrix
  3139. `y` is multiple right-hand sides of equations.
  3140. Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs is also
  3141. batches.
  3142. Equations can be described as:
  3143. .. math::
  3144. x * Out = y
  3145. Solution of Equations is:
  3146. .. math::
  3147. Out = x ^ {-1} * y
  3148. Args:
  3149. x (Tensor): The input triangular coefficient matrix. Its shape should be `[*, M, M]`, where `*` is zero or
  3150. more batch dimensions. Its data type should be float32, float64, complex64, complex128.
  3151. y (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
  3152. zero or more batch dimensions. Its data type should be float32, float64, complex64, complex128.
  3153. upper (bool, optional): Whether to solve the upper-triangular system of equations (default) or the lower-triangular
  3154. system of equations. Default: True.
  3155. transpose (bool, optional): whether `x` should be transposed before calculation. Default: False.
  3156. unitriangular (bool, optional): whether `x` is unit triangular. If True, the diagonal elements of `x` are assumed
  3157. to be 1 and not referenced from `x` . Default: False.
  3158. name (str, optional): Name for the operation (optional, default is None).
  3159. For more information, please refer to :ref:`api_guide_Name`.
  3160. Returns:
  3161. Tensor: The solution of the system of equations. Its data type should be the same as that of `x`.
  3162. Examples:
  3163. .. code-block:: python
  3164. >>> # a square system of linear equations:
  3165. >>> # x1 + x2 + x3 = 0
  3166. >>> # 2*x2 + x3 = -9
  3167. >>> # -x3 = 5
  3168. >>> import paddle
  3169. >>> x = paddle.to_tensor([[1, 1, 1],
  3170. ... [0, 2, 1],
  3171. ... [0, 0,-1]], dtype="float64")
  3172. >>> y = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
  3173. >>> out = paddle.linalg.triangular_solve(x, y, upper=True)
  3174. >>> print(out)
  3175. Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
  3176. [[ 7.],
  3177. [-2.],
  3178. [-5.]])
  3179. """
  3180. if in_dynamic_or_pir_mode():
  3181. return _C_ops.triangular_solve(x, y, upper, transpose, unitriangular)
  3182. else:
  3183. inputs = {"X": [x], "Y": [y]}
  3184. helper = LayerHelper("triangular_solve", **locals())
  3185. check_variable_and_dtype(
  3186. x,
  3187. 'x',
  3188. ['float32', 'float64', 'complex64', 'complex128'],
  3189. 'triangular_solve',
  3190. )
  3191. check_variable_and_dtype(
  3192. y,
  3193. 'y',
  3194. ['float32', 'float64', 'complex64', 'complex128'],
  3195. 'triangular_solve',
  3196. )
  3197. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  3198. helper.append_op(
  3199. type='triangular_solve',
  3200. inputs={'X': x, 'Y': y},
  3201. outputs={'Out': out},
  3202. attrs={
  3203. 'upper': upper,
  3204. 'transpose': transpose,
  3205. 'unitriangular': unitriangular,
  3206. },
  3207. )
  3208. return out
  3209. def cholesky_solve(x, y, upper=False, name=None):
  3210. r"""
  3211. Solves a linear system of equations A @ X = B, given A's Cholesky factor matrix u and matrix B.
  3212. Input `x` and `y` is 2D matrices or batches of 2D matrices. If the inputs are batches, the outputs
  3213. is also batches.
  3214. Args:
  3215. x (Tensor): Multiple right-hand sides of system of equations. Its shape should be `[*, M, K]`, where `*` is
  3216. zero or more batch dimensions. Its data type should be float32 or float64.
  3217. y (Tensor): The input matrix which is upper or lower triangular Cholesky factor of square matrix A. Its shape should be `[*, M, M]`, where `*` is zero or
  3218. more batch dimensions. Its data type should be float32 or float64.
  3219. upper (bool, optional): whether to consider the Cholesky factor as a lower or upper triangular matrix. Default: False.
  3220. name (str, optional): Name for the operation (optional, default is None).
  3221. For more information, please refer to :ref:`api_guide_Name`.
  3222. Returns:
  3223. Tensor: The solution of the system of equations. Its data type is the same as that of `x`.
  3224. Examples:
  3225. .. code-block:: python
  3226. >>> import paddle
  3227. >>> u = paddle.to_tensor([[1, 1, 1],
  3228. ... [0, 2, 1],
  3229. ... [0, 0,-1]], dtype="float64")
  3230. >>> b = paddle.to_tensor([[0], [-9], [5]], dtype="float64")
  3231. >>> out = paddle.linalg.cholesky_solve(b, u, upper=True)
  3232. >>> print(out)
  3233. Tensor(shape=[3, 1], dtype=float64, place=Place(cpu), stop_gradient=True,
  3234. [[-2.50000000],
  3235. [-7. ],
  3236. [ 9.50000000]])
  3237. """
  3238. if in_dynamic_or_pir_mode():
  3239. return _C_ops.cholesky_solve(x, y, upper)
  3240. else:
  3241. helper = LayerHelper("cholesky_solve", **locals())
  3242. check_variable_and_dtype(
  3243. x, 'x', ['float32', 'float64'], 'cholesky_solve'
  3244. )
  3245. check_variable_and_dtype(
  3246. y, 'y', ['float32', 'float64'], 'cholesky_solve'
  3247. )
  3248. out = helper.create_variable_for_type_inference(dtype=x.dtype)
  3249. helper.append_op(
  3250. type='cholesky_solve',
  3251. inputs={'X': x, 'Y': y},
  3252. outputs={'Out': out},
  3253. attrs={'upper': upper},
  3254. )
  3255. return out
  3256. def eigvalsh(x, UPLO='L', name=None):
  3257. """
  3258. Computes the eigenvalues of a
  3259. complex Hermitian (conjugate symmetric) or a real symmetric matrix.
  3260. Args:
  3261. x (Tensor): A tensor with shape :math:`[*, M, M]` , where * is zero or greater batch dimension. The data type of the input Tensor x
  3262. should be one of float32, float64, complex64, complex128.
  3263. UPLO(str, optional): Lower triangular part of a (‘L’, default) or the upper triangular part (‘U’).
  3264. name(str, optional): The default value is None. Normally there is no need for user to set this
  3265. property. For more information, please refer to :ref:`api_guide_Name`.
  3266. Returns:
  3267. Tensor: The tensor eigenvalues in ascending order.
  3268. Examples:
  3269. .. code-block:: python
  3270. >>> import paddle
  3271. >>> x = paddle.to_tensor([[1, -2j], [2j, 5]])
  3272. >>> out_value = paddle.eigvalsh(x, UPLO='L')
  3273. >>> print(out_value)
  3274. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3275. [0.17157286, 5.82842731])
  3276. """
  3277. if in_dynamic_mode():
  3278. values, _ = _C_ops.eigvalsh(x, UPLO, x.stop_gradient)
  3279. return values
  3280. def __check_input(x, UPLO):
  3281. x_shape = list(x.shape)
  3282. if len(x.shape) < 2:
  3283. raise ValueError(
  3284. "Input(input) only support >=2 tensor, but received "
  3285. "length of Input(input) is %s." % len(x.shape)
  3286. )
  3287. if x_shape[-1] != x_shape[-2]:
  3288. raise ValueError(
  3289. f"The input matrix must be batches of square matrices. But received x's dimension: {x_shape}"
  3290. )
  3291. if UPLO != 'L' and UPLO != 'U':
  3292. raise ValueError(
  3293. f"UPLO must be L or U. But received UPLO is: {UPLO}"
  3294. )
  3295. if in_pir_mode():
  3296. __check_input(x, UPLO)
  3297. values, _ = _C_ops.eigvalsh(x, UPLO, x.stop_gradient)
  3298. return values
  3299. else:
  3300. __check_input(x, UPLO)
  3301. helper = LayerHelper('eigvalsh', **locals())
  3302. check_variable_and_dtype(
  3303. x,
  3304. 'dtype',
  3305. ['float32', 'float64', 'complex64', 'complex128'],
  3306. 'eigvalsh',
  3307. )
  3308. out_value = helper.create_variable_for_type_inference(dtype=x.dtype)
  3309. out_vector = helper.create_variable_for_type_inference(dtype=x.dtype)
  3310. is_test = x.stop_gradient
  3311. helper.append_op(
  3312. type='eigvalsh',
  3313. inputs={'X': x},
  3314. outputs={'Eigenvalues': out_value, 'Eigenvectors': out_vector},
  3315. attrs={'UPLO': UPLO, 'is_test': is_test},
  3316. )
  3317. return out_value
  3318. def lstsq(x, y, rcond=None, driver=None, name=None):
  3319. """
  3320. Computes a solution to
  3321. the least squares problem of a system of linear equations.
  3322. Args:
  3323. x (Tensor): A tensor with shape ``(*, M, N)`` , the data type of the input Tensor ``x``
  3324. should be one of float32, float64.
  3325. y (Tensor): A tensor with shape ``(*, M, K)`` , the data type of the input Tensor ``y``
  3326. should be one of float32, float64.
  3327. rcond(float, optional): The default value is None. A float pointing number used to determine
  3328. the effective rank of ``x``. If ``rcond`` is None, it will be set to max(M, N) times the
  3329. machine precision of x_dtype.
  3330. driver(str, optional): The default value is None. The name of LAPACK method to be used. For
  3331. CPU inputs the valid values are ‘gels’, ‘gelsy’, ‘gelsd, ‘gelss’. For CUDA input, the only
  3332. valid driver is ‘gels’. If ``driver`` is None, ‘gelsy’ is used for CPU inputs and ‘gels’
  3333. for CUDA inputs.
  3334. name(str, optional): The default value is None. Normally there is no need for user to set
  3335. this property. For more information, please refer to :ref:`api_guide_Name`.
  3336. Returns:
  3337. Tuple: A tuple of 4 Tensors which is (``solution``, ``residuals``, ``rank``, ``singular_values``).
  3338. ``solution`` is a tensor with shape ``(*, N, K)``, meaning the least squares solution. ``residuals``
  3339. is a tensor with shape ``(*, K)``, meaning the squared residuals of the solutions, which is computed
  3340. when M > N and every matrix in ``x`` is full-rank, otherwise return an empty tensor. ``rank`` is a tensor
  3341. with shape ``(*)``, meaning the ranks of the matrices in ``x``, which is computed when ``driver`` in
  3342. (‘gelsy’, ‘gelsd’, ‘gelss’), otherwise return an empty tensor. ``singular_values`` is a tensor with
  3343. shape ``(*, min(M, N))``, meaning singular values of the matrices in ``x``, which is computed when
  3344. ``driver`` in (‘gelsd’, ‘gelss’), otherwise return an empty tensor.
  3345. Examples:
  3346. .. code-block:: python
  3347. >>> import paddle
  3348. >>> x = paddle.to_tensor([[1, 3], [3, 2], [5, 6.]])
  3349. >>> y = paddle.to_tensor([[3, 4, 6], [5, 3, 4], [1, 2, 1.]])
  3350. >>> results = paddle.linalg.lstsq(x, y, driver="gelsd")
  3351. >>> print(results[0])
  3352. Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  3353. [[ 0.78350395, -0.22165027, -0.62371236],
  3354. [-0.11340097, 0.78866047, 1.14948535]])
  3355. >>> print(results[1])
  3356. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  3357. [19.81443405, 10.43814468, 30.56185532])
  3358. >>> print(results[2])
  3359. Tensor(shape=[], dtype=int32, place=Place(cpu), stop_gradient=True,
  3360. 2)
  3361. >>> print(results[3])
  3362. Tensor(shape=[2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3363. [9.03455734, 1.54167950])
  3364. >>> x = paddle.to_tensor([[10, 2, 3], [3, 10, 5], [5, 6, 12.]])
  3365. >>> y = paddle.to_tensor([[4, 2, 9], [2, 0, 3], [2, 5, 3.]])
  3366. >>> results = paddle.linalg.lstsq(x, y, driver="gels")
  3367. >>> print(results[0])
  3368. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  3369. [[ 0.39386186, 0.10230169, 0.93606132],
  3370. [ 0.10741688, -0.29028130, 0.11892584],
  3371. [-0.05115093, 0.51918161, -0.19948851]])
  3372. >>> print(results[1])
  3373. Tensor(shape=[0], dtype=float32, place=Place(cpu), stop_gradient=True,
  3374. [])
  3375. """
  3376. device = paddle.get_device()
  3377. if device == "cpu":
  3378. if driver not in (None, "gels", "gelss", "gelsd", "gelsy"):
  3379. raise ValueError(
  3380. f"Only support valid driver is 'gels', 'gelss', 'gelsd', 'gelsy' or None for CPU inputs. But got {driver}"
  3381. )
  3382. driver = "gelsy" if driver is None else driver
  3383. elif "gpu" in device:
  3384. if driver not in (None, "gels"):
  3385. raise ValueError(
  3386. f"Only support valid driver is 'gels' or None for CUDA inputs. But got {driver}"
  3387. )
  3388. driver = "gels" if driver is None else driver
  3389. else:
  3390. raise RuntimeError("Only support lstsq api for CPU or CUDA device.")
  3391. if not (
  3392. x.dtype == y.dtype
  3393. and x.dtype
  3394. in (
  3395. paddle.float32,
  3396. paddle.float64,
  3397. paddle.base.core.DataType.FLOAT32,
  3398. paddle.base.core.DataType.FLOAT64,
  3399. )
  3400. ):
  3401. raise ValueError(
  3402. "Only support x and y have the same dtype such as 'float32' and 'float64'."
  3403. )
  3404. if x.ndim < 2:
  3405. raise ValueError(
  3406. f"The shape of x should be (*, M, N), but received ndim is [{x.ndim} < 2]"
  3407. )
  3408. if y.ndim < 2:
  3409. raise ValueError(
  3410. f"The shape of y should be (*, M, K), but received ndim is [{y.ndim} < 2]"
  3411. )
  3412. if x.shape[-2] != y.shape[-2]:
  3413. raise ValueError(
  3414. f"x with shape (*, M = {x.shape[-2]}, N) and y with shape (*, M = {y.shape[-2]}, K) should have same M."
  3415. )
  3416. if rcond is None:
  3417. if (
  3418. x.dtype == paddle.float32
  3419. or x.dtype == paddle.base.core.DataType.FLOAT32
  3420. ):
  3421. rcond = 1e-7 * max(x.shape[-2], x.shape[-1])
  3422. elif (
  3423. x.dtype == paddle.float64
  3424. or x.dtype == paddle.base.core.DataType.FLOAT64
  3425. ):
  3426. rcond = 1e-15 * max(x.shape[-2], x.shape[-1])
  3427. if in_dynamic_or_pir_mode():
  3428. solution, residuals, rank, singular_values = _C_ops.lstsq(
  3429. x, y, rcond, driver
  3430. )
  3431. if driver == "gels":
  3432. rank = paddle.empty(shape=[0], dtype="int32")
  3433. singular_values = paddle.empty(shape=[0], dtype=x.dtype)
  3434. elif driver == "gelsy":
  3435. singular_values = paddle.empty(shape=[0], dtype=x.dtype)
  3436. return solution, residuals, rank, singular_values
  3437. else:
  3438. helper = LayerHelper('lstsq', **locals())
  3439. check_variable_and_dtype(
  3440. x,
  3441. 'dtype',
  3442. ['float32', 'float64', 'complex64', 'complex128'],
  3443. 'lstsq',
  3444. )
  3445. check_variable_and_dtype(
  3446. y,
  3447. 'dtype',
  3448. ['float32', 'float64', 'complex64', 'complex128'],
  3449. 'lstsq',
  3450. )
  3451. solution = helper.create_variable_for_type_inference(dtype=x.dtype)
  3452. residuals = helper.create_variable_for_type_inference(dtype=x.dtype)
  3453. rank = helper.create_variable_for_type_inference(dtype=paddle.int32)
  3454. singular_values = helper.create_variable_for_type_inference(
  3455. dtype=x.dtype
  3456. )
  3457. helper.append_op(
  3458. type='lstsq',
  3459. inputs={'X': x, 'Y': y},
  3460. outputs={
  3461. 'Solution': solution,
  3462. 'Residuals': residuals,
  3463. 'Rank': rank,
  3464. 'SingularValues': singular_values,
  3465. },
  3466. attrs={'rcond': rcond, 'driver': driver},
  3467. )
  3468. if driver == "gels":
  3469. rank = paddle.static.data(name='rank', shape=[0])
  3470. singular_values = paddle.static.data(
  3471. name='singular_values', shape=[0]
  3472. )
  3473. elif driver == "gelsy":
  3474. singular_values = paddle.static.data(
  3475. name='singular_values', shape=[0]
  3476. )
  3477. return solution, residuals, rank, singular_values
  3478. def corrcoef(x, rowvar=True, name=None):
  3479. """
  3480. A correlation coefficient matrix indicate the correlation of each pair variables in the input matrix.
  3481. For example, for an N-dimensional samples X=[x1,x2,…xN]T, then the correlation coefficient matrix
  3482. element Rij is the correlation of xi and xj. The element Rii is the covariance of xi itself.
  3483. The relationship between the correlation coefficient matrix `R` and the
  3484. covariance matrix `C`, is
  3485. .. math:: R_{ij} = \\frac{ C_{ij} } { \\sqrt{ C_{ii} * C_{jj} } }
  3486. The values of `R` are between -1 and 1.
  3487. Args:
  3488. x (Tensor): A N-D(N<=2) Tensor containing multiple variables and observations. By default, each row of x represents a variable. Also see rowvar below.
  3489. rowvar (bool, optional): If rowvar is True (default), then each row represents a variable, with observations in the columns. Default: True.
  3490. name (str, optional): Name of the output. It's used to print debug info for developers. Details: :ref:`api_guide_Name`. Default: None.
  3491. Returns:
  3492. The correlation coefficient matrix of the variables.
  3493. Examples:
  3494. .. code-block:: python
  3495. >>> import paddle
  3496. >>> paddle.seed(2023)
  3497. >>> xt = paddle.rand((3,4))
  3498. >>> print(paddle.linalg.corrcoef(xt))
  3499. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  3500. [[ 0.99999988, -0.47689581, -0.89559376],
  3501. [-0.47689593, 1. , 0.16345492],
  3502. [-0.89559382, 0.16345496, 1. ]])
  3503. """
  3504. if len(x.shape) > 2 or len(x.shape) < 1:
  3505. raise ValueError(
  3506. "Input(x) only support N-D (1<=N<=2) tensor in corrcoef, but received "
  3507. "length of Input(input) is %s." % len(x.shape)
  3508. )
  3509. check_variable_and_dtype(x, 'dtype', ['float32', 'float64'], 'corrcoef')
  3510. c = cov(x, rowvar)
  3511. if c.ndim == 0:
  3512. # scalar covariance
  3513. # nan if incorrect value (nan, inf, 0), 1 otherwise
  3514. return c / c
  3515. d = paddle.diag(c)
  3516. if paddle.is_complex(d):
  3517. d = d.real()
  3518. stddev = paddle.sqrt(d)
  3519. c /= stddev[:, None]
  3520. c /= stddev[None, :]
  3521. # Clip to [-1, 1]. This does not guarantee
  3522. if paddle.is_complex(c):
  3523. return paddle.complex(
  3524. paddle.clip(c.real(), -1, 1), paddle.clip(c.imag(), -1, 1)
  3525. )
  3526. else:
  3527. c = paddle.clip(c, -1, 1)
  3528. return c
  3529. def cdist(
  3530. x, y, p=2.0, compute_mode="use_mm_for_euclid_dist_if_necessary", name=None
  3531. ):
  3532. r"""
  3533. Compute the p-norm distance between each pair of the two collections of inputs.
  3534. This function is equivalent to `scipy.spatial.distance.cdist(input,'minkowski', p=p)`
  3535. if :math:`p \in (0, \infty)`. When :math:`p = 0` it is equivalent to `scipy.spatial.distance.cdist(input, 'hamming') * M`.
  3536. When :math:`p = \infty`, the closest scipy function is `scipy.spatial.distance.cdist(xn, lambda x, y: np.abs(x - y).max())`.
  3537. Args:
  3538. x (Tensor): A tensor with shape :math:`B \times P \times M`.
  3539. y (Tensor): A tensor with shape :math:`B \times R \times M`.
  3540. p (float, optional): The value for the p-norm distance to calculate between each vector pair. Default: :math:`2.0`.
  3541. compute_mode (str, optional): The mode for compute distance.
  3542. - ``use_mm_for_euclid_dist_if_necessary`` , for p = 2.0 and (P > 25 or R > 25), it will use matrix multiplication to calculate euclid distance if possible.
  3543. - ``use_mm_for_euclid_dist`` , for p = 2.0, it will use matrix multiplication to calculate euclid distance.
  3544. - ``donot_use_mm_for_euclid_dist`` , it will not use matrix multiplication to calculate euclid distance.
  3545. Default: ``use_mm_for_euclid_dist_if_necessary``.
  3546. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  3547. Returns:
  3548. Tensor, the dtype is same as input tensor.
  3549. If x has shape :math:`B \times P \times M` and y has shape :math:`B \times R \times M` then
  3550. the output will have shape :math:`B \times P \times R`.
  3551. Examples:
  3552. .. code-block:: python
  3553. >>> import paddle
  3554. >>> x = paddle.to_tensor([[0.9041, 0.0196], [-0.3108, -2.4423], [-0.4821, 1.059]], dtype=paddle.float32)
  3555. >>> y = paddle.to_tensor([[-2.1763, -0.4713], [-0.6986, 1.3702]], dtype=paddle.float32)
  3556. >>> distance = paddle.cdist(x, y)
  3557. >>> print(distance)
  3558. Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3559. [[3.11927032, 2.09589314],
  3560. [2.71384072, 3.83217239],
  3561. [2.28300953, 0.37910119]])
  3562. """
  3563. check_variable_and_dtype(x, 'x', ('float32', 'float64'), 'cdist')
  3564. check_variable_and_dtype(y, 'y', ('float32', 'float64'), 'cdist')
  3565. check_type(p, 'p', (float, int), 'cdist')
  3566. if compute_mode not in [
  3567. 'use_mm_for_euclid_dist_if_necessary',
  3568. 'use_mm_for_euclid_dist',
  3569. 'donot_use_mm_for_euclid_dist',
  3570. ]:
  3571. raise ValueError(
  3572. "The compute_mode should be 'use_mm_for_euclid_dist_if_necessary', "
  3573. "'use_mm_for_euclid_dist' or 'donot_use_mm_for_euclid_dist', "
  3574. "but received compute_mode is %s." % compute_mode
  3575. )
  3576. mode = 0
  3577. if compute_mode == 'use_mm_for_euclid_dist_if_necessary':
  3578. mode = 0
  3579. elif compute_mode == 'use_mm_for_euclid_dist':
  3580. mode = 1
  3581. elif compute_mode == 'donot_use_mm_for_euclid_dist':
  3582. mode = 2
  3583. x_shape = list(x.shape)
  3584. assert len(x_shape) >= 2, (
  3585. "The x must be at least 2-dimensional, "
  3586. "But received Input x's dimensional is %s.\n" % len(x_shape)
  3587. )
  3588. y_shape = list(y.shape)
  3589. assert len(y_shape) >= 2, (
  3590. "The y must be at least 2-dimensional, "
  3591. "But received Input y's dimensional is %s.\n" % len(y_shape)
  3592. )
  3593. assert x_shape[-1] == y_shape[-1], (
  3594. "The x and y must have same last dimension, "
  3595. f"But received Input x's last dimension is {x_shape[-1]}, "
  3596. f"Input y's last dimension is {y_shape[-1]}.\n"
  3597. )
  3598. assert p >= 0, (
  3599. "The p must be greater than or equal to 0, "
  3600. "But received p is %s.\n" % p
  3601. )
  3602. r1 = x.shape[-2]
  3603. r2 = y.shape[-2]
  3604. c1 = x.shape[-1]
  3605. p = float(p)
  3606. if r1 == 0 or r2 == 0:
  3607. return paddle.empty((r1, r2), dtype=x.dtype)
  3608. if c1 == 0:
  3609. return paddle.zeros((r1, r2), dtype=x.dtype)
  3610. if p == 2.0 and (mode == 1 or (mode == 0 and (r1 > 25 or r2 > 25))):
  3611. x_norm = paddle.sum(x.pow(2), axis=-1, keepdim=True)
  3612. y_norm = paddle.sum(y.pow(2), axis=-1, keepdim=True)
  3613. y_transposed = paddle.transpose(
  3614. y, perm=[*range(y.ndim - 2), y.ndim - 1, y.ndim - 2]
  3615. )
  3616. y_norm_transposed = paddle.transpose(
  3617. y_norm,
  3618. perm=[*range(y_norm.ndim - 2), y_norm.ndim - 1, y_norm.ndim - 2],
  3619. )
  3620. res = paddle.matmul(x, y_transposed) * -2 + y_norm_transposed + x_norm
  3621. res = paddle.clip(res, min=0.0).sqrt()
  3622. return res
  3623. return paddle.linalg.norm(
  3624. x[..., None, :] - y[..., None, :, :], p=p, axis=-1
  3625. )
  3626. def householder_product(x, tau, name=None):
  3627. r"""
  3628. Computes the first n columns of a product of Householder matrices.
  3629. This function can get the vector :math:`\omega_{i}` from matrix `x` (m x n), the :math:`i-1` elements are zeros, and the i-th is `1`, the rest of the elements are from i-th column of `x`.
  3630. And with the vector `tau` can calculate the first n columns of a product of Householder matrices.
  3631. :math:`H_i = I_m - \tau_i \omega_i \omega_i^H`
  3632. Args:
  3633. x (Tensor): A tensor with shape (*, m, n) where * is zero or more batch dimensions.
  3634. tau (Tensor): A tensor with shape (*, k) where * is zero or more batch dimensions.
  3635. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  3636. Returns:
  3637. Tensor, the dtype is same as input tensor, the Q in QR decomposition.
  3638. :math:`out = Q = H_1H_2H_3...H_k`
  3639. Examples:
  3640. .. code-block:: python
  3641. >>> import paddle
  3642. >>> x = paddle.to_tensor([[-1.1280, 0.9012, -0.0190],
  3643. ... [ 0.3699, 2.2133, -1.4792],
  3644. ... [ 0.0308, 0.3361, -3.1761],
  3645. ... [-0.0726, 0.8245, -0.3812]])
  3646. >>> tau = paddle.to_tensor([1.7497, 1.1156, 1.7462])
  3647. >>> Q = paddle.linalg.householder_product(x, tau)
  3648. >>> print(Q)
  3649. Tensor(shape=[4, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  3650. [[-0.74969995, -0.02181768, 0.31115776],
  3651. [-0.64721400, -0.12367040, -0.21738708],
  3652. [-0.05389076, -0.37562513, -0.84836429],
  3653. [ 0.12702821, -0.91822827, 0.36892807]])
  3654. """
  3655. check_dtype(
  3656. x.dtype,
  3657. 'x',
  3658. [
  3659. 'float16',
  3660. 'float32',
  3661. 'float64',
  3662. 'complex64',
  3663. 'complex128',
  3664. ],
  3665. 'householder_product',
  3666. )
  3667. check_dtype(
  3668. tau.dtype,
  3669. 'tau',
  3670. [
  3671. 'float16',
  3672. 'float32',
  3673. 'float64',
  3674. 'complex64',
  3675. 'complex128',
  3676. ],
  3677. 'householder_product',
  3678. )
  3679. assert (
  3680. x.dtype == tau.dtype
  3681. ), "The input x must have the same dtype with input tau.\n"
  3682. assert (
  3683. len(x.shape) >= 2
  3684. and len(tau.shape) >= 1
  3685. and len(x.shape) == len(tau.shape) + 1
  3686. ), (
  3687. "The input x must have more than 2 dimensions, and input tau must have more than 1 dimension,"
  3688. "and the dimension of x is 1 larger than the dimension of tau\n"
  3689. )
  3690. assert (
  3691. x.shape[-2] >= x.shape[-1]
  3692. ), "The rows of input x must be greater than or equal to the columns of input x.\n"
  3693. assert (
  3694. x.shape[-1] >= tau.shape[-1]
  3695. ), "The last dim of x must be greater than tau.\n"
  3696. for idx, _ in enumerate(x.shape[:-2]):
  3697. assert (
  3698. x.shape[idx] == tau.shape[idx]
  3699. ), "The input x must have the same batch dimensions with input tau.\n"
  3700. def _householder_product(x, tau):
  3701. m, n = x.shape[-2:]
  3702. k = tau.shape[-1]
  3703. Q = paddle.eye(m).astype(x.dtype)
  3704. for i in range(min(k, n)):
  3705. w = x[i:, i]
  3706. if in_dynamic_mode():
  3707. w[0] = 1
  3708. else:
  3709. w = paddle.static.setitem(w, 0, 1)
  3710. w = w.reshape([-1, 1])
  3711. if in_dynamic_mode():
  3712. if x.dtype in [paddle.complex128, paddle.complex64]:
  3713. Q[:, i:] = Q[:, i:] - (
  3714. Q[:, i:] @ w @ paddle.conj(w).T * tau[i]
  3715. )
  3716. else:
  3717. Q[:, i:] = Q[:, i:] - (Q[:, i:] @ w @ w.T * tau[i])
  3718. else:
  3719. Q = paddle.static.setitem(
  3720. Q,
  3721. (slice(None), slice(i, None)),
  3722. Q[:, i:] - (Q[:, i:] @ w @ w.T * tau[i])
  3723. if x.dtype in [paddle.complex128, paddle.complex64]
  3724. else Q[:, i:] - (Q[:, i:] @ w @ w.T * tau[i]),
  3725. )
  3726. return Q[:, :n]
  3727. if len(x.shape) == 2:
  3728. return _householder_product(x, tau)
  3729. m, n = x.shape[-2:]
  3730. org_x_shape = x.shape
  3731. org_tau_shape = tau.shape
  3732. x = x.reshape((-1, org_x_shape[-2], org_x_shape[-1]))
  3733. tau = tau.reshape((-1, org_tau_shape[-1]))
  3734. n_batch = x.shape[0]
  3735. out = paddle.zeros([n_batch, m, n], dtype=x.dtype)
  3736. for i in range(n_batch):
  3737. if in_dynamic_mode():
  3738. out[i] = _householder_product(x[i], tau[i])
  3739. else:
  3740. out = paddle.static.setitem(
  3741. out, i, _householder_product(x[i], tau[i])
  3742. )
  3743. out = out.reshape(org_x_shape)
  3744. return out
  3745. # Reference: MatrixExponential, https://eigen.tuxfamily.org/dox/unsupported/MatrixExponential_8h_source.html
  3746. def _matrix_exp_pade3(mat_a, mat_i=None, mat_a2=None, *, dtype=None):
  3747. """3rd-order Pade approximant."""
  3748. b = [120.0, 60.0, 12.0]
  3749. if not paddle.framework.in_dynamic_mode():
  3750. b = [paddle.full((), x, dtype) for x in b]
  3751. if mat_a2 is None:
  3752. mat_a2, *_ = _matrix_mats(mat_a, 2, dtype)
  3753. tmp = mat_a2 + b[1] * mat_i
  3754. mat_u = paddle.matmul(mat_a, tmp)
  3755. mat_v = b[2] * mat_a2 + b[0] * mat_i
  3756. return mat_u, mat_v
  3757. def _matrix_exp_pade5(
  3758. mat_a, mat_i=None, mat_a2=None, mat_a4=None, *, dtype=None
  3759. ):
  3760. """5th-order Pade approximant."""
  3761. b = [30240.0, 15120.0, 3360.0, 420.0, 30.0]
  3762. if not paddle.framework.in_dynamic_mode():
  3763. b = [paddle.full((), x, dtype) for x in b]
  3764. if mat_a4 is None:
  3765. mat_a2, mat_a4, *_ = _matrix_mats(mat_a, 4, dtype)
  3766. tmp = mat_a4 + b[3] * mat_a2 + b[1] * mat_i
  3767. mat_u = paddle.matmul(mat_a, tmp)
  3768. mat_v = b[4] * mat_a4 + b[2] * mat_a2 + b[0] * mat_i
  3769. return mat_u, mat_v
  3770. def _matrix_exp_pade7(
  3771. mat_a, mat_i=None, mat_a2=None, mat_a4=None, mat_a6=None, *, dtype=None
  3772. ):
  3773. """7th-order Pade approximant."""
  3774. b = [17297280.0, 8648640.0, 1995840.0, 277200.0, 25200.0, 1512.0, 56.0]
  3775. if not paddle.framework.in_dynamic_mode():
  3776. b = [paddle.full((), x, dtype) for x in b]
  3777. if mat_a6 is None:
  3778. mat_a2, mat_a4, mat_a6, *_ = _matrix_mats(mat_a, 6, dtype)
  3779. tmp = mat_a6 + b[5] * mat_a4 + b[3] * mat_a2 + b[1] * mat_i
  3780. mat_u = paddle.matmul(mat_a, tmp)
  3781. mat_v = b[6] * mat_a6 + b[4] * mat_a4 + b[2] * mat_a2 + b[0] * mat_i
  3782. return mat_u, mat_v
  3783. def _matrix_exp_pade9(
  3784. mat_a,
  3785. mat_i=None,
  3786. mat_a2=None,
  3787. mat_a4=None,
  3788. mat_a6=None,
  3789. mat_a8=None,
  3790. *,
  3791. dtype=None,
  3792. ):
  3793. """9th-order Pade approximant."""
  3794. b = [
  3795. 17643225600.0,
  3796. 8821612800.0,
  3797. 2075673600.0,
  3798. 302702400.0,
  3799. 30270240.0,
  3800. 2162160.0,
  3801. 110880.0,
  3802. 3960.0,
  3803. 90.0,
  3804. ]
  3805. if not paddle.framework.in_dynamic_mode():
  3806. b = [paddle.full((), x, dtype) for x in b]
  3807. if mat_a8 is None:
  3808. mat_a2, mat_a4, mat_a6, mat_a8, *_ = _matrix_mats(mat_a, 8, dtype)
  3809. tmp = mat_a8 + b[7] * mat_a6 + b[5] * mat_a4 + b[3] * mat_a2 + b[1] * mat_i
  3810. mat_u = paddle.matmul(mat_a, tmp)
  3811. mat_v = (
  3812. b[8] * mat_a8
  3813. + b[6] * mat_a6
  3814. + b[4] * mat_a4
  3815. + b[2] * mat_a2
  3816. + b[0] * mat_i
  3817. )
  3818. return mat_u, mat_v
  3819. def _matrix_exp_pade13(
  3820. mat_a, mat_i=None, mat_a2=None, mat_a4=None, mat_a6=None, *, dtype=None
  3821. ):
  3822. """13th-order Pade approximant."""
  3823. b = [
  3824. 64764752532480000.0,
  3825. 32382376266240000.0,
  3826. 7771770303897600.0,
  3827. 1187353796428800.0,
  3828. 129060195264000.0,
  3829. 10559470521600.0,
  3830. 670442572800.0,
  3831. 33522128640.0,
  3832. 1323241920.0,
  3833. 40840800.0,
  3834. 960960.0,
  3835. 16380.0,
  3836. 182.0,
  3837. ]
  3838. if not paddle.framework.in_dynamic_mode():
  3839. b = [paddle.full((), x, dtype) for x in b]
  3840. if mat_a6 is None:
  3841. mat_a2, mat_a4, mat_a6, *_ = _matrix_mats(mat_a, 6, dtype)
  3842. tmp_u = (
  3843. paddle.matmul(mat_a6, mat_a6 + b[11] * mat_a4 + b[9] * mat_a2)
  3844. + b[7] * mat_a6
  3845. + b[5] * mat_a4
  3846. + b[3] * mat_a2
  3847. + b[1] * mat_i
  3848. )
  3849. mat_u = paddle.matmul(mat_a, tmp_u)
  3850. tmp_v = b[12] * mat_a6 + b[10] * mat_a4 + b[8] * mat_a2
  3851. mat_v = (
  3852. paddle.matmul(mat_a6, tmp_v)
  3853. + b[6] * mat_a6
  3854. + b[4] * mat_a4
  3855. + b[2] * mat_a2
  3856. + b[0] * mat_i
  3857. )
  3858. return mat_u, mat_v
  3859. def _matrix_uv_where(vals, cases, l1_norm):
  3860. if len(vals) == 1:
  3861. return paddle.where(
  3862. paddle.less_than(l1_norm, vals[0]), cases[0], cases[1]
  3863. )
  3864. else:
  3865. return paddle.where(
  3866. paddle.less_than(l1_norm, vals[0]),
  3867. cases[0],
  3868. _matrix_uv_where(vals[1:], cases[1:], l1_norm),
  3869. )
  3870. def _matrix_mats(mat_a, total, dtype):
  3871. mat_a2 = paddle.matmul(mat_a, mat_a)
  3872. mat_a4 = None
  3873. mat_a6 = None
  3874. mat_a8 = None
  3875. if total > 2:
  3876. mat_a4 = paddle.matmul(mat_a2, mat_a2)
  3877. if total > 4:
  3878. mat_a6 = paddle.matmul(mat_a4, mat_a2)
  3879. if total > 6:
  3880. mat_a8 = paddle.matmul(mat_a6, mat_a2)
  3881. return mat_a2, mat_a4, mat_a6, mat_a8
  3882. def _matrix_uv_float32(mat_a, l1_norm, squarings, dtype):
  3883. mat_i = paddle.eye(mat_a.shape[-1], dtype=dtype)
  3884. mat_a2, mat_a4, *_ = _matrix_mats(mat_a, 4, dtype)
  3885. u3, v3 = _matrix_exp_pade3(mat_a, mat_i, mat_a2, dtype=dtype)
  3886. u5, v5 = _matrix_exp_pade5(mat_a, mat_i, mat_a2, mat_a4, dtype=dtype)
  3887. u7, v7 = _matrix_exp_pade7(
  3888. mat_a
  3889. / paddle.cast(
  3890. paddle.pow(paddle.full((), 2.0, dtype), squarings),
  3891. dtype,
  3892. ),
  3893. mat_i,
  3894. dtype=dtype,
  3895. )
  3896. conds = (
  3897. paddle.full((), 4.258730016922831e-001, dtype),
  3898. paddle.full((), 1.880152677804762e000, dtype),
  3899. )
  3900. u = _matrix_uv_where(conds, (u3, u5, u7), l1_norm)
  3901. v = _matrix_uv_where(conds, (v3, v5, v7), l1_norm)
  3902. return u, v
  3903. def _matrix_uv_float64(mat_a, l1_norm, squarings, dtype):
  3904. mat_i = paddle.eye(mat_a.shape[-1], dtype=dtype)
  3905. mat_a2, mat_a4, mat_a6, mat_a8, *_ = _matrix_mats(mat_a, 8, dtype)
  3906. u3, v3 = _matrix_exp_pade3(mat_a, mat_i, mat_a2, dtype=dtype)
  3907. u5, v5 = _matrix_exp_pade5(mat_a, mat_i, mat_a2, mat_a4, dtype=dtype)
  3908. u7, v7 = _matrix_exp_pade7(
  3909. mat_a, mat_i, mat_a2, mat_a4, mat_a6, dtype=dtype
  3910. )
  3911. u9, v9 = _matrix_exp_pade9(
  3912. mat_a, mat_i, mat_a2, mat_a4, mat_a6, mat_a8, dtype=dtype
  3913. )
  3914. u13, v13 = _matrix_exp_pade13(
  3915. mat_a
  3916. / paddle.cast(
  3917. paddle.pow(paddle.full((), 2.0, dtype), squarings),
  3918. dtype,
  3919. ),
  3920. mat_i,
  3921. dtype=dtype,
  3922. )
  3923. conds = (
  3924. paddle.full((), 1.495585217958292e-002, dtype),
  3925. paddle.full((), 2.539398330063230e-001, dtype),
  3926. paddle.full((), 9.504178996162932e-001, dtype),
  3927. paddle.full((), 2.097847961257068e000, dtype),
  3928. )
  3929. u = _matrix_uv_where(conds, (u3, u5, u7, u9, u13), l1_norm)
  3930. v = _matrix_uv_where(conds, (v3, v5, v7, v9, v13), l1_norm)
  3931. return u, v
  3932. def matrix_exp(x, name=None):
  3933. r"""
  3934. Computes the matrix exponential of square matrices.
  3935. .. math::
  3936. exp(A) = \sum_{n=0}^\infty A^n/n!
  3937. The input tensor x should be of square matrices with shape like :math:`(*, M, M)`, and the
  3938. exponential output is computed by Pade approximation of the scaling and squaring method.
  3939. [1] Nicholas J. Higham, The scaling and squaring method for the matrix exponential revisited.
  3940. Args:
  3941. x (Tensor): A tensor with shape :math:`(*, M, M)` where :math:`*` is zero or more batch dimensions. The data type should be one of float32, float64.
  3942. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  3943. Returns:
  3944. Tensor, the shape and dtype are same as input tensor.
  3945. Examples:
  3946. .. code-block:: python
  3947. >>> import paddle
  3948. >>> mat_a = paddle.empty((2, 2, 2))
  3949. >>> mat_a[0, :, :] = paddle.eye(2, 2)
  3950. >>> mat_a[1, :, :] = 2 * paddle.eye(2, 2)
  3951. >>> print(mat_a)
  3952. Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3953. [[[1., 0.],
  3954. [0., 1.]],
  3955. [[2., 0.],
  3956. [0., 2.]]])
  3957. >>> out = paddle.linalg.matrix_exp(mat_a)
  3958. >>> print(out)
  3959. Tensor(shape=[2, 2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3960. [[[2.71828198, 0. ],
  3961. [0. , 2.71828198]],
  3962. [[7.38905621, 0. ],
  3963. [0. , 7.38905621]]])
  3964. >>> import math
  3965. >>> mat_a = paddle.to_tensor([[0, math.pi/3], [-math.pi/3, 0]])
  3966. >>> out = paddle.linalg.matrix_exp(mat_a)
  3967. >>> print(out)
  3968. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  3969. [[ 0.49999994, 0.86602545],
  3970. [-0.86602551, 0.50000000]])
  3971. """
  3972. # convert to tensor if necessary
  3973. if not isinstance(
  3974. x,
  3975. (
  3976. paddle.Tensor,
  3977. paddle.base.framework.Variable,
  3978. paddle.base.libpaddle.pir.Value,
  3979. ),
  3980. ):
  3981. mat_a = paddle.to_tensor(x)
  3982. else:
  3983. mat_a = x
  3984. dtype = convert_dtype(mat_a.dtype)
  3985. # check dtype, shape
  3986. if dtype not in ['float32', 'float64']:
  3987. raise ValueError(
  3988. f"The input tensor's dtype must be float32 or float64, but got {dtype}"
  3989. )
  3990. # 0-dim
  3991. if mat_a.ndim == 0:
  3992. return paddle.exp(mat_a)
  3993. # check tensor dim
  3994. if mat_a.ndim < 2:
  3995. raise ValueError('The input tensor must be at least two-dimensional')
  3996. if mat_a.shape[-1] != mat_a.shape[-2]:
  3997. raise ValueError('Last 2 dimensions of the tensor must be square')
  3998. # scalar case
  3999. if list(mat_a.shape[-2:]) == [1, 1]:
  4000. return paddle.exp(mat_a)
  4001. # compute uv
  4002. l1_norm = paddle.unsqueeze(
  4003. paddle.max(paddle.sum(paddle.abs(mat_a), axis=mat_a.ndim - 2), axis=-1),
  4004. axis=[-1, -2],
  4005. )
  4006. squarings = paddle.full(mat_a.shape, 0, dtype)
  4007. _matrix_uv_func = None
  4008. # dtype already checked before, we use `if-elif` only
  4009. if dtype == 'float32':
  4010. maxnorm = paddle.full((), 3.925724783138660, dtype)
  4011. squarings = paddle.floor(
  4012. paddle.log(l1_norm / maxnorm)
  4013. / paddle.log(paddle.full((), 2.0, dtype))
  4014. )
  4015. squarings = paddle.maximum(squarings, paddle.zeros_like(squarings))
  4016. _matrix_uv_func = _matrix_uv_float32
  4017. elif dtype == 'float64':
  4018. maxnorm = paddle.full((), 5.371920351148152, dtype)
  4019. squarings = paddle.floor(
  4020. paddle.log(l1_norm / maxnorm)
  4021. / paddle.log(paddle.full((), 2.0, dtype))
  4022. )
  4023. squarings = paddle.maximum(squarings, paddle.zeros_like(squarings))
  4024. _matrix_uv_func = _matrix_uv_float64
  4025. u, v = _matrix_uv_func(mat_a, l1_norm, squarings, dtype)
  4026. # compute result
  4027. is_finite = paddle.isfinite(paddle.max(l1_norm))
  4028. result = paddle.static.nn.cond(
  4029. is_finite,
  4030. lambda: paddle.linalg.solve(-u + v, u + v),
  4031. lambda: paddle.full(mat_a.shape, np.nan, dtype),
  4032. )
  4033. max_squaring = paddle.max(squarings)
  4034. i = paddle.full((), 0, dtype)
  4035. def cond(i, _):
  4036. return paddle.static.nn.cond(
  4037. is_finite,
  4038. lambda: paddle.less_than(i, max_squaring),
  4039. lambda: paddle.full((), False),
  4040. )
  4041. def body(i, result):
  4042. return i + 1, paddle.where(
  4043. paddle.less_than(i, squarings),
  4044. paddle.matmul(result, result),
  4045. result,
  4046. )
  4047. _, result = paddle.static.nn.while_loop(cond, body, [i, result])
  4048. return result
  4049. def histogramdd(
  4050. x, bins=10, ranges=None, density=False, weights=None, name=None
  4051. ):
  4052. r"""
  4053. Computes a multi-dimensional histogram of the values in a tensor.
  4054. Interprets the elements of an input tensor whose innermost dimension has size `N` as a collection of N-dimensional points. Maps each of the points into a set of N-dimensional bins and returns the number of points (or total weight) in each bin.
  4055. input `x` must be a tensor with at least 2 dimensions. If input has shape `(M, N)`, each of its `M` rows defines a point in N-dimensional space. If input has three or more dimensions, all but the last dimension are flattened.
  4056. Each dimension is independently associated with its own strictly increasing sequence of bin edges. Bin edges may be specified explicitly by passing a sequence of 1D tensors. Alternatively, bin edges may be constructed automatically by passing a sequence of integers specifying the number of equal-width bins in each dimension.
  4057. Args:
  4058. x (Tensor): The input tensor.
  4059. bins (Tensor[], int[], or int): If Tensor[], defines the sequences of bin edges. If int[], defines the number of equal-width bins in each dimension. If int, defines the number of equal-width bins for all dimensions.
  4060. ranges (sequence of float, optional): Defines the leftmost and rightmost bin edges in each dimension. If is None, set the minimum and maximum as leftmost and rightmost edges for each dimension.
  4061. density (bool, optional): If False (default), the result will contain the count (or total weight) in each bin. If True, each count (weight) is divided by the total count (total weight), then divided by the volume of its associated bin.
  4062. weights (Tensor, optional): By default, each value in the input has weight 1. If a weight tensor is passed, each N-dimensional coordinate in input contributes its associated weight towards its bin’s result. The weight tensor should have the same shape as the input tensor excluding its innermost dimension N.
  4063. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  4064. Returns:
  4065. N-dimensional Tensor containing the values of the histogram. ``bin_edges(Tensor[])``, sequence of N 1D Tensors containing the bin edges.
  4066. Examples:
  4067. .. code-block:: python
  4068. :name: exampl
  4069. >>> import paddle
  4070. >>> x = paddle.to_tensor([[0., 1.], [1., 0.], [2.,0.], [2., 2.]])
  4071. >>> bins = [3,3]
  4072. >>> weights = paddle.to_tensor([1., 2., 4., 8.])
  4073. >>> paddle.histogramdd(x, bins=bins, weights=weights)
  4074. (Tensor(shape=[3, 3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4075. [[0., 1., 0.],
  4076. [2., 0., 0.],
  4077. [4., 0., 8.]]), [Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4078. [0. , 0.66666669, 1.33333337, 2. ]), Tensor(shape=[4], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4079. [0. , 0.66666669, 1.33333337, 2. ])])
  4080. .. code-block:: python
  4081. :name: examp2
  4082. >>> import paddle
  4083. >>> y = paddle.to_tensor([[0., 0.], [1., 1.], [2., 2.]])
  4084. >>> bins = [2,2]
  4085. >>> ranges = [0., 1., 0., 1.]
  4086. >>> density = True
  4087. >>> paddle.histogramdd(y, bins=bins, ranges=ranges, density=density)
  4088. (Tensor(shape=[2, 2], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4089. [[2., 0.],
  4090. [0., 2.]]), [Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4091. [0. , 0.50000000, 1. ]), Tensor(shape=[3], dtype=float32, place=Place(gpu:0), stop_gradient=True,
  4092. [0. , 0.50000000, 1. ])])
  4093. """
  4094. def __check_x(x):
  4095. assert (
  4096. len(x.shape) >= 2
  4097. ), "input x must be a tensor with at least 2 dimensions."
  4098. check_variable_and_dtype(
  4099. x,
  4100. 'x',
  4101. [
  4102. 'float32',
  4103. 'float64',
  4104. ],
  4105. 'histogramdd',
  4106. )
  4107. def __check_bins(bins, x): # when Tensor[], check dtype
  4108. for bins_tensor in bins:
  4109. bins_tensor = paddle.to_tensor(bins_tensor)
  4110. check_variable_and_dtype(
  4111. bins_tensor,
  4112. 'bins',
  4113. [
  4114. 'float32',
  4115. 'float64',
  4116. ],
  4117. 'histogramdd',
  4118. )
  4119. assert (
  4120. bins_tensor.dtype == x.dtype
  4121. ), "When bins is Tensor[], the dtype of bins must be the same as x.\n"
  4122. def __check_weights(x, weights):
  4123. if weights is None:
  4124. return
  4125. x_shape, weights_shape = x.shape, weights.shape
  4126. assert len(x_shape) == len(weights_shape) + 1, (
  4127. "if weight tensor is provided,"
  4128. "it should have the same shape as the input tensor excluding its innermost dimension.\n"
  4129. )
  4130. for i, _ in enumerate(weights_shape):
  4131. assert weights_shape[i] == x_shape[i], (
  4132. "if weight tensor is provided,"
  4133. "it should have the same shape as the input tensor excluding its innermost dimension.\n"
  4134. )
  4135. check_variable_and_dtype(
  4136. weights,
  4137. 'weights',
  4138. [
  4139. 'float32',
  4140. 'float64',
  4141. ],
  4142. 'histogramdd',
  4143. )
  4144. assert (
  4145. weights.dtype == x.dtype
  4146. ), "The dtype of weights must be the same as x.\n"
  4147. def __check_ranges(D, ranges):
  4148. if ranges is None:
  4149. return
  4150. check_type(ranges, 'ranges', (list, tuple), 'histogramdd')
  4151. assert D * 2 == len(
  4152. ranges
  4153. ), "The length of ranges list must be %d\n" % (D * 2)
  4154. check_type(density, 'density', bool, 'histogramdd')
  4155. __check_x(x)
  4156. # weights
  4157. __check_weights(x, weights)
  4158. D = x.shape[-1]
  4159. reshaped_input = x.reshape([-1, D])
  4160. N = reshaped_input.shape[0]
  4161. reshaped_weights = None
  4162. if weights is not None:
  4163. weights = weights.astype(x.dtype)
  4164. reshaped_weights = weights.reshape([N])
  4165. assert reshaped_weights.shape[0] == N, (
  4166. "The size of weight must be %d" % N
  4167. )
  4168. # ranges
  4169. __check_ranges(D, ranges)
  4170. if ranges is None:
  4171. ranges = paddle.zeros([D, 2], dtype=x.dtype)
  4172. maxv = paddle.max(reshaped_input, axis=0).reshape([-1])
  4173. minv = paddle.min(reshaped_input, axis=0).reshape([-1])
  4174. if paddle.in_dynamic_mode():
  4175. ranges[:, 0] = minv
  4176. ranges[:, 1] = maxv
  4177. else:
  4178. ranges = paddle.static.setitem(ranges, (slice(None), 0), minv)
  4179. ranges = paddle.static.setitem(ranges, (slice(None), 1), maxv)
  4180. else:
  4181. ranges = paddle.to_tensor(ranges, dtype=x.dtype).reshape([D, 2])
  4182. # bins to edges
  4183. edges = []
  4184. hist_shape = []
  4185. dedges = []
  4186. if isinstance(bins, (int, list)): # int or int[]
  4187. if isinstance(bins, int):
  4188. bins = [bins] * D
  4189. assert len(bins) == D, (
  4190. "The length of bins must be %d when bins is a list.\n" % D
  4191. )
  4192. for idx, r in enumerate(ranges):
  4193. if not isinstance(bins[idx], int):
  4194. raise ValueError(
  4195. "The type of %d-th element in bins list must be int." % idx
  4196. )
  4197. e = paddle.linspace(r[0], r[1], bins[idx] + 1, x.dtype)
  4198. edges.append(e)
  4199. dedges.append(e.diff())
  4200. elif isinstance(
  4201. bins, tuple
  4202. ): # tuple with D tensors for each innermost dimension
  4203. __check_bins(bins, x)
  4204. for bin in bins:
  4205. bin = paddle.to_tensor(bin)
  4206. edges.append(bin)
  4207. dedges.append(bin.diff())
  4208. else:
  4209. raise ValueError("Input bins must be Tensor[], int[], or int.")
  4210. hist_shape = [edge.shape[0] + 1 for edge in edges]
  4211. index_list = []
  4212. # edges shape: [D, linspaced]
  4213. # index_list shape: [D, N]
  4214. for idx, edge in enumerate(edges):
  4215. edge = paddle.to_tensor(edge)
  4216. index_list.append(
  4217. paddle.searchsorted(edge, reshaped_input[:, idx], right=True)
  4218. )
  4219. index_list = paddle.to_tensor(index_list)
  4220. for i in range(D):
  4221. on_edge = reshaped_input[:, i] == edges[i][-1]
  4222. if paddle.in_dynamic_mode():
  4223. index_list[i] = paddle.where(
  4224. on_edge, index_list[i] - 1, index_list[i]
  4225. )
  4226. else:
  4227. index_list_i = paddle.where(
  4228. on_edge, index_list[i] - 1, index_list[i]
  4229. )
  4230. index_list = paddle.static.setitem(index_list, i, index_list_i)
  4231. index_list = tuple(index_list)
  4232. lut = paddle.arange(
  4233. paddle.to_tensor(hist_shape).prod(),
  4234. ).reshape(hist_shape)
  4235. flattened_index = lut[index_list]
  4236. hist = paddle.bincount(
  4237. flattened_index,
  4238. reshaped_weights,
  4239. minlength=paddle.to_tensor(hist_shape).prod(),
  4240. )
  4241. hist = hist.reshape(hist_shape)
  4242. hist = hist.astype(x.dtype)
  4243. core = D * (slice(1, -1),)
  4244. hist = hist[core]
  4245. if density:
  4246. s = hist.sum()
  4247. for i in range(D):
  4248. shape = D * [1]
  4249. shape[i] = hist_shape[i] - 2
  4250. hist = hist / dedges[i].reshape(shape)
  4251. hist /= s
  4252. return (hist, edges)
  4253. def ormqr(x, tau, y, left=True, transpose=False, name=None):
  4254. r'''
  4255. Calculate the product of a normal matrix and a householder matrix.
  4256. Compute the product of the matrix C (given by y) with dimensions (m, n) and a matrix Q,
  4257. where Q is generated by the Householder reflection coefficient (x, tau). Returns a Tensor.
  4258. Args:
  4259. x (Tensor): Shape(\*,mn, k), when left is True, the value of mn is equal to m, otherwise the value of mn is equal to n. \* indicates that the length of the tensor on axis 0 is 0 or greater.
  4260. tau (Tensor): Shape (\*, min(mn, k)), where \* indicates that the length of the Tensor on axis 0 is 0 or greater, and its type is the same as input.
  4261. y (Tensor): Shape (\*m,n), where \* indicates that the length of the Tensor on axis 0 is 0 or greater, and its type is the same as input.
  4262. left (bool, optional): Determines the order in which the matrix product operations are operated. If left is true, the order of evaluation is op(Q) \* y, otherwise, the order of evaluation is y \* op(Q). Default value: True.
  4263. transpose (bool, optional): If true, the matrix Q is conjugated and transposed, otherwise, the conjugate transpose transformation is not performed. Default value: False.
  4264. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  4265. Returns:
  4266. Tensor. Data type and dimension are equals with :attr:`y`.
  4267. Examples:
  4268. .. code-block:: python
  4269. >>> import paddle
  4270. >>> import numpy as np
  4271. >>> from paddle import linalg
  4272. >>> input = paddle.to_tensor([[-114.6, 10.9, 1.1], [-0.304, 38.07, 69.38], [-0.45, -0.17, 62]])
  4273. >>> tau = paddle.to_tensor([1.55, 1.94, 3.0])
  4274. >>> y = paddle.to_tensor([[-114.6, 10.9, 1.1], [-0.304, 38.07, 69.38], [-0.45, -0.17, 62]])
  4275. >>> output = linalg.ormqr(input, tau, y)
  4276. >>> print(output)
  4277. Tensor(shape=[3, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  4278. [[ 63.82712936 , -13.82312393 , -116.28614044],
  4279. [-53.65926361 , -28.15783691 , -70.42700958 ],
  4280. [-79.54292297 , 24.00182915 , -41.34253311 ]])
  4281. '''
  4282. check_dtype(
  4283. y.dtype,
  4284. 'y',
  4285. [
  4286. 'float16',
  4287. 'float32',
  4288. 'float64',
  4289. 'complex64',
  4290. 'complex128',
  4291. ],
  4292. 'ormqr',
  4293. )
  4294. check_type(left, 'left', bool, 'ormqr')
  4295. check_type(transpose, 'transpose', bool, 'ormqr')
  4296. assert (
  4297. x.dtype == tau.dtype and x.dtype == y.dtype
  4298. ), "The input tau and y must have the same dtype with the x.\n"
  4299. assert (
  4300. len(x.shape) >= 2 and len(y.shape) >= 2 and len(tau.shape) >= 1
  4301. ), "The input x and y must have more than 2 dimensions, and input tau must have more than 1 dimension"
  4302. assert len(x.shape) == len(tau.shape) + 1 and len(x.shape) == len(
  4303. y.shape
  4304. ), "the dimension of x is 1 larger than the dimension of tau\n and the dimension of x is equal to the dimension of input"
  4305. assert (
  4306. x.shape[-1] == tau.shape[-1]
  4307. ), "The innermost dimension of x and tau should be the same"
  4308. if transpose and left:
  4309. assert (
  4310. x.shape[-2] == y.shape[-2]
  4311. ), "The row dimensions of x and y should be the same"
  4312. elif not transpose and left:
  4313. assert (
  4314. x.shape[-1] == y.shape[-2]
  4315. ), "The column dimension of x and the row dimension of y should be the same"
  4316. elif transpose and not left:
  4317. assert (
  4318. x.shape[-2] == y.shape[-1]
  4319. ), "The row dimension of x and the column dimension of y should be the same"
  4320. else:
  4321. assert (
  4322. x.shape[-1] == y.shape[-1]
  4323. ), "The column dimensions of Impt and Osser's should be the same"
  4324. if len(x.shape) == 3:
  4325. assert (
  4326. x.shape[0] == y.shape[0] and x.shape[0] == tau.shape[0]
  4327. ), "The input and tau and y parameters should have the same batch"
  4328. Q = householder_product(x, tau)
  4329. if len(x.shape) == 2:
  4330. Q = Q.T if transpose else Q
  4331. else:
  4332. Q = paddle.transpose(Q, [0, 2, 1]) if transpose else Q
  4333. result = matmul(Q, y) if left else matmul(y, Q)
  4334. return result