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.

get_start_model.rst 6.2 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. .. _getstartmodel:
  2. ===============
  3. Define a model
  4. ===============
  5. TensorLayer provides two ways to define a model.
  6. Sequential model allows you to build model in a fluent way while dynamic model allows you to fully control the forward process.
  7. Sequential model
  8. ===============
  9. .. code-block:: python
  10. from tensorlayer.layers import SequentialLayer
  11. from tensorlayer.layers import Dense
  12. import tensorlayer as tl
  13. def get_model():
  14. layer_list = []
  15. layer_list.append(Dense(n_units=800, act=tl.ReLU, in_channels=784, name='Dense1'))
  16. layer_list.append(Dense(n_units=800, act=tl.ReLU, in_channels=800, name='Dense2'))
  17. layer_list.append(Dense(n_units=10, act=tl.ReLU, in_channels=800, name='Dense3'))
  18. MLP = SequentialLayer(layer_list)
  19. return MLP
  20. Dynamic model
  21. =======================
  22. In this case, you need to manually input the output shape of the previous layer to the new layer.
  23. .. code-block:: python
  24. import tensorlayer as tl
  25. from tensorlayer.layers import Module
  26. from tensorlayer.layers import Dropout, Dense
  27. class CustomModel(Module):
  28. def __init__(self):
  29. super(CustomModel, self).__init__()
  30. self.dropout1 = Dropout(keep=0.8)
  31. self.dense1 = Dense(n_units=800, act=tl.ReLU, in_channels=784)
  32. self.dropout2 = Dropout(keep=0.8)
  33. self.dense2 = Dense(n_units=800, act=tl.ReLU, in_channels=800)
  34. self.dropout3 = Dropout(keep=0.8)
  35. self.dense3 = Dense(n_units=10, act=None, in_channels=800)
  36. def forward(self, x, foo=False):
  37. z = self.dropout1(x)
  38. z = self.dense1(z)
  39. z = self.dropout2(z)
  40. z = self.dense2(z)
  41. z = self.dropout3(z)
  42. out = self.dense3(z)
  43. if foo:
  44. out = tf.nn.softmax(out)
  45. return out
  46. MLP = CustomModel()
  47. MLP.set_eval()
  48. outputs = MLP(data, foo=True) # controls the forward here
  49. outputs = MLP(data, foo=False)
  50. Dynamic model do not manually input the output shape
  51. =======================
  52. In this case, you do not manually input the output shape of the previous layer to the new layer.
  53. .. code-block:: python
  54. import tensorlayer as tl
  55. from tensorlayer.layers import Module
  56. from tensorlayer.layers import Dropout, Dense
  57. class CustomModel(Module):
  58. def __init__(self):
  59. super(CustomModel, self).__init__()
  60. self.dropout1 = Dropout(keep=0.8)
  61. self.dense1 = Dense(n_units=800, act=tl.ReLU)
  62. self.dropout2 = Dropout(keep=0.8)
  63. self.dense2 = Dense(n_units=800, act=tl.ReLU)
  64. self.dropout3 = Dropout(keep=0.8)
  65. self.dense3 = Dense(n_units=10, act=None)
  66. def forward(self, x, foo=False):
  67. z = self.dropout1(x)
  68. z = self.dense1(z)
  69. z = self.dropout2(z)
  70. z = self.dense2(z)
  71. z = self.dropout3(z)
  72. out = self.dense3(z)
  73. if foo:
  74. out = tf.nn.softmax(out)
  75. return out
  76. MLP = CustomModel()
  77. MLP.init_build(tl.layers.Input(shape=(1, 784))) # init_build must be called to initialize the weights.
  78. MLP.set_eval()
  79. outputs = MLP(data, foo=True) # controls the forward here
  80. outputs = MLP(data, foo=False)
  81. Switching train/test modes
  82. =============================
  83. .. code-block:: python
  84. # method 1: switch before forward
  85. MLP.set_train() # enable dropout, batch norm moving avg ...
  86. output = MLP(train_data)
  87. ... # training code here
  88. Model.set_eval() # disable dropout, batch norm moving avg ...
  89. output = MLP(test_data)
  90. ... # testing code here
  91. # method 2: Using packaged training modules
  92. model = tl.models.Model(network=MLP, loss_fn=tl.cost.softmax_cross_entropy_with_logits, optimizer=optimizer)
  93. model.train(n_epoch=n_epoch, train_dataset=train_ds)
  94. Reuse weights
  95. =======================
  96. For dynamic model, call the layer multiple time in forward function
  97. .. code-block:: python
  98. import tensorlayer as tl
  99. from tensorlayer.layers import Module, Dense, Concat
  100. class MyModel(Module):
  101. def __init__(self):
  102. super(MyModel, self).__init__()
  103. self.dense_shared = Dense(n_units=800, act=tl.ReLU, in_channels=784)
  104. self.dense1 = Dense(n_units=10, act=tl.ReLU, in_channels=800)
  105. self.dense2 = Dense(n_units=10, act=tl.ReLU, in_channels=800)
  106. self.cat = Concat()
  107. def forward(self, x):
  108. x1 = self.dense_shared(x) # call dense_shared twice
  109. x2 = self.dense_shared(x)
  110. x1 = self.dense1(x1)
  111. x2 = self.dense2(x2)
  112. out = self.cat([x1, x2])
  113. return out
  114. model = MyModel()
  115. Print model information
  116. =======================
  117. .. code-block:: python
  118. print(MLP) # simply call print function
  119. # Model(
  120. # (_inputlayer): Input(shape=[None, 784], name='_inputlayer')
  121. # (dropout): Dropout(keep=0.8, name='dropout')
  122. # (dense): Dense(n_units=800, relu, in_channels='784', name='dense')
  123. # (dropout_1): Dropout(keep=0.8, name='dropout_1')
  124. # (dense_1): Dense(n_units=800, relu, in_channels='800', name='dense_1')
  125. # (dropout_2): Dropout(keep=0.8, name='dropout_2')
  126. # (dense_2): Dense(n_units=10, None, in_channels='800', name='dense_2')
  127. # )
  128. Get specific weights
  129. =======================
  130. We can get the specific weights by indexing or naming.
  131. .. code-block:: python
  132. # indexing
  133. all_weights = MLP.all_weights
  134. some_weights = MLP.all_weights[1:3]
  135. Save and restore model
  136. =======================
  137. We provide two ways to save and restore models
  138. Save weights only
  139. ------------------
  140. .. code-block:: python
  141. MLP.save_weights('./model_weights.npz') # by default, file will be in hdf5 format
  142. MLP.load_weights('./model_weights.npz')
  143. Save model weights (optional)
  144. -----------------------------------------------
  145. .. code-block:: python
  146. # When using packaged training modules. Saving and loading the model can be done as follows
  147. model = tl.models.Model(network=MLP, loss_fn=tl.cost.softmax_cross_entropy_with_logits, optimizer=optimizer)
  148. model.train(n_epoch=n_epoch, train_dataset=train_ds)
  149. model.save_weights('./model.npz', format='npz_dict')
  150. model.load_weights('./model.npz', format='npz_dict')

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