keras icon indicating copy to clipboard operation
keras copied to clipboard

Use outputs dictionary keys as output_names if provided

Open szleb opened this issue 3 years ago • 7 comments

This PR is meant to resolve issue #16405.

This implementation is the first solution mentioned in https://github.com/keras-team/keras/issues/16405#issuecomment-1102255538. You are still invited to discuss other solutions.

The implementation relies on the behavior of tf.nest.flatten when model._output_layers are created. Particularly, on the ordering, which is the sorted order of the dictionary keys. An alternative might be to search each layer from model._output_layers in the nested structure and then select its name. That strategy seems more complicated but would be independent of the way how model._output_layers is created.

szleb avatar Apr 28 '22 12:04 szleb

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

For more information, open the CLA check for this pull request.

google-cla[bot] avatar Apr 28 '22 12:04 google-cla[bot]

We can't change the behavior on this. This is potentially confusing though, so a warning would be great!

LukeWood avatar Apr 28 '22 17:04 LukeWood

Thanks for the PR. Yes, so it would be inconsistent to use Input.name as the input name for every situation (list of Inputs, tuples of inputs...) except the dict case. We should always use Input.name, even in the dict case.

Now, it's certainly true that if you use dict keys that differ from Input.name, then "what is the name of my input?" becomes an ambiguous question. Users should make sure to use consistent input names and dict keys in order to avoid any confusion. Let's simply add a warning in case of mismatch.

fchollet avatar Apr 28 '22 20:04 fchollet

I thought the purpose of passing dicts is to overwrite the names. So if I pass a dict with different keys than layer names, the data dictionary I pass to fit or predict use that keys. If I pass a list or tuple the data dict keys are the layer names. So that is just the same "inconsistency", I guess. Problems also appear when I nest multi output models. Then I do not know which output comes from which layer inside the nested models. (see the code examples in the issue)

szleb avatar Apr 29 '22 06:04 szleb

If you keep two sets of names to refer to the same objects, that's on you. The framework cannot read your mind to determine which set you intended to use. Hence why it would be helpful to introduce a warning to force the user to keep a single consistent set of names to refer to Input objects.

fchollet avatar Apr 29 '22 23:04 fchollet

@szleb Any update on this PR? Please. Thank you!

gbaned avatar May 06 '22 11:05 gbaned

@szleb Any update on this PR? Please. Thank you!

gbaned avatar Aug 05 '22 07:08 gbaned

Hi @szleb Any update on this PR? Please. Thank you!

gbaned avatar Dec 16 '22 13:12 gbaned

Hi @szleb I'm going to go ahead and close this PR, because it seems to have stalled. If you're still interested in pursing this (and responding to my comments), please feel free to reopen! Thank you!

gbaned avatar Mar 21 '23 17:03 gbaned