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.

CONTRIBUTING.md 7.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. # TensorLayer Contributor Guideline
  2. ## Welcome to contribute!
  3. You are more than welcome to contribute to TensorLayer! If you have any improvement, please send us your [pull requests](https://help.github.com/en/articles/about-pull-requests). You may implement your improvement on your [fork](https://help.github.com/en/articles/working-with-forks).
  4. ## Checklist
  5. * Continuous integration
  6. * Build from sources
  7. * Unittest
  8. * Documentation
  9. * General intro to TensorLayer2
  10. * How to contribute a new `Layer`
  11. * How to contribute a new `Model`
  12. * How to contribute a new example/tutorial
  13. ## Continuous integration
  14. We appreciate contributions
  15. either by adding / improving examples or extending / fixing the core library.
  16. To make your contributions, you would need to follow the [pep8](https://www.python.org/dev/peps/pep-0008/) coding style and [numpydoc](https://numpydoc.readthedocs.io/en/latest/) document style.
  17. We rely on Continuous Integration (CI) for checking push commits.
  18. The following tools are used to ensure that your commits can pass through the CI test:
  19. * [yapf](https://github.com/google/yapf) (format code), compulsory
  20. * [isort](https://github.com/timothycrosley/isort) (sort imports), optional
  21. * [autoflake](https://github.com/myint/autoflake) (remove unused imports), optional
  22. You can simply run
  23. ```bash
  24. make format
  25. ```
  26. to apply those tools before submitting your PR.
  27. ## Build from sources
  28. ```bash
  29. # First clone the repository and change the current directory to the newly cloned repository
  30. git clone https://github.com/zsdonghao/tensorlayer2.git
  31. cd tensorlayer2
  32. # Install virtualenv if necessary
  33. pip install virtualenv
  34. # Then create a virtualenv called `venv`
  35. virtualenv venv
  36. # Activate the virtualenv
  37. ## Linux:
  38. source venv/bin/activate
  39. ## Windows:
  40. venv\Scripts\activate.bat
  41. # ============= IF TENSORFLOW IS NOT ALREADY INSTALLED ============= #
  42. # basic installation
  43. pip install .
  44. # advanced: for a machine **without** an NVIDIA GPU
  45. pip install -e ".[all_cpu_dev]"
  46. # advanced: for a machine **with** an NVIDIA GPU
  47. pip install -e ".[all_gpu_dev]"
  48. ```
  49. ## Unittest
  50. Launching the unittest for the whole repo:
  51. ```bash
  52. # install pytest
  53. pip install pytest
  54. # run pytest
  55. pytest
  56. ```
  57. Running your unittest code on your implemented module only:
  58. ```bash
  59. # install coverage
  60. pip install coverage
  61. cd /path/to/your/unittest/code
  62. # For example: cd tests/layers/
  63. # run unittest
  64. coverage run --source myproject.module -m unittest discover
  65. # For example: coverage run --source tensorlayer.layers -m unittest discover
  66. # generate html report
  67. coverage html
  68. ```
  69. ## Documentation
  70. Even though you follow [numpydoc](https://numpydoc.readthedocs.io/en/latest/) document style when writing your code,
  71. this does not ensure those lines appear on TensorLayer online documentation.
  72. You need further modify corresponding RST files in `docs/modules`.
  73. For example, to add your implemented new pooling layer into documentation, modify `docs/modules/layer.rst`. First, insert layer name under Layer list
  74. ```rst
  75. Layer list
  76. ----------
  77. .. autosummary::
  78. NewPoolingLayer
  79. ```
  80. Second, find pooling layer part and add:
  81. ```rst
  82. .. -----------------------------------------------------------
  83. .. Pooling Layers
  84. .. -----------------------------------------------------------
  85. Pooling Layers
  86. ------------------------
  87. New Pooling Layer
  88. ^^^^^^^^^^^^^^^^^^^^^^^^^^
  89. .. autoclass:: NewPoolingLayer
  90. ```
  91. Finally, test with local documentation:
  92. ```bash
  93. cd ./docs
  94. make clean
  95. make html
  96. # then view generated local documentation by ./html/index.html
  97. ```
  98. ## General intro to TensorLayer2
  99. * TensorLayer2 is built on [TensorFlow2](https://www.tensorflow.org/alpha), so TensorLayer2 is purely eager, no sessions, no globals.
  100. * TensorLayer2 supports APIs to build static models and dynamic models. Therefore, all `Layers` should be compatible with the two modes.
  101. ```python
  102. # An example of a static model
  103. # A static model has inputs and outputs with fixed shape.
  104. inputs = tl.layers.Input([32, 784])
  105. dense1 = tl.layers.Dense(n_units=800, act=tf.nn.relu, in_channels=784, name='dense1')(inputs)
  106. dense2 = tl.layers.Dense(n_units=10, act=tf.nn.relu, in_channels=800, name='dense2')(dense1)
  107. model = tl.models.Model(inputs=inputs, outputs=dense2)
  108. # An example of a dynamic model
  109. # A dynamic model has more flexibility. The inputs and outputs may be different in different runs.
  110. class CustomizeModel(tl.models.Model):
  111. def __init__(self):
  112. super(CustomizeModel, self).__init__()
  113. self.dense1 = tl.layers.Dense(n_units=800, act=tf.nn.relu, in_channels=784, name='dense1')
  114. self.dense2 = tl.layers.Dense(n_units=10, act=tf.nn.relu, in_channels=800, name='dense2')
  115. # a dynamic model allows more flexibility by customising forwarding.
  116. def forward(self, x, bar=None):
  117. d1 = self.dense1(x)
  118. if bar:
  119. return d1
  120. else:
  121. d2 = self.dense2(d1)
  122. return d1, d2
  123. model = CustomizeModel()
  124. ```
  125. * More examples can be found in [examples](examples/) and [tests/layers](tests/layers/). Note that not all of them are completed.
  126. ## How to contribute a new `Layer`
  127. * A `NewLayer` should be a derived from the base class [`Layer`](tensorlayer/layers/core.py).
  128. * Member methods to be overrided:
  129. - `__init__(self, args1, args2, inputs_shape=None, name=None)`: The constructor of the `NewLayer`, which should
  130. - Call `super(NewLayer, self).__init__(name)` to construct the base.
  131. - Define member variables based on the args1, args2 (or even more).
  132. - If the `inputs_shape` is provided, call `self.build(inputs_shape)` and set `self._built=True`. Note that sometimes only `in_channels` should be enough to build the layer like [`Dense`](tensorlayer/layers/dense/base_dense.py).
  133. - Logging by `logging.info(...)`.
  134. - `__repr__(self)`: Return a printable representation of the `NewLayer`.
  135. - `build(self, inputs_shape)`: Build the `NewLayer` by defining weights.
  136. - `forward(self, inputs, **kwargs)`: Forward feeding the `NewLayer`. Note that the forward feeding of some `Layers` may be different during training and testing like [`Dropout`](tensorlayer/layers/dropout.py).
  137. * Unittest:
  138. - Unittest should be done before a pull request. Unittest code can be written in [tests/](tests/)
  139. * Documents:
  140. - Please write a description for each class and method in RST format. The description may include the functionality, arguments, references, examples of the `NewLayer`.
  141. * Examples: [`Dense`](tensorlayer/layers/dense/base_dense.py), [`Dropout`](tensorlayer/layers/dropout.py), [`Conv`](tensorlayer/layers/convolution/simplified_conv.py).
  142. ## How to contribute a new `Model`
  143. * A `NewModel` should be derived from the base class [`Model`](tensorlayer/models/core.py) (if dynamic) or an instance of [`Model`](tensorlayer/models/core.py) (if static).
  144. * A static `NewModel` should have fixed inputs and outputs. Please check the example [`VGG_Static`](tensorlayer/models/vgg.py)
  145. * A dynamic `NewModel` has more flexiblility. Please check the example [`VGG16`](tensorlayer/models/vgg16.py)
  146. ## How to contribute a new example/tutorial
  147. * A new example/tutorial should implement a complete workflow of deep learning which includes (but not limited)
  148. - `Models` construction based on `Layers`.
  149. - Data processing and loading.
  150. - Training and testing.
  151. - Forward feeding by calling the models.
  152. - Loss function.
  153. - Back propagation by `tf.GradientTape()`.
  154. - Model saving and restoring.
  155. * Examples: [MNIST](examples/basic_tutorials/tutorial_mnist_mlp_static.py), [CIFAR10](examples/basic_tutorials/tutorial_cifar10_cnn_static.py), [FastText](examples/text_classification/tutorial_imdb_fasttext.py)

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