| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977 |
- # Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
- #
- # Licensed under the Apache License, Version 2.0 (the "License");
- # you may not use this file except in compliance with the License.
- # You may obtain a copy of the License at
- #
- # http://www.apache.org/licenses/LICENSE-2.0
- #
- # Unless required by applicable law or agreed to in writing, software
- # distributed under the License is distributed on an "AS IS" BASIS,
- # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- # See the License for the specific language governing permissions and
- # limitations under the License.
- # TODO: define functions to get create a tensor
- import math
- import re
- import numpy as np
- import paddle
- from paddle import _C_ops
- from paddle.utils.inplace_utils import inplace_apis_in_dygraph_only
- from ..base.data_feeder import (
- check_dtype,
- check_type,
- check_variable_and_dtype,
- convert_dtype,
- convert_float_to_uint16,
- )
- from ..base.framework import Variable, device_guard
- from ..base.param_attr import ParamAttr
- from ..framework import (
- LayerHelper,
- _current_expected_place,
- _current_expected_place_,
- _get_paddle_place,
- convert_np_dtype_to_dtype_,
- core,
- dygraph_only,
- in_dynamic_mode,
- in_dynamic_or_pir_mode,
- in_pir_mode,
- )
- __all__ = []
- def _complex_to_real_dtype(dtype):
- if dtype == core.VarDesc.VarType.COMPLEX64:
- return core.VarDesc.VarType.FP32
- elif dtype == core.VarDesc.VarType.COMPLEX128:
- return core.VarDesc.VarType.FP64
- elif dtype == paddle.pir.core.DataType.COMPLEX64:
- return paddle.pir.core.DataType.FP32
- elif dtype == paddle.pir.core.DataType.COMPLEX128:
- return paddle.pir.core.DataType.FP64
- else:
- return dtype
- def _real_to_complex_dtype(dtype):
- if dtype == core.VarDesc.VarType.FP32:
- return core.VarDesc.VarType.COMPLEX64
- elif dtype == core.VarDesc.VarType.FP64:
- return core.VarDesc.VarType.COMPLEX128
- elif dtype == paddle.pir.core.DataType.FP32:
- return paddle.pir.core.DataType.COMPLEX64
- elif dtype == paddle.pir.core.DataType.FP64:
- return paddle.pir.core.DataType.COMPLEX128
- else:
- return dtype
- def create_global_var(
- shape, value, dtype, persistable=False, force_cpu=False, name=None
- ):
- """
- This function creates a new tensor variable with value in the global block(block 0).
- Args:
- shape (list[int]|tuple[int]): Shape of the variable
- value (float): The value of the variable. The new created
- variable will be filled with it.
- dtype (str): Data type of the variable
- persistable (bool, optional): If this variable is persistable.
- Default: False
- force_cpu (bool, optional): Force this variable to be on CPU.
- Default: False
- name (str, optional): For detailed information, please refer to
- :ref:`api_guide_Name` . Usually name is no need to set and None by default.
- Returns:
- Variable: The created Variable
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> paddle.enable_static()
- >>> var = paddle.static.create_global_var(shape=[2,3], value=1.0, dtype='float32',
- ... persistable=True, force_cpu=True, name='new_var')
- """
- check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_global_var')
- for item in shape:
- check_type(
- item,
- 'item of shape',
- (
- int,
- np.uint8,
- np.int8,
- np.int16,
- np.int32,
- np.int64,
- ),
- 'create_global_var',
- )
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'uint8',
- 'uint16',
- ],
- 'create_global_var',
- )
- helper = LayerHelper("global_var", **locals())
- var = helper.create_global_variable(
- dtype=dtype,
- shape=shape,
- persistable=persistable,
- name=name,
- stop_gradient=True,
- )
- helper.set_variable_initializer(
- var,
- initializer=paddle.nn.initializer.ConstantInitializer(
- value=float(value), force_cpu=force_cpu
- ),
- )
- return var
- def create_parameter(
- shape, dtype, name=None, attr=None, is_bias=False, default_initializer=None
- ):
- """
- This function creates a parameter. The parameter is a learnable variable, which can have
- gradient, and can be optimized.
- Note:
- This is a very low-level API. This API is useful when you create operator by your self, instead of using layers.
- Args:
- shape (list of int): Shape of the parameter
- dtype (str): Data type of the parameter. It can be set as 'float16', 'float32', 'float64'.
- name (str, optional): For detailed information, please refer to
- :ref:`api_guide_Name` . Usually name is no need to set and None by default.
- attr (ParamAttr, optional): Attribute object of the specified argument. For detailed information, please refer to
- :ref:`api_paddle_ParamAttr` None by default, which means that ParamAttr will be initialized as it is.
- is_bias (bool, optional): This can affect which default initializer is chosen
- when default_initializer is None. If is_bias,
- initializer.Constant(0.0) will be used. Otherwise,
- Xavier() will be used.
- default_initializer (Initializer, optional): Initializer for the parameter
- Returns:
- The created parameter.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> paddle.enable_static()
- >>> W = paddle.create_parameter(shape=[784, 200], dtype='float32')
- """
- check_type(shape, 'shape', (list, tuple, np.ndarray), 'create_parameter')
- for item in shape:
- check_type(
- item,
- 'item of shape',
- (
- int,
- np.uint8,
- np.int8,
- np.int16,
- np.int32,
- np.int64,
- ),
- 'create_parameter',
- )
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'uint16',
- 'float32',
- 'float64',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'uint8',
- ],
- 'create_parameter',
- )
- check_type(attr, 'attr', (type(None), ParamAttr), 'create_parameter')
- check_type(
- default_initializer,
- 'default_initializer',
- (type(None), paddle.nn.initializer.Initializer),
- 'create_parameter',
- )
- helper = LayerHelper("create_parameter", **locals())
- if attr is None:
- attr = ParamAttr(name=name)
- return helper.create_parameter(
- attr, shape, convert_dtype(dtype), is_bias, default_initializer
- )
- def create_tensor(dtype, name=None, persistable=False):
- """
- Create a variable, which will hold a Tensor with data type dtype.
- Args:
- dtype(string|numpy.dtype): the data type of Tensor to be created, the
- data type is bool, float16, float32, float64, int8, int16, int32 and int64.
- name(string, optional): The default value is None. Normally there is no need for
- user to set this property. For more information, please refer to :ref:`api_guide_Name`
- persistable(bool): Set the persistable flag of the create tensor.
- default value is False.
- Returns:
- Variable: The tensor to be created according to dtype.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> tensor = paddle.tensor.create_tensor(dtype='float32')
- """
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'int32',
- 'int32',
- 'int64',
- ],
- 'create_tensor',
- )
- helper = LayerHelper("create_tensor", **locals())
- return helper.create_variable(
- name=helper.name, dtype=dtype, persistable=persistable
- )
- def linspace(start, stop, num, dtype=None, name=None):
- r"""
- Return fixed number of evenly spaced values within a given interval. Note: no gradient calculation is performed.
- Args:
- start(int|float|Tensor): The input :attr:`start` is start of range. It is a int, float, \
- or a 0-D Tensor with data type int32, int64, float32 or float64.
- stop(int|float|Tensor): The input :attr:`stop` is end of range. It is a int, float, \
- or a 0-D Tensor with data type int32, int64, float32 or float64.
- num(int|Tensor): The input :attr:`num` is given num of the sequence. It is an int, \
- or a 0-D Tensor with data type int32.
- dtype(np.dtype|str, optional): The data type of output tensor, it could be
- int32, int64, float32 and float64. Default: if None, the data type is float32.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: the output data type will be float32, float64. The 1-D tensor with fixed number of evenly spaced values, \
- the data shape of this tensor is :math:`[num]` . If the :attr:`num` is set 1, the output tensor just has \
- the value with input :attr:`start`.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> data = paddle.linspace(0, 10, 5, 'float32')
- >>> print(data.numpy())
- [0. 2.5 5. 7.5 10.]
- >>> data = paddle.linspace(0, 10, 1, 'float32')
- >>> print(data.numpy())
- [0.]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- tensor_num = num
- tensor_start = start
- tensor_stop = stop
- if not isinstance(num, (Variable, paddle.pir.Value)):
- check_type(num, 'num', (int), 'linspace')
- if not isinstance(dtype, (core.VarDesc.VarType, paddle.pir.core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if not isinstance(start, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_start = fill_constant([1], dtype, start, force_cpu=True)
- if not isinstance(stop, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_stop = fill_constant([1], dtype, stop, force_cpu=True)
- if not isinstance(num, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_num = fill_constant([1], 'int32', num, force_cpu=True)
- if in_dynamic_or_pir_mode():
- return _C_ops.linspace(
- tensor_start,
- tensor_stop,
- tensor_num,
- dtype,
- _current_expected_place(),
- )
- else:
- helper = LayerHelper("linspace", **locals())
- start_dtype = convert_dtype(tensor_start.dtype)
- stop_dtype = convert_dtype(tensor_stop.dtype)
- out_dtype = convert_dtype(dtype)
- if isinstance(start, Variable):
- check_dtype(
- start.dtype,
- 'start',
- ['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
- 'linspace',
- )
- else:
- check_type(start, 'start', (int, float), 'linspace')
- if isinstance(stop, Variable):
- check_dtype(
- stop.dtype,
- 'stop',
- ['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
- 'linspace',
- )
- else:
- check_type(stop, 'stop', (int, float), 'linspace')
- if isinstance(num, Variable):
- check_dtype(num.dtype, 'num', ['int32'], 'linspace')
- check_dtype(
- dtype,
- 'dtype',
- ['float16', 'uint16', 'float32', 'float64', 'int32', 'int64'],
- 'linspace',
- )
- if (
- (stop_dtype == "float64" or start_dtype == "float64")
- and out_dtype in ["float32", "int32"]
- ) or (
- (stop_dtype == "int64" or start_dtype == "int64")
- and out_dtype == "int32"
- ):
- raise ValueError(
- f"The dtype of start/stop is {start_dtype}/{stop_dtype} but the attr(dtype) of linspace is {dtype}, "
- "which may cause data type overflows. Please reset attr(dtype) of linspace."
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='linspace',
- inputs={
- 'Start': tensor_start,
- 'Stop': tensor_stop,
- 'Num': tensor_num,
- },
- attrs={'dtype': dtype},
- outputs={'Out': [out]},
- )
- if isinstance(num, int):
- out.desc.set_shape((num,))
- return out
- def logspace(start, stop, num, base=10.0, dtype=None, name=None):
- r"""
- Return fixed number of logarithmical-evenly spaced values within the interval \
- :math:`[base^{start}, base^{stop}]`.
- Notes:
- This API does not compute the gradient.
- Args:
- start(int|float|Tensor): The input :attr:`start` is exponent of first entry in \
- the sequence. It is a scalar, or a 0-D Tensor of shape [] with input data \
- type int32, int64, float32 or float64.
- stop(int|float|Tensor): The input :attr:`stop` is exponent of last entry in the \
- sequence. It is a scalar, or a 0-D Tensor of shape [] with input data \
- type int32, int64, float32 or float64.
- num(int|Tensor): The input :attr:`num` is given number of items in the sequence. \
- It is an int scalar, or a 0-D Tensor of shape [] with data type int32.
- base(int|float|Tensor): The input :attr:`base` is base of the logarithm function. \
- It is a scalar, or a 0-D Tensor of shape [] with input data type int32, int64, \
- float32 or float64.
- dtype(np.dtype|str, optional): The data type of output tensor, it could be \
- int32, int64, float32 or float64. Default: if None, the data type is float32. \
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: The output data type will be float32, float64. The 1-D tensor with \
- fixed number of logarithmical-evenly spaced values, the data shape of this \
- tensor is :math:`[num]`. If the :attr:`num` is set 1, the output tensor \
- just has the value with exponential of :attr:`start` with base :attr:`base`.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> data = paddle.logspace(0, 10, 5, 2, 'float32')
- >>> print(data.numpy())
- [1.0000000e+00 5.6568542e+00 3.2000000e+01 1.8101933e+02 1.0240000e+03]
- >>> data = paddle.logspace(0, 10, 1, 2, 'float32')
- >>> print(data.numpy())
- [1.]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- tensor_num = num
- tensor_start = start
- tensor_stop = stop
- tensor_base = base
- if not isinstance(num, (Variable, paddle.pir.Value)):
- check_type(num, 'num', (int), 'logspace')
- if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if not isinstance(start, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_start = fill_constant([1], dtype, start)
- if not isinstance(stop, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_stop = fill_constant([1], dtype, stop)
- if not isinstance(num, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_num = fill_constant([1], 'int32', num)
- if not isinstance(base, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- tensor_base = fill_constant([1], dtype, base)
- if in_dynamic_or_pir_mode():
- return _C_ops.logspace(
- tensor_start,
- tensor_stop,
- tensor_num,
- tensor_base,
- dtype,
- _current_expected_place(),
- )
- else:
- helper = LayerHelper("logspace", **locals())
- start_dtype = convert_dtype(tensor_start.dtype)
- stop_dtype = convert_dtype(tensor_stop.dtype)
- base_dtype = convert_dtype(tensor_base.dtype)
- out_dtype = convert_dtype(dtype)
- if isinstance(start, Variable):
- check_dtype(
- start.dtype,
- 'start',
- ['float32', 'float64', 'int32', 'int64'],
- 'logspace',
- )
- else:
- check_type(start, 'start', (int, float), 'logspace')
- if isinstance(stop, Variable):
- check_dtype(
- stop.dtype,
- 'stop',
- ['float32', 'float64', 'int32', 'int64'],
- 'logspace',
- )
- else:
- check_type(stop, 'stop', (int, float), 'logspace')
- if isinstance(num, Variable):
- check_dtype(num.dtype, 'num', ['int32'], 'logspace')
- if isinstance(base, Variable):
- check_dtype(
- base.dtype,
- 'base',
- ['float32', 'float64', 'int32', 'int64'],
- 'logspace',
- )
- else:
- check_type(base, 'base', (int, float), 'logspace')
- check_dtype(
- dtype, 'dtype', ['int32', 'int64', 'float32', 'float64'], 'logspace'
- )
- if (
- (
- stop_dtype == "float64"
- or start_dtype == "float64"
- or base_dtype == "float64"
- )
- and out_dtype in ["float32", "int32"]
- ) or (
- (
- stop_dtype == "int64"
- or start_dtype == "int64"
- or base_dtype == "int64"
- )
- and out_dtype == "int32"
- ):
- raise ValueError(
- f"The dtype of start/stop/base is {start_dtype}/{stop_dtype}/{base_dtype} but the attr(dtype) of logspace is {dtype}, "
- "which may cause data type overflows. Please reset attr(dtype) of logspace."
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='logspace',
- inputs={
- 'Start': tensor_start,
- 'Stop': tensor_stop,
- 'Num': tensor_num,
- 'Base': tensor_base,
- },
- attrs={'dtype': dtype},
- outputs={'Out': [out]},
- )
- if isinstance(num, int):
- out.desc.set_shape((num,))
- return out
- def _to_tensor_non_static(data, dtype=None, place=None, stop_gradient=True):
- def _handle_tensor_dtype(tensor, dtype):
- if dtype:
- if convert_dtype(dtype) != convert_dtype(tensor.dtype):
- return tensor.astype(convert_dtype(dtype))
- return tensor
- def _handle_np_dtype(ndarray, dtype):
- if dtype:
- if convert_dtype(dtype) != convert_dtype(ndarray.dtype):
- # should not ndarray.astype('uint16') directly, data bits is wrong
- if convert_dtype(dtype) in ['uint16']:
- return convert_float_to_uint16(ndarray.astype('float32'))
- else:
- return ndarray.astype(convert_dtype(dtype))
- return ndarray
- if isinstance(data, np.number): # Special case for numpy scalars
- data = np.array(data)
- if not isinstance(data, np.ndarray):
- if np.isscalar(data) and not isinstance(data, str):
- data = np.array(data)
- elif isinstance(data, (list, tuple)):
- data = np.array(data)
- if data.dtype == np.object_:
- raise ValueError(
- "\n\tFailed to convert input data to a regular ndarray :\n\t - Usually "
- "this means the input data contains nested lists with different lengths. "
- )
- elif isinstance(data, paddle.Tensor) and not in_dynamic_mode():
- data = data._copy_to(place, False)
- data = _handle_tensor_dtype(data, dtype)
- data.stop_gradient = stop_gradient
- return data
- elif isinstance(data, core.eager.Tensor) and in_dynamic_mode():
- data = data._copy_to(place, False)
- data = _handle_tensor_dtype(data, dtype)
- data.stop_gradient = stop_gradient
- return data
- elif isinstance(data, (core.LoDTensor, core.Tensor)):
- # should't expose it to users, just for internal use.
- # convert core.Tensor/core.LoDTensor to Tensor first
- # Currently, there is no copy when places are same
- if in_dynamic_mode():
- data = core.eager.Tensor(data)
- else:
- data = paddle.Tensor(data)
- if not data.place._equals(place):
- data = data._copy_to(place, False)
- data = _handle_tensor_dtype(data, dtype)
- data.stop_gradient = stop_gradient
- return data
- else:
- raise TypeError(
- f"Can't constructs a 'paddle.Tensor' with data type {type(data)}, data type must be scalar|list|tuple|np.ndarray|paddle.Tensor"
- )
- if not dtype:
- if data.dtype in [
- 'float16',
- 'float32',
- 'float64',
- 'complex64',
- 'complex128',
- ]:
- default_type = paddle.get_default_dtype()
- if np.iscomplexobj(data):
- default_type = (
- 'complex64'
- if default_type in ['float16', 'float32']
- else 'complex128'
- )
- data = _handle_np_dtype(data, default_type)
- # Windows default type is 'int32', while Linux/Mac is 'int64'. Unify they.
- if data.dtype in ['int32']:
- data = data.astype("int64")
- if dtype:
- data = _handle_np_dtype(data, dtype)
- if isinstance(data, np.ndarray):
- return core.eager.Tensor(
- value=data,
- place=place,
- persistable=False,
- zero_copy=False,
- name=None,
- stop_gradient=stop_gradient,
- )
- else:
- return paddle.Tensor(
- value=data,
- place=place,
- persistable=False,
- zero_copy=False,
- stop_gradient=stop_gradient,
- )
- def _to_tensor_static(data, dtype=None, stop_gradient=None):
- if isinstance(data, (Variable, paddle.pir.Value)):
- output = data
- if dtype is not None and dtype != data.dtype:
- output = paddle.cast(output, dtype)
- else:
- if isinstance(data, np.number): # Special case for numpy scalars
- data = np.array(data)
- if not isinstance(data, np.ndarray):
- if np.isscalar(data) and not isinstance(data, str):
- data = np.array(data)
- elif isinstance(data, (list, tuple)):
- try:
- '''
- In numpy version >= 1.24.0, case like:
- np.array([Variable, 1, 2])
- is not supported, it will raise error (numpy returns an numpy array with dtype='object' in version <= 1.23.5)
- Thus, process nested structure in except block
- '''
- data = np.array(data)
- # for numpy version <= 1.23.5
- if data.dtype == 'object':
- raise RuntimeError("Numpy get dtype `object`.")
- except:
- to_stack_list = [None] * len(data)
- for idx, d in enumerate(data):
- to_stack_list[idx] = _to_tensor_static(
- d, dtype, stop_gradient
- )
- data = paddle.stack(to_stack_list)
- else:
- raise RuntimeError(
- f"Do not support transform type `{type(data)}` to tensor"
- )
- # fix numpy default dtype
- if data.dtype in ['float16', 'float32', 'float64']:
- data = data.astype(paddle.get_default_dtype())
- # Windows default type is 'int32', while Linux/Mac is 'int64'. Unify they.
- elif data.dtype in ['int32']:
- data = data.astype("int64")
- if dtype:
- target_dtype = dtype
- elif hasattr(data, 'dtype') and data.dtype != 'object':
- target_dtype = data.dtype
- else:
- target_dtype = paddle.get_default_dtype()
- target_dtype = convert_dtype(target_dtype)
- if data.dtype == "int16":
- data = data.astype("int32")
- output = assign(data)
- if convert_dtype(output.dtype) != target_dtype:
- output = paddle.cast(output, target_dtype)
- output.stop_gradient = stop_gradient
- return output
- def to_tensor(data, dtype=None, place=None, stop_gradient=True):
- r"""
- Constructs a ``paddle.Tensor`` from ``data`` ,
- which can be scalar, tuple, list, numpy\.ndarray, paddle\.Tensor.
- If the ``data`` is already a Tensor, copy will be performed and return a new tensor.
- If you only want to change stop_gradient property, please call ``Tensor.stop_gradient = stop_gradient`` directly.
- .. code-block:: text
- We use the dtype conversion rules following this:
- Keep dtype
- np.number ───────────► paddle.Tensor
- (0-D Tensor)
- default_dtype
- Python Number ───────────────► paddle.Tensor
- (0-D Tensor)
- Keep dtype
- np.ndarray ───────────► paddle.Tensor
- Args:
- data(scalar|tuple|list|ndarray|Tensor): Initial data for the tensor.
- Can be a scalar, list, tuple, numpy\.ndarray, paddle\.Tensor.
- dtype(str|np.dtype, optional): The desired data type of returned tensor. Can be 'bool' , 'float16' ,
- 'float32' , 'float64' , 'int8' , 'int16' , 'int32' , 'int64' , 'uint8',
- 'complex64' , 'complex128'. Default: None, infers dtype from ``data``
- except for python float number which gets dtype from ``get_default_type`` .
- place(CPUPlace|CUDAPinnedPlace|CUDAPlace|str, optional): The place to allocate Tensor. Can be
- CPUPlace, CUDAPinnedPlace, CUDAPlace. Default: None, means global place. If ``place`` is
- string, It can be ``cpu``, ``gpu:x`` and ``gpu_pinned``, where ``x`` is the index of the GPUs.
- stop_gradient(bool, optional): Whether to block the gradient propagation of Autograd. Default: True.
- Returns:
- Tensor: A Tensor constructed from ``data`` .
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> type(paddle.to_tensor(1))
- <class 'paddle.Tensor'>
- >>> paddle.to_tensor(1)
- Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
- 1)
- >>> x = paddle.to_tensor(1, stop_gradient=False)
- >>> print(x)
- Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=False,
- 1)
- >>> paddle.to_tensor(x) # A new tensor will be created with default stop_gradient=True
- Tensor(shape=[], dtype=int64, place=Place(cpu), stop_gradient=True,
- 1)
- >>> paddle.to_tensor([[0.1, 0.2], [0.3, 0.4]], place=paddle.CPUPlace(), stop_gradient=False)
- Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=False,
- [[0.10000000, 0.20000000],
- [0.30000001, 0.40000001]])
- >>> type(paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64'))
- <class 'paddle.Tensor'>
- >>> paddle.to_tensor([[1+1j, 2], [3+2j, 4]], dtype='complex64')
- Tensor(shape=[2, 2], dtype=complex64, place=Place(cpu), stop_gradient=True,
- [[(1+1j), (2+0j)],
- [(3+2j), (4+0j)]])
- """
- place = _get_paddle_place(place)
- if place is None:
- place = _current_expected_place_()
- if in_dynamic_mode():
- return _to_tensor_non_static(data, dtype, place, stop_gradient)
- # call assign for static graph
- else:
- re_exp = re.compile(r'[(](.+?)[)]', re.S)
- place_str = re.findall(re_exp, str(place))[0]
- with paddle.static.device_guard(place_str):
- return _to_tensor_static(data, dtype, stop_gradient)
- def full_like(x, fill_value, dtype=None, name=None):
- """
- This function creates a tensor filled with ``fill_value`` which has identical shape of ``x`` and ``dtype``.
- If the ``dtype`` is None, the data type of Tensor is same with ``x``.
- Args:
- x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64.
- fill_value(bool|float|int): The value to fill the tensor with. Note: this value shouldn't exceed the range of the output data type.
- dtype(np.dtype|str, optional): The data type of output. The data type can be one
- of bool, float16, float32, float64, int32, int64. The default value is None, which means the output
- data type is the same as input.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Tensor which is created according to ``x``, ``fill_value`` and ``dtype``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> input = paddle.full(shape=[2, 3], fill_value=0.0, dtype='float32', name='input')
- >>> output = paddle.full_like(input, 2.0)
- >>> print(output.numpy())
- [[2. 2. 2.]
- [2. 2. 2.]]
- """
- if dtype is None:
- dtype = x.dtype
- else:
- if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if in_dynamic_mode():
- return _C_ops.full_like(x, fill_value, dtype, x.place)
- elif in_pir_mode():
- return _C_ops.full_like(x, fill_value, dtype, core.Place())
- else:
- helper = LayerHelper("full_like", **locals())
- check_variable_and_dtype(
- x,
- 'x',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int16',
- 'int32',
- 'int64',
- 'uint16',
- ],
- 'full_like',
- )
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int16',
- 'int32',
- 'int64',
- 'uint16',
- ],
- 'full_like/zeros_like/ones_like',
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='fill_any_like',
- inputs={'X': [x]},
- attrs={'value': fill_value, "dtype": dtype},
- outputs={'Out': [out]},
- )
- out.stop_gradient = True
- return out
- def fill_constant(shape, dtype, value, force_cpu=False, out=None, name=None):
- if in_dynamic_or_pir_mode():
- place = _current_expected_place()
- if force_cpu:
- place = core.CPUPlace()
- if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if in_pir_mode() and isinstance(dtype, core.VarDesc.VarType):
- dtype = paddle.pir.core.vartype_to_datatype[dtype]
- if in_dynamic_mode():
- value = float(value)
- if isinstance(shape, (list, tuple)):
- shape = paddle.utils.convert_shape_to_list(shape)
- else:
- paddle.utils.check_shape(shape)
- if isinstance(shape, (list, tuple)):
- if paddle.utils._contain_var(shape):
- shape = paddle.utils.get_int_tensor_list(shape)
- elif isinstance(shape, paddle.pir.Value):
- pass
- else:
- raise TypeError("Shape only supports Value, or list, or tuple.")
- if out is None:
- out = _C_ops.full(shape, value, dtype, place)
- out.stop_gradient = True
- return out
- if out is not None:
- _C_ops.full_(out, shape, value, dtype, place)
- out.stop_gradient = True
- return out
- else:
- attrs = {'force_cpu': force_cpu}
- dtype = convert_dtype(dtype)
- if not isinstance(value, Variable):
- if dtype in ['int8', 'uint8', 'int16', 'int32', 'int64']:
- attrs['str_value'] = str(int(value))
- attrs['value'] = int(value)
- else:
- attrs['str_value'] = str(float(value))
- attrs['value'] = float(value)
- helper = LayerHelper("fill_constant", **locals())
- inputs = {}
- if isinstance(value, Variable):
- if convert_dtype(value.dtype) != dtype:
- value = paddle.cast(value, dtype)
- inputs['ValueTensor'] = value
- paddle.utils.check_shape(shape)
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'uint8',
- 'int16',
- 'int32',
- 'int64',
- 'complex64',
- 'complex128',
- 'uint16',
- ],
- 'fill_constant',
- )
- check_type(shape, 'shape', (Variable, list, tuple), 'fill_constant')
- if out is not None:
- check_variable_and_dtype(
- out, 'out', [convert_dtype(dtype)], 'fill_constant'
- )
- helper = LayerHelper("fill_constant", **locals())
- paddle.utils.get_shape_tensor_inputs(
- inputs=inputs, attrs=attrs, shape=shape, op_type='fill_constant'
- )
- if out is None:
- out = helper.create_variable_for_type_inference(dtype=dtype)
- attrs['dtype'] = out.dtype
- helper.append_op(
- type='fill_constant',
- inputs=inputs,
- outputs={'Out': [out]},
- attrs=attrs,
- stop_gradient=True,
- )
- out.stop_gradient = True
- return out
- def ones(shape, dtype=None, name=None):
- """
- Create a Tensor of specified :attr:`shape` and :attr:`dtype` and fill it with 1.
- Args:
- shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` .
- If ``shape`` is a list or tuple, the elements of it should be integers or 0-D Tensor with shape [].
- If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list.
- dtype (np.dtype|str, optional): Data type of output Tensor, it should be one of
- bool, float16, float32, float64, int32 and int64. If it is set to None, the data type will be float32.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: A Tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements are 1.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # shape is a list/tuple
- >>> data1 = paddle.ones(shape=[3, 2])
- >>> print(data1.numpy())
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- >>> # shape is a Tensor
- >>> shape = paddle.to_tensor([3, 2])
- >>> data2 = paddle.ones(shape=shape)
- >>> print(data2.numpy())
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- >>> # shape is a Tensor List
- >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
- >>> data3 = paddle.ones(shape=shape)
- >>> print(data3.numpy())
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- return fill_constant(value=1.0, shape=shape, dtype=dtype, name=name)
- def ones_like(x, dtype=None, name=None):
- """
- Returns a Tensor filled with the value 1, with the same shape and
- data type (use ``dtype`` if ``dtype`` is not None) as ``x``.
- Args:
- x(Tensor): The input tensor which specifies shape and dtype. The
- dtype of ``x`` can be bool, float16, float32, float64, int32, int64.
- dtype(str|np.dtype, optional): The data type of the
- output tensor. Supported data types: bool, float16, float32, float64,
- int32, int64. If ``dtype`` is None, the data type is the same as ``x``.
- Default is None.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: A Tensor filled with the value 1, with the same shape and
- data type (use ``dtype`` if ``dtype`` is not None) as ``x``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.to_tensor([1,2,3])
- >>> out1 = paddle.ones_like(x)
- >>> print(out1.numpy())
- [1 1 1]
- >>> out2 = paddle.ones_like(x, dtype='int32')
- >>> print(out2.numpy())
- [1 1 1]
- """
- return full_like(x=x, fill_value=1, dtype=dtype, name=name)
- def zeros(shape, dtype=None, name=None):
- """
- Creates a tensor of specified :attr:`shape` and :attr:`dtype`, and fills it with 0.
- Args:
- shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` .
- If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape [].
- If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list.
- dtype(np.dtype|str, optional): Data type of output Tensor, it supports
- bool, float16, float32, float64, int32 and int64. Default: if None, the data type is float32.
- name(str, optional): The default value is None. Normally there is no need for user to set this
- property. For more information, please refer to :ref:`api_guide_Name`.
- Returns:
- Tensor: A tensor of data type :attr:`dtype` with shape :attr:`shape` and all elements set to 0.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # shape is a list/tuple
- >>> data1 = paddle.zeros(shape=[3, 2])
- >>> print(data1.numpy())
- [[0. 0.]
- [0. 0.]
- [0. 0.]]
- >>> # shape is a Tensor
- >>> shape = paddle.to_tensor([3, 2])
- >>> data2 = paddle.zeros(shape=shape)
- >>> print(data2.numpy())
- [[0. 0.]
- [0. 0.]
- [0. 0.]]
- >>> # shape is a Tensor List
- >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
- >>> data3 = paddle.zeros(shape=shape)
- >>> print(data3.numpy())
- [[0. 0.]
- [0. 0.]
- [0. 0.]]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- return fill_constant(value=0.0, shape=shape, dtype=dtype, name=name)
- def zeros_like(x, dtype=None, name=None):
- """
- Returns a Tensor filled with the value 0, with the same shape and
- data type (use ``dtype`` if ``dtype`` is not None) as ``x``.
- Args:
- x(Tensor): The input tensor which specifies shape and dtype. The
- dtype of ``x`` can be bool, float16, float32, float64, int32, int64.
- dtype(str|np.dtype, optional): The data type of the
- output tensor. Supported data types: bool, float16, float32, float64,
- int32, int64. If ``dtype`` is None, the data type is the same as ``x``.
- Default is None.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: A Tensor filled with the value 0, with the same shape and
- data type (use ``dtype`` if ``dtype`` is not None) as ``x``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.to_tensor([1, 2, 3])
- >>> out1 = paddle.zeros_like(x)
- >>> print(out1.numpy())
- [0 0 0]
- >>> out2 = paddle.zeros_like(x, dtype='int32')
- >>> print(out2.numpy())
- [0 0 0]
- """
- return full_like(x=x, fill_value=0, dtype=dtype, name=name)
- def eye(num_rows, num_columns=None, dtype=None, name=None):
- """
- This function constructs 2-D Tensor with ones on the diagonal and zeros elsewhere.
- Args:
- num_rows(int): the number of rows in each batch Tensor.
- num_columns(int, optional): the number of columns in each batch Tensor.
- If None, default: num_rows.
- dtype(np.dtype|str, optional): The data type of the returned Tensor.
- It should be int32, int64, float16, float32, float64, complex64, complex128. Default: if None, the data type
- is float32.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: An identity Tensor or LoDTensor of shape [num_rows, num_columns].
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> data = paddle.eye(3, dtype='int32')
- >>> print(data.numpy())
- [[1 0 0]
- [0 1 0]
- [0 0 1]]
- >>> data = paddle.eye(2, 3, dtype='int32')
- >>> print(data.numpy())
- [[1 0 0]
- [0 1 0]]
- """
- def _check_attr(attr, message):
- if isinstance(attr, ((Variable, core.eager.Tensor, paddle.pir.Value))):
- assert len(attr.shape) == 1 and attr.shape[0] in [1, -1]
- elif not isinstance(attr, int) or attr < 0:
- raise TypeError(f"{message} should be a non-negative int.")
- _check_attr(num_rows, "num_rows")
- if dtype is None:
- dtype = paddle.get_default_dtype()
- if not isinstance(dtype, (core.VarDesc.VarType, paddle.pir.core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if num_columns is not None:
- _check_attr(num_columns, "num_columns")
- else:
- num_columns = num_rows
- if in_dynamic_or_pir_mode():
- out = _C_ops.eye(
- num_rows, num_columns, dtype, _current_expected_place()
- )
- else:
- helper = LayerHelper("eye", **locals())
- check_dtype(
- dtype,
- 'dtype',
- [
- 'float16',
- 'float32',
- 'float64',
- 'uint16',
- 'int32',
- 'int64',
- 'complex64',
- 'comple128',
- ],
- 'eye',
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='eye',
- inputs={},
- outputs={'Out': [out]},
- attrs={
- 'num_rows': num_rows,
- 'num_columns': num_columns,
- 'dtype': dtype,
- },
- stop_gradient=True,
- )
- out.stop_gradient = True
- return out
- def full(shape, fill_value, dtype=None, name=None):
- """
- Return a Tensor with the ``fill_value`` which size is same as ``shape``.
- Args:
- shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` .
- If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape [].
- If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list.
- fill_value(bool|float|int|Tensor): The constant value used to initialize the Tensor to be created.
- If ``fill_value`` is an Tensor, it should be an 0-D Tensor which represents a scalar.
- dtype(np.dtype|str, optional): Data type of the output Tensor
- which can be float16, float32, float64, int32, int64, if dtype is `None`, the data
- type of created Tensor is `float32`.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Tensor which is created according to ``shape``, ``fill_value`` and ``dtype``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # shape is a list/tuple
- >>> data1 = paddle.full(shape=[3, 2], fill_value=1.)
- >>> print(data1.numpy())
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- >>> # shape is a Tensor
- >>> shape = paddle.to_tensor([3, 2])
- >>> data2 = paddle.full(shape=shape, fill_value=2.)
- >>> print(data2.numpy())
- [[2. 2.]
- [2. 2.]
- [2. 2.]]
- >>> # shape is a Tensor List
- >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
- >>> data3 = paddle.full(shape=shape, fill_value=3.)
- >>> print(data3.numpy())
- [[3. 3.]
- [3. 3.]
- [3. 3.]]
- >>> # fill_value is a Tensor.
- >>> val = paddle.full([], 2.0, "float32")
- >>> data5 = paddle.full(shape=[3, 2], fill_value=val)
- >>> print(data5.numpy())
- [[2. 2.]
- [2. 2.]
- [2. 2.]]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- return fill_constant(shape=shape, dtype=dtype, value=fill_value, name=name)
- def arange(start=0, end=None, step=1, dtype=None, name=None):
- """
- Returns a 1-D Tensor with spaced values within a given interval.
- Values are generated into the half-open interval [``start``, ``end``) with
- the ``step``. (the interval including ``start`` but excluding ``end``).
- If ``dtype`` is float32 or float64, we advise adding a small epsilon to
- ``end`` to avoid floating point rounding errors when comparing against ``end``.
- Parameters:
- start(float|int|Tensor): Start of interval. The interval includes this
- value. If ``end`` is None, the half-open interval is [0, ``start``).
- If ``start`` is a Tensor, it is a 0-D Tensor which represents a scalar
- and data type is int32, int64, float32, float64. Default is 0.
- end(float|int|Tensor, optional): End of interval. The interval does not
- include this value. If ``end`` is a Tensor, it is a 0-D Tensor which
- represents a scalar and data type is int32, int64, float32, float64.
- If ``end`` is None, the half-open interval is [0, ``start``).
- Default is None.
- step(float|int|Tensor, optional): Spacing between values. For any out,
- it is the instance between two adjacent values, out[i+1] - out[i].
- If ``step`` is a Tensor, it is a 0-D Tensor which represents a scalar
- and data type is int32, int64, float32, float64. . Default is 1.
- dtype(str|np.dtype, optional): The data type of the
- output tensor. Supported data types: int32, int64, float32, float64.
- If ``dtype`` is None, the data type is float32. Default is None.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: A 1-D Tensor with values from the interval [``start``, ``end``)
- taken with common difference ``step`` beginning from ``start``. Its
- data type is set by ``dtype``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> out1 = paddle.arange(5)
- >>> print(out1.numpy())
- [0 1 2 3 4]
- >>> out2 = paddle.arange(3, 9, 2.0)
- >>> print(out2.numpy())
- [3. 5. 7.]
- >>> # use 4.999 instead of 5.0 to avoid floating point rounding errors
- >>> out3 = paddle.arange(4.999, dtype='float32')
- >>> print(out3.numpy())
- [0. 1. 2. 3. 4.]
- >>> start_var = paddle.to_tensor(3)
- >>> out4 = paddle.arange(start_var, 7)
- >>> print(out4.numpy())
- [3 4 5 6]
- """
- if end is None:
- end = start
- start = 0
- if dtype is None:
- for val in [start, end, step]:
- if isinstance(val, (Variable, paddle.pir.Value)):
- if not paddle.is_integer(val):
- dtype = paddle.get_default_dtype()
- break
- else:
- dtype = 'int64'
- else:
- if not isinstance(val, np.integer) and not isinstance(val, int):
- dtype = paddle.get_default_dtype()
- break
- else:
- dtype = 'int64'
- out_shape = None
- is_value_input = (
- not isinstance(start, (Variable, paddle.pir.Value))
- and not isinstance(end, (Variable, paddle.pir.Value))
- and not isinstance(step, (Variable, paddle.pir.Value))
- )
- if not in_dynamic_mode() and is_value_input:
- out_shape = [int(math.ceil((end - start) / step))]
- if not isinstance(dtype, (core.VarDesc.VarType, core.DataType)):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if is_value_input and in_pir_mode():
- return _C_ops.arange(start, end, step, dtype, _current_expected_place())
- if not isinstance(start, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- start = fill_constant([1], dtype, start, force_cpu=True)
- elif start.dtype != dtype:
- start = paddle.cast(start, dtype)
- if not isinstance(end, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- end = fill_constant([1], dtype, end, force_cpu=True)
- elif end.dtype != dtype:
- end = paddle.cast(end, dtype)
- if not isinstance(step, (Variable, paddle.pir.Value)):
- with device_guard("cpu"):
- step = fill_constant([1], dtype, step, force_cpu=True)
- elif step.dtype != dtype:
- step = paddle.cast(step, dtype)
- if in_dynamic_or_pir_mode():
- return _C_ops.arange(start, end, step, dtype, _current_expected_place())
- else:
- check_dtype(
- dtype,
- 'dtype',
- ['float32', 'float64', 'int32', 'int64', 'float16', 'uint16'],
- 'range/arange',
- )
- helper = LayerHelper('range', **locals())
- out = helper.create_variable_for_type_inference(dtype, shape=out_shape)
- helper.append_op(
- type='range',
- inputs={'Start': start, 'End': end, 'Step': step},
- outputs={'Out': out},
- )
- out.stop_gradient = True
- if out_shape is not None:
- out.desc.set_shape(out_shape)
- return out
- def _tril_triu_op(helper):
- """Base op of tril_op and triu_op"""
- op_type = helper.layer_type
- x = helper.kwargs.get('x', None)
- assert x is not None, f'x cannot be None in {op_type}'
- check_variable_and_dtype(
- x,
- 'x',
- [
- 'float16',
- 'uint16',
- 'float32',
- 'float64',
- 'int32',
- 'int64',
- 'bool',
- 'complex64',
- 'complex128',
- ],
- op_type,
- )
- if len(x.shape) < 2:
- raise ValueError(f"x shape in {op_type} must be at least 2-D")
- diagonal = helper.kwargs.get('diagonal', 0)
- if not isinstance(diagonal, (int,)):
- raise TypeError(f"diagonal in {op_type} must be a python Int")
- name = helper.kwargs.get('name', None)
- if name is None:
- out = helper.create_variable_for_type_inference(dtype=x.dtype)
- else:
- out = helper.create_variable(
- name=name, dtype=x.dtype, persistable=False
- )
- helper.append_op(
- type="tril_triu",
- inputs={"X": x},
- attrs={
- "diagonal": diagonal,
- "lower": True if op_type == 'tril' else False,
- },
- outputs={"Out": out},
- )
- return out
- def tril(x, diagonal=0, name=None):
- r"""
- Returns the lower triangular part of a matrix (2-D tensor) or batch
- of matrices :attr:`x`, the other elements of the result tensor are set
- to 0. The lower triangular part of the matrix is defined as the elements
- on and below the diagonal.
- Args:
- x (Tensor): The input x which is a Tensor.
- Support data types: ``bool``, ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
- diagonal (int, optional): The diagonal to consider, default value is 0.
- If :attr:`diagonal` = 0, all elements on and below the main diagonal are
- retained. A positive value includes just as many diagonals above the main
- diagonal, and similarly a negative value excludes just as many diagonals below
- the main diagonal. The main diagonal are the set of indices
- :math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
- :math:`d_{1}, d_{2}` are the dimensions of the matrix.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Results of lower triangular operation by the specified diagonal of input tensor x,
- it's data type is the same as x's Tensor.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> data = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
- >>> print(data)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 2 , 3 , 4 ],
- [5 , 6 , 7 , 8 ],
- [9 , 10, 11, 12]])
- >>> tril1 = paddle.tril(data)
- >>> print(tril1)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 0 , 0 , 0 ],
- [5 , 6 , 0 , 0 ],
- [9 , 10, 11, 0 ]])
- >>> # example 2, positive diagonal value
- >>> tril2 = paddle.tril(data, diagonal=2)
- >>> print(tril2)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 2 , 3 , 0 ],
- [5 , 6 , 7 , 8 ],
- [9 , 10, 11, 12]])
- >>> # example 3, negative diagonal value
- >>> tril3 = paddle.tril(data, diagonal=-1)
- >>> print(tril3)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0 , 0 , 0 , 0 ],
- [5 , 0 , 0 , 0 ],
- [9 , 10, 0 , 0 ]])
- """
- if in_dynamic_or_pir_mode():
- return _C_ops.tril(x, diagonal)
- else:
- return _tril_triu_op(LayerHelper('tril', **locals()))
- @inplace_apis_in_dygraph_only
- def tril_(x, diagonal=0, name=None):
- r"""
- Inplace version of ``tril`` API, the output Tensor will be inplaced with input ``x``.
- Please refer to :ref:`api_paddle_tril`.
- """
- if in_dynamic_mode():
- return _C_ops.tril_(x, diagonal)
- def triu(x, diagonal=0, name=None):
- r"""
- Return the upper triangular part of a matrix (2-D tensor) or batch of matrices
- :attr:`x`, the other elements of the result tensor are set to 0.
- The upper triangular part of the matrix is defined as the elements on and
- above the diagonal.
- Args:
- x (Tensor): The input x which is a Tensor.
- Support data types: ``float64``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
- diagonal (int, optional): The diagonal to consider, default value is 0.
- If :attr:`diagonal` = 0, all elements on and above the main diagonal are
- retained. A positive value excludes just as many diagonals above the main
- diagonal, and similarly a negative value includes just as many diagonals below
- the main diagonal. The main diagonal are the set of indices
- :math:`\{(i, i)\}` for :math:`i \in [0, \min\{d_{1}, d_{2}\} - 1]` where
- :math:`d_{1}, d_{2}` are the dimensions of the matrix.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Results of upper triangular operation by the specified diagonal of input tensor x,
- it's data type is the same as x's Tensor.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.arange(1, 13, dtype="int64").reshape([3,-1])
- >>> print(x)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 2 , 3 , 4 ],
- [5 , 6 , 7 , 8 ],
- [9 , 10, 11, 12]])
- >>> # example 1, default diagonal
- >>> triu1 = paddle.tensor.triu(x)
- >>> print(triu1)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 2 , 3 , 4 ],
- [0 , 6 , 7 , 8 ],
- [0 , 0 , 11, 12]])
- >>> # example 2, positive diagonal value
- >>> triu2 = paddle.tensor.triu(x, diagonal=2)
- >>> print(triu2)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 3, 4],
- [0, 0, 0, 8],
- [0, 0, 0, 0]])
- >>> # example 3, negative diagonal value
- >>> triu3 = paddle.tensor.triu(x, diagonal=-1)
- >>> print(triu3)
- Tensor(shape=[3, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1 , 2 , 3 , 4 ],
- [5 , 6 , 7 , 8 ],
- [0 , 10, 11, 12]])
- """
- if in_dynamic_or_pir_mode():
- return _C_ops.triu(x, diagonal)
- else:
- return _tril_triu_op(LayerHelper('triu', **locals()))
- @inplace_apis_in_dygraph_only
- def triu_(x, diagonal=0, name=None):
- r"""
- Inplace version of ``triu`` API, the output Tensor will be inplaced with input ``x``.
- Please refer to :ref:`api_paddle_triu`.
- """
- if in_dynamic_mode():
- return _C_ops.triu_(x, diagonal)
- def meshgrid(*args, **kwargs):
- """
- Takes a list of N tensors as input :attr:`*args`, each of which is 1-dimensional vector, and creates N-dimensional grids.
- Args:
- *args(Tensor|list of Tensor) : tensors (tuple(list) of tensor): the shapes of input k tensors are (N1,),
- (N2,),..., (Nk,). Support data types: ``float64``, ``bfloat16``, ``float16``, ``float32``, ``int32``, ``int64``, ``complex64``, ``complex128``.
- **kwargs (optional): Currently, only accept name in **kwargs
- The default value is None. Normally there is no need for
- user to set this property. For more information, please refer to :ref:`api_guide_Name`.
- Returns:
- Tensor: k tensors. The shape of each tensor is (N1, N2, ..., Nk)
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.randint(low=0, high=100, shape=[100])
- >>> y = paddle.randint(low=0, high=100, shape=[200])
- >>> grid_x, grid_y = paddle.meshgrid(x, y)
- >>> print(grid_x.shape)
- [100, 200]
- >>> print(grid_y.shape)
- [100, 200]
- """
- if len(args) == 1 and isinstance(args[0], (list, tuple)):
- args = args[0]
- if in_dynamic_or_pir_mode():
- return _C_ops.meshgrid(list(args))
- else:
- name = kwargs.get("name", None)
- helper = LayerHelper('meshgrid', **locals())
- if not isinstance(args, (list, tuple)):
- raise TypeError(
- "The type of input args in meshgrid should be list."
- )
- for id, input_ in enumerate(args):
- check_dtype(
- input_.dtype,
- 'create data type',
- [
- 'uint16',
- 'float16',
- 'float32',
- 'float64',
- 'int32',
- 'int64',
- 'complex64',
- 'complex128',
- ],
- 'meshgrid',
- )
- num = len(args)
- out = [
- helper.create_variable_for_type_inference(dtype=args[i].dtype)
- for i in range(num)
- ]
- helper.append_op(
- type='meshgrid', inputs={'X': list(args)}, outputs={'Out': out}
- )
- return out
- def diag_embed(input, offset=0, dim1=-2, dim2=-1):
- """
- Creates a tensor whose diagonals of certain 2D planes (specified by dim1 and dim2)
- are filled by ``input``. By default, a 2D plane formed by the last two dimensions
- of the returned tensor will be selected.
- The argument ``offset`` determines which diagonal is generated:
- - If offset = 0, it is the main diagonal.
- - If offset > 0, it is above the main diagonal.
- - If offset < 0, it is below the main diagonal.
- Args:
- input(Tensor|numpy.ndarray): The input tensor. Must be at least 1-dimensional. The input data type should be float32, float64, int32, int64.
- offset(int, optional): Which diagonal to consider. Default: 0 (main diagonal).
- dim1(int, optional): The first dimension with respect to which to take diagonal. Default: -2.
- dim2(int, optional): The second dimension with respect to which to take diagonal. Default: -1.
- Returns:
- Tensor, the output data type is the same as input data type.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> diag_embed_input = paddle.arange(6)
- >>> diag_embed_output1 = paddle.diag_embed(diag_embed_input)
- >>> print(diag_embed_output1)
- Tensor(shape=[6, 6], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 0, 0, 0, 0],
- [0, 1, 0, 0, 0, 0],
- [0, 0, 2, 0, 0, 0],
- [0, 0, 0, 3, 0, 0],
- [0, 0, 0, 0, 4, 0],
- [0, 0, 0, 0, 0, 5]])
- >>> diag_embed_output2 = paddle.diag_embed(diag_embed_input, offset=-1, dim1=0,dim2=1 )
- >>> print(diag_embed_output2)
- Tensor(shape=[7, 7], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 0, 0, 0, 0, 0],
- [0, 0, 0, 0, 0, 0, 0],
- [0, 1, 0, 0, 0, 0, 0],
- [0, 0, 2, 0, 0, 0, 0],
- [0, 0, 0, 3, 0, 0, 0],
- [0, 0, 0, 0, 4, 0, 0],
- [0, 0, 0, 0, 0, 5, 0]])
- >>> diag_embed_input_2dim = paddle.reshape(diag_embed_input,[2,3])
- >>> print(diag_embed_input_2dim)
- Tensor(shape=[2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 1, 2],
- [3, 4, 5]])
- >>> diag_embed_output3 = paddle.diag_embed(diag_embed_input_2dim,offset= 0, dim1=0, dim2=2 )
- >>> print(diag_embed_output3)
- Tensor(shape=[3, 2, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[[0, 0, 0],
- [3, 0, 0]],
- [[0, 1, 0],
- [0, 4, 0]],
- [[0, 0, 2],
- [0, 0, 5]]])
- """
- if not isinstance(input, Variable):
- input = assign(input)
- if in_dynamic_or_pir_mode():
- return _C_ops.diag_embed(input, offset, dim1, dim2)
- inputs = {'Input': [input]}
- attrs = {'offset': offset, 'dim1': dim1, 'dim2': dim2}
- def __check_input(input, offset, dim1, dim2):
- check_dtype(
- input.dtype,
- 'Input',
- ['int32', 'int64', 'float16', 'float32', 'float64'],
- 'diag_embed',
- )
- input_shape = list(input.shape)
- assert len(input_shape) >= 1, (
- "Input must be at least 1-dimensional, "
- "But received Input's dimensional: %s.\n" % len(input_shape)
- )
- assert np.abs(dim1) <= len(input_shape), (
- "Dim1 is out of range (expected to be in range of [%d, %d], but got %d).\n"
- % (-(len(input_shape) + 1), len(input_shape), dim1)
- )
- assert np.abs(dim2) <= len(input_shape), (
- "Dim2 is out of range (expected to be in range of [%d, %d], but got %d).\n"
- % (-(len(input_shape) + 1), len(input_shape), dim2)
- )
- dim1_ = dim1 if dim1 >= 0 else len(input_shape) + dim1 + 1
- dim2_ = dim2 if dim2 >= 0 else len(input_shape) + dim2 + 1
- assert dim1_ != dim2_, (
- "dim1 and dim2 cannot be the same dimension."
- "But received dim1 = %d, dim2 = %d\n" % (dim1, dim2)
- )
- __check_input(input, offset, dim1, dim2)
- helper = LayerHelper("diag_embed", **locals())
- out = helper.create_variable_for_type_inference(dtype=input.dtype)
- helper.append_op(
- type='diag_embed',
- inputs={'Input': [input]},
- attrs={'offset': offset, 'dim1': dim1, 'dim2': dim2},
- outputs={'Out': [out]},
- )
- out.stop_gradient = True
- return out
- def diagflat(x, offset=0, name=None):
- """
- If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned.
- If ``x`` is a tensor (more than 1-D), a 2-D square tensor with the elements of flattened ``x`` as the diagonal is returned.
- The argument ``offset`` controls the diagonal offset.
- If ``offset`` = 0, it is the main diagonal.
- If ``offset`` > 0, it is superdiagonal.
- If ``offset`` < 0, it is subdiagonal.
- Args:
- x (Tensor): The input tensor. It can be any shape. Its data type should be float16, float32, float64, int32, int64.
- offset (int, optional): The diagonal offset. A positive value represents superdiagonal, 0 represents the main diagonal, and a negative value represents subdiagonal. Default: 0 (main diagonal).
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor, a square matrix. The output data type is the same as input data type.
- Examples:
- .. code-block:: python
- :name: diagflat-example-1
- >>> import paddle
- >>> x = paddle.to_tensor([1, 2, 3])
- >>> y = paddle.diagflat(x)
- >>> print(y)
- Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1, 0, 0],
- [0, 2, 0],
- [0, 0, 3]])
- >>> y = paddle.diagflat(x, offset=1)
- >>> print(y)
- Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 1, 0, 0],
- [0, 0, 2, 0],
- [0, 0, 0, 3],
- [0, 0, 0, 0]])
- >>> y = paddle.diagflat(x, offset=-1)
- >>> print(y)
- Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 0, 0],
- [1, 0, 0, 0],
- [0, 2, 0, 0],
- [0, 0, 3, 0]])
- .. code-block:: python
- :name: diagflat-example-2
- >>> import paddle
- >>> x = paddle.to_tensor([[1, 2], [3, 4]])
- >>> y = paddle.diagflat(x)
- >>> print(y)
- Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1, 0, 0, 0],
- [0, 2, 0, 0],
- [0, 0, 3, 0],
- [0, 0, 0, 4]])
- >>> y = paddle.diagflat(x, offset=1)
- >>> print(y)
- Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 1, 0, 0, 0],
- [0, 0, 2, 0, 0],
- [0, 0, 0, 3, 0],
- [0, 0, 0, 0, 4],
- [0, 0, 0, 0, 0]])
- >>> y = paddle.diagflat(x, offset=-1)
- >>> print(y)
- Tensor(shape=[5, 5], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 0, 0, 0],
- [1, 0, 0, 0, 0],
- [0, 2, 0, 0, 0],
- [0, 0, 3, 0, 0],
- [0, 0, 0, 4, 0]])
- """
- if in_dynamic_or_pir_mode():
- if len(x.shape) <= 1:
- return _C_ops.diag(x, offset, 0)
- else:
- y = _C_ops.flatten(x, 0, -1)
- return _C_ops.diag(y, offset, 0)
- else:
- padding_value = 0
- check_type(x, 'x', (Variable), 'diagflat')
- check_dtype(
- x.dtype,
- 'x',
- ['float16', 'float32', 'float64', 'int32', 'int64', 'uint16'],
- 'diagflat',
- )
- check_type(offset, 'offset', (int), 'diagflat')
- helper = LayerHelper("diagflat", **locals())
- out1 = helper.create_variable_for_type_inference(dtype=x.dtype)
- out1_shape = helper.create_variable_for_type_inference(x.dtype)
- out2 = helper.create_variable_for_type_inference(dtype=x.dtype)
- if len(x.shape) <= 1:
- helper.append_op(
- type='diag_v2',
- inputs={'X': x},
- outputs={'Out': out2},
- attrs={'offset': offset, 'padding_value': padding_value},
- )
- else:
- helper.append_op(
- type='flatten_contiguous_range',
- inputs={'X': x},
- outputs={'Out': out1, 'XShape': out1_shape},
- attrs={'start_axis': 0, 'stop_axis': -1},
- )
- out1.stop_gradient = True
- helper.append_op(
- type='diag_v2',
- inputs={'X': out1},
- outputs={'Out': out2},
- attrs={'offset': offset, 'padding_value': padding_value},
- )
- out2.stop_gradient = True
- return out2
- def diag(x, offset=0, padding_value=0, name=None):
- """
- If ``x`` is a vector (1-D tensor), a 2-D square tensor with the elements of ``x`` as the diagonal is returned.
- If ``x`` is a matrix (2-D tensor), a 1-D tensor with the diagonal elements of ``x`` is returned.
- The argument ``offset`` controls the diagonal offset:
- If ``offset`` = 0, it is the main diagonal.
- If ``offset`` > 0, it is superdiagonal.
- If ``offset`` < 0, it is subdiagonal.
- Args:
- x (Tensor): The input tensor. Its shape is either 1-D or 2-D. Its data type should be float16, float32, float64, int32, int64, complex64, complex128.
- offset (int, optional): The diagonal offset. A positive value represents superdiagonal, 0 represents the main diagonal, and a negative value represents subdiagonal.
- padding_value (int|float, optional): Use this value to fill the area outside the specified diagonal band. Only takes effect when the input is a 1-D Tensor. The default value is 0.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor, a square matrix or a vector. The output data type is the same as input data type.
- Examples:
- .. code-block:: python
- :name: diag-example-1
- >>> import paddle
- >>> paddle.disable_static()
- >>> x = paddle.to_tensor([1, 2, 3])
- >>> y = paddle.diag(x)
- >>> print(y)
- Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1, 0, 0],
- [0, 2, 0],
- [0, 0, 3]])
- >>> y = paddle.diag(x, offset=1)
- >>> print(y)
- Tensor(shape=[4, 4], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 1, 0, 0],
- [0, 0, 2, 0],
- [0, 0, 0, 3],
- [0, 0, 0, 0]])
- >>> y = paddle.diag(x, padding_value=6)
- >>> print(y)
- Tensor(shape=[3, 3], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1, 6, 6],
- [6, 2, 6],
- [6, 6, 3]])
- .. code-block:: python
- :name: diag-example-2
- >>> import paddle
- >>> paddle.disable_static()
- >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]])
- >>> y = paddle.diag(x)
- >>> print(y)
- Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
- [1, 5])
- >>> y = paddle.diag(x, offset=1)
- >>> print(y)
- Tensor(shape=[2], dtype=int64, place=Place(cpu), stop_gradient=True,
- [2, 6])
- >>> y = paddle.diag(x, offset=-1)
- >>> print(y)
- Tensor(shape=[1], dtype=int64, place=Place(cpu), stop_gradient=True,
- [4])
- """
- if in_dynamic_or_pir_mode():
- return _C_ops.diag(x, offset, padding_value)
- else:
- check_type(x, 'x', (Variable), 'diag_v2')
- check_dtype(
- x.dtype,
- 'x',
- [
- 'float16',
- 'uint16',
- 'float32',
- 'float64',
- 'uint16',
- 'int32',
- 'int64',
- 'complex64',
- 'complex128',
- ],
- 'diag_v2',
- )
- check_type(offset, 'offset', (int), 'diag_v2')
- check_type(padding_value, 'padding_value', (int, float), 'diag_v2')
- if len(x.shape) != 1 and len(x.shape) != 2:
- raise ValueError(
- f"The dimension of input x must be either 1 or 2, but received {len(x.shape)}"
- )
- helper = LayerHelper("diag_v2", **locals())
- out = helper.create_variable_for_type_inference(dtype=x.dtype)
- helper.append_op(
- type='diag_v2',
- inputs={'X': x},
- outputs={'Out': out},
- attrs={'offset': offset, 'padding_value': padding_value},
- )
- out.stop_gradient = True
- return out
- def empty(shape, dtype=None, name=None):
- """
- Returns a Tensor with uninitialized data which size is same as ``shape``.
- Args:
- shape (tuple|list|Tensor): Shape of the Tensor to be created. The data type is ``int32`` or ``int64`` .
- If ``shape`` is a list or tuple, each element of it should be integer or 0-D Tensor with shape [].
- If ``shape`` is an Tensor, it should be an 1-D Tensor which represents a list.
- dtype(np.dtype|str, optional): Data type of the output Tensor
- which can be bool, float16, float32, float64, int32, int64, complex64, complex128 if dtype is `None`, the data
- type of created Tensor use global default dtype (see ``get_default_dtype``
- for details).
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Tensor which is created according to ``shape`` and ``dtype``, and is uninitialized.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # shape is a list/tuple
- >>> data1 = paddle.empty(shape=[3, 2])
- >>> print(data1.numpy())
- >>> # doctest: +SKIP('change everytime')
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- >>> # shape is a Tensor
- >>> shape = paddle.to_tensor([3, 2])
- >>> data2 = paddle.empty(shape=shape)
- >>> print(data2.numpy())
- >>> # doctest: +SKIP('change everytime')
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- >>> # shape is a Tensor List
- >>> shape = [paddle.to_tensor(3), paddle.to_tensor(2)]
- >>> data3 = paddle.empty(shape=shape)
- >>> print(data3.numpy())
- >>> # doctest: +SKIP('change everytime')
- [[1. 1.]
- [1. 1.]
- [1. 1.]]
- """
- if dtype is None:
- dtype = paddle.get_default_dtype()
- dtype = convert_dtype(dtype)
- if in_dynamic_or_pir_mode():
- if in_dynamic_mode():
- shape = paddle.utils.convert_shape_to_list(shape)
- else:
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'uint16',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'complex64',
- 'complex128',
- ],
- 'empty',
- )
- paddle.utils.check_shape(shape)
- if isinstance(shape, np.ndarray):
- shape = shape.tolist()
- if isinstance(shape, (list, tuple)):
- if paddle.utils._contain_var(shape):
- shape = paddle.utils.get_int_tensor_list(shape)
- elif isinstance(shape, paddle.pir.Value):
- pass
- else:
- raise TypeError("Shape only supports Value, or list, or tuple.")
- out = _C_ops.empty(
- shape, convert_np_dtype_to_dtype_(dtype), _current_expected_place()
- )
- out.stop_gradient = True
- return out
- else:
- helper = LayerHelper("empty", **locals())
- inputs = {}
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'complex64',
- 'complex128',
- ],
- 'empty',
- )
- check_type(shape, 'shape', (Variable, list, tuple), 'empty')
- if isinstance(shape, Variable):
- check_dtype(shape.dtype, 'shape', ['int32', 'int64'], 'empty')
- attrs = {}
- paddle.utils.get_shape_tensor_inputs(
- inputs=inputs, attrs=attrs, shape=shape, op_type='empty'
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- attrs['dtype'] = convert_np_dtype_to_dtype_(dtype)
- helper.append_op(
- type='empty',
- inputs=inputs,
- outputs={'Out': [out]},
- attrs=attrs,
- stop_gradient=True,
- )
- out.stop_gradient = True
- return out
- def empty_like(x, dtype=None, name=None):
- """
- Returns a Tensor with uninitialized data which has identical shape of ``x`` and ``dtype``.
- If the ``dtype`` is None, the data type of Tensor is same with ``x``.
- Args:
- x(Tensor): The input tensor which specifies shape and data type. The data type can be bool, float16, float32, float64, int32, int64.
- dtype(np.dtype|str, optional): The data type of output. The data type can be one
- of bool, float16, float32, float64, int32, int64. The default value is None, which means the output
- data type is the same as input.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: Tensor which is created according to ``x`` and ``dtype``, and is uninitialized.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> paddle.set_device("cpu") # and use cpu device
- >>> x = paddle.randn([2, 3], 'float32')
- >>> output = paddle.empty_like(x)
- >>> print(output)
- >>> # doctest: +SKIP('change everytime')
- [[1.8491974e+20 1.8037303e+28 1.7443726e+28]
- [4.9640171e+28 3.0186127e+32 5.6715899e-11]]
- """
- if dtype is None:
- dtype = x.dtype
- dtype = convert_dtype(dtype)
- if in_dynamic_mode():
- out = _C_ops.empty(
- x.shape,
- convert_np_dtype_to_dtype_(dtype),
- _current_expected_place(),
- )
- out.stop_gradient = True
- return out
- elif in_pir_mode():
- shape = paddle.shape(x)
- out = _C_ops.empty(
- shape,
- convert_np_dtype_to_dtype_(dtype),
- _current_expected_place(),
- )
- out.stop_gradient = True
- return out
- else:
- helper = LayerHelper("empty_like", **locals())
- check_variable_and_dtype(
- x,
- 'x',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'uint16',
- 'complex64',
- 'complex128',
- ],
- 'empty_like',
- )
- check_dtype(
- dtype,
- 'dtype',
- [
- 'bool',
- 'float16',
- 'float32',
- 'float64',
- 'int8',
- 'int16',
- 'int32',
- 'int64',
- 'uint16',
- 'complex64',
- 'complex128',
- ],
- 'empty_like',
- )
- out = helper.create_variable_for_type_inference(dtype=dtype)
- inputs = {}
- attrs = {}
- attrs['dtype'] = convert_np_dtype_to_dtype_(dtype)
- shape = paddle.shape(x)
- paddle.utils.get_shape_tensor_inputs(
- inputs=inputs, attrs=attrs, shape=shape, op_type='empty_like'
- )
- helper.append_op(
- type='empty',
- inputs=inputs,
- outputs={'Out': [out]},
- attrs=attrs,
- stop_gradient=True,
- )
- out.stop_gradient = True
- return out
- def assign(x, output=None):
- """
- Copy value of the :attr:`x` to the :attr:`output`.
- Parameters:
- x (Tensor|np.ndarray|list|tuple|scalar): A Tensor, numpy ndarray, tuple/list of scalar,
- or scalar. Its data type can be float16, float32, float64, int32, int64 or bool. Note: the float64 data will be converted to float32 because of current platform protobuf
- data limitation.
- output (Tensor, optional): A Tensor. If :attr:`output` is None, a new Tensor will be created as :attr:`output`. Default: None.
- Returns:
- Tensor: A Tensor with the same shape, data type and value as :attr:`x`.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> import numpy as np
- >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64')
- >>> print(data.numpy())
- [[2.5 2.5]
- [2.5 2.5]
- [2.5 2.5]]
- >>> array = np.array([[1, 1],
- ... [3, 4],
- ... [1, 3]]).astype(np.int64)
- >>> result1 = paddle.zeros(shape=[3, 3], dtype='float32')
- >>> paddle.assign(array, result1)
- >>> print(result1.numpy())
- [[1 1]
- [3 4]
- [1 3]]
- >>> result2 = paddle.assign(data)
- >>> print(result2.numpy())
- [[2.5 2.5]
- [2.5 2.5]
- [2.5 2.5]]
- >>> result3 = paddle.assign(np.array([[2.5, 2.5], [2.5, 2.5], [2.5, 2.5]], dtype='float32'))
- >>> print(result3.numpy())
- [[2.5 2.5]
- [2.5 2.5]
- [2.5 2.5]]
- """
- # speed up
- if x is output and isinstance(x, (Variable, paddle.pir.Value)):
- return x
- input = x
- helper = LayerHelper('assign', **locals())
- check_type(
- input,
- 'input',
- (
- Variable,
- paddle.pir.Value,
- np.ndarray,
- list,
- tuple,
- float,
- int,
- bool,
- ),
- 'assign',
- )
- if np.isscalar(input) and not isinstance(input, str):
- input = np.array([input])
- elif isinstance(input, (list, tuple)):
- input = np.array(input)
- # NOTE(Aurelius84): Why we judge core.Tensor?
- # In case of @to_static, a Tensor can be as input of `assign`,
- # but in_dynamic_mode()==False under @to_static, which means
- # isinstance(Tensor, Variable) == False. It will cause return None
- # after this api.
- if isinstance(input, (Variable, core.eager.Tensor, paddle.pir.Value)):
- if in_dynamic_mode():
- if output is None:
- output = _C_ops.assign(input)
- else:
- _C_ops.assign_out_(input, output)
- elif in_pir_mode():
- if output is None:
- output = _C_ops.assign(input)
- else:
- _C_ops.assign_out_(input, output)
- else:
- check_dtype(
- input.dtype,
- 'input',
- [
- 'float16',
- 'uint16',
- 'float32',
- 'float64',
- 'int32',
- 'int64',
- 'uint8',
- 'int8',
- 'bool',
- 'complex64',
- 'complex128',
- ],
- 'assign',
- '(When the type of input in assign is Variable.)',
- )
- if output is None:
- output = helper.create_variable_for_type_inference(
- dtype=input.dtype
- )
- helper.append_op(
- type='assign', inputs={'X': [input]}, outputs={'Out': [output]}
- )
- elif isinstance(input, np.ndarray):
- # We now support the form of [var, VAR...] if the Var.shape=[1,]
- if len(input.shape) > 0 and any(
- isinstance(x, (Variable, paddle.pir.Value)) for x in input
- ):
- # We only deal with the case where the list is nested one level, convert all scalars into variables, and then use stack to process. It is necessary to ensure the consistency of types.
- if not all(
- x.shape == (1,)
- for x in input
- if isinstance(
- x, (Variable, core.eager.Tensor, paddle.pir.Value)
- )
- ):
- raise TypeError(
- "Unsupport paddle.assign([Variable, Variable...]) with non-scalar variable."
- )
- def convert_scalar(x):
- if not isinstance(
- x, (Variable, core.eager.Tensor, paddle.pir.Value)
- ):
- return assign(x)
- return x
- to_stack_list = list(map(convert_scalar, input))
- ret = paddle.stack(to_stack_list)
- ret = paddle.squeeze(ret, -1)
- return ret
- if input.dtype == 'object':
- """may be this form [[Var], [Var], [3], [4]], we reject them."""
- raise TypeError(
- "The type of received input == `object`, it is not supported to convert to tensor, such as [[Var], [Var], [3], [4]]"
- )
- dtype = convert_np_dtype_to_dtype_(input.dtype)
- check_dtype(
- dtype,
- 'input',
- [
- 'float32',
- 'float64',
- 'int32',
- 'int64',
- 'bool',
- 'complex64',
- 'complex128',
- ],
- 'assign',
- '(When the type of input in assign is numpy array.)',
- )
- value_name = "values"
- values = input.ravel().tolist()
- if input.size > 1024 * 1024:
- raise ValueError(
- "The size of input is too big. Please consider "
- "saving it to file and 'load_op' to load it"
- )
- if in_dynamic_or_pir_mode():
- if output is None:
- output = zeros(list(input.shape), dtype)
- if in_dynamic_mode():
- _C_ops.assign_value_(
- output,
- list(input.shape),
- dtype,
- values,
- _current_expected_place(),
- )
- else:
- output = _C_ops.assign_value_(
- output,
- list(input.shape),
- dtype,
- values,
- _current_expected_place(),
- )
- else:
- if output is None:
- output = helper.create_variable_for_type_inference(
- dtype=input.dtype
- )
- helper.append_op(
- type='assign_value',
- outputs={'Out': [output]},
- attrs={
- 'dtype': dtype,
- 'shape': list(input.shape),
- value_name: values,
- },
- )
- return output
- def clone(x, name=None):
- """
- Returns a copy of input Tensor. It will always have a Tensor copy.
- In addition, This function is derivable, so gradients will flow back from the output to input.
- Parameters:
- x (Tensor): The input Tensor.
- name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor, A Tensor copied from ``input``.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> import numpy as np
- >>> x = paddle.ones([2])
- >>> x.stop_gradient = False
- >>> x.retain_grads()
- >>> clone_x = paddle.clone(x)
- >>> clone_x.retain_grads()
- >>> y = clone_x**3
- >>> y.backward()
- >>> print(clone_x.grad.numpy())
- [3. 3.]
- >>> print(x.grad.numpy())
- [3. 3.]
- """
- return x.clone()
- # NOTE(zhiqiu): not public
- def _memcpy(input, place=None, output=None):
- """
- The OP copies the :attr:`input` to the :attr:`output`.
- NOTE: currently, only support CUDAPlace <-> CUDAPinnedPlace.
- Parameters:
- input (Tensor): A tensor. Its data type supports float16, float32, float64, int32, int64, and bool.
- device (Place): Target place for the output.
- output (Tensor, optional): A tensor. If :attr:`output` is None, a new tensor will
- be created as :attr:`output`. Default: None.
- Returns:
- Tensor, A tensor with the same shape, data type and value as :attr:`input`.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> data = paddle.full(shape=[3, 2], fill_value=2.5, dtype='float64')
- >>> print(data.numpy())
- [[2.5 2.5]
- [2.5 2.5]
- [2.5 2.5]]
- >>> # doctest: +SKIP('NOTE(zhiqiu): not public')
- >>> result = paddle._memcpy(data, place=paddle.CPUPlace())
- >>> print(result2)
- [[2.5 2.5]
- [2.5 2.5]
- [2.5 2.5]]
- """
- dst_place_type = -1
- if place is None:
- dst_place_type = -1
- else:
- p = core.Place()
- p.set_place(place)
- if p.is_cpu_place():
- dst_place_type = 0
- elif p.is_gpu_place():
- dst_place_type = 1
- elif p.is_cuda_pinned_place():
- dst_place_type = 2
- elif p.is_xpu_place():
- dst_place_type = 3
- if in_pir_mode():
- return _C_ops.memcpy(input, dst_place_type)
- helper = LayerHelper('memcpy', **locals())
- check_type(input, 'input', (Variable), 'memcpy')
- if isinstance(input, (Variable, core.eager.Tensor)):
- check_dtype(
- input.dtype,
- 'input',
- [
- 'float16',
- 'uint16',
- 'float32',
- 'float64',
- 'int32',
- 'int64',
- 'uint8',
- 'int8',
- 'bool',
- ],
- 'memcpy',
- '(When the type of input in memcpy is Variable.)',
- )
- if output is None:
- output = helper.create_variable_for_type_inference(dtype=input.dtype)
- attrs = {'dst_place_type': dst_place_type}
- helper.append_op(
- type='memcpy',
- inputs={'X': [input]},
- outputs={'Out': [output]},
- attrs=attrs,
- )
- return output
- def complex(real, imag, name=None):
- """Return a complex tensor given the real and image component.
- Args:
- real (Tensor): The real component. The data type should be 'float32' or 'float64'.
- imag (Tensor): The image component. The data type should be the same as ``real``.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``real`` and ``imag``.
- Note:
- ``paddle.complex`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
- .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.arange(2, dtype=paddle.float32).unsqueeze(-1)
- >>> y = paddle.arange(3, dtype=paddle.float32)
- >>> z = paddle.complex(x, y)
- >>> print(z)
- Tensor(shape=[2, 3], dtype=complex64, place=Place(cpu), stop_gradient=True,
- [[0j , 1j , 2j ],
- [(1+0j), (1+1j), (1+2j)]])
- """
- if in_dynamic_or_pir_mode():
- return _C_ops.complex(real, imag)
- else:
- check_variable_and_dtype(
- real, 'real', ['float32', 'float64'], 'complex'
- )
- check_variable_and_dtype(
- imag, 'imag', ['float32', 'float64'], 'complex'
- )
- op_type = "complex"
- helper = LayerHelper(op_type, **locals())
- inputs = {"X": real, "Y": imag}
- out = helper.create_variable_for_type_inference(
- dtype=_real_to_complex_dtype(real.dtype)
- )
- outputs = {"Out": out}
- attrs = {}
- helper.append_op(
- type=op_type, inputs=inputs, attrs=attrs, outputs=outputs
- )
- return out
- def tril_indices(row, col, offset=0, dtype='int64'):
- """
- Return the indices of the lower triangular part of the 2-D matrix
- whose row and col is known. Indices are ordered based on row and then columns.
- The lower triangular part of the matrix is defined as the elements on
- and below the diagonal.
- Args:
- row (int): The input x which is a int number describe the number of row of the matrix.
- col (int): The input x which is a int number describe the number of col of the matrix.
- offset (int, optional): The offset to consider, default value is 0.
- - If offset = 0, all elements on and below the main diagonal are retained.
- - If offset > 0, include just as many diagonals above the main diagonal.
- - If offset < 0, excludes just as many diagonals below the main diagonal.
- dtype (int, optional): the data type of the output tensor, can be int32, int64.
- Returns:
- Tensor: Results of the indices of lower triangular part of a row * col matrix,
- where the first row contains row coordinates of and the second row contains column coordinates.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # example 1, default offset value
- >>> data1 = paddle.tril_indices(4,4,0)
- >>> print(data1)
- Tensor(shape=[2, 10], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 1, 1, 2, 2, 2, 3, 3, 3, 3],
- [0, 0, 1, 0, 1, 2, 0, 1, 2, 3]])
- >>> # example 2, positive offset value
- >>> data2 = paddle.tril_indices(4,4,2)
- >>> print(data2)
- Tensor(shape=[2, 15], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3],
- [0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3]])
- >>> # example 3, negative offset value
- >>> data3 = paddle.tril_indices(4,4,-1)
- >>> print(data3)
- Tensor(shape=[2, 6], dtype=int64, place=Place(cpu), stop_gradient=True,
- [[1, 2, 2, 3, 3, 3],
- [0, 0, 1, 0, 1, 2]])
- """
- if not isinstance(dtype, core.VarDesc.VarType):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if not isinstance(row, int) or row < 0:
- raise TypeError("row should be a non-negative int")
- if col is not None:
- if not isinstance(col, int) or col < 0:
- raise TypeError("col should be a non-negative int")
- else:
- col = row
- if in_dynamic_or_pir_mode():
- if col is None:
- col = row
- out = _C_ops.tril_indices(
- row, col, offset, dtype, _current_expected_place()
- )
- return out
- else:
- if not isinstance(offset, int):
- raise TypeError("offset should be a int")
- helper = LayerHelper("tril_indices", **locals())
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='tril_indices',
- inputs={},
- outputs={'out': [out]},
- attrs={'rows': row, 'cols': col, 'offset': offset, 'dtype': dtype},
- )
- return out
- def triu_indices(row, col=None, offset=0, dtype='int64'):
- """
- Return the indices of the upper triangular part of the 2-D matrix
- whose row and col is known. Indices are ordered based on row and then columns.
- The upper triangular part of the matrix is defined as the elements on
- and above the diagonal.
- Args:
- row (int): The input x which is a int number describe the number of row of the matrix.
- col (int, optional): The input x which is a int number describe the number of col of the matrix.
- default value for col is None, then it will be set equal to row, indicting a square matrix.
- offset (int, optional): The offset to consider, default value is 0.
- - If offset = 0, all elements on and above the main diagonal are retained.
- - If offset > 0, include just as few diagonals above the main diagonal.
- - If offset < 0, excludes just as few diagonals below the main diagonal.
- dtype (str|np.dtype|paddle.dtype, optional): the data type of the output tensor,
- can be int32, int64, default value is int64.
- Returns:
- Tensor: Results of the indices of upper triangular part of a row * col matrix,
- where the first row contains row coordinates of and the second row contains column coordinates.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> # example 1, default offset value
- >>> data1 = paddle.triu_indices(4,4,0)
- >>> print(data1.numpy())
- [[0 0 0 0 1 1 1 2 2 3]
- [0 1 2 3 1 2 3 2 3 3]]
- >>> # example 2, positive offset value
- >>> data2 = paddle.triu_indices(4,4,2)
- >>> print(data2.numpy())
- [[0 0 1]
- [2 3 3]]
- >>> # example 3, negative offset value
- >>> data3 = paddle.triu_indices(4,4,-1)
- >>> print(data3.numpy())
- [[0 0 0 0 1 1 1 1 2 2 2 3 3]
- [0 1 2 3 0 1 2 3 1 2 3 2 3]]
- """
- if not isinstance(dtype, core.VarDesc.VarType):
- dtype = convert_np_dtype_to_dtype_(dtype)
- if not isinstance(row, int) or row < 0:
- raise TypeError("row should be a non-negative int")
- if col is not None:
- if not isinstance(col, int) or col < 0:
- raise TypeError("col should be a non-negative int")
- else:
- col = row
- if in_dynamic_or_pir_mode():
- if col is None:
- col = row
- out = _C_ops.triu_indices(
- row, col, offset, dtype, _current_expected_place()
- )
- return out
- else:
- if not isinstance(offset, int):
- raise TypeError("offset should be a int")
- helper = LayerHelper("triu_indices", **locals())
- out = helper.create_variable_for_type_inference(dtype=dtype)
- helper.append_op(
- type='triu_indices',
- inputs={},
- outputs={'out': [out]},
- attrs={'row': row, 'col': col, 'offset': offset, 'dtype': dtype},
- )
- return out
- def polar(abs, angle, name=None):
- """Return a Cartesian coordinates corresponding to the polar coordinates complex tensor given the ``abs`` and ``angle`` component.
- Args:
- abs (Tensor): The abs component. The data type should be 'float32' or 'float64'.
- angle (Tensor): The angle component. The data type should be the same as ``abs``.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: The output tensor. The data type is 'complex64' or 'complex128', with the same precision as ``abs`` and ``angle``.
- Note:
- ``paddle.polar`` supports broadcasting. If you want know more about broadcasting, please refer to `Introduction to Tensor`_ .
- .. _Introduction to Tensor: ../../guides/beginner/tensor_en.html#chapter5-broadcasting-of-tensor
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> import numpy as np
- >>> abs = paddle.to_tensor([1, 2], dtype=paddle.float64)
- >>> angle = paddle.to_tensor([np.pi / 2, 5 * np.pi / 4], dtype=paddle.float64)
- >>> out = paddle.polar(abs, angle)
- >>> print(out)
- Tensor(shape=[2], dtype=complex128, place=Place(cpu), stop_gradient=True,
- [ (6.123233995736766e-17+1j) ,
- (-1.4142135623730954-1.414213562373095j)])
- """
- check_variable_and_dtype(abs, 'abs', ['float32', 'float64'], 'paddle.polar')
- check_variable_and_dtype(
- angle, 'angle', ['float32', 'float64'], 'paddle.polar'
- )
- return paddle.complex(abs * paddle.cos(angle), abs * paddle.sin(angle))
- @dygraph_only
- def cauchy_(x, loc=0, scale=1, name=None):
- """Fills the tensor with numbers drawn from the Cauchy distribution.
- Args:
- x (Tensor): the tensor will be filled, The data type is float32 or float64.
- loc (scalar, optional): Location of the peak of the distribution. The data type is float32 or float64.
- scale (scalar, optional): The half-width at half-maximum (HWHM). The data type is float32 or float64. Must be positive values.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: input tensor with numbers drawn from the Cauchy distribution.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.randn([3, 4])
- >>> x.cauchy_(1, 2)
- >>> # doctest: +SKIP('random check')
- >>> print(x)
- Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
- [[ 3.80087137, 2.25415039, 2.77960515, 7.64125967],
- [ 0.76541221, 2.74023032, 1.99383152, -0.12685823],
- [ 1.45228469, 1.76275957, -4.30458832, 34.74880219]])
- """
- x.normal_()
- loc = paddle.to_tensor(loc).astype(x.dtype)
- half = paddle.to_tensor(0.5).astype(x.dtype)
- x.subtract_(half).scale_(np.pi).tan_().scale_(scale).add_(loc)
- return x
- @dygraph_only
- def geometric_(x, probs, name=None):
- """Fills the tensor with numbers drawn from the Geometric distribution.
- Args:
- x (Tensor): the tensor will be filled, The data type is float32 or float64.
- probs (Real|Tensor): Probability parameter.
- The value of probs must be positive. When the parameter is a tensor, probs is probability of success for each trial.
- name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
- Returns:
- Tensor: input tensor with numbers drawn from the Geometric distribution.
- Examples:
- .. code-block:: python
- >>> import paddle
- >>> x = paddle.randn([3, 4])
- >>> x.geometric_(0.3)
- >>> # doctest: +SKIP('random check')
- >>> print(x)
- Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
- [[2.42739224, 4.78268528, 1.23302543, 3.76555204],
- [1.38877118, 0.16075331, 0.16401523, 2.47349310],
- [1.72872102, 2.76533413, 0.33410925, 1.63351011]])
- """
- tiny = np.finfo(dtype=convert_dtype(x.dtype)).tiny
- probs = paddle.to_tensor(probs).astype(x.dtype)
- x.uniform_(min=float(tiny), max=float(1))
- x.log_().divide_(paddle.log1p(-(probs)))
- return x
|