8000 Activation layers inherit rank from source by dguest · Pull Request #99 · lwtnn/lwtnn · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Activation layers inherit rank from source #99

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 4, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 24 additions & 2 deletions converters/kerasfunc2json.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ def _run():
input_layer_arch = arch['config']['input_layers']
nodes = _build_node_list(node_dict, input_layer_arch)

# deal with the nodes that need a type based on their source
_resolve_inheriting_types(nodes)

vars_per_input = _get_vars_per_input(input_layer_arch, node_dict)
out_dict = {
'layers': layers, 'nodes': nodes,
Expand All @@ -61,6 +64,21 @@ def _run():
}
print(json.dumps(out_dict, indent=2, sort_keys=True))

def _resolve_inheriting_types(nodes):
"""
Sometimes we're not sure what the type of node it is when we parse
the graph. In these cases we have to inhrit from the source node.
"""
sequence_types = {'sequence', 'input_sequences','time_distributed'}
for node in nodes:
if node['type'] == 'INHERIT_FROM_SOURCE':
assert len(node['sources']) == 1
parent_type = nodes[node['sources'][0]]['type']
if parent_type in sequence_types:
node['type'] = 'time_distributed'
else:
node['type'] = 'feed_forward'

def _check_version(arch):
if arch["class_name"] != "Model":
sys.exit("this is not a graph, try using keras2json")
Expand Down Expand Up @@ -297,7 +315,9 @@ def _build_layer(output_layers, node_key, h5, node_dict, layer_dict):
'concatenate': 'concatenate', # <- v2
'inputlayer': 'input',
'dense': 'feed_forward',
'activation': 'feed_forward',
# we don't know what type of node based on the activation type
# instead we tell the node to inherit from its source
'activation': 'INHERIT_FROM_SOURCE',
'lstm': 'sequence',
'gru': 'sequence',
'sum': 'sum',
Expand All @@ -312,6 +332,8 @@ def _build_node_list(node_dict, input_layer_arch):
no effort is made to sort this list in any way, but the ordering
is important because each node contains indices for other nodes
"""
nodes_with_layers = {
'feed_forward', 'sequence', 'time_distributed', 'INHERIT_FROM_SOURCE'}
node_list = []
input_map = {}
for kname, kid, ks in input_layer_arch:
Expand All @@ -331,7 +353,7 @@ def _build_node_list(node_dict, input_layer_arch):
out_node['size'] = node.n_outputs
if node.dims > 1:
out_node['type'] = 'input_sequence'
elif node_type in ['feed_forward', 'sequence', 'time_distributed']:
elif node_type in nodes_with_layers:
out_node['layer_index'] = node.layer_number
node_list.append(out_node)
return node_list
Expand Down
0