Skip to content

Commit 2885e27

Browse files
committed
[layers] get_layers_with_name
1 parent d66d07f commit 2885e27

File tree

2 files changed

+27
-0
lines changed

2 files changed

+27
-0
lines changed

docs/modules/layers.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ Layer list
258258
.. autosummary::
259259

260260
get_variables_with_name
261+
get_layers_with_name
261262
set_name_reuse
262263
print_all_variables
263264
initialize_global_variables
@@ -340,6 +341,10 @@ Get variables with name
340341
^^^^^^^^^^^^^^^^^^^^^^^^^^
341342
.. autofunction:: get_variables_with_name
342343

344+
Get layers with name
345+
^^^^^^^^^^^^^^^^^^^^^^^^^^
346+
.. autofunction:: get_layers_with_name
347+
343348
Enable layer name reuse
344349
^^^^^^^^^^^^^^^^^^^^^^^^^
345350
.. autofunction:: set_name_reuse

tensorlayer/layers.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,28 @@ def get_variables_with_name(name, train_only=True, printable=False):
181181
print(" got {:3}: {:15} {}".format(idx, v.name, str(v.get_shape())))
182182
return d_vars
183183

184+
def get_layers_with_name(network=None, name="", printable=False):
185+
"""Get layer list by a given name scope.
186+
187+
Examples
188+
---------
189+
>>> layers = tl.layers.get_layers_with_name(network, "CNN", True)
190+
"""
191+
assert network is not None
192+
print(" [*] geting layers with %s" % name)
193+
194+
layers = []
195+
i = 0
196+
for layer in network.all_layers:
197+
# print(type(layer.name))
198+
if name in layer.name:
199+
layers.append(layer)
200+
if printable:
201+
# print(layer.name)
202+
print(" got {:3}: {:15} {}".format(i, layer.name, str(layer.get_shape())))
203+
i = i + 1
204+
return layers
205+
184206
def list_remove_repeat(l=None):
185207
"""Remove the repeated items in a list, and return the processed list.
186208
You may need it to create merged layer like Concat, Elementwise and etc.

0 commit comments

Comments
 (0)