【问题标题】:Error while defining my Keras model: " AttributeError: 'NoneType' object has no attribute '_inbound_nodes' "定义我的 Keras 模型时出错:“AttributeError: 'NoneType' object has no attribute '_inbound_nodes'”
【发布时间】:2019-01-28 15:53:10
【问题描述】:

我正在尝试构建一个 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_)

这是错误信息:

Traceback(最近一次调用最后一次):文件 "/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,在 初始化 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章 tensor_index=tensor_index) 文件“/usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行 第1340章 node_index,tensor_index)文件“/usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行 第1340章 node_index,tensor_index)文件“/usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行 第1340章 node_index,tensor_index)文件“/usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行 第1340章 node_index,tensor_index)文件“/usr/local/lib/python3.5/dist-packages/keras/engine/network.py”,行 第1312章 node = layer._inbound_nodes[node_index] AttributeError: 'NoneType' object has no attribute '_inbound_nodes'

【问题讨论】:

  • 你的代码在哪里定义了querydoc
  • 抱歉,我刚刚添加了缺少的声明。

标签: python-3.x tensorflow model keras


【解决方案1】:

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

在声明 Keras model 时传递的输入和输出层应该通过 Layer 对象相互连接。您的代码中并非如此:分配给 word_level_paddcontxt_paddedK.reshape 操作不是 Layer 实例。

您可以通过将重塑操作包装在Lambda 层中来解决此问题:

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

Lambda 层允许包装在Layer 实例中对张量进行操作的任意函数,以将它们包含在您的模型中。

【讨论】:

  • 它有效。我按照您的示例修改了Reshape 层,如下所示:word_level_padd = Lambda(lambda x: K.reshape(ZeroPadding1D((0, contxt.shape[2] -x.shape[2]))(K.reshape(x, (-1, x.shape[2], x.shape[1]))), (-1, x.shape[1], contxt.shape[2])) if x.shape[-1] &lt; contxt.shape[-1] else x)(word_level)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-12-15
  • 2017-10-20
  • 2017-03-08
  • 1970-01-01
  • 2021-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多