Sequential

class paddle.nn.Sequential ( \layers* ) [源代码]

顺序容器。子Layer将按构造函数参数的顺序添加到此容器中。传递给构造函数的参数可以Layers或可迭代的name Layer元组。

参数:

  • layers (tuple) - Layers或可迭代的name Layer对。

返回:无

代码示例

  1. import paddle
  2. import numpy as np
  3. data = np.random.uniform(-1, 1, [30, 10]).astype('float32')
  4. data = paddle.to_tensor(data)
  5. # create Sequential with iterable Layers
  6. model1 = paddle.nn.Sequential(
  7. paddle.nn.Linear(10, 1), paddle.nn.Linear(1, 2)
  8. )
  9. model1[0] # access the first layer
  10. res1 = model1(data) # sequential execution
  11. # create Sequential with name Layer pairs
  12. model2 = paddle.nn.Sequential(
  13. ('l1', paddle.nn.Linear(10, 2)),
  14. ('l2', paddle.nn.Linear(2, 3))
  15. )
  16. model2['l1'] # access l1 layer
  17. model2.add_sublayer('l3', paddle.nn.Linear(3, 3)) # add sublayer
  18. res2 = model2(data) # sequential execution