我已经阅读了有关seq2seq的其他答案,但它们似乎并没有帮助我解决我遇到的错误,我被困在这里几天了,有人可以帮助我吗。
n_units = 128
n_input = 1000
n_output = 1000
embedding_size = 128
# encoder
encoder_inputs = Input(shape=(None, ), name = 'encoder_inputs')
encoder_embedding = Embedding(input_dim=num_words, output_dim = embedding_size, name = 'encoder_embedding')(encoder_inputs)
encoder_lstm1 = Bidirectional(LSTM(n_units,return_sequences=True), name = 'encoder_lstm1')(encoder_embedding)
encoder_lstm2 = Bidirectional(LSTM(n_units,return_sequences=True), name = 'encoder_lstm2')(encoder_lstm1)
encoder_lstm3 = Bidirectional(LSTM(n_units,return_state=True, name = 'encoder_lstm3'))
encoder_outputs, forward_h, forward_c, backward_h, backward_c = encoder_lstm3(encoder_lstm2)
state_h = Concatenate()([forward_h, backward_h])
state_c = Concatenate()([forward_c, backward_c])
encoder_states = [state_h, state_c]
# decoder
decoder_inputs = Input(shape=(None, ), name = 'decoder_inputs')
decoder_embedding = Embedding(input_dim = num_words, output_dim = embedding_size, name = 'decoder_embedding')(decoder_inputs)
decoder_lstm1 = LSTM(n_units*2, return_sequences=True, name = 'decoder_lstm1')(decoder_embedding, initial_state = encoder_states)
decoder_lstm2 = LSTM(n_units*2, return_sequences=True, name = 'decoder_lstm2')(decoder_lstm1, initial_state = encoder_states)
decoder_lstm3 = LSTM(n_units*2, return_sequences=True, return_state=True, name = 'decoder_lstm3')
decoder_outputs, _, _ = decoder_lstm3(decoder_lstm2, initial_state=encoder_states)
decoder_dense = Dense(n_output, activation='softmax')
decoder_outputs = decoder_dense(decoder_outputs)
model = Model([encoder_inputs, decoder_inputs], decoder_outputs)
model.summary()
# define inference encoder
encoder_model = Model(encoder_inputs, encoder_states)
# define inference decoder
decoder_state_input_h = Input(shape=(n_units*2,))
decoder_state_input_c = Input(shape=(n_units*2,))
decoder_states_inputs = [decoder_state_input_h, decoder_state_input_c]
decoder_outputs, state_h, state_c = decoder_lstm3(decoder_lstm2, initial_state=decoder_states_inputs)
decoder_states = [state_h, state_c]
decoder_outputs = decoder_dense(decoder_outputs)
decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)`
我正在使用此代码,生成的模型摘要似乎很好
Layer (type) Output Shape Param # Connected to
==================================================================================================
encoder_inputs (InputLayer) (None, None) 0
__________________________________________________________________________________________________
encoder_embedding (Embedding) (None, None, 128) 128000 encoder_inputs[0][0]
__________________________________________________________________________________________________
encoder_lstm1 (Bidirectional) (None, None, 256) 263168 encoder_embedding[0][0]
__________________________________________________________________________________________________
encoder_lstm2 (Bidirectional) (None, None, 256) 394240 encoder_lstm1[0][0]
__________________________________________________________________________________________________
decoder_inputs (InputLayer) (None, None) 0
__________________________________________________________________________________________________
bidirectional_2 (Bidirectional) [(None, 256), (None, 394240 encoder_lstm2[0][0]
__________________________________________________________________________________________________
decoder_embedding (Embedding) (None, None, 128) 128000 decoder_inputs[0][0]
__________________________________________________________________________________________________
concatenate_3 (Concatenate) (None, 256) 0 bidirectional_2[0][1]
bidirectional_2[0][3]
__________________________________________________________________________________________________
concatenate_4 (Concatenate) (None, 256) 0 bidirectional_2[0][2]
bidirectional_2[0][4]
__________________________________________________________________________________________________
decoder_lstm1 (LSTM) (None, None, 256) 394240 decoder_embedding[0][0]
concatenate_3[0][0]
concatenate_4[0][0]
__________________________________________________________________________________________________
decoder_lstm2 (LSTM) (None, None, 256) 525312 decoder_lstm1[0][0]
concatenate_3[0][0]
concatenate_4[0][0]
__________________________________________________________________________________________________
decoder_lstm3 (LSTM) [(None, None, 256), 525312 decoder_lstm2[0][0]
concatenate_3[0][0]
concatenate_4[0][0]
__________________________________________________________________________________________________
dense_2 (Dense) (None, None, 1000) 257000 decoder_lstm3[0][0]
==================================================================================================
Total params: 3,009,512
Trainable params: 3,009,512
Non-trainable params: 0
__________________________________________________________________________________________________
但是,在建立推理模型时出现此错误
ValueError Traceback (most recent call last)
<ipython-input-10-9b0a6d40dd68> in <module>()
35 decoder_states = [state_h, state_c]
36 decoder_outputs = decoder_dense(decoder_outputs)
---> 37 decoder_model = Model([decoder_inputs] + decoder_states_inputs, [decoder_outputs] + decoder_states)
/usr/local/lib/python3.6/dist-packages/keras/legacy/interfaces.py in wrapper(*args, **kwargs)
89 warnings.warn('Update your `' + object_name +
90 '` call to the Keras 2 API: ' + signature, stacklevel=2)
---> 91 return func(*args, **kwargs)
92 wrapper._original_function = func
93 return wrapper
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in __init__(self, *args, **kwargs)
89 'inputs' in kwargs and 'outputs' in kwargs):
90 # Graph network
---> 91 self._init_graph_network(*args, **kwargs)
92 else:
93 # Subclassed network
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in _init_graph_network(self, inputs, outputs, name)
233 # Keep track of the network's nodes and layers.
234 nodes, nodes_by_depth, layers, layers_by_depth = _map_graph_network(
--> 235 self.inputs, self.outputs)
236 self._network_nodes = nodes
237 self._nodes_by_depth = nodes_by_depth
/usr/local/lib/python3.6/dist-packages/keras/engine/network.py in _map_graph_network(inputs, outputs)
1487 'The following previous layers '
1488 'were accessed without issue: ' +
-> 1489 str(layers_with_complete_input))
1490 for x in node.output_tensors:
1491 computable_tensors.append(x)
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("encoder_inputs:0", shape=(?, ?), dtype=float32) at layer "encoder_inputs". The following previous layers were accessed without issue: []
我似乎无法弄清楚为什么在模型摘要中连接时它显示为断开连接