我想按以下方法制作模型。
输入数据输入数据输入数据
| | |
conv_in1 conv_in2 conv_in3
| | |
maxpooling maxpooling maxpooling
- Dense layer - - Dense layer -
| |
Dense layer Dense layer
|
Dense layer
我正在使用以下代码。
kernel_stmt = []
kernel_pos = []
kernel_dep = []
use_pos=False
use_meta=True
use_dep=True
statement_input = Input(shape=(num_steps,), dtype='int32', name='main_input')
x_stmt = Embedding(vocab_length+1,EMBED_DIM,weights=[embedding_matrix],input_length=num_steps,trainable=False)(statement_input)
# pos embed LSTM
pos_input = Input(shape=(num_steps,), dtype='int32', name='pos_input')
x_pos = Embedding(max(pos_dict.values()), max(pos_dict.values()), weights=[pos_embeddings], input_length=num_steps, trainable=False)(pos_input)
# dep embed LSTM
dep_input = Input(shape=(num_steps,), dtype='int32', name='dep_input')
x_dep = Embedding(max(dep_dict.values()), max(dep_dict.values()), weights=[dep_embeddings], input_length=num_steps, trainable=False)(dep_input)
for kernel in kernel_sizes:
x_1 = Conv1D(filters=filter_size,kernel_size=kernel)(x_stmt)
x_1 = GlobalMaxPool1D()(x_1)
kernel_stmt.append(x_1)
x_2 = Conv1D(filters=filter_size,kernel_size=kernel)(x_pos)
x_2 = GlobalMaxPool1D()(x_2)
kernel_pos.append(x_2)
x_3 = Conv1D(filters=filter_size,kernel_size=kernel)(x_dep)
x_3 = GlobalMaxPool1D()(x_3)
kernel_dep.append(x_3)
conv_in1 = keras.layers.concatenate(kernel_stmt)
conv_in1 = Dropout(0.6)(conv_in1)
conv_in1 = Dense(128, activation='relu')(conv_in1)
conv_in2 = keras.layers.concatenate(kernel_pos)
conv_in2 = Dropout(0.6)(conv_in2)
conv_in2 = Dense(128, activation='relu')(conv_in2)
conv_in3 = keras.layers.concatenate(kernel_dep)
conv_in3 = Dropout(0.6)(conv_in3)
conv_in3 = Dense(128, activation='relu')(conv_in3)
# meta data
meta_input = Input(shape=(X_train_meta.shape[1],), name='aux_input')
x_meta = Dense(64, activation='relu')(meta_input)
if use_pos and use_meta:
if use_dep:
x = keras.layers.concatenate([conv_in1, conv_in2, conv_in3, x_meta])
else:
x = keras.layers.concatenate([conv_in1, conv_in2, x_meta])
elif use_meta:
if use_dep:
x = keras.layers.concatenate([conv_in1, conv_in3, x_meta])
else:
x = keras.layers.concatenate([conv_in1, x_meta])
elif use_pos:
if use_dep:
x = keras.layers.concatenate([conv_in1, conv_in2, conv_in3])
else:
x = keras.layers.concatenate([conv_in1, conv_in2])
else:
if use_dep:
x = keras.layers.concatenate([conv_in1, conv_in3])
else:
x = conv_in1
main_output = Dense(6, activation='softmax', name='main_output')(x)
if use_pos and use_meta:
if use_dep:
model_cnn = Model(inputs=[statement_input, pos_input, dep_input, meta_input], outputs=[main_output])
else:
model_cnn = Model(inputs=[statement_input, pos_input, meta_input], outputs=[main_output])
elif use_meta:
if use_dep:
model_cnn = Model(inputs=[statement_input, dep_input, meta_input], outputs=[main_output])
else:
model_cnn = Model(inputs=[statement_input, meta_input], outputs=[main_output])
elif use_pos:
if use_dep:
model_cnn = Model(inputs=[statement_input, pos_input, dep_input], outputs=[main_output])
else:
model_cnn = Model(inputs=[statement_input, pos_input], outputs=[main_output])
else:
if use_dep:
model_cnn = Model(inputs=[statement_input, dep_input], outputs=[main_output])
else:
model_cnn = Model(inputs=[statement_input], outputs=[main_output])
但是,我得到了错误。
整个错误回溯如下。
AttributeErrorTraceback (most recent call last)
<ipython-input-121-c919448d2730> in <module>()
35
36 conv_in1 = keras.layers.concatenate(kernel_stmt)
---> 37 conv_in1 = Dropout(0.6)(conv_in1)
38 conv_in1 = Dense(128, activation='relu')(conv_in1)
39
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.pyc in __call__(self, inputs, *args, **kwargs)
582 if base_layer_utils.have_all_keras_metadata(inputs):
583 inputs, outputs = self._set_connectivity_metadata_(
--> 584 inputs, outputs, args, kwargs)
585 if hasattr(self, '_set_inputs') and not self.inputs:
586 # Subclassed network: explicitly set metadata normally set by
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.pyc in _set_connectivity_metadata_(self, inputs, outputs, args, kwargs)
1414 kwargs.pop('mask', None) # `mask` should not be serialized.
1415 self._add_inbound_node(
-> 1416 input_tensors=inputs, output_tensors=outputs, arguments=kwargs)
1417 return inputs, outputs
1418
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.pyc in _add_inbound_node(self, input_tensors, output_tensors, arguments)
1522 input_tensors=input_tensors,
1523 output_tensors=output_tensors,
-> 1524 arguments=arguments)
1525
1526 # Update tensor history metadata.
/usr/local/lib/python2.7/dist-packages/tensorflow/python/keras/engine/base_layer.pyc in __init__(self, outbound_layer, inbound_layers, node_indices, tensor_indices, input_tensors, output_tensors, arguments)
1740 # For compatibility with external Keras, we use the deprecated
1741 # accessor here.
-> 1742 layer.outbound_nodes.append(self)
1743 # For compatibility with external Keras, we use the deprecated
1744 # accessor here.
答案 0 :(得分:0)
我遇到了同样的问题,以下修复程序对我有用。
代替
conv_in1 = keras.layers.concatenate(kernel_stmt)
尝试
concat = keras.layers.Concatenate(axis=1) ## or whatever axis is relevant for you. In my case it was axis =1
conv_in1 = concat([kernel_stmt])