MCPcopy Create free account
hub / github.com/espnet/espnet / get_layer

Function get_layer

espnet2/torch_utils/get_layer_from_string.py:6–43  ·  view source on GitHub ↗

Return layer object handler from library e.g. from torch.nn E.g. if l_name=="elu", returns torch.nn.ELU. Args: l_name (string): Case insensitive name for layer in library (e.g. .'elu'). library (module): Name of library/module where to search for object handler with

(l_name, library=torch.nn)

Source from the content-addressed store, hash-verified

4
5
6def get_layer(l_name, library=torch.nn):
7 """Return layer object handler from library e.g. from torch.nn
8
9 E.g. if l_name=="elu", returns torch.nn.ELU.
10
11 Args:
12 l_name (string): Case insensitive name for layer in library (e.g. .'elu').
13 library (module): Name of library/module where to search for object handler
14 with l_name e.g. "torch.nn".
15
16 Returns:
17 layer_handler (object): handler for the requested layer e.g. (torch.nn.ELU)
18
19 """
20
21 all_torch_layers = [x for x in dir(torch.nn)]
22 match = [x for x in all_torch_layers if l_name.lower() == x.lower()]
23 if len(match) == 0:
24 close_matches = difflib.get_close_matches(
25 l_name, [x.lower() for x in all_torch_layers]
26 )
27 raise NotImplementedError(
28 "Layer with name {} not found in {}.\n Closest matches: {}".format(
29 l_name, str(library), close_matches
30 )
31 )
32 elif len(match) > 1:
33 close_matches = difflib.get_close_matches(
34 l_name, [x.lower() for x in all_torch_layers]
35 )
36 raise NotImplementedError(
37 "Multiple matchs for layer with name {} not found in {}.\n "
38 "All matches: {}".format(l_name, str(library), close_matches)
39 )
40 else:
41 # valid
42 layer_handler = getattr(library, match[0])
43 return layer_handler

Callers 6

__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90
__init__Method · 0.90

Calls 1

formatMethod · 0.80

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…