定义我的Keras模型时出错:" AttributeError:"NoneType"对象没有属性"_inbound_nodes" [英] Error while defining my Keras model: " AttributeError: 'NoneType' object has no attribute '_inbound_nodes' "

查看:130
本文介绍了定义我的Keras模型时出错:" AttributeError:"NoneType"对象没有属性"_inbound_nodes"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这是错误消息:

回溯(最近通话最近):文件 "/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,张量索引)文件"/usr/local/lib/python3.5/dist-packages/keras/engine/network.py",行 1340,在build_map中 node_index,张量索引)文件"/usr/local/lib/python3.5/dist-packages/keras/engine/network.py",行 1340,在build_map中 node_index,张量索引)文件"/usr/local/lib/python3.5/dist-packages/keras/engine/network.py",行 1340,在build_map中 node_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'

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'

推荐答案

由于使用if条件,因此无法完全确定,但这可能会有所帮助:

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

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

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.

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

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))(...)

Lambda层允许包装对Layer实例中的张量进行操作的任意函数,以将其包括在模型中.

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

这篇关于定义我的Keras模型时出错:&quot; AttributeError:"NoneType"对象没有属性"_inbound_nodes"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆