Skip to content
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

Bug fix: Models with shared layers shouldn't be considered Sequential like #8025

Merged
merged 3 commits into from
Oct 2, 2017
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
18 changes: 17 additions & 1 deletion keras/utils/layer_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,28 @@ def print_summary(model, line_length=None, positions=None, print_fn=print):
sequential_like = True
else:
sequential_like = True
for v in model.nodes_by_depth.values():
nodes_by_depth = model.nodes_by_depth.values()
nodes = []
for v in nodes_by_depth:
if (len(v) > 1) or (len(v) == 1 and len(v[0].inbound_layers) > 1):
# if the model has multiple nodes or if the nodes have multiple inbound_layers
# the model is no longer sequential
sequential_like = False
break
nodes += v
if sequential_like:
# search for shared layers
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't get the criteria you use for shared layers. A comment here would help

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A layer is shared in a model if it has 2 or more inbound nodes and those inbound nodes are part of the graph.

for layer in model.layers:
flag = False
for node in layer.inbound_nodes:
if node in nodes:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't this always be True?

Copy link
Contributor Author

@farizrahman4u farizrahman4u Sep 30, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessarily. A layer might be shared between 2 models and hence have more than one inbound node. But we are looking for layers which are being shared within the same model. The inbound nodes should be part of the graph of the model for which we are printing the summary.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dense = Dense(10)
a = Input((10,))
b = dense(a)
model1 = Model(a, b)

a2 = Input((10,))
b2 = dense(a2)
model2 = Model(a2, b2)

model1.summary()  
# Here dense layer has 2 inbound nodes, 
#but the second node is not part of model1.
# So should be considered sequential like

if flag:
sequential_like = False
break
else:
flag = True
if not sequential_like:
break

if sequential_like:
line_length = line_length or 65
Expand Down