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

Something wrong with output_shape in merge layer #2855

Closed
henry0312 opened this issue May 31, 2016 · 8 comments
Closed

Something wrong with output_shape in merge layer #2855

henry0312 opened this issue May 31, 2016 · 8 comments

Comments

@henry0312
Copy link
Contributor

Sample code: https://gist.github.com/henry0312/9c1aee0a4330c860ff7b2bb139acbb15

Run: python gistfile1.py

Then:

Traceback (most recent call last):
  File "gistfile1.py", line 58, in <module>
    model, net = create_model()
  File "gistfile1.py", line 32, in create_model
    output = Dense(10, activation='sigmoid')(merged_vector)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 458, in __call__
    self.build(input_shapes[0])
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/layers/core.py", line 589, in build
    assert len(input_shape) == 2
AssertionError
@henry0312
Copy link
Contributor Author

maybe related to #2854 and #2818

@fchollet
Copy link
Collaborator

There was an issue with the Merge layer, which is that output_shape was expecting the full shape (including the batch size). This was inconsistent with the Lambda layer, so I change the Merge layer to have the right behavior. This is pushed.

There is another issue with your snippet: you are sharing a BatchNormalization layer, which is impossible. As a fix, you could use mode=2 in BatchNorm.

@fchollet
Copy link
Collaborator

Your snippet works with me after the latest fix (and changing the BatchNorm mode to 2).

@henry0312
Copy link
Contributor Author

thanks!
I'll check later.

2016年5月31日火曜日、François [email protected]さんは書きました:

Your snippet works with me after the latest fix (and changing the
BatchNorm mode to 2).


You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
#2855 (comment),
or mute the thread
https://github.com/notifications/unsubscribe/AAadGtNBtj2LToUE1ZQChO6NheuU5BMKks5qG7QXgaJpZM4IqKUr
.

@henry0312
Copy link
Contributor Author

91b9302 fixed the issue, but another issue occurs 😢

sample code: https://gist.github.com/henry0312/0007239173035d8bc5725647a2f2a552
Run: python keras_import_bug.py
(Note that https://gist.github.com/henry0312/9c1aee0a4330c860ff7b2bb139acbb15 will create my_model_architecture.json)

Then:

Traceback (most recent call last):
  File "keras_import_bug.py", line 2, in <module>
    model = model_from_json(open('my_model_architecture.json').read())
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/models.py", line 36, in model_from_json
    return layer_from_config(config, custom_objects=custom_objects)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/utils/layer_utils.py", line 35, in layer_from_config
    return layer_class.from_config(config['config'])
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 2238, in from_config
    process_layer(layer_data)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 2235, in process_layer
    layer(input_tensors)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 1267, in __call__
    self.add_inbound_node(layers, node_indices, tensor_indices)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 543, in add_inbound_node
    Node.create_node(self, inbound_layers, node_indices, tensor_indices)
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 155, in create_node
    output_shapes = to_list(outbound_layer.get_output_shape_for(input_shapes))
  File "/usr/local/var/pyenv/versions/3.5.1/lib/python3.5/site-packages/Keras-1.0.3-py3.5.egg/keras/engine/topology.py", line 1282, in get_output_shape_for
    return (input_shape[0],) + self._output_shape
TypeError: can only concatenate tuple (not "list") to tuple

@henry0312
Copy link
Contributor Author

Maybe, this will fix.

diff --git a/keras/engine/topology.py b/keras/engine/topology.py
index a5e147d..82bed08 100644
--- a/keras/engine/topology.py
+++ b/keras/engine/topology.py
@@ -1279,7 +1279,7 @@ class Merge(Layer):
                 output_shape = self._output_shape(input_shape)
                 return output_shape
             elif self._output_shape is not None:
-                return (input_shape[0],) + self._output_shape
+                return (input_shape[0],) + tuple(self._output_shape)
             else:
                 # TODO: consider shape auto-inference with TF
                 raise Exception('The Merge layer ' + self.name +

@fchollet
Copy link
Collaborator

Right. I fixed it. This is because JSON deserialization converts all tuples to lists.

@henry0312
Copy link
Contributor Author

This is because JSON deserialization converts all tuples to lists.

I see.
I confirmed 80bfec7 fixed the issue.

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants