common.py 74 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # TODO: define the common classes to build a neural network
  15. import paddle
  16. from paddle import in_dynamic_mode
  17. from .. import functional as F
  18. from .layers import Layer
  19. __all__ = []
  20. def _npairs(x, n):
  21. if isinstance(x, (paddle.Tensor, list, tuple)):
  22. return x
  23. x = [x] * (n * 2)
  24. return x
  25. class Identity(Layer):
  26. r"""
  27. A placeholder identity operator that is argument-insensitive. For each input :math:`X` ,
  28. the output :math:`Out` is:
  29. .. math::
  30. Out = X
  31. Parameters:
  32. args: any argument (unused)
  33. kwargs: any keyword argument (unused)
  34. Shape:
  35. - input: Multi-dimensional tensor with shape :math:`[batch\_size, n1, n2, ...]` .
  36. - output: Multi-dimensional tensor with shape :math:`[batch\_size, n1, n2, ...]` .
  37. Examples:
  38. .. code-block:: python
  39. >>> import paddle
  40. >>> paddle.seed(100)
  41. >>> input_tensor = paddle.randn(shape=[3, 2])
  42. >>> layer = paddle.nn.Identity()
  43. >>> out = layer(input_tensor)
  44. >>> print(input_tensor)
  45. Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  46. [[-1.41661501, 0.25904641],
  47. [ 0.00979547, -0.30324230],
  48. [-1.34256756, -0.76540256]])
  49. >>> print(out)
  50. Tensor(shape=[3, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  51. [[-1.41661501, 0.25904641],
  52. [ 0.00979547, -0.30324230],
  53. [-1.34256756, -0.76540256]])
  54. """
  55. def __init__(self, *args, **kwargs):
  56. super().__init__()
  57. def forward(self, input):
  58. return input
  59. class Linear(Layer):
  60. r"""
  61. Fully-connected linear transformation layer. For each input :math:`X` ,
  62. the equation is:
  63. .. math::
  64. Out = XW + b
  65. where :math:`W` is the weight and :math:`b` is the bias.
  66. Linear layer takes only one multi-dimensional tensor as input with the
  67. shape :math:`[batch\_size, *, in\_features]` , where :math:`*` means any
  68. number of additional dimensions. It multiplies input tensor with the weight
  69. (a 2-D tensor of shape :math:`[in\_features, out\_features]` ) and produces
  70. an output tensor of shape :math:`[batch\_size, *, out\_features]` .
  71. If :math:`bias\_attr` is not False, the bias (a 1-D tensor of
  72. shape :math:`[out\_features]` ) will be created and added to the output.
  73. Parameters:
  74. in_features (int): The number of input units.
  75. out_features (int): The number of output units.
  76. weight_attr (ParamAttr, optional): The attribute for the learnable
  77. weight of this layer. The default value is None. If the Initializer of the
  78. param_attr is not set, the parameter is initialized with Xavier.
  79. For detailed information, please refer to paddle.ParamAttr.
  80. bias_attr (ParamAttr|bool, optional): The attribute for the learnable bias
  81. of this layer. If it is set to False, no bias will be added to the output.
  82. If it is set to None or one kind of ParamAttr, a bias parameter will
  83. be created according to ParamAttr. For detailed information, please refer
  84. to paddle.ParamAttr. The default value is None and the bias will be
  85. initialized to zero.
  86. name (str, optional): Normally there is no need for user to set this parameter.
  87. For detailed information, please refer to :ref:`api_guide_Name` .
  88. Attribute:
  89. **weight** (Parameter): the learnable weight of this layer.
  90. **bias** (Parameter): the learnable bias of this layer.
  91. Shape:
  92. - input: Multi-dimensional tensor with shape :math:`[batch\_size, *, in\_features]` . Its data types are float16, float32, float64 ,The default is float32 .
  93. - output: Multi-dimensional tensor with shape :math:`[batch\_size, *, out\_features]` . The data type is the same as the input .
  94. Examples:
  95. .. code-block:: python
  96. >>> import paddle
  97. >>> paddle.seed(100)
  98. >>> # Define the linear layer.
  99. >>> weight_attr = paddle.ParamAttr(
  100. ... name="weight",
  101. ... initializer=paddle.nn.initializer.Constant(value=0.5))
  102. >>> bias_attr = paddle.ParamAttr(
  103. ... name="bias",
  104. ... initializer=paddle.nn.initializer.Constant(value=1.0))
  105. >>> linear = paddle.nn.Linear(2, 4, weight_attr=weight_attr, bias_attr=bias_attr)
  106. >>> print(linear.weight)
  107. Parameter containing:
  108. Tensor(shape=[2, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
  109. [[0.50000000, 0.50000000, 0.50000000, 0.50000000],
  110. [0.50000000, 0.50000000, 0.50000000, 0.50000000]])
  111. >>> print(linear.bias)
  112. Parameter containing:
  113. Tensor(shape=[4], dtype=float32, place=Place(cpu), stop_gradient=False,
  114. [1., 1., 1., 1.])
  115. >>> x = paddle.randn((3, 2), dtype="float32")
  116. >>> y = linear(x)
  117. >>> print(y)
  118. Tensor(shape=[3, 4], dtype=float32, place=Place(cpu), stop_gradient=False,
  119. [[ 0.42121571, 0.42121571, 0.42121571, 0.42121571],
  120. [ 0.85327661, 0.85327661, 0.85327661, 0.85327661],
  121. [-0.05398512, -0.05398512, -0.05398512, -0.05398512]])
  122. """
  123. def __init__(
  124. self,
  125. in_features,
  126. out_features,
  127. weight_attr=None,
  128. bias_attr=None,
  129. name=None,
  130. ):
  131. super().__init__()
  132. self._dtype = self._helper.get_default_dtype()
  133. self._weight_attr = weight_attr
  134. self._bias_attr = bias_attr
  135. self.weight = self.create_parameter(
  136. shape=[in_features, out_features],
  137. attr=self._weight_attr,
  138. dtype=self._dtype,
  139. is_bias=False,
  140. )
  141. self.bias = self.create_parameter(
  142. shape=[out_features],
  143. attr=self._bias_attr,
  144. dtype=self._dtype,
  145. is_bias=True,
  146. )
  147. self.name = name
  148. def forward(self, input):
  149. out = F.linear(
  150. x=input, weight=self.weight, bias=self.bias, name=self.name
  151. )
  152. return out
  153. def extra_repr(self):
  154. name_str = f', name={self.name}' if self.name else ''
  155. return f'in_features={self.weight.shape[0]}, out_features={self.weight.shape[1]}, dtype={self._dtype}{name_str}'
  156. class Upsample(Layer):
  157. """
  158. This op resizes a batch of images.
  159. The input must be a 3-D Tensor of the shape (num_batches, channels, in_w)
  160. or (num_batches, in_w, channels), or 4-D (num_batches, channels, in_h, in_w) or
  161. (num_batches, in_h, in_w, channels), or a 5-D Tensor of the shape
  162. (num_batches, channels, in_d, in_h, in_w) or (num_batches, in_d, in_h, in_w, channels),
  163. Where in_w is width of the input tensor, in_h is the height of the input tensor,
  164. in_d is the depth of the input tensor.
  165. and the resizing only applies on the three dimensions(depth, height and width).
  166. Supporting resample methods:
  167. 'linear' : Linear interpolation
  168. 'bilinear' : Bilinear interpolation
  169. 'trilinear' : Trilinear interpolation
  170. 'nearest' : Nearest neighbor interpolation
  171. 'bicubic' : Bicubic interpolation
  172. Linear interpolation is the method of using a line connecting two known quantities
  173. to determine the value of an unknown quantity between the two known quantities.
  174. Nearest neighbor interpolation is to perform nearest neighbor interpolation
  175. in both the 3rd dimension(in height direction) and the 4th dimension(in width
  176. direction) on input tensor.
  177. Bilinear interpolation is an extension of linear interpolation for
  178. interpolating functions of two variables (e.g. H-direction and
  179. W-direction in this op) on a rectilinear 2D grid. The key idea is
  180. to perform linear interpolation first in one direction, and then
  181. again in the other direction.
  182. Bicubic interpolation is an extension of cubic interpolation for interpolating
  183. data points on a two-dimensional regular grid. The interpolated surface is
  184. smoother than corresponding surfaces obtained by bilinear interpolation or
  185. nearest-neighbor interpolation.
  186. Trilinear interpolation is an extension of linear interpolation for
  187. interpolating functions of three variables (e.g. D-direction,
  188. H-direction and W-direction in this op) on a rectilinear 3D grid.
  189. The linear interpolation is performed on three directions.
  190. align_corners and align_mode are optional parameters,the calculation method
  191. of interpolation can be selected by them.
  192. Area interpolation is to perform area interpolation
  193. in both the 3rd dimension(in height direction) , the 4th dimension(in width
  194. direction) and the 5th dimension(in depth direction) on input tensor. Set to
  195. area will directly call `paddle.nn.functional.adaptive_avg_pool1d` or
  196. `paddle.nn.functional.adaptive_avg_pool2d` or `paddle.nn.functional.adaptive_avg_pool3d`.
  197. Example:
  198. .. code-block:: text
  199. For scale_factor:
  200. if align_corners = True && out_size > 1 :
  201. scale_factor = (in_size-1.0)/(out_size-1.0)
  202. else:
  203. scale_factor = float(in_size/out_size)
  204. Linear interpolation:
  205. if:
  206. align_corners = False , align_mode = 0
  207. input : (N,C,W_in)
  208. output: (N,C,W_out) where:
  209. W_out = (W_{in}+0.5) * scale_{factor} - 0.5
  210. else:
  211. input : (N,C,W_in)
  212. output: (N,C,W_out) where:
  213. W_out = W_{in} * scale_{factor}
  214. Nearest neighbor interpolation:
  215. if:
  216. align_corners = False
  217. input : (N,C,H_in,W_in)
  218. output: (N,C,H_out,W_out) where:
  219. H_out = floor (H_{in} * scale_{factor})
  220. W_out = floor (W_{in} * scale_{factor})
  221. else:
  222. align_corners = True
  223. input : (N,C,H_in,W_in)
  224. output: (N,C,H_out,W_out) where:
  225. H_out = round(H_{in} * scale_{factor})
  226. W_out = round(W_{in} * scale_{factor})
  227. Bilinear interpolation:
  228. if:
  229. align_corners = False , align_mode = 0
  230. input : (N,C,H_in,W_in)
  231. output: (N,C,H_out,W_out) where:
  232. H_out = (H_{in}+0.5) * scale_{factor} - 0.5
  233. W_out = (W_{in}+0.5) * scale_{factor} - 0.5
  234. else:
  235. input : (N,C,H_in,W_in)
  236. output: (N,C,H_out,W_out) where:
  237. H_out = H_{in} * scale_{factor}
  238. W_out = W_{in} * scale_{factor}
  239. Bicubic interpolation:
  240. if:
  241. align_corners = False
  242. input : (N,C,H_in,W_in)
  243. output: (N,C,H_out,W_out) where:
  244. H_out = (H_{in}+0.5) * scale_{factor} - 0.5
  245. W_out = (W_{in}+0.5) * scale_{factor} - 0.5
  246. else:
  247. input : (N,C,H_in,W_in)
  248. output: (N,C,H_out,W_out) where:
  249. H_out = H_{in} * scale_{factor}
  250. W_out = W_{in} * scale_{factor}
  251. Trilinear interpolation:
  252. if:
  253. align_corners = False , align_mode = 0
  254. input : (N,C,D_in,H_in,W_in)
  255. output: (N,C,D_out,H_out,W_out) where:
  256. D_out = (D_{in}+0.5) * scale_{factor} - 0.5
  257. H_out = (H_{in}+0.5) * scale_{factor} - 0.5
  258. W_out = (W_{in}+0.5) * scale_{factor} - 0.5
  259. else:
  260. input : (N,C,D_in,H_in,W_in)
  261. output: (N,C,D_out,H_out,W_out) where:
  262. D_out = D_{in} * scale_{factor}
  263. H_out = H_{in} * scale_{factor}
  264. W_out = W_{in} * scale_{factor}
  265. https://en.wikipedia.org/wiki/Linear_interpolation.
  266. For details of linear interpolation, please refer to Wikipedia:
  267. For details of nearest neighbor interpolation, please refer to Wikipedia:
  268. https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.
  269. For details of bilinear interpolation, please refer to Wikipedia:
  270. https://en.wikipedia.org/wiki/Bilinear_interpolation.
  271. For details of bicubic interpolation, please refer to Wikipedia:
  272. https://en.wikipedia.org/wiki/Bicubic_interpolation
  273. For details of trilinear interpolation, please refer to Wikipedia:
  274. https://en.wikipedia.org/wiki/Trilinear_interpolation.
  275. Parameters:
  276. size (list|tuple|Tensor|None): Output shape of image resize
  277. layer, the shape is (out_w, ) when input is a 3-D Tensor, the shape is (out_h, out_w)
  278. when input is a 4-D Tensor and is (out_d, out_h, out_w) when input is a 5-D Tensor.
  279. Default: None. If a list/tuple, each element can be an integer or a Tensor of shape: [1].
  280. If a Tensor , its dimensions size should be a 1.
  281. scale_factor (float|Tensor|list|tuple|None): The multiplier for the input height or width. At
  282. least one of :attr:`size` or :attr:`scale_factor` must be set.
  283. And :attr:`size` has a higher priority than :attr:`scale_factor`. Has to match input size if it is either a list or a tuple or a Tensor.
  284. Default: None.
  285. mode (str): The resample method. It supports 'linear', 'nearst', 'bilinear',
  286. 'bicubic' and 'trilinear' currently. Default: 'nearest'
  287. align_corners(bool) : An optional bool, If True, the centers of the 4 corner pixels of the
  288. input and output tensors are aligned, preserving the values at the
  289. corner pixels.
  290. Default: False
  291. align_mode(int) : An optional for linear/bilinear/trilinear interpolation. Refer to the formula in the example above,
  292. it can be \'0\' for src_idx = scale_factor*(dst_indx+0.5)-0.5 , can be \'1\' for
  293. src_idx = scale_factor*dst_index.
  294. data_format (str, optional): Specify the data format of the input, and the data format of
  295. the output will be consistent with that of the input. An optional string from:`"NCW"`,
  296. `"NWC"`, `"NCHW"`, `"NHWC"`, `"NCDHW"`, `"NDHWC"`. The default value is None.
  297. When :attr:`data_format` is not specified, it will be automatically inferred from the
  298. input dimension of :attr:`x`. When :attr:`x` is a 3-D Tensor, :attr:`data_format` will be
  299. set to `"NCW"`; When :attr:`x` is a 4-D Tensor, :attr:`data_format` will be set to
  300. `"NCHW"`; When :attr:`x` is a 5-D Tensor, :attr:`data_format` will be set to `"NCDHW"`.
  301. When it is `"NCHW"`, the data should be stored in the order of:
  302. `[batch_size, input_channels, input_height, input_width]`. When it is `"NCDHW"`, the
  303. data should be stored in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
  304. name(str, optional): The default value is None.
  305. Normally there is no need for user to set this property.
  306. For more information, please refer to :ref:`api_guide_Name`
  307. Returns:
  308. A callable object of Upsample.
  309. Examples:
  310. .. code-block:: python
  311. >>> import paddle
  312. >>> input = paddle.rand([2, 3, 6, 10], dtype="float32")
  313. >>> upsample_out = paddle.nn.Upsample(size=[12, 12])
  314. >>> output = upsample_out(x=input)
  315. >>> print(output.shape)
  316. [2, 3, 12, 12]
  317. """
  318. def __init__(
  319. self,
  320. size=None,
  321. scale_factor=None,
  322. mode='nearest',
  323. align_corners=False,
  324. align_mode=0,
  325. data_format=None,
  326. name=None,
  327. ):
  328. super().__init__()
  329. self.size = size
  330. self.scale_factor = scale_factor
  331. self.mode = mode.lower()
  332. self.align_corners = align_corners
  333. self.align_mode = align_mode
  334. self.data_format = data_format
  335. self.name = name
  336. def forward(self, x):
  337. if self.data_format is None:
  338. dim_size = len(x.shape)
  339. if dim_size == 3:
  340. self.data_format = 'NCW'
  341. elif dim_size == 4:
  342. self.data_format = 'NCHW'
  343. elif dim_size == 5:
  344. self.data_format = 'NCDHW'
  345. else:
  346. raise ValueError(
  347. f"The dimension of the input tensor should only be 3-D, 4-D or 5-D, but the received dimension is {dim_size}."
  348. )
  349. out = F.interpolate(
  350. x,
  351. size=self.size,
  352. scale_factor=self.scale_factor,
  353. mode=self.mode,
  354. align_corners=self.align_corners,
  355. align_mode=self.align_mode,
  356. data_format=self.data_format,
  357. name=self.name,
  358. )
  359. return out
  360. def extra_repr(self):
  361. if self.scale_factor is not None:
  362. main_str = f'scale_factor={self.scale_factor}'
  363. else:
  364. main_str = f'size={self.size}'
  365. name_str = f', name={self.name}' if self.name else ''
  366. return f'{main_str}, mode={self.mode}, align_corners={self.align_corners}, align_mode={self.align_mode}, data_format={self.data_format}{name_str}'
  367. class UpsamplingNearest2D(Layer):
  368. """
  369. This op upsamples a batch of images, using nearest neighbours' pixel values.
  370. The input must be a 4-D Tensor of the shape (num_batches, channels, in_h, in_w),
  371. where in_w is width of the input tensor, in_h is the height of the input tensor.
  372. And the upsampling only applies on the two dimensions(height and width).
  373. Nearest neighbor interpolation is to perform nearest neighbor interpolation
  374. in both the 3rd dimension(in height direction) and the 4th dimension(in width
  375. direction) on input tensor.
  376. For details of nearest neighbor interpolation, please refer to Wikipedia:
  377. https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation.
  378. Parameters:
  379. x (Tensor): 4-D Tensor, its data type is float32, float64, or uint8,
  380. its data format is specified by :attr:`data_format`.
  381. size (int|list|tuple|Tensor|None): Output shape of image resize
  382. layer, the shape is (out_h, out_w) when input is a 4-D Tensor.
  383. Default: None. If an int value, the `out_h` and `out_w` will be set as the number.
  384. If a list/tuple, each element can be an integer or a Tensor of shape: [1].
  385. If a Tensor, its dimensions size should be a 1.
  386. scale_factor (float|int|list|tuple|Tensor|None): The multiplier for the input height or width. At
  387. least one of :attr:`size` or :attr:`scale_factor` must be set.
  388. And :attr:`size` has a higher priority than :attr:`scale_factor`.
  389. Has to match input size if it is either a list or a tuple or a Tensor.
  390. Default: None.
  391. data_format (str, optional): Specify the data format of the input, and the data format of the output
  392. will be consistent with that of the input. An optional string from:`NCW`, `NWC`, `"NCHW"`, `"NHWC"`, `"NCDHW"`,
  393. `"NDHWC"`. The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
  394. `[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is stored
  395. in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
  396. name(str, optional): The default value is None.
  397. Normally there is no need for user to set this property.
  398. For more information, please refer to :ref:`api_guide_Name`
  399. Returns:
  400. A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
  401. Examples:
  402. .. code-block:: python
  403. >>> import paddle
  404. >>> import paddle.nn as nn
  405. >>> input_data = paddle.rand(shape=(2, 3, 6, 10)).astype("float32")
  406. >>> upsample_out = paddle.nn.UpsamplingNearest2D(size=[12, 12])
  407. >>> input = paddle.to_tensor(input_data)
  408. >>> output = upsample_out(x=input)
  409. >>> print(output.shape)
  410. [2, 3, 12, 12]
  411. """
  412. def __init__(
  413. self, size=None, scale_factor=None, data_format='NCHW', name=None
  414. ):
  415. super().__init__()
  416. if isinstance(size, int):
  417. size = [size, size]
  418. self.size = size
  419. self.scale_factor = scale_factor
  420. self.data_format = data_format
  421. self.name = name
  422. def forward(self, x):
  423. out = F.interpolate(
  424. x,
  425. size=self.size,
  426. scale_factor=self.scale_factor,
  427. mode='nearest',
  428. align_corners=False,
  429. align_mode=0,
  430. data_format=self.data_format,
  431. name=self.name,
  432. )
  433. return out
  434. def extra_repr(self):
  435. if self.scale_factor is not None:
  436. main_str = f'scale_factor={self.scale_factor}'
  437. else:
  438. main_str = f'size={self.size}'
  439. name_str = f', name={self.name}' if self.name else ''
  440. return f'{main_str}, data_format={self.data_format}{name_str}'
  441. class UpsamplingBilinear2D(Layer):
  442. """
  443. This op upsamples a batch of images, using bilinear' pixel values.
  444. The input must be a 4-D Tensor of the shape (num_batches, channels, in_h, in_w),
  445. where in_w is width of the input tensor, in_h is the height of the input tensor.
  446. And the upsampling only applies on the two dimensions(height and width).
  447. Bilinear interpolation is an extension of linear interpolation for
  448. interpolating functions of two variables (e.g. H-direction and
  449. W-direction in this op) on a rectilinear 2D grid. The key idea is
  450. to perform linear interpolation first in one direction, and then
  451. again in the other direction.
  452. For details of bilinear interpolation, please refer to Wikipedia:
  453. https://en.wikipedia.org/wiki/Bilinear_interpolation.
  454. Parameters:
  455. x (Tensor): 4-D Tensor, its data type is float32, float64, or uint8,
  456. its data format is specified by :attr:`data_format`.
  457. size (int|list|tuple|Tensor|None): Output shape of image resize
  458. layer, the shape is (out_h, out_w) when input is a 4-D Tensor.
  459. Default: None. If an int value, the `out_h` and `out_w` will be set as the number.
  460. If a list/tuple, each element can be an integer or a Tensor of shape: [1].
  461. If a Tensor , its dimensions size should be a 1.
  462. scale_factor (float|int|list|tuple|Tensor|None): The multiplier for the input height or width. At
  463. least one of :attr:`size` or :attr:`scale_factor` must be set.
  464. And :attr:`size` has a higher priority than :attr:`scale_factor`.
  465. Has to match input size if it is either a list or a tuple or a Tensor.
  466. Default: None.
  467. data_format (str, optional): Specify the data format of the input, and the data format of the output
  468. will be consistent with that of the input. An optional string from:`NCW`, `NWC`, `"NCHW"`, `"NHWC"`, `"NCDHW"`,
  469. `"NDHWC"`. The default is `"NCHW"`. When it is `"NCHW"`, the data is stored in the order of:
  470. `[batch_size, input_channels, input_height, input_width]`. When it is `"NCHW"`, the data is stored
  471. in the order of: `[batch_size, input_channels, input_depth, input_height, input_width]`.
  472. name(str, optional): The default value is None.
  473. Normally there is no need for user to set this property.
  474. For more information, please refer to :ref:`api_guide_Name`
  475. Returns:
  476. A 4-D Tensor of the shape (num_batches, channels, out_h, out_w) or (num_batches, out_h, out_w, channels),
  477. Examples:
  478. .. code-block:: python
  479. >>> import paddle
  480. >>> import paddle.nn as nn
  481. >>> input_data = paddle.rand(shape=(2, 3, 6, 10)).astype("float32")
  482. >>> upsample_out = paddle.nn.UpsamplingBilinear2D(size=[12, 12])
  483. >>> input = paddle.to_tensor(input_data)
  484. >>> output = upsample_out(x=input)
  485. >>> print(output.shape)
  486. [2, 3, 12, 12]
  487. """
  488. def __init__(
  489. self, size=None, scale_factor=None, data_format='NCHW', name=None
  490. ):
  491. super().__init__()
  492. if isinstance(size, int):
  493. size = [size, size]
  494. self.size = size
  495. self.scale_factor = scale_factor
  496. self.data_format = data_format
  497. self.name = name
  498. def forward(self, x):
  499. out = F.interpolate(
  500. x,
  501. size=self.size,
  502. scale_factor=self.scale_factor,
  503. mode='bilinear',
  504. align_corners=True,
  505. align_mode=0,
  506. data_format=self.data_format,
  507. name=self.name,
  508. )
  509. return out
  510. def extra_repr(self):
  511. if self.scale_factor is not None:
  512. main_str = f'scale_factor={self.scale_factor}'
  513. else:
  514. main_str = f'size={self.size}'
  515. name_str = f', name={self.name}' if self.name else ''
  516. return f'{main_str}, data_format={self.data_format}{name_str}'
  517. class Bilinear(Layer):
  518. r"""
  519. This layer performs bilinear on two inputs.
  520. .. math::
  521. out_{i} = x1 * W_{i} * {x2^\mathrm{T}}, i=0,1,...,outfeatures-1
  522. out = out + b
  523. In this formula:
  524. - :math:`x1`: the first input contains in1_features elements, shape is [batch_size, in1_features].
  525. - :math:`x2`: the second input contains in2_features elements, shape is [batch_size, in2_features].
  526. - :math:`W_{i}`: the i-th learned weight, shape is [in1_features, in2_features], and learned weight's shape is [out_features, in1_features, in2_features].
  527. - :math:`out_{i}`: the i-th element of out, shape is [batch_size], and out's shape is [batch_size, out_features].
  528. - :math:`b`: the learned bias, shape is [1, out_features].
  529. - :math:`x2^\mathrm{T}`: the transpose of :math:`x2`.
  530. Parameters:
  531. in1_features (int): The dimension of each first input(`x1`).
  532. in2_features (int): The dimension of each second input(`x2`).
  533. out_features (int): The dimension of output of this layer.
  534. weight_attr (ParamAttr, optional): The parameter attribute for the learnable w, parameters/weights of
  535. this layer. The default value is None.
  536. bias_attr (ParamAttr, optional): The parameter attribute for the bias
  537. of this layer. If it is set to False, no bias will be added to the output units.
  538. If it is set to None, the bias is initialized zero. The default value is None.
  539. name (str, optional): The default value is None. Normally there is no need for user
  540. to set this property. For more information, please refer to :ref:`api_guide_Name`. Default: None.
  541. Attribute:
  542. **weight** (Parameter): the learnable weights of this layer.
  543. **bias** (Parameter): the learnable bias of this layer.
  544. Returns:
  545. Tensor: A 2-D Tensor of shape [batch_size, out_features].
  546. Examples:
  547. .. code-block:: python
  548. >>> import paddle
  549. >>> layer1 = paddle.rand((5, 5)).astype('float32')
  550. >>> layer2 = paddle.rand((5, 4)).astype('float32')
  551. >>> bilinear = paddle.nn.Bilinear(in1_features=5,
  552. ... in2_features=4,
  553. ... out_features=1000)
  554. >>> result = bilinear(layer1,layer2)
  555. >>> print(result.shape)
  556. [5, 1000]
  557. """
  558. def __init__(
  559. self,
  560. in1_features,
  561. in2_features,
  562. out_features,
  563. weight_attr=None,
  564. bias_attr=None,
  565. name=None,
  566. ):
  567. super().__init__()
  568. self._weight_attr = weight_attr
  569. self._bias_attr = bias_attr
  570. self._name = name
  571. self._in1_features = in1_features
  572. self._in2_features = in2_features
  573. self._out_features = out_features
  574. self._dtype = self._helper.get_default_dtype()
  575. weight_shape = [
  576. self._out_features,
  577. self._in1_features,
  578. self._in2_features,
  579. ]
  580. self.weight = self.create_parameter(
  581. attr=self._weight_attr,
  582. shape=weight_shape,
  583. dtype=self._dtype,
  584. is_bias=False,
  585. )
  586. bias_shape = [1, self._out_features]
  587. self.bias = self.create_parameter(
  588. attr=self._bias_attr,
  589. shape=bias_shape,
  590. dtype=self._dtype,
  591. is_bias=True,
  592. )
  593. def forward(self, x1, x2):
  594. return F.bilinear(x1, x2, self.weight, self.bias, self._name)
  595. def extra_repr(self):
  596. name_str = f', name={self._name}' if self._name else ''
  597. return f'in1_features={self._in1_features}, in2_features={self._in2_features}, out_features={self._out_features}, dtype={self._dtype}{name_str}'
  598. class Dropout(Layer):
  599. r"""
  600. Dropout is a regularization technique for reducing overfitting by preventing
  601. neuron co-adaption during training as described in the paper:
  602. `Improving neural networks by preventing co-adaptation of feature detectors <https://arxiv.org/abs/1207.0580>`_
  603. The dropout operator randomly sets the outputs of some units to zero, while upscale others
  604. according to the given dropout probability.
  605. See :ref:`api_paddle_nn_functional_dropout` for more details.
  606. In dygraph mode, please use ``eval()`` to switch to evaluation mode, where dropout is disabled.
  607. Warning:
  608. The corresponding `functional methods` please reference :ref:`api_paddle_nn_functional_dropout`.
  609. Parameters:
  610. p (float|int, optional): Probability of setting units to zero. Default: 0.5
  611. axis (int|list|tuple, optional): The axis along which the dropout is performed. Default: None.
  612. mode(str, optional): ['upscale_in_train'(default) | 'downscale_in_infer']
  613. 1. upscale_in_train (default), upscale the output at training time
  614. - train: :math:`out = input \times \frac{mask}{(1.0 - p)}`
  615. - inference: :math:`out = input`
  616. 2. downscale_in_infer, downscale the output at inference
  617. - train: :math:`out = input \times mask`
  618. - inference: :math:`out = input \times (1.0 - p)`
  619. name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.
  620. Shape:
  621. - input: N-D tensor.
  622. - output: N-D tensor, the same shape as input.
  623. Examples:
  624. .. code-block:: python
  625. >>> import paddle
  626. >>> paddle.seed(2023)
  627. >>> x = paddle.to_tensor([[1, 2, 3], [4, 5, 6]], dtype="float32")
  628. >>> m = paddle.nn.Dropout(p=0.5)
  629. >>> y_train = m(x)
  630. >>> print(y_train)
  631. Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  632. [[2., 4., 0.],
  633. [8., 0., 0.]])
  634. >>> m.eval() # switch the model to test phase
  635. >>> y_test = m(x)
  636. >>> print(y_test)
  637. Tensor(shape=[2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  638. [[1., 2., 3.],
  639. [4., 5., 6.]])
  640. """
  641. def __init__(self, p=0.5, axis=None, mode="upscale_in_train", name=None):
  642. super().__init__()
  643. self.p = p
  644. self.axis = axis
  645. self.mode = mode
  646. self.name = name
  647. def forward(self, input):
  648. out = F.dropout(
  649. input,
  650. p=self.p,
  651. axis=self.axis,
  652. training=self.training,
  653. mode=self.mode,
  654. name=self.name,
  655. )
  656. return out
  657. def extra_repr(self):
  658. name_str = f', name={self.name}' if self.name else ''
  659. return f'p={self.p}, axis={self.axis}, mode={self.mode}{name_str}'
  660. class Dropout2D(Layer):
  661. """
  662. Randomly zero out entire channels (in the batched input 4d tensor with the shape `NCHW` ,
  663. a channel is a 2D feature map with the shape `HW`). Each channel will be zeroed out independently
  664. on every forward call with probability `p` using samples from a Bernoulli distribution.
  665. Dropout2D will help promote independence between feature maps as described in the paper:
  666. `Efficient Object Localization Using Convolutional Networks <https://arxiv.org/abs/1411.4280>`_
  667. See :ref:`api_paddle_nn_functional_dropout2d` for more details.
  668. In dygraph mode, please use ``eval()`` to switch to evaluation mode, where dropout is disabled.
  669. Parameters:
  670. p (float, optional): Probability of setting units to zero. Default: 0.5.
  671. data_format (str, optional): Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from `NCHW` or `NHWC`. When it is `NCHW`, the data is stored in the order of: [batch_size, input_channels, input_height, input_width]. Default: `NCHW`.
  672. name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.
  673. Shape:
  674. - input: 4-D tensor.
  675. - output: 4-D tensor, the same shape as input.
  676. Examples:
  677. .. code-block:: python
  678. >>> import paddle
  679. >>> paddle.seed(100)
  680. >>> x = paddle.rand([2, 2, 1, 3], dtype="float32")
  681. >>> print(x)
  682. Tensor(shape=[2, 2, 1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  683. [[[[0.55355281, 0.20714243, 0.01162981]],
  684. [[0.51577556, 0.36369765, 0.26091650]]],
  685. [[[0.18905126, 0.56219709, 0.00808361]],
  686. [[0.78120756, 0.32112977, 0.90572405]]]])
  687. >>> m = paddle.nn.Dropout2D(p=0.5)
  688. >>> y_train = m(x)
  689. >>> print(y_train)
  690. Tensor(shape=[2, 2, 1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  691. [[[[1.10710561, 0.41428486, 0.02325963]],
  692. [[1.03155112, 0.72739530, 0.52183300]]],
  693. [[[0. , 0. , 0. ]],
  694. [[0. , 0. , 0. ]]]])
  695. >>> m.eval() # switch the model to test phase
  696. >>> y_test = m(x)
  697. >>> print(y_test)
  698. Tensor(shape=[2, 2, 1, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  699. [[[[0.55355281, 0.20714243, 0.01162981]],
  700. [[0.51577556, 0.36369765, 0.26091650]]],
  701. [[[0.18905126, 0.56219709, 0.00808361]],
  702. [[0.78120756, 0.32112977, 0.90572405]]]])
  703. """
  704. def __init__(self, p=0.5, data_format='NCHW', name=None):
  705. super().__init__()
  706. self.p = p
  707. self.data_format = data_format
  708. self.name = name
  709. def forward(self, input):
  710. out = F.dropout2d(
  711. input,
  712. p=self.p,
  713. training=self.training,
  714. data_format=self.data_format,
  715. name=self.name,
  716. )
  717. return out
  718. def extra_repr(self):
  719. name_str = f', name={self.name}' if self.name else ''
  720. return f'p={self.p}, data_format={self.data_format}{name_str}'
  721. class Dropout3D(Layer):
  722. """
  723. Randomly zero out entire channels (in the batched input 5d tensor with the shape `NCDHW` ,
  724. a channel is a 3D feature map with the shape `DHW` ). Each channel will be zeroed out independently
  725. on every forward call with probability `p` using samples from a Bernoulli distribution.
  726. Dropout3D will help promote independence between feature maps as described in the paper:
  727. `Efficient Object Localization Using Convolutional Networks <https://arxiv.org/abs/1411.4280>`_
  728. See :ref:`api_paddle_nn_functional_dropout3d` for more details.
  729. In dygraph mode, please use ``eval()`` to switch to evaluation mode, where dropout is disabled.
  730. Parameters:
  731. p (float | int, optional): Probability of setting units to zero. Default: 0.5.
  732. data_format (str, optional): Specify the data format of the input, and the data format of the output will be consistent with that of the input. An optional string from `NCDHW` or `NDHWC`. When it is `NCDHW`, the data is stored in the order of: [batch_size, input_channels, input_depth, input_height, input_width]. Default: `NCDHW`.
  733. name (str, optional): Name for the operation, Default: None. For more information, please refer to :ref:`api_guide_Name`.
  734. Shape:
  735. - input: 5-D tensor.
  736. - output: 5-D tensor, the same shape as input.
  737. Examples:
  738. .. code-block:: python
  739. >>> import paddle
  740. >>> x = paddle.arange(24, dtype="float32").reshape((1, 2, 2, 2, 3))
  741. >>> print(x)
  742. Tensor(shape=[1, 2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  743. [[[[[0. , 1. , 2. ],
  744. [3. , 4. , 5. ]],
  745. [[6. , 7. , 8. ],
  746. [9. , 10., 11.]]],
  747. [[[12., 13., 14.],
  748. [15., 16., 17.]],
  749. [[18., 19., 20.],
  750. [21., 22., 23.]]]]])
  751. >>> m = paddle.nn.Dropout3D(p=0.5)
  752. >>> y_train = m(x)
  753. >>> m.eval() # switch the model to test phase
  754. >>> y_test = m(x)
  755. >>> print(y_test)
  756. Tensor(shape=[1, 2, 2, 2, 3], dtype=float32, place=Place(cpu), stop_gradient=True,
  757. [[[[[0. , 1. , 2. ],
  758. [3. , 4. , 5. ]],
  759. [[6. , 7. , 8. ],
  760. [9. , 10., 11.]]],
  761. [[[12., 13., 14.],
  762. [15., 16., 17.]],
  763. [[18., 19., 20.],
  764. [21., 22., 23.]]]]])
  765. """
  766. def __init__(self, p=0.5, data_format='NCDHW', name=None):
  767. super().__init__()
  768. self.p = p
  769. self.data_format = data_format
  770. self.name = name
  771. def forward(self, input):
  772. out = F.dropout3d(
  773. input,
  774. p=self.p,
  775. training=self.training,
  776. data_format=self.data_format,
  777. name=self.name,
  778. )
  779. return out
  780. def extra_repr(self):
  781. name_str = f', name={self.name}' if self.name else ''
  782. return f'p={self.p}, data_format={self.data_format}{name_str}'
  783. class AlphaDropout(Layer):
  784. """
  785. Alpha Dropout is a type of Dropout that maintains the self-normalizing property. For an input with
  786. zero mean and unit standard deviation, the output of Alpha Dropout maintains the original mean and
  787. standard deviation of the input. Alpha Dropout fits well to SELU activate function by randomly setting
  788. activations to the negative saturation value.
  789. For more information, please refer to:
  790. `Self-Normalizing Neural Networks <https://arxiv.org/abs/1706.02515>`_
  791. In dygraph mode, please use ``eval()`` to switch to evaluation mode, where dropout is disabled.
  792. Parameters:
  793. p (float | int): Probability of setting units to zero. Default: 0.5
  794. name (str, optional): Name for the operation (optional, default is None). For more information, please refer to :ref:`api_guide_Name`.
  795. Shape:
  796. - input: N-D tensor.
  797. - output: N-D tensor, the same shape as input.
  798. Examples:
  799. .. code-block:: python
  800. >>> import paddle
  801. >>> paddle.seed(2023)
  802. >>> x = paddle.to_tensor([[-1, 1], [-1, 1]], dtype="float32")
  803. >>> m = paddle.nn.AlphaDropout(p=0.5)
  804. >>> y_train = m(x)
  805. >>> print(y_train)
  806. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  807. [[-0.10721093, 1.66559887],
  808. [-0.77919382, 1.66559887]])
  809. >>> m.eval() # switch the model to test phase
  810. >>> y_test = m(x)
  811. >>> print(y_test)
  812. Tensor(shape=[2, 2], dtype=float32, place=Place(cpu), stop_gradient=True,
  813. [[-1., 1.],
  814. [-1., 1.]])
  815. """
  816. def __init__(self, p=0.5, name=None):
  817. super().__init__()
  818. self.p = p
  819. self.name = name
  820. def forward(self, input):
  821. out = F.alpha_dropout(
  822. input, p=self.p, training=self.training, name=self.name
  823. )
  824. return out
  825. def extra_repr(self):
  826. name_str = f', name={self.name}' if self.name else ''
  827. return f'p={self.p}{name_str}'
  828. class Pad1D(Layer):
  829. """
  830. This interface is used to construct a callable object of the ``Pad1D`` class.
  831. Pad tensor according to ``pad``, ``mode`` and ``value``.
  832. If mode is ``reflect``, pad[0] and pad[1] must be no greater than width-1.
  833. Parameters:
  834. padding (Tensor|list[int]|int): The padding size with data type ``'int'``. If is ``'int'``, use the
  835. same padding in both dimensions. Else [len(padding)/2] dimensions
  836. of input will be padded. The pad has the form (pad_left, pad_right).
  837. mode (str, optional): Four modes: ``'constant'`` (default), ``'reflect'``, ``'replicate'``, ``'circular'``. Default: ``'constant'``.
  838. - 'constant' mode, uses a constant value to pad the input tensor.
  839. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
  840. - 'replicate' mode, uses input boundaries to pad the input tensor.
  841. - 'circular' mode, uses circular input to pad the input tensor.
  842. value (float, optional): The value to fill the padded areas. Default is :math:`0.0`.
  843. data_format (str, optional): An string from: ``'NCL'``, ``'NLC'``. Specify the data format of the input data.
  844. Default: ``'NCL'``.
  845. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: ``'None'``.
  846. Returns:
  847. None
  848. Examples:
  849. .. code-block:: python
  850. >>> import paddle
  851. >>> import paddle.nn as nn
  852. >>> input_shape = (1, 2, 3)
  853. >>> pad = [1, 2]
  854. >>> mode = "constant"
  855. >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1
  856. >>> my_pad = nn.Pad1D(padding=pad, mode=mode)
  857. >>> result = my_pad(data)
  858. >>> print(result)
  859. Tensor(shape=[1, 2, 6], dtype=float32, place=Place(cpu), stop_gradient=True,
  860. [[[0., 1., 2., 3., 0., 0.],
  861. [0., 4., 5., 6., 0., 0.]]])
  862. """
  863. def __init__(
  864. self, padding, mode='constant', value=0.0, data_format="NCL", name=None
  865. ):
  866. super().__init__()
  867. self._pad = _npairs(padding, 1)
  868. self._mode = mode
  869. self._value = value
  870. self._data_format = data_format
  871. self._name = name
  872. def forward(self, x):
  873. return F.pad(
  874. x,
  875. pad=self._pad,
  876. mode=self._mode,
  877. value=self._value,
  878. data_format=self._data_format,
  879. name=self._name,
  880. )
  881. def extra_repr(self):
  882. name_str = f', name={self._name}' if self._name else ''
  883. return f'padding={self._pad}, mode={self._mode}, value={self._value}, data_format={self._data_format}{name_str}'
  884. class ZeroPad1D(Layer):
  885. """
  886. This interface is used to construct a callable object of the ``ZeroPad1D`` class.
  887. Pads the input tensor boundaries with zero.
  888. Parameters:
  889. padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
  890. same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded.
  891. The pad has the form (pad_left, pad_right).
  892. data_format (str): An string from: "NCL", "NLC". Specify the data format of the input data.
  893. Default is "NCL"
  894. name (str, optional) : The default value is None. Normally there is no need for
  895. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  896. Shape:
  897. - x(Tensor): The input tensor of zeropad1d operator, which is a 3-D tensor.
  898. The data type can be float32, float64.
  899. - output(Tensor): The output tensor of zeropad1d operator, which is a 3-D tensor.
  900. The data type is same as input x.
  901. Examples:
  902. .. code-block:: python
  903. >>> import paddle
  904. >>> import paddle.nn as nn
  905. >>> input_shape = (1, 2, 3)
  906. >>> pad = [1, 2]
  907. >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1
  908. >>> my_pad = nn.ZeroPad1D(padding=pad)
  909. >>> result = my_pad(data)
  910. >>> print(result)
  911. Tensor(shape=[1, 2, 6], dtype=float32, place=Place(cpu), stop_gradient=True,
  912. [[[0., 1., 2., 3., 0., 0.],
  913. [0., 4., 5., 6., 0., 0.]]])
  914. """
  915. def __init__(self, padding, data_format="NCL", name=None):
  916. super().__init__()
  917. self._pad = _npairs(padding, 1)
  918. self._mode = 'constant'
  919. self._value = 0.0
  920. self._data_format = data_format
  921. self._name = name
  922. def forward(self, x):
  923. return F.pad(
  924. x,
  925. pad=self._pad,
  926. mode=self._mode,
  927. value=self._value,
  928. data_format=self._data_format,
  929. name=self._name,
  930. )
  931. def extra_repr(self):
  932. name_str = f', name={self._name}' if self._name else ''
  933. return f'padding={self._pad}, data_format={self._data_format}{name_str}'
  934. class Pad2D(Layer):
  935. """
  936. This interface is used to construct a callable object of the ``Pad2D`` class.
  937. Pad tensor according to ``pad``, ``mode`` and ``value``.
  938. If mode is ``'reflect'``, pad[0] and pad[1] must be no greater
  939. than width-1. The height dimension has the same condition.
  940. Parameters:
  941. padding (Tensor|list[int]|int): The padding size with data type ``'int'``. If is ``'int'``, use the
  942. same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded.
  943. The pad has the form (pad_left, pad_right, pad_top, pad_bottom).
  944. mode (str, optional): Four modes: ``'constant'`` (default), ``'reflect'``, ``'replicate'``, ``'circular'``. Default: ``'constant'``.
  945. - 'constant' mode, uses a constant value to pad the input tensor.
  946. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
  947. - 'replicate' mode, uses input boundaries to pad the input tensor.
  948. - 'circular' mode, uses circular input to pad the input tensor.
  949. value (float, optional): The value to fill the padded areas. Default is :math:`0.0`.
  950. data_format (str, optional): An string from: ``'NCHW'``, ``'NHWC'``. Specify the data format of the input data.
  951. Default: ``'NCHW'``.
  952. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: ``'None'``.
  953. Returns:
  954. None
  955. Examples:
  956. .. code-block:: python
  957. >>> import paddle
  958. >>> import paddle.nn as nn
  959. >>> input_shape = (1, 1, 2, 3)
  960. >>> pad = [1, 0, 1, 2]
  961. >>> mode = "constant"
  962. >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1
  963. >>> my_pad = nn.Pad2D(padding=pad, mode=mode)
  964. >>> result = my_pad(data)
  965. >>> print(result)
  966. Tensor(shape=[1, 1, 5, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  967. [[[[0., 0., 0., 0.],
  968. [0., 1., 2., 3.],
  969. [0., 4., 5., 6.],
  970. [0., 0., 0., 0.],
  971. [0., 0., 0., 0.]]]])
  972. """
  973. def __init__(
  974. self, padding, mode='constant', value=0.0, data_format="NCHW", name=None
  975. ):
  976. super().__init__()
  977. self._pad = _npairs(padding, 2)
  978. self._mode = mode
  979. self._value = value
  980. self._data_format = data_format
  981. self._name = name
  982. def forward(self, x):
  983. return F.pad(
  984. x,
  985. pad=self._pad,
  986. mode=self._mode,
  987. value=self._value,
  988. data_format=self._data_format,
  989. name=self._name,
  990. )
  991. def extra_repr(self):
  992. name_str = f', name={self._name}' if self._name else ''
  993. return f'padding={self._pad}, mode={self._mode}, value={self._value}, data_format={self._data_format}{name_str}'
  994. class ZeroPad2D(Layer):
  995. """
  996. This interface is used to construct a callable object of the ``ZeroPad2D`` class.
  997. Pads the input tensor boundaries with zero.
  998. Parameters:
  999. padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
  1000. same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded.
  1001. The pad has the form (pad_left, pad_right, pad_top, pad_bottom).
  1002. data_format (str): An string from: "NCHW", "NHWC". Specify the data format of the input data.
  1003. Default is "NCHW"
  1004. name (str, optional) : The default value is None. Normally there is no need for
  1005. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1006. Shape:
  1007. - x(Tensor): The input tensor of zeropad2d operator, which is a 4-D tensor.
  1008. The data type can be float32, float64.
  1009. - output(Tensor): The output tensor of zeropad2d operator, which is a 4-D tensor.
  1010. The data type is same as input x.
  1011. Examples:
  1012. .. code-block:: python
  1013. >>> import paddle
  1014. >>> import paddle.nn as nn
  1015. >>> input_shape = paddle.to_tensor([1, 1, 2, 3])
  1016. >>> pad = [1, 0, 1, 2]
  1017. >>> data = paddle.arange(paddle.prod(input_shape), dtype="float32").reshape(input_shape) + 1
  1018. >>> my_pad = nn.ZeroPad2D(padding=pad)
  1019. >>> result = my_pad(data)
  1020. >>> print(result)
  1021. Tensor(shape=[1, 1, 5, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1022. [[[[0., 0., 0., 0.],
  1023. [0., 1., 2., 3.],
  1024. [0., 4., 5., 6.],
  1025. [0., 0., 0., 0.],
  1026. [0., 0., 0., 0.]]]])
  1027. """
  1028. def __init__(self, padding, data_format="NCHW", name=None):
  1029. super().__init__()
  1030. self._pad = _npairs(padding, 2)
  1031. self._mode = 'constant'
  1032. self._value = 0.0
  1033. self._data_format = data_format
  1034. self._name = name
  1035. def forward(self, x):
  1036. return F.pad(
  1037. x,
  1038. pad=self._pad,
  1039. mode=self._mode,
  1040. value=self._value,
  1041. data_format=self._data_format,
  1042. name=self._name,
  1043. )
  1044. def extra_repr(self):
  1045. name_str = f', name={self._name}' if self._name else ''
  1046. return f'padding={self._pad}, data_format={self._data_format}{name_str}'
  1047. class Pad3D(Layer):
  1048. """
  1049. This interface is used to construct a callable object of the ``Pad3D`` class.
  1050. Pad tensor according to ``'pad'``, ``'mode'`` and ``'value'``.
  1051. If mode is ``'reflect'``, pad[0] and pad[1] must be no greater
  1052. than width-1. The height and depth dimension has the same condition.
  1053. Parameters:
  1054. padding (Tensor|list[int]|int): The padding size with data type ``'int'``. If is ``'int'``, use the
  1055. same padding in all dimensions. Else [len(padding)/2] dimensions
  1056. of input will be padded. The pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).
  1057. mode (str, optional): Four modes: ``'constant'`` (default), ``'reflect'``, ``'replicate'``, ``'circular'``. Default: ``'constant'``.
  1058. - 'constant' mode, uses a constant value to pad the input tensor.
  1059. - 'reflect' mode, uses reflection of the input boundaries to pad the input tensor.
  1060. - 'replicate' mode, uses input boundaries to pad the input tensor.
  1061. - 'circular' mode, uses circular input to pad the input tensor.
  1062. value (float, optional): The value to fill the padded areas. Default is :math:`0.0`.
  1063. data_format (str, optional): An string from: ``'NCDHW'``, ``'NDHWC'``. Specify the data format of the input data.
  1064. Default: ``'NCDHW'``。
  1065. name (str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: ``'None'``.
  1066. Returns:
  1067. None
  1068. Examples:
  1069. .. code-block:: python
  1070. >>> import paddle
  1071. >>> import paddle.nn as nn
  1072. >>> input_shape = (1, 1, 1, 2, 3)
  1073. >>> pad = [1, 0, 1, 2, 0, 0]
  1074. >>> mode = "constant"
  1075. >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1
  1076. >>> my_pad = nn.Pad3D(padding=pad, mode=mode)
  1077. >>> result = my_pad(data)
  1078. >>> print(result)
  1079. Tensor(shape=[1, 1, 1, 5, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1080. [[[[[0., 0., 0., 0.],
  1081. [0., 1., 2., 3.],
  1082. [0., 4., 5., 6.],
  1083. [0., 0., 0., 0.],
  1084. [0., 0., 0., 0.]]]]])
  1085. """
  1086. def __init__(
  1087. self,
  1088. padding,
  1089. mode='constant',
  1090. value=0.0,
  1091. data_format="NCDHW",
  1092. name=None,
  1093. ):
  1094. super().__init__()
  1095. self._pad = _npairs(padding, 3)
  1096. self._mode = mode
  1097. self._value = value
  1098. self._data_format = data_format
  1099. self._name = name
  1100. def forward(self, x):
  1101. return F.pad(
  1102. x,
  1103. pad=self._pad,
  1104. mode=self._mode,
  1105. value=self._value,
  1106. data_format=self._data_format,
  1107. name=self._name,
  1108. )
  1109. def extra_repr(self):
  1110. name_str = f', name={self._name}' if self._name else ''
  1111. return f'padding={self._pad}, mode={self._mode}, value={self._value}, data_format={self._data_format}{name_str}'
  1112. class ZeroPad3D(Layer):
  1113. """
  1114. This interface is used to construct a callable object of the ``ZeroPad3D`` class.
  1115. Pads the input tensor boundaries with zero.
  1116. Parameters:
  1117. padding (Tensor | List[int] | int): The padding size with data type int. If is int, use the
  1118. same padding in all dimensions. Else [len(padding)/2] dimensions of input will be padded.
  1119. The pad has the form (pad_left, pad_right, pad_top, pad_bottom, pad_front, pad_back).
  1120. data_format (str): An string from: "NCDHW", "NDHWC". Specify the data format of the input data.
  1121. Default is "NCDHW"
  1122. name (str, optional) : The default value is None. Normally there is no need for
  1123. user to set this property. For more information, please refer to :ref:`api_guide_Name`.
  1124. Shape:
  1125. - x(Tensor): The input tensor of zeropad3d operator, which is a 5-D tensor.
  1126. The data type can be float32, float64.
  1127. - output(Tensor): The output tensor of zeropad3d operator, which is a 5-D tensor.
  1128. The data type is same as input x.
  1129. Examples:
  1130. .. code-block:: python
  1131. >>> import paddle
  1132. >>> import paddle.nn as nn
  1133. >>> input_shape = (1, 1, 1, 2, 3)
  1134. >>> pad = [1, 0, 1, 2, 0, 0]
  1135. >>> data = paddle.arange(paddle.prod(paddle.to_tensor(input_shape)), dtype="float32").reshape(input_shape) + 1
  1136. >>> my_pad = nn.ZeroPad3D(padding=pad)
  1137. >>> result = my_pad(data)
  1138. >>> print(result)
  1139. Tensor(shape=[1, 1, 1, 5, 4], dtype=float32, place=Place(cpu), stop_gradient=True,
  1140. [[[[[0., 0., 0., 0.],
  1141. [0., 1., 2., 3.],
  1142. [0., 4., 5., 6.],
  1143. [0., 0., 0., 0.],
  1144. [0., 0., 0., 0.]]]]])
  1145. """
  1146. def __init__(self, padding, data_format="NCDHW", name=None):
  1147. super().__init__()
  1148. self._pad = _npairs(padding, 3)
  1149. self._mode = 'constant'
  1150. self._value = 0.0
  1151. self._data_format = data_format
  1152. self._name = name
  1153. def forward(self, x):
  1154. return F.pad(
  1155. x,
  1156. pad=self._pad,
  1157. mode=self._mode,
  1158. value=self._value,
  1159. data_format=self._data_format,
  1160. name=self._name,
  1161. )
  1162. def extra_repr(self):
  1163. name_str = f', name={self._name}' if self._name else ''
  1164. return f'padding={self._pad}, data_format={self._data_format}{name_str}'
  1165. class CosineSimilarity(Layer):
  1166. """
  1167. This interface is used to compute cosine similarity between x1 and x2 along axis.
  1168. Parameters:
  1169. axis (int): Dimension of vectors to compute cosine similarity. Default is 1.
  1170. eps(float): Small value to avoid division by zero. Default is 1e-8.
  1171. Returns:
  1172. None
  1173. Examples:
  1174. .. code-block:: text
  1175. Case 0:
  1176. x1 = [[0.8024077 0.9927354 0.27238318 0.8344984 ]
  1177. [0.48949873 0.5797396 0.65444374 0.66510963]
  1178. [0.1031398 0.9614342 0.08365563 0.6796464 ]
  1179. [0.10760343 0.7461209 0.7726148 0.5801006 ]]
  1180. x2 = [[0.62913156 0.1536727 0.9847992 0.04591406]
  1181. [0.9098952 0.15715368 0.8671125 0.3156102 ]
  1182. [0.4427798 0.54136837 0.5276275 0.32394758]
  1183. [0.3769419 0.8535014 0.48041078 0.9256797 ]]
  1184. axis = 1
  1185. eps = 1e-8
  1186. Out: [0.5275037 0.8368967 0.75037485 0.9245899]
  1187. Code Examples:
  1188. .. code-block:: python
  1189. >>> import paddle
  1190. >>> import paddle.nn as nn
  1191. >>> x1 = paddle.to_tensor([[1., 2., 3.],
  1192. ... [2., 3., 4.]], dtype="float32")
  1193. >>> x2 = paddle.to_tensor([[8., 3., 3.],
  1194. ... [2., 3., 4.]], dtype="float32")
  1195. >>> cos_sim_func = nn.CosineSimilarity(axis=0)
  1196. >>> result = cos_sim_func(x1, x2)
  1197. >>> print(result)
  1198. Tensor(shape=[3], dtype=float32, place=Place(cpu), stop_gradient=True,
  1199. [0.65079135, 0.98058069, 1. ])
  1200. """
  1201. def __init__(self, axis=1, eps=1e-8):
  1202. super().__init__()
  1203. self._axis = axis
  1204. self._eps = eps
  1205. def forward(self, x1, x2):
  1206. return F.cosine_similarity(x1, x2, axis=self._axis, eps=self._eps)
  1207. def extra_repr(self):
  1208. return 'axis={_axis}, eps={_eps}'.format(**self.__dict__)
  1209. class Embedding(Layer):
  1210. r"""
  1211. Embedding Layer, used to construct a callable object of the ``Embedding`` class.
  1212. For specific usage, refer to code examples. It implements the function of the Embedding Layer.
  1213. This layer is used to lookup embeddings vector of ids provided by :attr:`x` .
  1214. It automatically constructs a 2D embedding matrix based on the
  1215. input :attr:`num_embeddings` and :attr:`embedding_dim`.
  1216. The shape of output Tensor is generated by appending an emb_size dimension to the
  1217. last dimension of the input Tensor shape.
  1218. Note:
  1219. The id in :attr:`x` must satisfy :math:`0 =< id < num_embeddings` ,
  1220. otherwise the program will throw an exception and exit.
  1221. .. code-block:: text
  1222. Case 1:
  1223. x is a Tensor. padding_idx = -1
  1224. x.data = [[1, 3], [2, 4], [4, 127]
  1225. x.shape = [3, 2]
  1226. Given size = [128, 16]
  1227. output is a Tensor:
  1228. out.shape = [3, 2, 16]
  1229. out.data = [[[0.129435295, 0.244512452, ..., 0.436322452],
  1230. [0.345421456, 0.524563927, ..., 0.144534654]],
  1231. [[0.345249859, 0.124939536, ..., 0.194353745],
  1232. [0.945345345, 0.435394634, ..., 0.435345365]],
  1233. [[0.945345345, 0.435394634, ..., 0.435345365],
  1234. [0.0, 0.0, ..., 0.0 ]]] # padding data
  1235. The input padding_idx is less than 0, it is automatically converted to padding_idx = -1 + 128 = 127
  1236. It will pad all-zero data when ids is 127.
  1237. Parameters:
  1238. num_embeddings (int): Just one element which indicate the size of the dictionary of embeddings.
  1239. embedding_dim (int): Just one element which indicate the size of each embedding vector respectively.
  1240. padding_idx(int|long|None, optional): padding_idx needs to be in the interval [-num_embeddings, num_embeddings).
  1241. If :math:`padding\_idx < 0`, the :math:`padding\_idx` will automatically be converted
  1242. to :math:`vocab\_size + padding\_idx` . It will output all-zero padding data whenever lookup
  1243. encounters :math:`padding\_idx` in id. And the padding data will not be updated while training.
  1244. If set None, it makes no effect to output. Default: None.
  1245. sparse(bool, optional): The flag indicating whether to use sparse update. This parameter only
  1246. affects the performance of the backwards gradient update. It is recommended to set
  1247. True because sparse update is faster. But some optimizer does not support sparse update,
  1248. such as :ref:`api_paddle_optimizer_adadelta_Adadelta` , :ref:`api_paddle_optimizer_adamax_Adamax` , :ref:`api_paddle_optimizer_lamb_Lamb`.
  1249. In these case, sparse must be False. Default: False.
  1250. weight_attr(ParamAttr, optional): To specify the weight parameter property. Default: None, which means the
  1251. default weight parameter property is used. See usage for details in :ref:`api_paddle_ParamAttr` . In addition,
  1252. user-defined or pre-trained word vectors can be loaded with the :attr:`param_attr` parameter.
  1253. The local word vector needs to be transformed into numpy format, and the shape of local word
  1254. vector should be consistent with :attr:`num_embeddings` . Then :ref:`api_paddle_nn_initializer_Assign`
  1255. is used to load custom or pre-trained word vectors. See code example for details.
  1256. name(str, optional): For detailed information, please refer to :ref:`api_guide_Name`. Usually name is no need to set and
  1257. None by default.
  1258. Attribute:
  1259. **weight** (Parameter): the learnable weights of this layer.
  1260. Returns:
  1261. None
  1262. Examples:
  1263. .. code-block:: python
  1264. >>> import paddle
  1265. >>> x = paddle.to_tensor([[0], [1], [3]], dtype="int64", stop_gradient=False)
  1266. >>> embedding = paddle.nn.Embedding(4, 3, sparse=True)
  1267. >>> w0 = paddle.to_tensor([[0., 0., 0.],
  1268. ... [1., 1., 1.],
  1269. ... [2., 2., 2.],
  1270. ... [3., 3., 3.]], dtype="float32")
  1271. >>> embedding.weight.set_value(w0)
  1272. >>> print(embedding.weight)
  1273. Parameter containing:
  1274. Tensor(shape=[4, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
  1275. [[0., 0., 0.],
  1276. [1., 1., 1.],
  1277. [2., 2., 2.],
  1278. [3., 3., 3.]])
  1279. >>> adam = paddle.optimizer.Adam(parameters=[embedding.weight], learning_rate=0.01)
  1280. >>> adam.clear_grad()
  1281. >>> out = embedding(x)
  1282. >>> print(out)
  1283. Tensor(shape=[3, 1, 3], dtype=float32, place=Place(cpu), stop_gradient=False,
  1284. [[[0., 0., 0.]],
  1285. [[1., 1., 1.]],
  1286. [[3., 3., 3.]]])
  1287. >>> out.backward()
  1288. >>> adam.step()
  1289. """
  1290. def __init__(
  1291. self,
  1292. num_embeddings,
  1293. embedding_dim,
  1294. padding_idx=None,
  1295. sparse=False,
  1296. weight_attr=None,
  1297. name=None,
  1298. ):
  1299. super().__init__()
  1300. self._num_embeddings = num_embeddings
  1301. self._embedding_dim = embedding_dim
  1302. self._sparse = sparse
  1303. self._is_distributed = False
  1304. self._padding_idx = padding_idx
  1305. if self._num_embeddings <= 0:
  1306. raise ValueError("num_embeddings must be gather than 0")
  1307. if self._embedding_dim <= 0:
  1308. raise ValueError("embedding_dim must be gather than 0")
  1309. padding_idx = (
  1310. -1
  1311. if padding_idx is None
  1312. else padding_idx
  1313. if padding_idx >= 0
  1314. else (num_embeddings + padding_idx)
  1315. )
  1316. if padding_idx >= num_embeddings or padding_idx < -num_embeddings:
  1317. raise ValueError(
  1318. f"padding_idx must be within [-{num_embeddings}, {num_embeddings})"
  1319. )
  1320. self._dtype = self._helper.get_default_dtype()
  1321. self._size = [self._num_embeddings, self._embedding_dim]
  1322. self._weight_attr = weight_attr
  1323. self._remote_prefetch = False
  1324. self._name = name
  1325. self.weight = self.create_parameter(
  1326. attr=self._weight_attr,
  1327. shape=self._size,
  1328. dtype=self._dtype,
  1329. is_bias=False,
  1330. )
  1331. if in_dynamic_mode() and padding_idx != -1:
  1332. with paddle.no_grad():
  1333. self.weight[padding_idx] = 0.0
  1334. def forward(self, x):
  1335. return F.embedding(
  1336. x,
  1337. weight=self.weight,
  1338. padding_idx=self._padding_idx,
  1339. sparse=self._sparse,
  1340. name=self._name,
  1341. )
  1342. def extra_repr(self):
  1343. main_str = '{_num_embeddings}, {_embedding_dim}'
  1344. if self._padding_idx is not None:
  1345. main_str += ', padding_idx={_padding_idx}'
  1346. main_str += ', sparse={_sparse}'
  1347. if self._name is not None:
  1348. main_str += ', name={_name}'
  1349. return main_str.format(**self.__dict__)
  1350. class Unfold(Layer):
  1351. """
  1352. Returns a col buffer of sliding local blocks of input x, also known
  1353. as im2col for batched 2D image tensors. For each block under the convolution filter,
  1354. all element will be rearranged as a column. While the convolution filter sliding over
  1355. the input feature map, a series of such columns will be formed.
  1356. For each input :math:`x` with shape [N, C, H, W], the output shape [N, Cout, Lout]
  1357. can be calculated as following.
  1358. See ``paddle.nn.functional.unfold`` for more details.
  1359. Parameters:
  1360. kernel_sizes(int|list|tuple): The size of convolution kernel, should be [k_h, k_w]
  1361. or an integer k treated as [k, k].
  1362. strides(int|list|tuple, optional): The strides, should be [stride_h, stride_w]
  1363. or an integer stride treated as [sride, stride]. For default, strides will be [1, 1].
  1364. paddings(int|list|tuple, optional): The paddings of each dimension, should be
  1365. [padding_top, padding_left, padding_bottom, padding_right] or [padding_h, padding_w]
  1366. or an integer padding. If [padding_h, padding_w] was given, it will expanded to
  1367. [padding_h, padding_w, padding_h, padding_w]. If an integer padding was given,
  1368. [padding, padding, padding, padding] will be used. For default,
  1369. paddings will be [0, 0, 0, 0].
  1370. dilations(int|list|tuple, optional): The dilations of convolution kernel, should be
  1371. [dilation_h, dilation_w], or an integer dilation treated as [dilation, dilation].
  1372. For default, it will be [1, 1].
  1373. name(str, optional): The default value is None. Normally there is no need for user to
  1374. set this property. For more information, please refer to :ref:`api_guide_Name`
  1375. Examples:
  1376. .. code-block:: python
  1377. >>> import paddle
  1378. >>> import paddle.nn as nn
  1379. >>> x = paddle.randn((100, 3, 224, 224))
  1380. >>> unfold = nn.Unfold(kernel_sizes=[3, 3])
  1381. >>> result = unfold(x)
  1382. >>> print(result.shape)
  1383. [100, 27, 49284]
  1384. """
  1385. def __init__(
  1386. self, kernel_sizes, dilations=1, paddings=0, strides=1, name=None
  1387. ):
  1388. super().__init__()
  1389. self.kernel_sizes = kernel_sizes
  1390. self.dilations = dilations
  1391. self.paddings = paddings
  1392. self.strides = strides
  1393. self.name = name
  1394. def forward(self, input):
  1395. return F.unfold(
  1396. input,
  1397. kernel_sizes=self.kernel_sizes,
  1398. strides=self.strides,
  1399. paddings=self.paddings,
  1400. dilations=self.dilations,
  1401. name=self.name,
  1402. )
  1403. def extra_repr(self):
  1404. name_str = f', name={self.name}' if self.name else ''
  1405. return f'kernel_size={self.kernel_sizes}, dilation={self.dilations}, padding={self.paddings}, stride={self.strides}{name_str}'
  1406. class Fold(Layer):
  1407. r"""
  1408. Combines an array of sliding local blocks into a large containing
  1409. tensor. also known as col2im when operated on batched 2D image tensor. Fold calculates each
  1410. combined value in the resulting large tensor by summing all values from all containing blocks.
  1411. For each input :math:`x` with shape [N, C_in , L], the output shape [N, C_out, H_out, W_out]
  1412. can be calculated as following.
  1413. .. math::
  1414. H_{out} &= output\_size[0] \\
  1415. W_{out} &= output\_size[1] \\
  1416. C_{out} &= \frac{C_{in}}{kernel\_sizes[0]\times kernel\_sizes[1]} \\
  1417. Parameters:
  1418. output_sizes(list): The size of output size, should be [output_size_h, output_size_w]
  1419. or an interger o treated as [o, o].
  1420. kernel_sizes(int|list|tuple): The size of convolution kernel, should be [k_h, k_w]
  1421. or an integer k treated as [k, k].
  1422. strides(int|list|tuple, optional): The strides, should be [stride_h, stride_w]
  1423. or an integer stride treated as [sride, stride].
  1424. For default, strides will be [1, 1].
  1425. paddings(int|list|tuple, optional): The paddings of each dimension, should be
  1426. [padding_top, padding_left, padding_bottom, padding_right]
  1427. or [padding_h, padding_w] or an integer padding.
  1428. If [padding_h, padding_w] was given, it will expanded to
  1429. [padding_h, padding_w, padding_h, padding_w]. If an integer
  1430. padding was given, [padding, padding, padding, padding] will
  1431. be used. For default, paddings will be [0, 0, 0, 0]
  1432. dilations(int|list|tuple, optional): The dilations of convolution kernel, should be
  1433. [dilation_h, dilation_w], or an integer dilation treated as
  1434. [dilation, dilation]. For default, it will be [1, 1].
  1435. name(str, optional): The default value is None.
  1436. Normally there is no need for user to set this property.
  1437. For more information, please refer to :ref:`api_guide_Name`
  1438. Returns:
  1439. The tensor formed by combining a group of sliding local blocks
  1440. The output shape is [N, Cout, H, W] as decriabled above.
  1441. Examples:
  1442. .. code-block:: python
  1443. >>> import paddle
  1444. >>> import paddle.nn as nn
  1445. >>> x = paddle.randn([2, 3*2*2, 12])
  1446. >>> fold = nn.Fold(output_sizes=[4, 5], kernel_sizes=2)
  1447. >>> y = fold(x)
  1448. >>> print(y.shape)
  1449. [2, 3, 4, 5]
  1450. """
  1451. def __init__(
  1452. self,
  1453. output_sizes,
  1454. kernel_sizes,
  1455. dilations=1,
  1456. paddings=0,
  1457. strides=1,
  1458. name=None,
  1459. ):
  1460. super().__init__()
  1461. self.output_sizes = output_sizes
  1462. self.kernel_sizes = kernel_sizes
  1463. self.dilations = dilations
  1464. self.paddings = paddings
  1465. self.strides = strides
  1466. self.name = name
  1467. def forward(self, input):
  1468. return F.fold(
  1469. input,
  1470. output_sizes=self.output_sizes,
  1471. kernel_sizes=self.kernel_sizes,
  1472. strides=self.strides,
  1473. paddings=self.paddings,
  1474. dilations=self.dilations,
  1475. name=self.name,
  1476. )
  1477. def extra_repr(self):
  1478. name_str = f', name={self.name}' if self.name else ''
  1479. return f'kernel_size={self.kernel_sizes}, dilation={self.dilations}, padding={self.paddings}, stride={self.strides}{name_str}'
  1480. class Flatten(Layer):
  1481. """
  1482. This interface is used to construct a callable object of the ``FLatten`` class.
  1483. For more details, refer to code examples.
  1484. It implements flatten a contiguous range of dims into a tensor.
  1485. Parameters:
  1486. start_axis(int): first dim to flatten (default = 1)
  1487. stop_axis(int): last dim to flatten (default = -1).
  1488. Returns:
  1489. None
  1490. Examples:
  1491. .. code-block:: python
  1492. >>> import paddle
  1493. >>> inp = paddle.ones([5, 2, 3, 4]).astype('float32')
  1494. >>> flatten = paddle.nn.Flatten(start_axis=1, stop_axis=2)
  1495. >>> y = flatten(inp)
  1496. >>> print(y.shape)
  1497. [5, 6, 4]
  1498. """
  1499. def __init__(self, start_axis=1, stop_axis=-1):
  1500. super().__init__()
  1501. self.start_axis = start_axis
  1502. self.stop_axis = stop_axis
  1503. def forward(self, input):
  1504. out = paddle.flatten(
  1505. input, start_axis=self.start_axis, stop_axis=self.stop_axis
  1506. )
  1507. return out
  1508. class Unflatten(Layer):
  1509. """
  1510. This interface is used to construct a callable object of the ``Unflatten`` class.
  1511. For more details, refer to code examples.
  1512. It a certain dimension of the input x Tensor into a desired shape.
  1513. Parameters:
  1514. axis (int): :attr:`axis` to be unflattened, specified as an index into `x.shape`.
  1515. shape (list|tuple|Tensor): Unflatten :attr:`shape` on the specified :attr:`axis`. At most one dimension of the target :attr:`shape` can be -1.
  1516. If the input :attr:`shape` does not contain -1 , the product of all elements in ``shape`` should be equal to ``x.shape[axis]``.
  1517. The data type is `int` . If :attr:`shape` is a list or tuple, the elements of it should be integers or Tensors with shape [].
  1518. If :attr:`shape` is an Tensor, it should be an 1-D Tensor.
  1519. name(str, optional): For details, please refer to :ref:`api_guide_Name`. Generally, no setting is required. Default: None.
  1520. Returns:
  1521. None
  1522. Examples:
  1523. .. code-block:: python
  1524. >>> import paddle
  1525. >>> x = paddle.randn(shape=[4, 6, 8])
  1526. >>> shape = [2, 3]
  1527. >>> axis = 1
  1528. >>> unflatten = paddle.nn.Unflatten(axis, shape)
  1529. >>> res = unflatten(x)
  1530. >>> print(res.shape)
  1531. [4, 2, 3, 8]
  1532. """
  1533. def __init__(self, axis, shape, name=None):
  1534. super().__init__()
  1535. self.axis = axis
  1536. self.shape = shape
  1537. self.name = name
  1538. def forward(self, input):
  1539. out = paddle.unflatten(
  1540. input, axis=self.axis, shape=self.shape, name=self.name
  1541. )
  1542. return out
  1543. def extra_repr(self):
  1544. name_str = f', name={self.name}' if self.name else ''
  1545. return f'axis={self.axis}, shape={self.shape}{name_str}'