Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

unable to build model as backend.squeeze has no layer

I am trying to build a model where I have a tensor that has to be squeezed and then fed into an LSTM.

The model does not compile as the squeezed tensor does not have a layer attribute.

Using TensorFlow backend.
Traceback (most recent call last):
  File "C:/workspace/keras_test/src/testing.py", line 10, in <module>
    model = Model(inputs=model_in, outputs=output)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\legacy\interfaces.py", line 91, in wrapper
    return func(*args, **kwargs)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 93, in __init__
    self._init_graph_network(*args, **kwargs)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 237, in _init_graph_network
    self.inputs, self.outputs)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1353, in _map_graph_network
    tensor_index=tensor_index)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1340, in build_map
    node_index, tensor_index)
  File "E:\ProgramData\Miniconda3\envs\py37\lib\site-packages\keras\engine\network.py", line 1312, in build_map
    node = layer._inbound_nodes[node_index]
AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

For a minimum example see:

from keras import Input, backend, Model
from keras.layers import LSTM, Dense

input_shape = (128, 1, 1)
model_in = Input(tensor=Input(input_shape), shape=input_shape)
squeezed = backend.squeeze(model_in, 2)
hidden1 = LSTM(10)(squeezed)
output = Dense(1, activation='sigmoid')(hidden1)

model = Model(inputs=model_in, outputs=output)
model.summary()

How can I remove one dimension of model_in without losing the layer information?

like image 524
HennyKo Avatar asked Sep 17 '18 10:09

HennyKo


1 Answers

The backend operation squeeze is not wrapped within a Lambda layer, so the resulting tensor is not a Keras tensor. As a consequence, it lacks some attributes such as _inbound_nodes. You could wrap the squeeze operation as follows:

from keras import Input, backend, Model
from keras.layers import LSTM, Dense, Lambda

input_shape = (128, 1, 1)
model_in = Input(tensor=Input(input_shape), shape=input_shape)
squeezed = Lambda(lambda x: backend.squeeze(x, 2))(model_in)
hidden1 = LSTM(10)(squeezed)
output = Dense(1, activation='sigmoid')(hidden1)

model = Model(inputs=model_in, outputs=output)
model.summary()
like image 193
rvinas Avatar answered Nov 18 '22 01:11

rvinas