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.

dropconnect.py 4.2 kB

4 years ago
4 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import numbers
  4. import tensorlayer as tl
  5. from tensorlayer import logging
  6. from tensorlayer.layers.core import Module
  7. __all__ = [
  8. 'DropconnectDense',
  9. ]
  10. class DropconnectDense(Module):
  11. """
  12. The :class:`DropconnectDense` class is :class:`Dense` with DropConnect
  13. behaviour which randomly removes connections between this layer and the previous
  14. layer according to a keeping probability.
  15. Parameters
  16. ----------
  17. keep : float
  18. The keeping probability.
  19. The lower the probability it is, the more activations are set to zero.
  20. n_units : int
  21. The number of units of this layer.
  22. act : activation function
  23. The activation function of this layer.
  24. W_init : weights initializer
  25. The initializer for the weight matrix.
  26. b_init : biases initializer
  27. The initializer for the bias vector.
  28. in_channels: int
  29. The number of channels of the previous layer.
  30. If None, it will be automatically detected when the layer is forwarded for the first time.
  31. name : str
  32. A unique layer name.
  33. Examples
  34. --------
  35. >>> net = tl.layers.Input([10, 784], name='input')
  36. >>> net = tl.layers.DropconnectDense(keep=0.8, n_units=800, act=tl.ReLU, name='relu1')(net)
  37. >>> output shape :(10, 800)
  38. >>> net = tl.layers.DropconnectDense(keep=0.5, n_units=800, act=tl.ReLU, name='relu2')(net)
  39. >>> output shape :(10, 800)
  40. >>> net = tl.layers.DropconnectDense(keep=0.5, n_units=10, name='output')(net)
  41. >>> output shape :(10, 10)
  42. References
  43. ----------
  44. - `Wan, L. (2013). Regularization of neural networks using dropconnect <http://machinelearning.wustl.edu/mlpapers/papers/icml2013_wan13>`__
  45. """
  46. def __init__(
  47. self,
  48. keep=0.5,
  49. n_units=100,
  50. act=None,
  51. W_init=tl.initializers.truncated_normal(stddev=0.05),
  52. b_init=tl.initializers.constant(value=0.0),
  53. in_channels=None,
  54. name=None, # 'dropconnect',
  55. ):
  56. super().__init__(name, act=act)
  57. if isinstance(keep, numbers.Real) and not (keep > 0 and keep <= 1):
  58. raise ValueError("keep must be a scalar tensor or a float in the " "range (0, 1], got %g" % keep)
  59. self.keep = keep
  60. self.n_units = n_units
  61. self.W_init = W_init
  62. self.b_init = b_init
  63. self.in_channels = in_channels
  64. if self.in_channels is not None:
  65. self.build((None, self.in_channels))
  66. self._built = True
  67. logging.info(
  68. "DropconnectDense %s: %d %s" %
  69. (self.name, n_units, self.act.__class__.__name__ if self.act is not None else 'No Activation')
  70. )
  71. def __repr__(self):
  72. actstr = self.act.__name__ if self.act is not None else 'No Activation'
  73. s = ('{classname}(n_units={n_units}, ' + actstr)
  74. s += ', keep={keep}'
  75. if self.in_channels is not None:
  76. s += ', in_channels=\'{in_channels}\''
  77. if self.name is not None:
  78. s += ', name=\'{name}\''
  79. s += ')'
  80. return s.format(classname=self.__class__.__name__, **self.__dict__)
  81. def build(self, inputs_shape):
  82. if len(inputs_shape) != 2:
  83. raise Exception("The input dimension must be rank 2")
  84. if self.in_channels is None:
  85. self.in_channels = inputs_shape[1]
  86. n_in = inputs_shape[-1]
  87. self.W = self._get_weights("weights", shape=(n_in, self.n_units), init=self.W_init)
  88. if self.b_init:
  89. self.b = self._get_weights("biases", shape=(self.n_units), init=self.b_init)
  90. self.dropout = tl.ops.Dropout(keep=self.keep)
  91. self.matmul = tl.ops.MatMul()
  92. self.bias_add = tl.ops.BiasAdd()
  93. def forward(self, inputs):
  94. if self._forward_state == False:
  95. if self._built == False:
  96. self.build(tl.get_tensor_shape(inputs))
  97. self._built = True
  98. self._forward_state = True
  99. W_dropcon = self.dropout(self.W)
  100. outputs = self.matmul(inputs, W_dropcon)
  101. if self.b_init:
  102. outputs = self.bias_add(outputs, self.b)
  103. if self.act:
  104. outputs = self.act(outputs)
  105. return outputs

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