1
votes

I'm trying to build a keras model, the problem appeared after adding lines to perform zero-padding then merge 2 layers. The code is as follows:

import keras
from keras.layers import *
from keras.models import Model
from keras.activations import softmax
import keras.backend as K

query = Input(name='query', shape=(10,))
doc = Input(name='doc', shape=(100,))
embedding = Embedding(1125, 50, trainable=True)
q_embed = embedding(query)
d_embed = embedding(doc)
q_w = Dense(1, use_bias=False)(q_embed)
q_w = Lambda(lambda x: softmax(x, axis=1), output_shape=(10,))(q_w)
q_w_layer = Lambda(lambda x: K.repeat_elements(q_w, rep=50, axis=2))(q_w)
q_embed = Multiply()([q_w_layer, q_embed])
cross = Dot(axes=[2, 2], normalize=False)([q_embed, d_embed])
cross = Permute((2, 1))(cross)
contxt = Conv1D(30, 5, strides=5, activation='relu', name="conv")(cross)
contxt = BatchNormalization()(contxt)
contxt = Dropout(0.2)(contxt)
attention = Dense(1, use_bias=False)(contxt)
attention = Activation('softmax')(attention)
contxt = Multiply()([contxt, attention])
important_context = MaxPooling1D(pool_size=2, strides=2)
contxt = important_context(contxt)
word_level = Permute((2, 1))(cross)

# ############ This is the part that caused the problem

word_level_padd = K.reshape(ZeroPadding1D((0, contxt.shape[2] - word_level.shape[2]))(K.reshape(word_level,
                                (-1, 
                                 word_level.shape[2], 
                                 word_level.shape[1]
                                ))),
                           (-1, word_level.shape[1], contxt.shape[2])
                           ) if word_level.shape[-1] < contxt.shape[-1] else word_level
contxt_padded = K.reshape(ZeroPadding1D((0, word_level.shape[2] -contxt.shape[2]))(K.reshape(contxt,
                            (-1, 
                             contxt.shape[2], 
                             contxt.shape[1]
                           ))), 
                         (-1, contxt.shape[1], word_level.shape[2])
                         ) if contxt.shape[-1] < word_level.shape[-1] else contxt
contxt = Concatenate(axis=1, name="merge_levels")([word_level_padd, contxt_padded])

# This is the part that caused the problem #############

lstm_units = int(contxt.shape[1])
contxt = Bidirectional(LSTM(lstm_units, return_sequences=False))(contxt)
contxt = BatchNormalization()(contxt)
contxt = Dropout(0.2)(contxt)
out_ = Dense(1)(contxt)
model = Model(inputs=[query, doc], outputs=out_)

Here is the error message:

Traceback (most recent call last): File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2910, in run_code exec(code_obj, self.user_global_ns, self.user_ns) File "", line 1, in model = Model(inputs=[query, doc], outputs=contxt) File "/usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py", line 91, in wrapper return func(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 93, in init self._init_graph_network(*args, **kwargs) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 237, in _init_graph_network self.inputs, self.outputs) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1353, in _map_graph_network tensor_index=tensor_index) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1340, in build_map node_index, tensor_index) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1340, in build_map node_index, tensor_index) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1340, in build_map node_index, tensor_index) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1340, in build_map node_index, tensor_index) File "/usr/local/lib/python3.5/dist-packages/keras/engine/network.py", line 1312, in build_map node = layer._inbound_nodes[node_index] AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

1
Where in your code are query and doc defined?sdcbr
Sorry, I've just added the missing declarations.Belkacem Thiziri

1 Answers

2
votes

Not completely sure because of the if-conditions, but this might help:

The input and output layers that you pass when declaring a Keras model should be connected to each other by means of Layer objects. This is not the case in your code: the K.reshape operation that you assign to word_level_padd and contxt_padded are no Layer instances.

You can solve this by wrapping the reshape operation in a Lambda layer:

from keras.layers import Lambda
contxt_padded = Lambda(lambda x: K.reshape(x))(...)

A Lambda layers allows to wrap arbitrary functions that operate on tensors in a Layer instance to include them in your model.