定义我的Keras模型时出错:“ AttributeError:'NoneType'对象没有属性'_inbound_nodes'”

时间:2019-01-26 10:50:48

标签: python-3.x tensorflow model keras

我正在尝试构建一个keras模型,在添加行以执行零填充然后合并2层之后出现了问题。代码如下:

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_)

这是错误消息:

  

回溯(最近通话最近):文件   “ /usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py”,   第2910行,在run_code中       exec(code_obj,self.user_global_ns,self.user_ns)文件“”,第1行,在       模型=模型(输入= [查询,文档],输出= CONTXT)文件“ /usr/local/lib/python3.5/dist-packages/keras/legacy/interfaces.py”,   第91行,在包装器中       返回func(* args,** kwargs)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   93,在 init 中       self._init_graph_network(* args,** kwargs)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   237,在_init_graph_network中       self.inputs,self.outputs)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1353,在_map_graph_network中       tensor_index = tensor_index)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1340,在build_map中       node_index,tensor_index)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1340,在build_map中       node_index,tensor_index)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1340,在build_map中       node_index,tensor_index)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1340,在build_map中       node_index,tensor_index)文件“ /usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行   1312,在build_map中       node = layer._inbound_nodes [node_index] AttributeError:'NoneType'对象没有属性'_inbound_nodes'

1 个答案:

答案 0 :(得分:2)

由于if条件而不能完全确定,但这可能会有所帮助:

在声明Keras model时传递的输入和输出层应通过Layer对象相互连接。在您的代码中情况并非如此:分配给K.reshapeword_level_padd的{​​{1}}操作不是contxt_padded实例。

您可以通过将整形操作包装在Layer层中来解决此问题:

Lambda

from keras.layers import Lambda contxt_padded = Lambda(lambda x: K.reshape(x))(...) 层允许包装在Lambda实例中对张量进行操作的任意函数,以将它们包括在模型中。