Skip to content

Commit

Permalink
add tutorial LayerList
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Lai authored and Eric Lai committed Jul 6, 2021
1 parent 339fb03 commit 81e6c21
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 3 deletions.
34 changes: 34 additions & 0 deletions examples/basic_tutorials/tutorial_LayerList.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#! /usr/bin/python
# -*- coding: utf-8 -*-

from tensorlayer.layers import Module, LayerList, Dense
import tensorlayer as tl

d1 = Dense(n_units=800, act=tl.ReLU, in_channels=784, name='Dense1')
d2 = Dense(n_units=800, act=tl.ReLU, in_channels=800, name='Dense2')
d3 = Dense(n_units=10, act=tl.ReLU, in_channels=800, name='Dense3')

layer_list = LayerList([d1, d2])
# Inserts a given d2 before a given index in the list
layer_list.insert(1, d2)
layer_list.insert(2, d2)
# Appends d2 from a Python iterable to the end of the list.
layer_list.extend([d2])
# Appends a given d3 to the end of the list.
layer_list.append(d3)

print(layer_list)

class model(Module):
def __init__(self):
super(model, self).__init__()
self._list = layer_list
def forward(self, inputs):
output = self._list[0](inputs)
for i in range(1, len(self._list)):
output = self._list[i](output)
return output

net = model()
print(net)
print(net(tl.layers.Input((10, 784))))
5 changes: 2 additions & 3 deletions tensorlayer/layers/core/core_tensorflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,10 +678,9 @@ class LayerList(Module):
Examples:
"""
def __init__(self, *args, **kwargs):
def __init__(self, args):
super(LayerList, self).__init__()
if len(args) == 1:
self.extend(args[0])
self.extend(args)

def __getitem__(self, index):
if isinstance(index, slice):
Expand Down

0 comments on commit 81e6c21

Please sign in to comment.