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.

mindspore_cost.py 29 kB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. from mindspore import nn
  4. from mindspore.nn import Cell
  5. import mindspore.ops as P
  6. __all__ = [
  7. 'softmax_cross_entropy_with_logits',
  8. 'sigmoid_cross_entropy',
  9. 'binary_cross_entropy',
  10. 'mean_squared_error',
  11. 'normalized_mean_square_error',
  12. 'absolute_difference_error',
  13. 'dice_coe',
  14. 'dice_hard_coe',
  15. 'iou_coe',
  16. 'cross_entropy_seq',
  17. 'cross_entropy_seq_with_mask',
  18. 'cosine_similarity',
  19. 'li_regularizer',
  20. 'lo_regularizer',
  21. 'maxnorm_regularizer',
  22. 'maxnorm_o_regularizer',
  23. 'maxnorm_i_regularizer',
  24. ]
  25. softmax_cross_entropy_with_logits = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction='mean')
  26. sigmoid_cross_entropy = P.SigmoidCrossEntropyWithLogits()
  27. def binary_cross_entropy(output, target, epsilon=1e-8, name='bce_loss'):
  28. """Binary cross entropy operation.
  29. Parameters
  30. ----------
  31. output : Tensor
  32. Tensor with type of `float32` or `float64`.
  33. target : Tensor
  34. The target distribution, format the same with `output`.
  35. epsilon : float
  36. A small value to avoid output to be zero.
  37. name : str
  38. An optional name to attach to this function.
  39. References
  40. -----------
  41. - `ericjang-DRAW <https://github.com/ericjang/draw/blob/master/draw.py#L73>`__
  42. """
  43. # return tf.reduce_mean(
  44. # tf.reduce_sum(
  45. # -(target * tf.math.log(output + epsilon) + (1. - target) * tf.math.log(1. - output + epsilon)), axis=1
  46. # ), name=name
  47. # )
  48. raise NotImplementedError("Not Implemented.")
  49. mean_squared_error = nn.MSELoss()
  50. def normalized_mean_square_error(output, target, axis=-1, name="normalized_mean_squared_error_loss"):
  51. """Return the TensorFlow expression of normalized mean-square-error of two distributions.
  52. Parameters
  53. ----------
  54. output : Tensor
  55. 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, height, width] or [batch_size, height, width, channel].
  56. target : Tensor
  57. The target distribution, format the same with `output`.
  58. axis : int or list of int
  59. The dimensions to reduce.
  60. name : str
  61. An optional name to attach to this function.
  62. """
  63. # with tf.name_scope("normalized_mean_squared_error_loss"):
  64. # nmse_a = tf.sqrt(tf.reduce_sum(tf.math.squared_difference(output, target), axis=axis))
  65. # nmse_b = tf.sqrt(tf.reduce_sum(tf.square(target), axis=axis))
  66. # nmse = tf.reduce_mean(nmse_a / nmse_b, name=name)
  67. raise NotImplementedError("Not Implemented.")
  68. def absolute_difference_error(output, target, is_mean=False, axis=-1, name="absolute_difference_error_loss"):
  69. """Return the TensorFlow expression of absolute difference error (L1) of two batch of data.
  70. Parameters
  71. ----------
  72. output : Tensor
  73. 2D, 3D or 4D tensor i.e. [batch_size, n_feature], [batch_size, height, width] or [batch_size, height, width, channel].
  74. target : Tensor
  75. The target distribution, format the same with `output`.
  76. is_mean : boolean
  77. Whether compute the mean or sum for each example.
  78. - If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data.
  79. - If False, use ``tf.reduce_sum`` (default).
  80. axis : int or list of int
  81. The dimensions to reduce.
  82. name : str
  83. An optional name to attach to this function.
  84. """
  85. # if is_mean:
  86. # loss = tf.reduce_mean(tf.reduce_mean(tf.abs(output - target), axis), name=name)
  87. # else:
  88. # loss = tf.reduce_mean(tf.reduce_sum(tf.abs(output - target), axis), name=name)
  89. raise NotImplementedError("Not Implemented.")
  90. def dice_coe(output, target, loss_type='jaccard', axis=(1, 2, 3), smooth=1e-5):
  91. """Soft dice (Sørensen or Jaccard) coefficient for comparing the similarity
  92. of two batch of data, usually be used for binary image segmentation
  93. i.e. labels are binary. The coefficient between 0 to 1, 1 means totally match.
  94. Parameters
  95. -----------
  96. output : Tensor
  97. A distribution with shape: [batch_size, ....], (any dimensions).
  98. target : Tensor
  99. The target distribution, format the same with `output`.
  100. loss_type : str
  101. ``jaccard`` or ``sorensen``, default is ``jaccard``.
  102. axis : tuple of int
  103. All dimensions are reduced, default ``[1,2,3]``.
  104. smooth : float
  105. This small value will be added to the numerator and denominator.
  106. - If both output and target are empty, it makes sure dice is 1.
  107. - If either output or target are empty (all pixels are background), dice = ```smooth/(small_value + smooth)``, then if smooth is very small, dice close to 0 (even the image values lower than the threshold), so in this case, higher smooth can have a higher dice.
  108. Examples
  109. ---------
  110. >>> import tensorlayer as tl
  111. >>> outputs = tl.act.pixel_wise_softmax(outputs)
  112. >>> dice_loss = 1 - tl.cost.dice_coe(outputs, y_)
  113. References
  114. -----------
  115. - `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`__
  116. """
  117. # inse = tf.reduce_sum(output * target, axis=axis)
  118. # if loss_type == 'jaccard':
  119. # l = tf.reduce_sum(output * output, axis=axis)
  120. # r = tf.reduce_sum(target * target, axis=axis)
  121. # elif loss_type == 'sorensen':
  122. # l = tf.reduce_sum(output, axis=axis)
  123. # r = tf.reduce_sum(target, axis=axis)
  124. # else:
  125. # raise Exception("Unknow loss_type")
  126. # dice = (2. * inse + smooth) / (l + r + smooth)
  127. # ##
  128. # dice = tf.reduce_mean(dice, name='dice_coe')
  129. raise NotImplementedError("Not Implemented.")
  130. def dice_hard_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
  131. """Non-differentiable Sørensen–Dice coefficient for comparing the similarity
  132. of two batch of data, usually be used for binary image segmentation i.e. labels are binary.
  133. The coefficient between 0 to 1, 1 if totally match.
  134. Parameters
  135. -----------
  136. output : tensor
  137. A distribution with shape: [batch_size, ....], (any dimensions).
  138. target : tensor
  139. The target distribution, format the same with `output`.
  140. threshold : float
  141. The threshold value to be true.
  142. axis : tuple of integer
  143. All dimensions are reduced, default ``(1,2,3)``.
  144. smooth : float
  145. This small value will be added to the numerator and denominator, see ``dice_coe``.
  146. References
  147. -----------
  148. - `Wiki-Dice <https://en.wikipedia.org/wiki/Sørensen–Dice_coefficient>`__
  149. """
  150. # output = tf.cast(output > threshold, dtype=tf.float32)
  151. # target = tf.cast(target > threshold, dtype=tf.float32)
  152. # inse = tf.reduce_sum(tf.multiply(output, target), axis=axis)
  153. # l = tf.reduce_sum(output, axis=axis)
  154. # r = tf.reduce_sum(target, axis=axis)
  155. # hard_dice = (2. * inse + smooth) / (l + r + smooth)
  156. # ##
  157. # hard_dice = tf.reduce_mean(hard_dice, name='hard_dice')
  158. raise NotImplementedError("Not Implemented.")
  159. def iou_coe(output, target, threshold=0.5, axis=(1, 2, 3), smooth=1e-5):
  160. """Non-differentiable Intersection over Union (IoU) for comparing the
  161. similarity of two batch of data, usually be used for evaluating binary image segmentation.
  162. The coefficient between 0 to 1, and 1 means totally match.
  163. Parameters
  164. -----------
  165. output : tensor
  166. A batch of distribution with shape: [batch_size, ....], (any dimensions).
  167. target : tensor
  168. The target distribution, format the same with `output`.
  169. threshold : float
  170. The threshold value to be true.
  171. axis : tuple of integer
  172. All dimensions are reduced, default ``(1,2,3)``.
  173. smooth : float
  174. This small value will be added to the numerator and denominator, see ``dice_coe``.
  175. Notes
  176. ------
  177. - IoU cannot be used as training loss, people usually use dice coefficient for training, IoU and hard-dice for evaluating.
  178. """
  179. # pre = tf.cast(output > threshold, dtype=tf.float32)
  180. # truth = tf.cast(target > threshold, dtype=tf.float32)
  181. # inse = tf.reduce_sum(tf.multiply(pre, truth), axis=axis) # AND
  182. # union = tf.reduce_sum(tf.cast(tf.add(pre, truth) >= 1, dtype=tf.float32), axis=axis) # OR
  183. # batch_iou = (inse + smooth) / (union + smooth)
  184. # iou = tf.reduce_mean(batch_iou, name='iou_coe')
  185. raise NotImplementedError("Not Implemented.")
  186. def sequence_loss_by_example(
  187. logits, targets, weights, average_across_timesteps=True, softmax_loss_function=None, name=None
  188. ):
  189. """Weighted cross-entropy loss for a sequence of logits (per example). see original tensorflow code :
  190. <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/legacy_seq2seq/python/ops/seq2seq.py#L1057>
  191. Parameters
  192. ----------
  193. logits: List
  194. List of 2D Tensors of shape [batch_size x num_decoder_symbols].
  195. targets: List
  196. List of 1D batch-sized int32 Tensors of the same length as logits.
  197. weights: List
  198. List of 1D batch-sized float-Tensors of the same length as logits.
  199. average_across_timesteps: Boolean
  200. If set, divide the returned cost by the total label weight.
  201. softmax_loss_function: None or Function
  202. Function (labels, logits) -> loss-batch to be used instead of the standard softmax (the default if this is None).
  203. **Note that to avoid confusion, it is required for the function to accept named arguments.**
  204. name: None or str
  205. Optional name for this operation, default: "sequence_loss_by_example".
  206. Returns
  207. -------
  208. 1D batch-sized float Tensor: The log-perplexity for each sequence.
  209. Raises
  210. ------
  211. ValueError: If len(logits) is different from len(targets) or len(weights).
  212. """
  213. # if len(targets) != len(logits) or len(weights) != len(logits):
  214. # raise ValueError(
  215. # "Lengths of logits, weights, and targets must be the same "
  216. # "%d, %d, %d." % (len(logits), len(weights), len(targets))
  217. # )
  218. # with ops.name_scope(name, "sequence_loss_by_example", logits + targets + weights):
  219. # log_perp_list = []
  220. # for logit, target, weight in zip(logits, targets, weights):
  221. # if softmax_loss_function is None:
  222. # # TODO(irving,ebrevdo): This reshape is needed because
  223. # # sequence_loss_by_example is called with scalars sometimes, which
  224. # # violates our general scalar strictness policy.
  225. # target = array_ops.reshape(target, [-1])
  226. # crossent = nn_ops.sparse_softmax_cross_entropy_with_logits(labels=target, logits=logit)
  227. # else:
  228. # crossent = softmax_loss_function(labels=target, logits=logit)
  229. # log_perp_list.append(crossent * weight)
  230. # log_perps = math_ops.add_n(log_perp_list)
  231. # if average_across_timesteps:
  232. # total_size = math_ops.add_n(weights)
  233. # total_size += 1e-12 # Just to avoid division by 0 for all-0 weights.
  234. # log_perps /= total_size
  235. raise NotImplementedError("Not Implemented.")
  236. def cross_entropy_seq(logits, target_seqs, batch_size=None):
  237. """Returns the expression of cross-entropy of two sequences, implement
  238. softmax internally. Normally be used for fixed length RNN outputs, see `PTB example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_ptb_lstm.py>`__.
  239. Parameters
  240. ----------
  241. logits : Tensor
  242. 2D tensor with shape of `[batch_size * n_steps, n_classes]`.
  243. target_seqs : Tensor
  244. The target sequence, 2D tensor `[batch_size, n_steps]`, if the number of step is dynamic, please use ``tl.cost.cross_entropy_seq_with_mask`` instead.
  245. batch_size : None or int.
  246. Whether to divide the cost by batch size.
  247. - If integer, the return cost will be divided by `batch_size`.
  248. - If None (default), the return cost will not be divided by anything.
  249. Examples
  250. --------
  251. >>> import tensorlayer as tl
  252. >>> # see `PTB example <https://github.com/tensorlayer/tensorlayer/blob/master/example/tutorial_ptb_lstm.py>`__.for more details
  253. >>> # outputs shape : (batch_size * n_steps, n_classes)
  254. >>> # targets shape : (batch_size, n_steps)
  255. >>> cost = tl.cost.cross_entropy_seq(outputs, targets)
  256. """
  257. # sequence_loss_by_example_fn = sequence_loss_by_example
  258. #
  259. # loss = sequence_loss_by_example_fn(
  260. # [logits], [tf.reshape(target_seqs, [-1])], [tf.ones_like(tf.reshape(target_seqs, [-1]), dtype=tf.float32)]
  261. # )
  262. # # [tf.ones([batch_size * num_steps])])
  263. # cost = tf.reduce_sum(loss) # / batch_size
  264. # if batch_size is not None:
  265. # cost = cost / batch_size
  266. raise NotImplementedError("Not Implemented.")
  267. def cross_entropy_seq_with_mask(logits, target_seqs, input_mask, return_details=False, name=None):
  268. """Returns the expression of cross-entropy of two sequences, implement
  269. softmax internally. Normally be used for Dynamic RNN with Synced sequence input and output.
  270. Parameters
  271. -----------
  272. logits : Tensor
  273. 2D tensor with shape of [batch_size * ?, n_classes], `?` means dynamic IDs for each example.
  274. - Can be get from `DynamicRNNLayer` by setting ``return_seq_2d`` to `True`.
  275. target_seqs : Tensor
  276. int of tensor, like word ID. [batch_size, ?], `?` means dynamic IDs for each example.
  277. input_mask : Tensor
  278. The mask to compute loss, it has the same size with `target_seqs`, normally 0 or 1.
  279. return_details : boolean
  280. Whether to return detailed losses.
  281. - If False (default), only returns the loss.
  282. - If True, returns the loss, losses, weights and targets (see source code).
  283. Examples
  284. --------
  285. >>> import tensorlayer as tl
  286. >>> import tensorflow as tf
  287. >>> import numpy as np
  288. >>> batch_size = 64
  289. >>> vocab_size = 10000
  290. >>> embedding_size = 256
  291. >>> ni = tl.layers.Input([batch_size, None], dtype=tf.int64)
  292. >>> net = tl.layers.Embedding(
  293. ... vocabulary_size = vocab_size,
  294. ... embedding_size = embedding_size,
  295. ... name = 'seq_embedding')(ni)
  296. >>> net = tl.layers.RNN(
  297. ... cell =tf.keras.layers.LSTMCell(units=embedding_size, dropout=0.1),
  298. ... return_seq_2d = True,
  299. ... name = 'dynamicrnn')(net)
  300. >>> net = tl.layers.Dense(n_units=vocab_size, name="output")(net)
  301. >>> model = tl.models.Model(inputs=ni, outputs=net)
  302. >>> input_seqs = np.random.randint(0, 10, size=(batch_size, 10), dtype=np.int64)
  303. >>> target_seqs = np.random.randint(0, 10, size=(batch_size, 10), dtype=np.int64)
  304. >>> input_mask = np.random.randint(0, 2, size=(batch_size, 10), dtype=np.int64)
  305. >>> outputs = model(input_seqs, is_train=True)
  306. >>> loss = tl.cost.cross_entropy_seq_with_mask(outputs, target_seqs, input_mask)
  307. """
  308. # targets = tf.reshape(target_seqs, [-1]) # to one vector
  309. # weights = tf.cast(tf.reshape(input_mask, [-1]), dtype=tf.float32) # to one vector like targets
  310. # losses = tf.nn.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name) * weights
  311. # # losses = tf.reduce_mean(tf.ops.sparse_softmax_cross_entropy_with_logits(logits=logits, labels=targets, name=name)) # for TF1.0 and others
  312. #
  313. # loss = tf.divide(
  314. # tf.reduce_sum(losses), # loss from mask. reduce_sum before element-wise mul with mask !!
  315. # tf.reduce_sum(weights),
  316. # name="seq_loss_with_mask"
  317. # )
  318. #
  319. # if return_details:
  320. # return loss, losses, weights, targets
  321. # else:
  322. # return loss
  323. raise NotImplementedError("Not Implemented.")
  324. def cosine_similarity(v1, v2):
  325. """Cosine similarity [-1, 1].
  326. Parameters
  327. ----------
  328. v1, v2 : Tensor
  329. Tensor with the same shape [batch_size, n_feature].
  330. References
  331. ----------
  332. - `Wiki <https://en.wikipedia.org/wiki/Cosine_similarity>`__.
  333. """
  334. # return tf.reduce_sum(tf.multiply(v1, v2), 1) / \
  335. # (tf.sqrt(tf.reduce_sum(tf.multiply(v1, v1), 1)) *
  336. # tf.sqrt(tf.reduce_sum(tf.multiply(v2, v2), 1)))
  337. raise NotImplementedError("Not Implemented.")
  338. # Regularization Functions
  339. def li_regularizer(scale, scope=None):
  340. """Li regularization removes the neurons of previous layer. The `i` represents `inputs`.
  341. Returns a function that can be used to apply group li regularization to weights.
  342. The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
  343. Parameters
  344. ----------
  345. scale : float
  346. A scalar multiplier `Tensor`. 0.0 disables the regularizer.
  347. scope: str
  348. An optional scope name for this function.
  349. Returns
  350. --------
  351. A function with signature `li(weights, name=None)` that apply Li regularization.
  352. Raises
  353. ------
  354. ValueError : if scale is outside of the range [0.0, 1.0] or if scale is not a float.
  355. """
  356. # if isinstance(scale, numbers.Integral):
  357. # raise ValueError('scale cannot be an integer: %s' % scale)
  358. # if isinstance(scale, numbers.Real):
  359. # if scale < 0.:
  360. # raise ValueError('Setting a scale less than 0 on a regularizer: %g' % scale)
  361. # if scale >= 1.:
  362. # raise ValueError('Setting a scale greater than 1 on a regularizer: %g' % scale)
  363. # if scale == 0.:
  364. # logging.info('Scale of 0 disables regularizer.')
  365. # return lambda _, name=None: None
  366. #
  367. # def li(weights):
  368. # """Applies li regularization to weights."""
  369. # with tf.name_scope('li_regularizer') as scope:
  370. # my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale')
  371. # # if tf.__version__ <= '0.12':
  372. # # standard_ops_fn = standard_ops.mul
  373. # # else:
  374. # standard_ops_fn = standard_ops.multiply
  375. # return standard_ops_fn(
  376. # my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 1))),
  377. # name=scope
  378. # )
  379. raise NotImplementedError("Not Implemented.")
  380. def lo_regularizer(scale):
  381. """Lo regularization removes the neurons of current layer. The `o` represents `outputs`
  382. Returns a function that can be used to apply group lo regularization to weights.
  383. The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
  384. Parameters
  385. ----------
  386. scale : float
  387. A scalar multiplier `Tensor`. 0.0 disables the regularizer.
  388. Returns
  389. -------
  390. A function with signature `lo(weights, name=None)` that apply Lo regularization.
  391. Raises
  392. ------
  393. ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.
  394. """
  395. # if isinstance(scale, numbers.Integral):
  396. # raise ValueError('scale cannot be an integer: %s' % scale)
  397. #
  398. # if isinstance(scale, numbers.Real):
  399. # if scale < 0.:
  400. # raise ValueError('Setting a scale less than 0 on a regularizer: %g' % scale)
  401. # if scale >= 1.:
  402. # raise ValueError('Setting a scale greater than 1 on a regularizer: %g' % scale)
  403. # if scale == 0.:
  404. # logging.info('Scale of 0 disables regularizer.')
  405. # return lambda _, name=None: None
  406. #
  407. # def lo(weights, name='lo_regularizer'):
  408. # """Applies group column regularization to weights."""
  409. # with tf.name_scope(name) as scope:
  410. # my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale')
  411. # # if tf.__version__ <= '0.12':
  412. # # standard_ops_fn = standard_ops.mul
  413. # # else:
  414. # standard_ops_fn = standard_ops.multiply
  415. # return standard_ops_fn(
  416. # my_scale, standard_ops.reduce_sum(standard_ops.sqrt(standard_ops.reduce_sum(tf.square(weights), 0))),
  417. # name=scope
  418. # )
  419. raise NotImplementedError("Not Implemented.")
  420. def maxnorm_regularizer(scale=1.0):
  421. """Max-norm regularization returns a function that can be used to apply max-norm regularization to weights.
  422. More about max-norm, see `wiki-max norm <https://en.wikipedia.org/wiki/Matrix_norm#Max_norm>`_.
  423. The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
  424. Parameters
  425. ----------
  426. scale : float
  427. A scalar multiplier `Tensor`. 0.0 disables the regularizer.
  428. Returns
  429. ---------
  430. A function with signature `mn(weights, name=None)` that apply Lo regularization.
  431. Raises
  432. --------
  433. ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.
  434. """
  435. # if isinstance(scale, numbers.Integral):
  436. # raise ValueError('scale cannot be an integer: %s' % scale)
  437. #
  438. # if isinstance(scale, numbers.Real):
  439. # if scale < 0.:
  440. # raise ValueError('Setting a scale less than 0 on a regularizer: %g' % scale)
  441. # # if scale >= 1.:
  442. # # raise ValueError('Setting a scale greater than 1 on a regularizer: %g' %
  443. # # scale)
  444. # if scale == 0.:
  445. # logging.info('Scale of 0 disables regularizer.')
  446. # return lambda _, name=None: None
  447. #
  448. # def mn(weights, name='max_regularizer'):
  449. # """Applies max-norm regularization to weights."""
  450. # with tf.name_scope(name) as scope:
  451. # my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale')
  452. # # if tf.__version__ <= '0.12':
  453. # # standard_ops_fn = standard_ops.mul
  454. # # else:
  455. # standard_ops_fn = standard_ops.multiply
  456. # return standard_ops_fn(my_scale, standard_ops.reduce_max(standard_ops.abs(weights)), name=scope)
  457. #
  458. # return mn
  459. raise NotImplementedError("Not Implemented.")
  460. def maxnorm_o_regularizer(scale):
  461. """Max-norm output regularization removes the neurons of current layer.
  462. Returns a function that can be used to apply max-norm regularization to each column of weight matrix.
  463. The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
  464. Parameters
  465. ----------
  466. scale : float
  467. A scalar multiplier `Tensor`. 0.0 disables the regularizer.
  468. Returns
  469. ---------
  470. A function with signature `mn_o(weights, name=None)` that apply Lo regularization.
  471. Raises
  472. ---------
  473. ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.
  474. """
  475. # if isinstance(scale, numbers.Integral):
  476. # raise ValueError('scale cannot be an integer: %s' % scale)
  477. #
  478. # if isinstance(scale, numbers.Real):
  479. # if scale < 0.:
  480. # raise ValueError('Setting a scale less than 0 on a regularizer: %g' % scale)
  481. # # if scale >= 1.:
  482. # # raise ValueError('Setting a scale greater than 1 on a regularizer: %g' %
  483. # # scale)
  484. # if scale == 0.:
  485. # logging.info('Scale of 0 disables regularizer.')
  486. # return lambda _, name=None: None
  487. #
  488. # def mn_o(weights, name='maxnorm_o_regularizer'):
  489. # """Applies max-norm regularization to weights."""
  490. # with tf.name_scope(name) as scope:
  491. # my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale')
  492. # if tf.__version__ <= '0.12':
  493. # standard_ops_fn = standard_ops.mul
  494. # else:
  495. # standard_ops_fn = standard_ops.multiply
  496. # return standard_ops_fn(
  497. # my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 0)), name=scope
  498. # )
  499. #
  500. # return mn_o
  501. raise NotImplementedError("Not Implemented.")
  502. def maxnorm_i_regularizer(scale):
  503. """Max-norm input regularization removes the neurons of previous layer.
  504. Returns a function that can be used to apply max-norm regularization to each row of weight matrix.
  505. The implementation follows `TensorFlow contrib <https://github.com/tensorflow/tensorflow/blob/master/tensorflow/contrib/layers/python/layers/regularizers.py>`__.
  506. Parameters
  507. ----------
  508. scale : float
  509. A scalar multiplier `Tensor`. 0.0 disables the regularizer.
  510. Returns
  511. ---------
  512. A function with signature `mn_i(weights, name=None)` that apply Lo regularization.
  513. Raises
  514. ---------
  515. ValueError : If scale is outside of the range [0.0, 1.0] or if scale is not a float.
  516. """
  517. # if isinstance(scale, numbers.Integral):
  518. # raise ValueError('scale cannot be an integer: %s' % scale)
  519. #
  520. # if isinstance(scale, numbers.Real):
  521. # if scale < 0.:
  522. # raise ValueError('Setting a scale less than 0 on a regularizer: %g' % scale)
  523. # # if scale >= 1.:
  524. # # raise ValueError('Setting a scale greater than 1 on a regularizer: %g' %
  525. # # scale)
  526. # if scale == 0.:
  527. # logging.info('Scale of 0 disables regularizer.')
  528. # return lambda _, name=None: None
  529. #
  530. # def mn_i(weights, name='maxnorm_i_regularizer'):
  531. # """Applies max-norm regularization to weights."""
  532. # with tf.name_scope(name) as scope:
  533. # my_scale = ops.convert_to_tensor(scale, dtype=weights.dtype.base_dtype, name='scale')
  534. # if tf.__version__ <= '0.12':
  535. # standard_ops_fn = standard_ops.mul
  536. # else:
  537. # standard_ops_fn = standard_ops.multiply
  538. # return standard_ops_fn(
  539. # my_scale, standard_ops.reduce_sum(standard_ops.reduce_max(standard_ops.abs(weights), 1)), name=scope
  540. # )
  541. #
  542. # return mn_i
  543. raise NotImplementedError("Not Implemented.")
  544. def huber_loss(
  545. output, target, is_mean=True, delta=1.0, dynamichuber=False, reverse=False, axis=-1, epsilon=0.00001, name=None
  546. ):
  547. """Huber Loss operation, see ``https://en.wikipedia.org/wiki/Huber_loss`` .
  548. Reverse Huber Loss operation, see ''https://statweb.stanford.edu/~owen/reports/hhu.pdf''.
  549. Dynamic Reverse Huber Loss operation, see ''https://arxiv.org/pdf/1606.00373.pdf''.
  550. Parameters
  551. ----------
  552. output : Tensor
  553. A distribution with shape: [batch_size, ....], (any dimensions).
  554. target : Tensor
  555. The target distribution, format the same with `output`.
  556. is_mean : boolean
  557. Whether compute the mean or sum for each example.
  558. - If True, use ``tf.reduce_mean`` to compute the loss between one target and predict data (default).
  559. - If False, use ``tf.reduce_sum``.
  560. delta: float
  561. The point where the huber loss function changes from a quadratic to linear.
  562. dynamichuber: boolean
  563. Whether compute the coefficient c for each batch.
  564. - If True, c is 20% of the maximal per-batch error.
  565. - If False, c is delta.
  566. reverse: boolean
  567. Whether compute the reverse huber loss.
  568. axis : int or list of int
  569. The dimensions to reduce.
  570. epsilon:
  571. Eplison.
  572. name : string
  573. Name of this loss.
  574. """
  575. # if reverse:
  576. # if dynamichuber:
  577. # huber_c = 0.2 * tf.reduce_max(tf.abs(output - target))
  578. # else:
  579. # huber_c = delta
  580. # if is_mean:
  581. # loss = tf.reduce_mean(
  582. # tf.where(
  583. # tf.less_equal(tf.abs(output - target), huber_c), tf.abs(output - target),
  584. # tf.multiply(
  585. # tf.pow(output - target, 2.0) + tf.pow(huber_c, 2.0),
  586. # tf.math.divide_no_nan(.5, huber_c + epsilon)
  587. # )
  588. # ), name=name
  589. # )
  590. # else:
  591. # loss = tf.reduce_mean(
  592. # tf.reduce_sum(
  593. # tf.where(
  594. # tf.less_equal(tf.abs(output - target), huber_c), tf.abs(output - target),
  595. # tf.multiply(
  596. # tf.pow(output - target, 2.0) + tf.pow(huber_c, 2.0),
  597. # tf.math.divide_no_nan(.5, huber_c + epsilon)
  598. # )
  599. # ), axis
  600. # ), name=name
  601. # )
  602. # elif is_mean:
  603. # loss = tf.reduce_mean(
  604. # tf.where(
  605. # tf.less_equal(tf.abs(output - target), delta), 0.5 * tf.pow(output - target, 2),
  606. # delta * (tf.abs(output - target) - 0.5 * delta)
  607. # ), name=name
  608. # )
  609. # else:
  610. # loss = tf.reduce_mean(
  611. # tf.reduce_sum(
  612. # tf.where(
  613. # tf.less_equal(tf.abs(output - target), delta), 0.5 * tf.pow(output - target, 2),
  614. # delta * (tf.abs(output - target) - 0.5 * delta)
  615. # ), axis
  616. # ), name=name
  617. # )
  618. # return loss
  619. raise NotImplementedError("Not Implemented.")

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