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.

tensorflow_initializers.py 8.5 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  1. #! /usr/bin/python
  2. # -*- coding: utf-8 -*-
  3. import numpy as np
  4. import tensorlayer as tl
  5. __all__ = [
  6. 'Initializer', 'Zeros', 'Ones', 'Constant', 'RandomUniform', 'RandomNormal', 'TruncatedNormal',
  7. 'deconv2d_bilinear_upsampling_initializer', 'HeNormal'
  8. ]
  9. class Initializer(object):
  10. """Initializer base class: all initializers inherit from this class.
  11. """
  12. def __call__(self, shape, dtype=None):
  13. """Returns a tensor object initialized as specified by the initializer.
  14. Parameters
  15. ----------
  16. shape : tuple of int.
  17. The shape of the tensor.
  18. dtype : Optional dtype of the tensor.
  19. If not provided will return tensor of `tl.float32`.
  20. Returns
  21. -------
  22. """
  23. raise NotImplementedError
  24. def get_config(self):
  25. """Returns the configuration of the initializer as a JSON-serializable dict.
  26. Returns
  27. -------
  28. A JSON-serializable Python dict.
  29. """
  30. return {}
  31. @classmethod
  32. def from_config(cls, config):
  33. """Instantiates an initializer from a configuration dictionary.
  34. Parameters
  35. ----------
  36. config : A python dictionary.
  37. It will typically be the output of `get_config`.
  38. Returns
  39. -------
  40. An Initializer instance.
  41. """
  42. if 'dtype' in config:
  43. config.pop('dtype')
  44. return cls(**config)
  45. class Zeros(Initializer):
  46. """Initializer that generates tensors initialized to 0.
  47. Examples
  48. --------
  49. >>> import tensorlayer as tl
  50. >>> init = tl.initializers.zeros()
  51. >>> print(init(shape=(5, 10), dtype=tl.float32))
  52. """
  53. def __call__(self, shape, dtype=tl.float32):
  54. return tl.zeros(shape, dtype=dtype)
  55. class Ones(Initializer):
  56. """Initializer that generates tensors initialized to 1.
  57. Examples
  58. --------
  59. >>> import tensorlayer as tl
  60. >>> init = tl.initializers.ones()
  61. >>> print(init(shape=(5, 10), dtype=tl.float32))
  62. """
  63. def __call__(self, shape, dtype=tl.float32):
  64. return tl.ones(shape, dtype=dtype)
  65. class Constant(Initializer):
  66. """Initializer that generates tensors initialized to a constant value.
  67. Parameters
  68. ----------
  69. value : A python scalar or a numpy array.
  70. The assigned value.
  71. Examples
  72. --------
  73. >>> import tensorlayer as tl
  74. >>> init = tl.initializers.constant(value=10)
  75. >>> print(init(shape=(5, 10), dtype=tl.float32))
  76. """
  77. def __init__(self, value=0):
  78. self.value = value
  79. def __call__(self, shape, dtype=tl.float32):
  80. return tl.constant(self.value, shape=shape, dtype=dtype)
  81. def get_config(self):
  82. return {"value": self.value}
  83. class RandomUniform(Initializer):
  84. """Initializer that generates tensors with a uniform distribution.
  85. Parameters
  86. ----------
  87. minval : A python scalar or a scalar tensor.
  88. Lower bound of the range of random values to generate.
  89. maxval : A python scalar or a scalar tensor.
  90. Upper bound of the range of random values to generate.
  91. seed : A Python integer.
  92. Used to seed the random generator.
  93. Examples
  94. --------
  95. >>> import tensorlayer as tl
  96. >>> init = tl.initializers.random_uniform(minval=-0.05, maxval=0.05)
  97. >>> print(init(shape=(5, 10), dtype=tl.float32))
  98. """
  99. def __init__(self, minval=-0.05, maxval=0.05, seed=None):
  100. self.minval = minval
  101. self.maxval = maxval
  102. self.seed = seed
  103. def __call__(self, shape, dtype=tl.float32):
  104. return tl.random_uniform(shape, self.minval, self.maxval, dtype=dtype, seed=self.seed)
  105. def get_config(self):
  106. return {"minval": self.minval, "maxval": self.maxval, "seed": self.seed}
  107. class RandomNormal(Initializer):
  108. """Initializer that generates tensors with a normal distribution.
  109. Parameters
  110. ----------
  111. mean : A python scalar or a scalar tensor.
  112. Mean of the random values to generate.
  113. stddev : A python scalar or a scalar tensor.
  114. Standard deviation of the random values to generate.
  115. seed : A Python integer.
  116. Used to seed the random generator.
  117. minval=-0.05, maxval=0.05
  118. Examples
  119. --------
  120. >>> import tensorlayer as tl
  121. >>> init = tl.initializers.random_normal(mean=0.0, stddev=0.05)
  122. >>> print(init(shape=(5, 10), dtype=tl.float32))
  123. """
  124. def __init__(self, mean=0.0, stddev=0.05, seed=None):
  125. self.mean = mean
  126. self.stddev = stddev
  127. self.seed = seed
  128. def __call__(self, shape, dtype=tl.float32):
  129. return tl.random_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)
  130. def get_config(self):
  131. return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}
  132. class TruncatedNormal(Initializer):
  133. """Initializer that generates a truncated normal distribution.
  134. These values are similar to values from a `RandomNormal`
  135. except that values more than two standard deviations from the mean
  136. are discarded and re-drawn. This is the recommended initializer for
  137. neural network weights and filters.
  138. Parameters
  139. ----------
  140. mean : A python scalar or a scalar tensor.
  141. Mean of the random values to generate.
  142. stddev : A python scalar or a scalar tensor.
  143. Standard deviation of the andom values to generate.
  144. seed : A Python integer.
  145. Used to seed the random generator.
  146. Examples
  147. --------
  148. >>> import tensorlayer as tl
  149. >>> init = tl.initializers.truncated_normal(mean=0.0, stddev=0.05)
  150. >>> print(init(shape=(5, 10), dtype=tl.float32))
  151. """
  152. def __init__(self, mean=0.0, stddev=0.05, seed=None):
  153. self.mean = mean
  154. self.stddev = stddev
  155. self.seed = seed
  156. def __call__(self, shape, dtype=tl.float32):
  157. return tl.truncated_normal(shape, self.mean, self.stddev, dtype=dtype, seed=self.seed)
  158. def get_config(self):
  159. return {"mean": self.mean, "stddev": self.stddev, "seed": self.seed}
  160. class HeNormal(Initializer):
  161. """He normal initializer.
  162. Parameters
  163. ----------
  164. seed : A Python integer.
  165. Used to seed the random generator.
  166. Examples
  167. --------
  168. >>> import tensorlayer as tl
  169. >>> init = tl.initializers.he_normal()
  170. >>> print(init(shape=(5, 10), dtype=tl.float32))
  171. """
  172. def __init__(self, seed=None):
  173. self.seed = seed
  174. def __call__(self, shape, dtype=tl.float32):
  175. return tl.he_normal(seed=self.seed, shape=shape, dtype=dtype)
  176. def get_config(self):
  177. return {"seed", self.seed}
  178. def deconv2d_bilinear_upsampling_initializer(shape):
  179. """Returns the initializer that can be passed to DeConv2dLayer for initializing the
  180. weights in correspondence to channel-wise bilinear up-sampling.
  181. Used in segmentation approaches such as [FCN](https://arxiv.org/abs/1605.06211)
  182. Parameters
  183. ----------
  184. shape : tuple of int
  185. The shape of the filters, [height, width, output_channels, in_channels].
  186. It must match the shape passed to DeConv2dLayer.
  187. Returns
  188. -------
  189. ``tf.constant_initializer``
  190. A constant initializer with weights set to correspond to per channel bilinear upsampling
  191. when passed as W_int in DeConv2dLayer
  192. """
  193. if shape[0] != shape[1]:
  194. raise Exception('deconv2d_bilinear_upsampling_initializer only supports symmetrical filter sizes')
  195. if shape[3] < shape[2]:
  196. raise Exception(
  197. 'deconv2d_bilinear_upsampling_initializer behaviour is not defined for num_in_channels < num_out_channels '
  198. )
  199. filter_size = shape[0]
  200. num_out_channels = shape[2]
  201. num_in_channels = shape[3]
  202. # Create bilinear filter kernel as numpy array
  203. bilinear_kernel = np.zeros([filter_size, filter_size], dtype=np.float32)
  204. scale_factor = (filter_size + 1) // 2
  205. if filter_size % 2 == 1:
  206. center = scale_factor - 1
  207. else:
  208. center = scale_factor - 0.5
  209. for x in range(filter_size):
  210. for y in range(filter_size):
  211. bilinear_kernel[x, y] = (1 - abs(x - center) / scale_factor) * (1 - abs(y - center) / scale_factor)
  212. weights = np.zeros((filter_size, filter_size, num_out_channels, num_in_channels), dtype=np.float32)
  213. for i in range(num_out_channels):
  214. weights[:, :, i, i] = bilinear_kernel
  215. # assign numpy array to constant_initalizer and pass to get_variable
  216. return Constant(value=weights)

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