You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

paddle_nn.py 57 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import paddle as pd
  4. import paddle.nn.functional as F
  5. import numpy as np
  6. import paddle.fluid as fluid
  7. from paddle.nn import initializer as I
  8. from paddle.fluid.layers.utils import map_structure, flatten, pack_sequence_as
  9. from paddle.fluid.data_feeder import convert_dtype
  10. from paddle.fluid.dygraph import Layer
  11. def padding_format(padding):
  12. """
  13. Checks that the padding format correspond format.
  14. Parameters
  15. ----------
  16. padding : str
  17. Must be one of the following:"same", "SAME", "VALID", "valid"
  18. Returns
  19. -------
  20. str "SAME" or "VALID"
  21. """
  22. if padding in ["SAME", "same"]:
  23. padding = "SAME"
  24. elif padding in ["VALID", "valid"]:
  25. padding = "VALID"
  26. elif padding == None:
  27. padding = None
  28. else:
  29. raise Exception("Unsupported padding: " + str(padding))
  30. return padding
  31. def preprocess_1d_format(data_format, padding):
  32. """
  33. Checks that the 1-D dataformat format correspond format.
  34. Parameters
  35. ----------
  36. data_format : str
  37. Must be one of the following:"channels_last","NWC","NCW","channels_first"
  38. padding : str
  39. Must be one of the following:"same","valid","SAME","VALID"
  40. Returns
  41. -------
  42. str "NWC" or "NCW" and "SAME" or "VALID"
  43. """
  44. if data_format in ["channels_last", "NWC", "NLC"]:
  45. data_format = "NLC"
  46. elif data_format in ["channels_first", "NCW", "NCL"]:
  47. data_format = "NCL"
  48. elif data_format == None:
  49. data_format = None
  50. else:
  51. raise Exception("Unsupported data format: " + str(data_format))
  52. padding = padding_format(padding)
  53. return data_format, padding
  54. def preprocess_2d_format(data_format, padding):
  55. """
  56. Checks that the 2-D dataformat format correspond format.
  57. Parameters
  58. ----------
  59. data_format : str
  60. Must be one of the following:"channels_last","NHWC","NCHW","channels_first"
  61. padding : str
  62. Must be one of the following:"same","valid","SAME","VALID"
  63. Returns
  64. -------
  65. str "NHWC" or "NCHW" and "SAME" or "VALID"
  66. """
  67. if data_format in ["channels_last", "NHWC", "nhwc"]:
  68. data_format = "NHWC"
  69. elif data_format in ["channels_first", "NCHW", "nchw"]:
  70. data_format = "NCHW"
  71. elif data_format == None:
  72. data_format = None
  73. else:
  74. raise Exception("Unsupported data format: " + str(data_format))
  75. padding = padding_format(padding)
  76. return data_format, padding
  77. def preprocess_3d_format(data_format, padding):
  78. """
  79. Checks that the 3-D dataformat format correspond format.
  80. Parameters
  81. ----------
  82. data_format : str
  83. Must be one of the following:"channels_last","NDHWC","NCDHW","channels_first"
  84. padding : str
  85. Must be one of the following:"same","valid","SAME","VALID"
  86. Returns
  87. -------
  88. str "NDHWC" or "NCDHW" and "SAME" or "VALID"
  89. """
  90. if data_format in ['channels_last', 'NDHWC']:
  91. data_format = 'NDHWC'
  92. elif data_format in ['channels_first', 'NCDHW']:
  93. data_format = 'NCDHW'
  94. elif data_format == None:
  95. data_format = None
  96. else:
  97. raise Exception("Unsupported data format: " + str(data_format))
  98. padding = padding_format(padding)
  99. return data_format, padding
  100. def nchw_to_nhwc(x):
  101. """
  102. Channels first to channels last
  103. Parameters
  104. ----------
  105. x : tensor
  106. channels first tensor data
  107. Returns
  108. -------
  109. channels last tensor data
  110. """
  111. if len(x.shape) == 3:
  112. x = pd.transpose(x, (0, 2, 1))
  113. elif len(x.shape) == 4:
  114. x = pd.transpose(x, (0, 2, 3, 1))
  115. elif len(x.shape) == 5:
  116. x = pd.transpose(x, (0, 2, 3, 4, 1))
  117. else:
  118. raise Exception("Unsupported dimensions")
  119. return x
  120. def nhwc_to_nchw(x):
  121. """
  122. Channles last to channels first
  123. Parameters
  124. ----------
  125. x : tensor
  126. channels last tensor data
  127. Returns
  128. -------
  129. channels first tensor data
  130. """
  131. if len(x.shape) == 3:
  132. x = pd.transpose(x, (0, 2, 1))
  133. elif len(x.shape) == 4:
  134. x = pd.transpose(x, (0, 3, 1, 2))
  135. elif len(x.shape) == 5:
  136. x = pd.transpose(x, (0, 4, 1, 2, 3))
  137. else:
  138. raise Exception("Unsupported dimensions")
  139. return x
  140. class ReLU(object):
  141. def __init__(self):
  142. pass
  143. def __call__(self, x):
  144. return F.relu(x)
  145. def relu(x):
  146. """
  147. Computes rectified linear: max(features, 0).
  148. Parameters
  149. ----------
  150. x : tensor
  151. Must be one of the following types: float32, float64, int32, uint8, int16,
  152. int8, int64, bfloat16, uint16, half, uint32, uint64, qint8.
  153. Returns
  154. -------
  155. A Tensor. Has the same type as features.
  156. """
  157. return F.relu(x)
  158. class ReLU6(object):
  159. def __init__(self):
  160. pass
  161. def __call__(self, x):
  162. return F.relu6(x)
  163. def relu6(x):
  164. """
  165. Computes Rectified Linear 6: min(max(features, 0), 6).
  166. Parameters
  167. ----------
  168. x : tensor
  169. Must be one of the following types: float32, float64, int32, uint8, int16,
  170. int8, int64, bfloat16, uint16, half, uint32, uint64, qint8.
  171. Returns
  172. -------
  173. A Tensor with the same type as features.
  174. """
  175. return F.relu6(x)
  176. class LeakyReLU(object):
  177. def __init__(self, alpha=0.2):
  178. self.alpha = alpha
  179. def __call__(self, x):
  180. return F.leaky_relu(x, negative_slope=self.alpha)
  181. def leaky_relu(x):
  182. """
  183. Compute the Leaky ReLU activation function.
  184. Parameters
  185. ----------
  186. x : tensor
  187. representing preactivation values. Must be one of the following types:
  188. float16, float32, float64, int32, int64.
  189. Returns
  190. -------
  191. The activation value.
  192. """
  193. return F.leaky_relu(x)
  194. class Softplus(object):
  195. def __init__(self):
  196. pass
  197. def __call__(self, x):
  198. return F.softplus(x)
  199. def softplus(x):
  200. """
  201. Computes softplus: log(exp(features) + 1).
  202. Parameters
  203. ----------
  204. x : tensor
  205. Must be one of the following types: half, bfloat16, float32, float64.
  206. Returns
  207. -------
  208. A Tensor. Has the same type as features.
  209. """
  210. return F.softplus(x)
  211. class Tanh(object):
  212. def __init__(self):
  213. pass
  214. def __call__(self, x):
  215. return F.tanh(x)
  216. def tanh(x):
  217. """
  218. Computes hyperbolic tangent of x element-wise.
  219. Parameters
  220. ----------
  221. x : tensor
  222. Must be one of the following types: bfloat16, half, float32, float64, complex64, complex128.
  223. Returns
  224. -------
  225. A Tensor. Has the same type as x.
  226. """
  227. return F.tanh(x)
  228. class Sigmoid(object):
  229. def __init__(self):
  230. pass
  231. def __call__(self, x):
  232. return F.sigmoid(x)
  233. def sigmoid(x):
  234. """
  235. Computes sigmoid of x element-wise.
  236. Parameters
  237. ----------
  238. x : tensor
  239. A Tensor with type float16, float32, float64, complex64, or complex128.
  240. Returns
  241. -------
  242. A Tensor with the same type as x.
  243. """
  244. return F.sigmoid(x)
  245. class Softmax(object):
  246. def __init__(self):
  247. pass
  248. def __call__(self, x):
  249. return F.softmax(x)
  250. def softmax(logits, axis=-1):
  251. """
  252. Computes softmax activations.
  253. Parameters
  254. ----------
  255. logits : tensor
  256. Must be one of the following types: half, float32, float64.
  257. axis : int
  258. The dimension softmax would be performed on. The default is -1 which indicates the last dimension.
  259. Returns
  260. -------
  261. A Tensor. Has the same type and shape as logits.
  262. """
  263. return F.softmax(logits, axis=axis)
  264. class Dropout(object):
  265. def __init__(self, keep, seed=1):
  266. self.keep = 1 - keep
  267. self.seed = seed
  268. def __call__(self, inputs):
  269. output = F.dropout(inputs, p=self.keep, mode='upscale_in_train')
  270. return output
  271. class BiasAdd(object):
  272. """
  273. Adds bias to value.
  274. Parameters
  275. ----------
  276. x : tensor
  277. A Tensor with type float, double, int64, int32, uint8, int16, int8, complex64, or complex128.
  278. bias : tensor
  279. Must be the same type as value unless value is a quantized type,
  280. in which case a different quantized type may be used.
  281. Returns
  282. -------
  283. A Tensor with the same type as value.
  284. """
  285. def __init__(self, data_format='channels_last'):
  286. super(BiasAdd, self).__init__()
  287. if data_format in ['channels_first', 'NCL', 'NCHW', 'NCDHW']:
  288. self.data_format = 'channels_first'
  289. elif data_format in ['channels_last', 'NLC', 'NHWC', 'NDHWC']:
  290. self.data_format = 'channels_last'
  291. else:
  292. raise ("Unsupported data format: " + str(data_format))
  293. def __call__(self, x, bias):
  294. if len(x.shape) > 2 and self.data_format == 'channels_first':
  295. x = nchw_to_nhwc(x)
  296. outputs = pd.add(x, bias)
  297. if len(x.shape) > 2 and self.data_format == 'channels_first':
  298. outputs = nhwc_to_nchw(outputs)
  299. return outputs
  300. def bias_add(x, bias):
  301. """
  302. Adds bias to value.
  303. Parameters
  304. ----------
  305. x : tensor
  306. A Tensor with type float, double, int64, int32, uint8, int16, int8, complex64, or complex128.
  307. bias : tensor
  308. Must be the same type as value unless value is a quantized type,
  309. in which case a different quantized type may be used.
  310. data_format : A string.
  311. 'N...C' and 'NC...' are supported.
  312. name : str
  313. A name for the operation (optional).
  314. Returns
  315. -------
  316. A Tensor with the same type as value.
  317. """
  318. #TODO the bias_add only supports channels_last
  319. outputs = pd.add(x, bias)
  320. return outputs
  321. class Conv1D(object):
  322. def __init__(self, stride, padding, data_format='NWC', dilations=None, out_channel=None, k_size=None):
  323. super(Conv1D, self).__init__()
  324. self.data_format, self.padding = preprocess_1d_format(padding=padding, data_format=data_format)
  325. self.stride = stride
  326. self.dilations = dilations
  327. def __call__(self, input, filters):
  328. output = F.conv1d(
  329. x=input, weight=filters, stride=self.stride, dilation=self.dilations, data_format=self.data_format,
  330. padding=self.padding
  331. )
  332. return output
  333. def conv1d(input, filters, stride, padding, data_format='NWC', dilations=None, name=None):
  334. """
  335. Computes a 1-D convolution given 3-D input and filter tensors.
  336. Parameters
  337. ----------
  338. input : tensor
  339. A 3D Tensor. Must be of type float16, float32, or float64
  340. filters : tensor
  341. A 3D Tensor. Must have the same type as input.
  342. stride : int of list
  343. An int or list of ints that has length 1 or 3. The number of entries by which the filter is moved right at each step.
  344. padding : string
  345. 'SAME' or 'VALID'
  346. data_format : string
  347. An optional string from "NWC", "NCW". Defaults to "NWC", the data is stored in the order of
  348. [batch, in_width, in_channels]. The "NCW" format stores data as [batch, in_channels, in_width].
  349. dilations : int or list
  350. An int or list of ints that has length 1 or 3 which defaults to 1.
  351. The dilation factor for each dimension of input. If set to k > 1,
  352. there will be k-1 skipped cells between each filter element on that dimension.
  353. Dilations in the batch and depth dimensions must be 1.
  354. name : string
  355. A name for the operation (optional).
  356. Returns
  357. -------
  358. A Tensor. Has the same type as input.
  359. """
  360. outputs = F.conv1d(
  361. x=input, weight=filters, stride=stride, padding=padding, data_format=data_format, dilation=dilations, name=name
  362. )
  363. return outputs
  364. class Conv2D(object):
  365. def __init__(self, strides, padding, data_format='NHWC', dilations=None, out_channel=None, k_size=None):
  366. self.data_format, self.padding = preprocess_2d_format(data_format, padding)
  367. if self.data_format is 'NHWC':
  368. self._stride = (strides[1], strides[2])
  369. self._dilation = (dilations[1], dilations[2])
  370. elif self.data_format is 'NCHW':
  371. self._stride = (strides[2], strides[3])
  372. self._dilation = (dilations[2], dilations[3])
  373. def __call__(self, inputs, filters):
  374. outputs = F.conv2d(
  375. x=inputs, weight=filters, stride=self._stride, dilation=self._dilation, padding=self.padding,
  376. data_format=self.data_format
  377. )
  378. return outputs
  379. def conv2d(input, filters, strides, padding, data_format='NCHW', dilations=None):
  380. """
  381. Computes a 2-D convolution given 4-D input and filters tensors.
  382. Parameters
  383. ----------
  384. input : tensor
  385. Must be one of the following types: half, bfloat16, float32, float64. A 4-D tensor.
  386. The dimension order is interpreted according to the value of data_format, see below for details.
  387. filters : tensor
  388. Must have the same type as input. A 4-D tensor of shape [filter_height, filter_width, in_channels, out_channels]
  389. strides : int of list
  390. The stride of the sliding window for each dimension of input. If a single value is given it is replicated in the H and W dimension.
  391. By default the N and C dimensions are set to 1. The dimension order is determined by the value of data_format, see below for details.
  392. padding : string
  393. "SAME" or "VALID"
  394. data_format : string
  395. "NHWC", "NCHW". Defaults to "NCHW".
  396. dilations : list or ints
  397. list of ints that has length 1, 2 or 4, defaults to 1. The dilation factor for each dimension ofinput.
  398. Returns
  399. -------
  400. A Tensor. Has the same type as input.
  401. """
  402. data_format, padding = preprocess_2d_format(data_format, padding)
  403. if data_format is 'NHWC':
  404. _stride = (strides[1], strides[2])
  405. _dilation = (dilations[1], dilations[2])
  406. elif data_format is 'NCHW':
  407. _stride = (strides[2], strides[3])
  408. _dilation = (dilations[2], dilations[3])
  409. outputs = F.conv2d(
  410. x=input, weight=filters, stride=_stride, dilation=_dilation, padding=padding, data_format=data_format
  411. )
  412. return outputs
  413. class Conv3D(object):
  414. def __init__(self, strides, padding, data_format='NDHWC', dilations=None, out_channel=None, k_size=None):
  415. self.data_format, self.padding = preprocess_3d_format(data_format, padding)
  416. if data_format is 'NDHWC':
  417. self._strides = (strides[1], strides[2], strides[3])
  418. self._dilations = (dilations[1], dilations[2], dilations[3])
  419. elif data_format is 'NCDHW':
  420. self._strides = (strides[2], strides[3], strides[4])
  421. self._dilations = (dilations[2], dilations[3], dilations[4])
  422. def __call__(self, input, filters):
  423. outputs = F.conv3d(
  424. x=input, weight=filters, stride=self._strides, dilation=self._dilations, data_format=self.data_format,
  425. padding=self.padding
  426. )
  427. return outputs
  428. def conv3d(input, filters, strides, padding, data_format='NDHWC', dilations=None, name=None):
  429. """
  430. Computes a 3-D convolution given 5-D input and filters tensors.
  431. Parameters
  432. ----------
  433. input : tensor
  434. Must be one of the following types: half, bfloat16, float32, float64.
  435. Shape [batch, in_depth, in_height, in_width, in_channels].
  436. filters : tensor
  437. Must have the same type as input. Shape [filter_depth, filter_height, filter_width, in_channels, out_channels].
  438. in_channels must match between input and filters.
  439. strides : tuple of ints
  440. A list of ints that has length >= 5. 1-D tensor of length 5.
  441. The stride of the sliding window for each dimension of input.
  442. Must have strides[0] = strides[4] = 1.
  443. padding : string
  444. A string from: "SAME", "VALID". The type of padding algorithm to use.
  445. data_format : string
  446. An optional string from: "NDHWC", "NCDHW". Defaults to "NDHWC". The data format of the input and output data.
  447. With the default format "NDHWC", the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels].
  448. Alternatively, the format could be "NCDHW", the data storage order is: [batch, in_channels, in_depth, in_height, in_width].
  449. dilations : touple of ints
  450. Defaults to [1, 1, 1, 1, 1]. 1-D tensor of length 5. The dilation factor for each dimension of input.
  451. If set to k > 1, there will be k-1 skipped cells between each filter element on that dimension.
  452. The dimension order is determined by the value of data_format, see above for details.
  453. Dilations in the batch and depth dimensions must be 1.
  454. name : string
  455. A name for the operation (optional).
  456. Returns
  457. -------
  458. A Tensor. Has the same type as input.
  459. """
  460. data_format, padding = preprocess_3d_format(data_format, padding)
  461. if data_format is 'NDHWC':
  462. _strides = (strides[1], strides[2], strides[3])
  463. _dilations = (dilations[1], dilations[2], dilations[3])
  464. elif data_format is 'NCDHW':
  465. _strides = (strides[2], strides[3], strides[4])
  466. _dilations = (dilations[2], dilations[3], dilations[4])
  467. outputs = F.conv3d(
  468. x=input, weight=filters, stride=_strides, dilation=_dilations, data_format=data_format, padding=padding,
  469. name=name
  470. )
  471. return outputs
  472. def lrn(inputs, depth_radius, bias, alpha, beta):
  473. """
  474. Local Response Normalization.
  475. Parameters
  476. ----------
  477. inputs : tensor
  478. Must be one of the following types: half, bfloat16, float32. 4-D.
  479. depth_radius : int
  480. Defaults to 5. 0-D. Half-width of the 1-D normalization window.
  481. bias : float
  482. Defaults to 1. An offset (usually positive to avoid dividing by 0).
  483. alpha : float
  484. Defaults to 1. A scale factor, usually positive.
  485. beta : float
  486. Defaults to 0.5. An exponent.
  487. Returns
  488. -------
  489. A Tensor. Has the same type as input.
  490. """
  491. pass
  492. def moments(x, axes, shift=None, keepdims=False):
  493. """
  494. Calculates the mean and variance of x.
  495. Parameters
  496. ----------
  497. x : tensor
  498. A Tensor
  499. axes : ints
  500. Axes along which to compute mean and variance.
  501. shift : int
  502. Not used in the current implementation.
  503. keepdims : bool
  504. produce moments with the same dimensionality as the input.
  505. Returns
  506. -------
  507. Two Tensor objects: mean and variance.
  508. """
  509. pass
  510. class MaxPool1d(object):
  511. def __init__(self, ksize, strides, padding, data_format=None):
  512. self.data_format, self.padding = preprocess_1d_format(data_format=data_format, padding=padding)
  513. self.ksize = ksize
  514. self.strides = strides
  515. def __call__(self, inputs):
  516. if self.data_format == 'NLC':
  517. inputs = nhwc_to_nchw(inputs)
  518. outputs = F.max_pool1d(inputs, self.ksize, self.strides, self.padding)
  519. if self.data_format == 'NLC':
  520. outputs = nchw_to_nhwc(outputs)
  521. return outputs
  522. class MaxPool(object):
  523. def __init__(self, ksize, strides, padding, data_format=None):
  524. self.data_format, self.padding = preprocess_2d_format(data_format, padding)
  525. self.ksize = ksize
  526. if self.data_format is 'NHWC':
  527. self._stride = (strides[1], strides[2])
  528. elif self.data_format is 'NCHW':
  529. self._stride = (strides[2], strides[3])
  530. def __call__(self, inputs):
  531. outputs = F.max_pool2d(
  532. x=inputs, kernel_size=self.ksize, stride=self._stride, padding=self.padding, data_format=self.data_format
  533. )
  534. return outputs
  535. def max_pool(input, ksize, strides, padding, data_format=None):
  536. """
  537. Performs the max pooling on the input.
  538. Parameters
  539. ----------
  540. input : tensor
  541. Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels] if data_format does not start
  542. with "NC" (default), or [batch_size, num_channels] + input_spatial_shape if data_format starts with "NC".
  543. Pooling happens over the spatial dimensions only.
  544. ksize : int or list of ints
  545. An int or list of ints that has length 1, N or N+2.
  546. The size of the window for each dimension of the input tensor.
  547. strides : int or list of ints
  548. An int or list of ints that has length 1, N or N+2.
  549. The stride of the sliding window for each dimension of the input tensor.
  550. padding : string
  551. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  552. Returns
  553. -------
  554. A Tensor of format specified by data_format. The max pooled output tensor.
  555. """
  556. pass
  557. class AvgPool1d(object):
  558. def __init__(self, ksize, strides, padding, data_format=None):
  559. self.data_format, self.padding = preprocess_1d_format(data_format=data_format, padding=padding)
  560. self.ksize = ksize
  561. self.strides = strides
  562. def __call__(self, inputs):
  563. if self.data_format == 'NLC':
  564. inputs = nhwc_to_nchw(inputs)
  565. outputs = F.avg_pool1d(inputs, self.ksize, self.strides, self.padding)
  566. if self.data_format == 'NLC':
  567. outputs = nchw_to_nhwc(outputs)
  568. return outputs
  569. class AvgPool(object):
  570. def __init__(self, ksize, strides, padding, data_format=None):
  571. self.data_format, self.padding = preprocess_2d_format(data_format, padding)
  572. self.filter_size = ksize
  573. if self.data_format is 'NHWC':
  574. self._stride = (strides[1], strides[2])
  575. elif self.data_format is 'NCHW':
  576. self._stride = (strides[2], strides[3])
  577. def __call__(self, inputs):
  578. outputs = F.avg_pool2d(
  579. inputs, kernel_size=self.filter_size, stride=self._stride, padding=self.padding,
  580. data_format=self.data_format
  581. )
  582. return outputs
  583. def avg_pool(input, ksize, strides, padding):
  584. """
  585. Performs the avg pooling on the input.
  586. Parameters
  587. ----------
  588. input : tensor
  589. Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels]
  590. if data_format does not start with "NC" (default), or [batch_size, num_channels] + input_spatial_shape
  591. if data_format starts with "NC". Pooling happens over the spatial dimensions only.
  592. ksize : int or list of ints
  593. An int or list of ints that has length 1, N or N+2.
  594. The size of the window for each dimension of the input tensor.
  595. strides : int or list of ints
  596. An int or list of ints that has length 1, N or N+2.
  597. The stride of the sliding window for each dimension of the input tensor.
  598. padding : string
  599. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  600. Returns
  601. -------
  602. A Tensor of format specified by data_format. The average pooled output tensor.
  603. """
  604. pass
  605. class MaxPool3d(object):
  606. def __init__(self, ksize, strides, padding, data_format=None):
  607. self.data_format, self.padding = preprocess_3d_format(data_format, padding)
  608. self.ksize = ksize
  609. if self.data_format == 'NCDHW':
  610. self.strides = (strides[2], strides[3], strides[4])
  611. if self.data_format == 'NDHWC':
  612. self.strides = (strides[1], strides[2], strides[3])
  613. def __call__(self, inputs):
  614. outputs = F.max_pool3d(
  615. inputs, kernel_size=self.ksize, stride=self.strides, padding=self.padding, data_format=self.data_format
  616. )
  617. return outputs
  618. def max_pool3d(input, ksize, strides, padding, data_format=None, name=None):
  619. """
  620. Performs the max pooling on the input.
  621. Parameters
  622. ----------
  623. input : tensor
  624. A 5-D Tensor of the format specified by data_format.
  625. ksize : int or list of ints
  626. An int or list of ints that has length 1, 3 or 5.
  627. The size of the window for each dimension of the input tensor.
  628. strides : int or list of ints
  629. An int or list of ints that has length 1, 3 or 5.
  630. The stride of the sliding window for each dimension of the input tensor.
  631. padding : string
  632. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  633. data_format : string
  634. "NDHWC", "NCDHW". Defaults to "NDHWC". The data format of the input and output data.
  635. With the default format "NDHWC", the data is stored in the order of: [batch, in_depth, in_height, in_width, in_channels].
  636. Alternatively, the format could be "NCDHW", the data storage order is: [batch, in_channels, in_depth, in_height, in_width].
  637. name : string
  638. A name for the operation (optional).
  639. Returns
  640. -------
  641. A Tensor of format specified by data_format. The max pooled output tensor.
  642. """
  643. pass
  644. class AvgPool3d(object):
  645. def __init__(self, ksize, strides, padding, data_format=None):
  646. self.data_format, self.padding = preprocess_3d_format(data_format, padding)
  647. self.ksize = ksize
  648. if self.data_format == 'NCDHW':
  649. self.strides = (strides[2], strides[3], strides[4])
  650. if self.data_format == 'NDHWC':
  651. self.strides = (strides[1], strides[2], strides[3])
  652. def __call__(self, inputs):
  653. outputs = F.avg_pool3d(
  654. inputs, kernel_size=self.ksize, stride=self.strides, padding=self.padding, data_format=self.data_format
  655. )
  656. return outputs
  657. def avg_pool3d(input, ksize, strides, padding, data_format=None, name=None):
  658. """
  659. Performs the average pooling on the input.
  660. Parameters
  661. ----------
  662. input : tensor
  663. A 5-D Tensor of shape [batch, height, width, channels] and type float32, float64, qint8, quint8, or qint32.
  664. ksize : int or list of ints
  665. An int or list of ints that has length 1, 3 or 5. The size of the window for each dimension of the input tensor.
  666. strides : int or list of ints
  667. An int or list of ints that has length 1, 3 or 5.
  668. The stride of the sliding window for each dimension of the input tensor.
  669. padding : string
  670. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  671. data_format : string
  672. 'NDHWC' and 'NCDHW' are supported.
  673. name : string
  674. Optional name for the operation.
  675. Returns
  676. -------
  677. A Tensor with the same type as value. The average pooled output tensor.
  678. """
  679. pass
  680. def pool(input, window_shape, pooling_type, strides=None, padding='VALID', data_format=None, dilations=None, name=None):
  681. """
  682. Performs an N-D pooling operation.
  683. Parameters
  684. ----------
  685. input : tensor
  686. Tensor of rank N+2, of shape [batch_size] + input_spatial_shape + [num_channels]
  687. if data_format does not start with "NC" (default), or [batch_size, num_channels] + input_spatial_shape
  688. if data_format starts with "NC". Pooling happens over the spatial dimensions only.
  689. window_shape : int
  690. Sequence of N ints >= 1.
  691. pooling_type : string
  692. Specifies pooling operation, must be "AVG" or "MAX".
  693. strides : ints
  694. Sequence of N ints >= 1. Defaults to [1]*N. If any value of strides is > 1, then all values of dilation_rate must be 1.
  695. padding : string
  696. The padding algorithm, must be "SAME" or "VALID". Defaults to "SAME".
  697. See the "returns" section of tf.ops.convolution for details.
  698. data_format : string
  699. Specifies whether the channel dimension of the input and output is the last dimension (default, or if data_format does not start with "NC"),
  700. or the second dimension (if data_format starts with "NC").
  701. For N=1, the valid values are "NWC" (default) and "NCW". For N=2, the valid values are "NHWC" (default) and "NCHW".
  702. For N=3, the valid values are "NDHWC" (default) and "NCDHW".
  703. dilations : list of ints
  704. Dilation rate. List of N ints >= 1. Defaults to [1]*N. If any value of dilation_rate is > 1, then all values of strides must be 1.
  705. name : string
  706. Optional. Name of the op.
  707. Returns
  708. -------
  709. Tensor of rank N+2, of shape [batch_size] + output_spatial_shape + [num_channels]
  710. """
  711. pass
  712. class DepthwiseConv2d(object):
  713. def __init__(self, strides, padding, data_format=None, dilations=None, ksize=None, channel_multiplier=1):
  714. self.data_format, self.padding = preprocess_2d_format(data_format, padding)
  715. self.stride = strides
  716. self.dilations = dilations
  717. def __call__(self, input, filter):
  718. raise NotImplementedError("Not implemented depthwiseconv2d")
  719. def depthwise_conv2d(input, filter, strides, padding, data_format=None, dilations=None, name=None):
  720. """
  721. Depthwise 2-D convolution.
  722. Parameters
  723. ----------
  724. input : tensor
  725. 4-D with shape according to data_format.
  726. filter : tensor
  727. 4-D with shape [filter_height, filter_width, in_channels, channel_multiplier].
  728. strides : list
  729. 1-D of size 4. The stride of the sliding window for each dimension of input.
  730. padding : string
  731. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  732. data_format : string
  733. The data format for input. Either "NHWC" (default) or "NCHW".
  734. dilations : list
  735. 1-D of size 2. The dilation rate in which we sample input values across the height and width dimensions in atrous convolution.
  736. If it is greater than 1, then all values of strides must be 1.
  737. name : string
  738. A name for this operation (optional).
  739. Returns
  740. -------
  741. A 4-D Tensor with shape according to data_format.
  742. E.g., for "NHWC" format, shape is [batch, out_height, out_width, in_channels * channel_multiplier].
  743. """
  744. pass
  745. class Conv1d_transpose(object):
  746. def __init__(
  747. self, stride, padding, data_format='NWC', dilations=None, out_channel=None, k_size=None, in_channels=None
  748. ):
  749. self.stride = stride
  750. self.dilations = dilations
  751. self.data_format, self.padding = preprocess_1d_format(data_format, padding)
  752. def __call__(self, input, filters):
  753. out = F.conv1d_transpose(
  754. x=input,
  755. weight=filters,
  756. padding=self.padding,
  757. stride=self.stride,
  758. dilation=self.dilations,
  759. data_format=self.data_format,
  760. )
  761. return out
  762. def conv1d_transpose(
  763. input, filters, output_shape, stride, padding='SAME', data_format='NWC', dilations=None, name=None
  764. ):
  765. """
  766. The transpose of conv1d.
  767. Parameters
  768. ----------
  769. input : tensor
  770. A 3-D Tensor of type float and shape [batch, in_width, in_channels]
  771. for NWC data format or [batch, in_channels, in_width] for NCW data format.
  772. filters : tensor
  773. A 3-D Tensor with the same type as value and shape [filter_width, output_channels, in_channels].
  774. filter's in_channels dimension must match that of value.
  775. output_shape : tensor
  776. A 1-D Tensor, containing three elements, representing the output shape of the deconvolution op.
  777. strides : list
  778. An int or list of ints that has length 1 or 3. The number of entries by which the filter is moved right at each step.
  779. padding : string
  780. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  781. data_format : string
  782. 'NWC' and 'NCW' are supported.
  783. dilations : list
  784. An int or list of ints that has length 1 or 3 which defaults to 1.
  785. The dilation factor for each dimension of input. If set to k > 1,
  786. there will be k-1 skipped cells between each filter element on that dimension.
  787. Dilations in the batch and depth dimensions must be 1.
  788. name : string
  789. Optional name for the returned tensor.
  790. Returns
  791. -------
  792. A Tensor with the same type as value.
  793. """
  794. data_format, padding = preprocess_1d_format(data_format, padding)
  795. output = F.conv1d_transpose(
  796. x=input,
  797. weight=filters,
  798. stride=stride,
  799. padding=padding,
  800. dilation=dilations,
  801. data_format=data_format,
  802. output_size=output_shape,
  803. )
  804. return output
  805. class Conv2d_transpose(object):
  806. def __init__(
  807. self, strides, padding, data_format='NHWC', dilations=None, name=None, out_channel=None, k_size=None,
  808. in_channels=None
  809. ):
  810. self.strides = strides
  811. self.dilations = dilations
  812. self.data_format, self.padding = preprocess_2d_format(data_format, padding)
  813. def __call__(self, input, filters):
  814. output = F.conv2d_transpose(
  815. x=input, weight=filters, stride=self.strides, padding=self.padding, dilation=self.dilations,
  816. data_format=self.data_format
  817. )
  818. return output
  819. def conv2d_transpose(
  820. input, filters, output_shape, strides, padding='SAME', data_format='NHWC', dilations=None, name=None
  821. ):
  822. """
  823. The transpose of conv2d.
  824. Parameters
  825. ----------
  826. input : tensor
  827. A 4-D Tensor of type float and shape [batch, height, width, in_channels]
  828. for NHWC data format or [batch, in_channels, height, width] for NCHW data format.
  829. filters : tensor
  830. A 4-D Tensor with the same type as input and shape [height, width,
  831. output_channels, in_channels]. filter's in_channels dimension must match that of input.
  832. output_shape : tensor
  833. A 1-D Tensor representing the output shape of the deconvolution op.
  834. strides : list
  835. An int or list of ints that has length 1, 2 or 4. The stride of the sliding window for each dimension of input.
  836. If a single value is given it is replicated in the H and W dimension.
  837. By default the N and C dimensions are set to 0.
  838. The dimension order is determined by the value of data_format, see below for details.
  839. padding : string
  840. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  841. data_format : string
  842. 'NHWC' and 'NCHW' are supported.
  843. dilations : list
  844. An int or list of ints that has length 1, 2 or 4, defaults to 1.
  845. name : string
  846. Optional name for the returned tensor.
  847. Returns
  848. -------
  849. A Tensor with the same type as input.
  850. """
  851. data_format, padding = preprocess_2d_format(data_format, padding)
  852. output = F.conv2d_transpose(
  853. x=input,
  854. weight=filters,
  855. output_size=output_shape,
  856. stride=strides,
  857. padding=padding,
  858. dilation=dilations,
  859. data_format=data_format,
  860. )
  861. return output
  862. class Conv3d_transpose(object):
  863. def __init__(
  864. self, strides, padding, data_format='NDHWC', dilations=None, name=None, out_channel=None, k_size=None,
  865. in_channels=None
  866. ):
  867. self.strides = strides
  868. self.dilations = dilations
  869. self.data_format, self.padding = preprocess_3d_format(data_format, padding)
  870. def __call__(self, input, filters):
  871. output = F.conv3d_transpose(
  872. x=input, weight=filters, stride=self.strides, padding=self.padding, dilation=self.dilations,
  873. data_format=self.data_format
  874. )
  875. def conv3d_transpose(
  876. input, filters, output_shape, strides, padding='SAME', data_format='NDHWC', dilations=None, name=None
  877. ):
  878. """
  879. The transpose of conv3d.
  880. Parameters
  881. ----------
  882. input : tensor
  883. A 5-D Tensor of type float and shape [batch, height, width, in_channels] for
  884. NHWC data format or [batch, in_channels, height, width] for NCHW data format.
  885. filters : tensor
  886. A 5-D Tensor with the same type as value and shape [height, width, output_channels, in_channels].
  887. filter's in_channels dimension must match that of value.
  888. output_shape : tensor
  889. A 1-D Tensor representing the output shape of the deconvolution op.
  890. strides : list
  891. An int or list of ints that has length 1, 3 or 5.
  892. padding : string
  893. 'VALID' or 'SAME'. The padding algorithm. See the "returns" section of tf.ops.convolution for details.
  894. data_format : string
  895. 'NDHWC' and 'NCDHW' are supported.
  896. dilations : list of ints
  897. An int or list of ints that has length 1, 3 or 5, defaults to 1.
  898. name : string
  899. Optional name for the returned tensor.
  900. Returns
  901. -------
  902. A Tensor with the same type as value.
  903. """
  904. data_format, padding = preprocess_3d_format(data_format, padding)
  905. output = F.conv3d_transpose(
  906. x=input,
  907. weight=filters,
  908. output_size=output_shape,
  909. stride=strides,
  910. padding=padding,
  911. dilation=dilations,
  912. data_format=data_format,
  913. )
  914. return output
  915. class BatchNorm(object):
  916. def __init__(
  917. self, decay=0.9, epsilon=0.00001, beta=None, gamma=None, moving_mean=None, moving_var=None, num_features=None,
  918. data_format='channels_last', is_train=False
  919. ):
  920. self.decay = decay
  921. self.epsilon = epsilon
  922. self.data_format = data_format
  923. self.beta = beta
  924. self.gamma = gamma
  925. self.moving_mean = moving_mean
  926. self.moving_var = moving_var
  927. self.num_features = num_features
  928. self.is_train = is_train
  929. self.axes = None
  930. def __call__(self, inputs):
  931. data_format = self.channel_format(inputs)
  932. outputs = pd.nn.functional.batch_norm(
  933. inputs, self.moving_mean, self.moving_var, weight=self.gamma, bias=self.beta, training=self.is_train,
  934. momentum=self.decay, epsilon=self.epsilon, data_format=data_format
  935. )
  936. return outputs
  937. def channel_format(self, inputs):
  938. """ return "NC", "NCL", "NCHW", "NCDHW", "NLC", "NHWC" or "NDHWC". """
  939. len_in_shape = len(inputs.shape)
  940. if len_in_shape == 2:
  941. return 'NC'
  942. if self.data_format == 'channels_last':
  943. if len_in_shape == 3:
  944. return 'NLC'
  945. if len_in_shape == 4:
  946. return 'NHWC'
  947. if len_in_shape == 5:
  948. return 'NDHWC'
  949. if self.data_format == 'channels_first':
  950. if len_in_shape == 3:
  951. return 'NCL'
  952. if len_in_shape == 4:
  953. return 'NCHW'
  954. if len_in_shape == 5:
  955. return 'NCDHW'
  956. class GroupConv2D(object):
  957. def __init__(self, strides, padding, data_format, dilations, out_channel, k_size, groups):
  958. pass
  959. def __call__(self, input, filters):
  960. raise NotImplementedError
  961. class SeparableConv1D(object):
  962. def __init__(self, stride, padding, data_format, dilations, out_channel, k_size, in_channel, depth_multiplier):
  963. pass
  964. def __call__(self, inputs, depthwise_filters, pointwise_filters):
  965. raise NotImplementedError
  966. class SeparableConv2D(object):
  967. def __init__(self, strides, padding, data_format, dilations, out_channel, k_size, in_channel, depth_multiplier):
  968. pass
  969. def __call__(self, inputs, depthwise_filters, pointwise_filters):
  970. raise NotImplementedError
  971. class AdaptiveMeanPool1D(object):
  972. def __init__(self, output_size, data_format):
  973. self.data_format, _ = preprocess_1d_format(data_format, None)
  974. self.output_size = output_size
  975. def __call__(self, input):
  976. if self.data_format == 'NLC':
  977. input = nhwc_to_nchw(input)
  978. output = F.adaptive_avg_pool1d(input, self.output_size)
  979. if self.data_format == 'NLC':
  980. output = nchw_to_nhwc(output)
  981. return output
  982. class AdaptiveMeanPool2D(object):
  983. def __init__(self, output_size, data_format):
  984. self.data_format, _ = preprocess_2d_format(data_format, None)
  985. self.output_size = output_size
  986. def __call__(self, inputs):
  987. return F.adaptive_avg_pool2d(inputs, output_size=self.output_size, data_format=self.data_format)
  988. class AdaptiveMeanPool3D(object):
  989. def __init__(self, output_size, data_format):
  990. self.data_format, _ = preprocess_3d_format(data_format, None)
  991. self.output_size = output_size
  992. def __call__(self, inputs):
  993. return F.adaptive_avg_pool3d(inputs, output_size=self.output_size, data_format=self.data_format)
  994. class AdaptiveMaxPool1D(object):
  995. def __init__(self, output_size, data_format):
  996. self.data_format, _ = preprocess_1d_format(data_format, None)
  997. self.output_size = output_size
  998. def __call__(self, input):
  999. if self.data_format == 'NLC':
  1000. input = nhwc_to_nchw(input)
  1001. output = F.adaptive_max_pool1d(input, self.output_size)
  1002. if self.data_format == 'NLC':
  1003. output = nchw_to_nhwc(output)
  1004. return output
  1005. class AdaptiveMaxPool2D(object):
  1006. def __init__(self, output_size, data_format):
  1007. self.data_format, _ = preprocess_2d_format(data_format, None)
  1008. self.output_size = output_size
  1009. def __call__(self, inputs):
  1010. if self.data_format == 'NHWC':
  1011. inputs = nhwc_to_nchw(inputs)
  1012. output = F.adaptive_max_pool2d(inputs, self.output_size)
  1013. if self.data_format == 'NHWC':
  1014. output = nchw_to_nhwc(output)
  1015. return output
  1016. class AdaptiveMaxPool3D(object):
  1017. def __init__(self, output_size, data_format):
  1018. self.data_format, _ = preprocess_3d_format(data_format, None)
  1019. self.output_size = output_size
  1020. def __call__(self, inputs):
  1021. if self.data_format == 'NDHWC':
  1022. inputs = nhwc_to_nchw(inputs)
  1023. output = F.adaptive_max_pool3d(inputs, self.output_size)
  1024. if self.data_format == 'NDHWC':
  1025. output = nchw_to_nhwc(output)
  1026. return output
  1027. class BinaryConv2D(object):
  1028. def __init__(self, strides, padding, data_format, dilations, out_channel, k_size, in_channel):
  1029. pass
  1030. def __call__(self, inputs, filters):
  1031. raise NotImplementedError
  1032. class DorefaConv2D(object):
  1033. def __init__(self, bitW, bitA, strides, padding, data_format, dilations, out_channel, k_size, in_channel):
  1034. pass
  1035. def __call__(self, inputs, filters):
  1036. raise NotImplementedError
  1037. class rnncell(object):
  1038. def __init__(self, weight_ih, weight_hh, bias_ih, bias_hh, bias, act):
  1039. self.weight_ih = weight_ih
  1040. self.weight_hh = weight_hh
  1041. self.bias_ih = bias_ih
  1042. self.bias_hh = bias_hh
  1043. self.bias = bias
  1044. self.act_fn = F.relu if act == 'relu' else F.tanh
  1045. def __call__(self, input, h):
  1046. i2h = pd.matmul(input, self.weight_ih, transpose_y=True)
  1047. if self.bias_ih is not None:
  1048. i2h += self.bias_ih
  1049. h2h = pd.matmul(h, self.weight_hh, transpose_y=True)
  1050. if self.bias_hh is not None:
  1051. h2h += self.bias_hh
  1052. h = self.act_fn(i2h + h2h)
  1053. return h, h
  1054. class lstmcell(object):
  1055. def __init__(self, weight_ih, weight_hh, bias_ih, bias_hh, bias):
  1056. self.weight_ih = weight_ih
  1057. self.weight_hh = weight_hh
  1058. self.bias_ih = bias_ih
  1059. self.bias_hh = bias_hh
  1060. self.bias = bias
  1061. self.gate_act_fn = F.sigmoid
  1062. self.act_fn = F.tanh
  1063. def __call__(self, inputs, h, c):
  1064. gates = pd.matmul(inputs, self.weight_ih, transpose_y=True)
  1065. if self.bias_ih is not None:
  1066. gates += self.bias_ih
  1067. gates += pd.matmul(h, self.weight_hh, transpose_y=True)
  1068. if self.bias_hh is not None:
  1069. gates += self.bias_hh
  1070. gates_slices = pd.split(gates, num_or_sections=4, axis=-1)
  1071. i = self.gate_act_fn(gates_slices[0])
  1072. f = self.gate_act_fn(gates_slices[1])
  1073. o = self.gate_act_fn(gates_slices[3])
  1074. c = f * c + i * self.act_fn(gates_slices[2])
  1075. h = o * self.act_fn(c)
  1076. return h, h, c
  1077. class grucell(object):
  1078. def __init__(self, weight_ih, weight_hh, bias_ih, bias_hh, bias):
  1079. self.weight_ih = weight_ih
  1080. self.weight_hh = weight_hh
  1081. self.bias_ih = bias_ih
  1082. self.bias_hh = bias_hh
  1083. self.bias = bias
  1084. self.gate_act_fn = F.sigmoid
  1085. self.act_fn = F.tanh
  1086. def __call__(self, input, h):
  1087. x_gates = pd.matmul(input, self.weight_ih, transpose_y=True)
  1088. if self.bias_ih is not None:
  1089. x_gates = x_gates + self.bias_ih
  1090. h_gates = pd.matmul(h, self.weight_hh, transpose_y=True)
  1091. if self.bias_hh is not None:
  1092. h_gates = h_gates + self.bias_hh
  1093. x_r, x_z, x_c = pd.split(x_gates, num_or_sections=3, axis=-1)
  1094. h_r, h_z, h_c = pd.split(h_gates, num_or_sections=3, axis=-1)
  1095. r = self.gate_act_fn(x_r + h_r)
  1096. z = self.gate_act_fn(x_z + h_z)
  1097. c = self.act_fn(x_c + r * h_c) # apply reset gate after mm
  1098. h = (h - c) * z + c
  1099. return h, h
  1100. def split_states(states, bidirectional=False, state_components=1):
  1101. r"""
  1102. Split states of RNN network into possibly nested list or tuple of
  1103. states of each RNN cells of the RNN network.
  1104. Parameters:
  1105. states (Tensor|tuple|list): the concatenated states for RNN network.
  1106. When `state_components` is 1, states in a Tensor with shape
  1107. `(L*D, N, C)` where `L` is the number of layers of the RNN
  1108. network, `D` is the number of directions of the RNN network(1
  1109. for unidirectional RNNs and 2 for bidirectional RNNs), `N` is
  1110. the batch size of the input to the RNN network, `C` is the
  1111. hidden size of the RNN network.
  1112. When `state_components` is larger than 1, `states` is a tuple of
  1113. `state_components` Tensors that meet the requirements described
  1114. above.
  1115. For SimpleRNNs and GRUs, `state_components` is 1, and for LSTMs,
  1116. `state_components` is 2.
  1117. bidirectional (bool): whether the state is of a bidirectional RNN
  1118. network. Defaults to False.
  1119. state_components (int): the number of the components of the states. see
  1120. `states` above. Defaults to 1.
  1121. Returns:
  1122. A nested list or tuple of RNN cell states.
  1123. If `bidirectional` is True, it can be indexed twice to get an RNN
  1124. cell state. The first index indicates the layer, the second index
  1125. indicates the direction.
  1126. If `bidirectional` is False, it can be indexed once to get an RNN
  1127. cell state. The index indicates the layer.
  1128. Note that if `state_components` is larger than 1, an RNN cell state
  1129. can be indexed one more time to get a tensor of shape(N, C), where
  1130. `N` is the batch size of the input to the RNN cell, and `C` is the
  1131. hidden size of the RNN cell.
  1132. """
  1133. if state_components == 1:
  1134. states = pd.unstack(states)
  1135. if not bidirectional:
  1136. return states
  1137. else:
  1138. return list(zip(states[::2], states[1::2]))
  1139. else:
  1140. assert len(states) == state_components
  1141. states = tuple([pd.unstack(item) for item in states])
  1142. if not bidirectional:
  1143. return list(zip(*states))
  1144. else:
  1145. states = list(zip(*states))
  1146. return list(zip(states[::2], states[1::2]))
  1147. def concat_states(states, bidirectional=False, state_components=1):
  1148. r"""
  1149. Concatenate a possibly nested list or tuple of RNN cell states into a
  1150. compact form.
  1151. Parameters:
  1152. states (list|tuple): a possibly nested list or tuple of RNN cell
  1153. states.
  1154. If `bidirectional` is True, it can be indexed twice to get an
  1155. RNN cell state. The first index indicates the layer, the second
  1156. index indicates the direction.
  1157. If `bidirectional` is False, it can be indexed once to get an RNN
  1158. cell state. The index indicates the layer.
  1159. Note that if `state_components` is larger than 1, an RNN cell
  1160. state can be indexed one more time to get a tensor of shape(N, C),
  1161. where `N` is the batch size of the input to the RNN cell, and
  1162. `C` is the hidden size of the RNN cell.
  1163. bidirectional (bool): whether the state is of a bidirectional RNN
  1164. network. Defaults to False.
  1165. state_components (int): the number of the components of the states. see
  1166. `states` above. Defaults to 1.
  1167. Returns:
  1168. Concatenated states for RNN network.
  1169. When `state_components` is 1, states in a Tensor with shape
  1170. `(L\*D, N, C)` where `L` is the number of layers of the RNN
  1171. network, `D` is the number of directions of the RNN network(1 for
  1172. unidirectional RNNs and 2 for bidirectional RNNs), `N` is the batch
  1173. size of the input to the RNN network, `C` is the hidden size of the
  1174. RNN network.
  1175. """
  1176. if state_components == 1:
  1177. return pd.stack(flatten(states))
  1178. else:
  1179. states = flatten(states)
  1180. componnets = []
  1181. for i in range(state_components):
  1182. componnets.append(states[i::state_components])
  1183. return tuple([pd.stack(item) for item in componnets])
  1184. class rnnbase(Layer):
  1185. def __init__(
  1186. self,
  1187. mode,
  1188. input_size,
  1189. hidden_size,
  1190. num_layers,
  1191. bias,
  1192. batch_first,
  1193. dropout,
  1194. bidirectional,
  1195. is_train,
  1196. ):
  1197. super(rnnbase, self).__init__()
  1198. self.mode = mode
  1199. self.input_size = input_size
  1200. self.hidden_size = hidden_size
  1201. self.num_layers = num_layers
  1202. self.time_major = False if batch_first else True
  1203. self.dropout = dropout
  1204. self.bidirect = 2 if bidirectional else 1
  1205. self.state_components = 2 if mode == 'LSTM' else 1
  1206. self.rnn = pd.nn.LayerList()
  1207. RNN = pd.nn.RNN
  1208. BiRNN = pd.nn.BiRNN
  1209. weight_ih_attr = None
  1210. weight_hh_attr = None
  1211. if bias:
  1212. bias_ih_attr = None
  1213. bias_hh_attr = None
  1214. else:
  1215. bias_ih_attr = False
  1216. bias_hh_attr = False
  1217. kwargs = {
  1218. "weight_ih_attr": weight_ih_attr,
  1219. "weight_hh_attr": weight_hh_attr,
  1220. "bias_ih_attr": bias_ih_attr,
  1221. "bias_hh_attr": bias_hh_attr
  1222. }
  1223. if mode == "LSTM":
  1224. rnn_cls = pd.nn.LSTMCell
  1225. elif mode == "GRU":
  1226. rnn_cls = pd.nn.GRUCell
  1227. elif mode == 'RNN_TANH':
  1228. rnn_cls = pd.nn.SimpleRNNCell
  1229. kwargs["activation"] = 'tanh'
  1230. elif mode == 'RNN_RELU':
  1231. rnn_cls = pd.nn.SimpleRNNCell
  1232. kwargs["activation"] = 'relu'
  1233. if not bidirectional:
  1234. is_reverse = False
  1235. cell = rnn_cls(input_size, hidden_size, **kwargs)
  1236. self.rnn.append(RNN(cell, is_reverse, self.time_major))
  1237. for i in range(1, num_layers):
  1238. cell = rnn_cls(hidden_size, hidden_size, **kwargs)
  1239. self.rnn.append(RNN(cell, is_reverse, self.time_major))
  1240. else:
  1241. cell_fw = rnn_cls(input_size, hidden_size, **kwargs)
  1242. cell_bw = rnn_cls(input_size, hidden_size, **kwargs)
  1243. self.rnn.append(BiRNN(cell_fw, cell_bw, self.time_major))
  1244. for i in range(1, num_layers):
  1245. cell_fw = rnn_cls(2 * hidden_size, hidden_size, **kwargs)
  1246. cell_bw = rnn_cls(2 * hidden_size, hidden_size, **kwargs)
  1247. self.rnn.append(BiRNN(cell_fw, cell_bw, self.time_major))
  1248. self.could_use_cudnn = True
  1249. self.could_use_cudnn &= len(self.rnn.parameters()) == num_layers * 4 * self.bidirect
  1250. param_names = []
  1251. for layer in range(self.num_layers):
  1252. for direction in range(self.bidirect):
  1253. suffix = '_reverse' if direction == 1 else ''
  1254. param_names.extend(['weight_ih_l{}{}', 'weight_hh_l{}{}'])
  1255. if bias_ih_attr != False: param_names.append('bias_ih_l{}{}')
  1256. if bias_hh_attr != False: param_names.append('bias_hh_l{}{}')
  1257. param_names = [x.format(layer, suffix) for x in param_names]
  1258. for name, param in zip(param_names, self.rnn.parameters()):
  1259. setattr(self.rnn, name, param)
  1260. self.flatten_parameters()
  1261. def flatten_parameters(self):
  1262. """
  1263. Resets parameter data pointer to address in continuous memory block for
  1264. cudnn usage.
  1265. """
  1266. if self.could_use_cudnn:
  1267. # layer.parameters() is depth first and ordered
  1268. # for i in layer: for j in direct: w_ih, w_hh, b_ih, b_hh
  1269. # need to reorganize to cudnn param layout:
  1270. # all bias following all weights
  1271. params = self.rnn.parameters(include_sublayers=False)
  1272. shape = [np.prod(param.shape) for param in params]
  1273. self._all_weights = [None] * len(params)
  1274. for i, param in enumerate(params):
  1275. offset = 0 if i % 4 < 2 else (2 * self.num_layers * self.bidirect)
  1276. layer_idx = i // 4
  1277. self._all_weights[offset + layer_idx * 2 + i % 2] = param
  1278. # Wrap using a list to avoid registed into params and saving, maybe
  1279. # need a better way to handle this later. Use `create_parameter` to
  1280. # add both to main_program and startup_program for static-graph.
  1281. # Use Constant initializer to avoid make effect on random generator.
  1282. self._flat_weight = [
  1283. self.rnn.create_parameter(
  1284. shape=[np.sum(shape)], dtype=params[0].dtype, default_initializer=I.Constant(0.0)
  1285. )
  1286. ]
  1287. # dropout state may also can be hided and avoid saving
  1288. # should dropout state be persistable for static-graph
  1289. self._dropout_state = self.rnn.create_variable(dtype=fluid.core.VarDesc.VarType.UINT8)
  1290. # for static-graph, append coalesce_tensor into startup program
  1291. with fluid.program_guard(fluid.default_startup_program(), fluid.default_startup_program()):
  1292. with pd.framework.no_grad():
  1293. self.rnn._helper.append_op(
  1294. type="coalesce_tensor", inputs={"Input": self._all_weights}, outputs={
  1295. "Output": self._all_weights,
  1296. "FusedOutput": self._flat_weight
  1297. }, attrs={
  1298. "copy_data": True,
  1299. "use_align": False,
  1300. "dtype": params[0].dtype
  1301. }
  1302. )
  1303. def _cudnn_impl(self, inputs, initial_states, sequence_length):
  1304. if not self.time_major:
  1305. inputs = pd.tensor.transpose(inputs, [1, 0, 2])
  1306. out = self.rnn._helper.create_variable_for_type_inference(inputs.dtype)
  1307. state = [
  1308. self.rnn._helper.create_variable_for_type_inference(inputs.dtype) for i in range(self.state_components)
  1309. ]
  1310. reserve = self.rnn._helper.create_variable_for_type_inference(
  1311. dtype=fluid.core.VarDesc.VarType.UINT8, stop_gradient=True
  1312. )
  1313. inputs = {
  1314. 'Input': inputs,
  1315. 'WeightList': self._all_weights,
  1316. 'PreState': initial_states,
  1317. 'SequenceLength': sequence_length
  1318. }
  1319. attrs = {
  1320. 'dropout_prob': self.dropout,
  1321. 'is_bidirec': self.bidirect == 2,
  1322. 'input_size': self.input_size,
  1323. 'hidden_size': self.hidden_size,
  1324. 'num_layers': self.num_layers,
  1325. 'mode': self.mode,
  1326. 'is_test': not self.rnn.training
  1327. }
  1328. outputs = {
  1329. 'Out': out,
  1330. 'State': state,
  1331. 'Reserve': reserve,
  1332. 'DropoutState': self._dropout_state,
  1333. }
  1334. self.rnn._helper.append_op(type="rnn", inputs=inputs, outputs=outputs, attrs=attrs)
  1335. out = pd.tensor.transpose(out, [1, 0, 2]) if not self.time_major else out
  1336. return out, tuple(state) if len(state) > 1 else state[0]
  1337. def forward(self, inputs, initial_states=None, sequence_length=None):
  1338. batch_index = 1 if self.time_major else 0
  1339. dtype = inputs.dtype
  1340. if initial_states is None:
  1341. state_shape = [self.num_layers * self.bidirect, -1, self.hidden_size]
  1342. if self.state_components == 1:
  1343. initial_states = fluid.layers.fill_constant_batch_size_like(
  1344. inputs, state_shape, dtype, 0, batch_index, 1
  1345. )
  1346. else:
  1347. initial_states = tuple(
  1348. [
  1349. fluid.layers.fill_constant_batch_size_like(inputs, state_shape, dtype, 0, batch_index, 1)
  1350. for _ in range(self.state_components)
  1351. ]
  1352. )
  1353. if self.could_use_cudnn:
  1354. # Add CPU kernel and dispatch in backend later
  1355. return self._cudnn_impl(inputs, initial_states, sequence_length)
  1356. states = split_states(initial_states, self.bidirect == 2, self.state_components)
  1357. final_states = []
  1358. for i, rnn_layer in enumerate(self.rnn):
  1359. if i > 0:
  1360. inputs = F.dropout(inputs, self.dropout, training=self.rnn.training, mode="upscale_in_train")
  1361. outputs, final_state = rnn_layer(inputs, states[i], sequence_length)
  1362. final_states.append(final_state)
  1363. inputs = outputs
  1364. final_states = concat_states(final_states, self.bidirect == 2, self.state_components)
  1365. return outputs, final_states

TensorLayer3.0 是一款兼容多种深度学习框架为计算后端的深度学习库。计划兼容TensorFlow, Pytorch, MindSpore, Paddle.