【发布时间】: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'
【问题讨论】:
-
你的代码在哪里定义了
query和doc? -
抱歉,我刚刚添加了缺少的声明。
标签: python-3.x tensorflow model keras