【发布时间】:2019-12-27 15:36:48
【问题描述】:
我正在尝试使用注意力机制实现一个序列到序列模型来构建一个聊天机器人,但是我遇到了以下错误,任何人都可以在我做错的地方提供帮助
下面是受张量流示例启发的注意力机制(Bhaidu attention)代码
#Bahdanu attention
#parameters to pass this attention
'''
1.Encoder state's i.e.., state_c, state_h
2.encoder_outputs
3.decoder_embedding which is in decoder part
4.you will get a context vector named "input_to_decoder" pass this as input to decoder lstm layer
'''
def B_Attention_layer(state_h,state_c,encoder_outputs,decoder_embedding):
d0 = tf.keras.layers.Dense(1024,name='dense_layer_1')
d1 = tf.keras.layers.Dense(1024,name='dense_layer_2')
d2 = tf.keras.layers.Dense(1024,name='dense_layer_3')
hidden_with_time_axis_1 = tf.keras.backend.expand_dims(state_h, 1)
#hidden_with_time_axis_1 = state_h
hidden_with_time_axis_2 = tf.keras.backend.expand_dims(state_c, 1)
#hidden_with_time_axis_2 = state_c
#hidden_states = tf.keras.layers.concatenate([state_h,state_c],axis=-1)
#all_states = tf.keras.layers.concatenate()
score = d0(tf.keras.activations.tanh(encoder_outputs) + d1(hidden_with_time_axis_1) + d2(hidden_with_time_axis_2))
attention_weights = tf.keras.activations.softmax(score, axis=1)
#attention = Dense(38)(attention_weights)
context_vector = attention_weights * encoder_outputs
context_vector = tf.keras.backend.sum(context_vector, axis=1)
context_vector = tf.keras.backend.expand_dims(context_vector, 1)
context_vector = tf.keras.backend.reshape(context_vector,[-1,38,1024])
input_to_decoder = tf.keras.layers.Concatenate(axis=-1)([context_vector,decoder_embedding])
return input_to_decoder
下面是Encoder-decoder Model i,e.,,seq-seq model
#Encoder inputs
encoder_inputs = tf.keras.layers.Input(shape=(38,),name='encoder_input_layer')
encoder_embedding = tf.keras.layers.Embedding(vocab_size, 1024, mask_zero=True,name='encoder_embedding_layer')(encoder_inputs)
encoder_outputs , state_h , state_c = tf.keras.layers.LSTM(1024, return_state=True,return_sequences=True)(encoder_embedding)
# We discard `encoder_outputs` and only keep the states.
encoder_states = [state_h, state_c]
# Set up the decoder, using `encoder_states` as initial state.
decoder_inputs = tf.keras.layers.Input(shape=(38,),name='decoder_input_layer')
# We set up our decoder to return full output sequences,
# and to return internal states as well. We don't use the.
# return states in the training model, but we will use them in inference.
decoder_embedding = tf.keras.layers.Embedding(vocab_size, 1024, mask_zero=True,name='decoder_embedding_layer')(decoder_inputs)
decoder_lstm = tf.keras.layers.LSTM(1024, return_state=True, return_sequences=True)
#Attention layer which is defind in above function
attention_layer = B_Attention_layer(state_h, state_c, encoder_outputs, decoder_embedding)
decoder_outputs, _, _ = decoder_lstm(attention_layer, initial_state=encoder_states)
decoder_dense = tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(vocab_size, activation='softmax'))
output = decoder_dense(decoder_outputs)
# `encoder_input_data` & `decoder_input_data` into `decoder_target_data`
model = tf.keras.models.Model([encoder_inputs, decoder_inputs], output)
#compiling the model
model.compile(optimizer=tf.keras.optimizers.Adam(), loss='categorical_crossentropy')
#model summary
model.summary()
下面是我的模型输出总结
Model: "model_11"
__________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
==================================================================================================
encoder_input_layer (InputLayer [(None, 38)] 0
__________________________________________________________________________________________________
encoder_embedding_layer (Embedd (None, 38, 1024) 5796864 encoder_input_layer[0][0]
__________________________________________________________________________________________________
lstm_32 (LSTM) [(None, 38, 1024), ( 8392704 encoder_embedding_layer[0][0]
__________________________________________________________________________________________________
tf_op_layer_ExpandDims_30 (Tens [(None, 1, 1024)] 0 lstm_32[0][1]
__________________________________________________________________________________________________
tf_op_layer_Tanh_9 (TensorFlowO [(None, 38, 1024)] 0 lstm_32[0][0]
__________________________________________________________________________________________________
dense_layer_2 (Dense) (None, 1, 1024) 1049600 tf_op_layer_ExpandDims_30[0][0]
__________________________________________________________________________________________________
tf_op_layer_ExpandDims_31 (Tens [(None, 1, 1024)] 0 lstm_32[0][2]
__________________________________________________________________________________________________
tf_op_layer_add_19 (TensorFlowO [(None, 38, 1024)] 0 tf_op_layer_Tanh_9[0][0]
dense_layer_2[0][0]
__________________________________________________________________________________________________
dense_layer_3 (Dense) (None, 1, 1024) 1049600 tf_op_layer_ExpandDims_31[0][0]
__________________________________________________________________________________________________
tf_op_layer_add_20 (TensorFlowO [(None, 38, 1024)] 0 tf_op_layer_add_19[0][0]
dense_layer_3[0][0]
__________________________________________________________________________________________________
dense_layer_1 (Dense) (None, 38, 1024) 1049600 tf_op_layer_add_20[0][0]
__________________________________________________________________________________________________
tf_op_layer_Max_7 (TensorFlowOp [(None, 1, 1024)] 0 dense_layer_1[0][0]
__________________________________________________________________________________________________
tf_op_layer_sub_7 (TensorFlowOp [(None, 38, 1024)] 0 dense_layer_1[0][0]
tf_op_layer_Max_7[0][0]
__________________________________________________________________________________________________
tf_op_layer_Exp_7 (TensorFlowOp [(None, 38, 1024)] 0 tf_op_layer_sub_7[0][0]
__________________________________________________________________________________________________
tf_op_layer_Sum_14 (TensorFlowO [(None, 1, 1024)] 0 tf_op_layer_Exp_7[0][0]
__________________________________________________________________________________________________
tf_op_layer_truediv_7 (TensorFl [(None, 38, 1024)] 0 tf_op_layer_Exp_7[0][0]
tf_op_layer_Sum_14[0][0]
__________________________________________________________________________________________________
tf_op_layer_mul_9 (TensorFlowOp [(None, 38, 1024)] 0 tf_op_layer_truediv_7[0][0]
lstm_32[0][0]
__________________________________________________________________________________________________
tf_op_layer_Sum_15 (TensorFlowO [(None, 1024)] 0 tf_op_layer_mul_9[0][0]
__________________________________________________________________________________________________
tf_op_layer_ExpandDims_32 (Tens [(None, 1, 1024)] 0 tf_op_layer_Sum_15[0][0]
__________________________________________________________________________________________________
decoder_input_layer (InputLayer [(None, 38)] 0
__________________________________________________________________________________________________
tf_op_layer_Reshape_17 (TensorF [(None, 38, 1024)] 0 tf_op_layer_ExpandDims_32[0][0]
__________________________________________________________________________________________________
decoder_embedding_layer (Embedd (None, 38, 1024) 5796864 decoder_input_layer[0][0]
__________________________________________________________________________________________________
concatenate_13 (Concatenate) (None, 38, 2048) 0 tf_op_layer_Reshape_17[0][0]
decoder_embedding_layer[0][0]
__________________________________________________________________________________________________
lstm_33 (LSTM) [(None, 38, 1024), ( 12587008 concatenate_13[0][0]
lstm_32[0][1]
lstm_32[0][2]
__________________________________________________________________________________________________
time_distributed_11 (TimeDistri (None, 38, 5661) 5802525 lstm_33[0][0]
==================================================================================================
Total params: 41,524,765
Trainable params: 41,524,765
Non-trainable params: 0
__________________________________________________________________________________________________
当我拟合模型时,我所面临的张量形状错误以下任何人都可以帮我解决这个问题
model.fit([encoder_input_data, decoder_input_data], decoder_output_data, batch_size=86, epochs=10, validation_split=0.2)
错误:
Train on 4644 samples, validate on 1162 samples
Epoch 1/10
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
<ipython-input-78-781d7ca43c98> in <module>()
----> 1 get_ipython().run_cell_magic('time', '', 'model.fit([encoder_input_data, decoder_input_data], decoder_output_data, batch_size=86, epochs=10, validation_split=0.2) ')
7 frames
/usr/local/lib/python3.6/dist-packages/IPython/core/interactiveshell.py in run_cell_magic(self, magic_name, line, cell)
2115 magic_arg_s = self.var_expand(line, stack_depth)
2116 with self.builtin_trap:
-> 2117 result = fn(magic_arg_s, cell)
2118 return result
2119
</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-60> in time(self, line, cell, local_ns)
/usr/local/lib/python3.6/dist-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
186 # but it's overkill for just that one bit of state.
187 def magic_deco(arg):
--> 188 call = lambda f, *a, **k: f(*a, **k)
189
190 if callable(arg):
/usr/local/lib/python3.6/dist-packages/IPython/core/magics/execution.py in time(self, line, cell, local_ns)
1187 if mode=='eval':
1188 st = clock2()
-> 1189 out = eval(code, glob, local_ns)
1190 end = clock2()
1191 else:
<timed eval> in <module>()
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training.py in fit(self, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, max_queue_size, workers, use_multiprocessing, **kwargs)
725 max_queue_size=max_queue_size,
726 workers=workers,
--> 727 use_multiprocessing=use_multiprocessing)
728
729 def evaluate(self,
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_arrays.py in fit(self, model, x, y, batch_size, epochs, verbose, callbacks, validation_split, validation_data, shuffle, class_weight, sample_weight, initial_epoch, steps_per_epoch, validation_steps, validation_freq, **kwargs)
673 validation_steps=validation_steps,
674 validation_freq=validation_freq,
--> 675 steps_name='steps_per_epoch')
676
677 def evaluate(self,
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/engine/training_arrays.py in model_iteration(model, inputs, targets, sample_weights, batch_size, epochs, verbose, callbacks, val_inputs, val_targets, val_sample_weights, shuffle, initial_epoch, steps_per_epoch, validation_steps, validation_freq, mode, validation_in_fit, prepared_feed_values_from_dataset, steps_name, **kwargs)
392
393 # Get outputs.
--> 394 batch_outs = f(ins_batch)
395 if not isinstance(batch_outs, list):
396 batch_outs = [batch_outs]
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/keras/backend.py in __call__(self, inputs)
3474
3475 fetched = self._callable_fn(*array_vals,
-> 3476 run_metadata=self.run_metadata)
3477 self._call_fetch_callbacks(fetched[-len(self._fetches):])
3478 output_structure = nest.pack_sequence_as(
/usr/local/lib/python3.6/dist-packages/tensorflow_core/python/client/session.py in __call__(self, *args, **kwargs)
1470 ret = tf_session.TF_SessionRunCallable(self._session._session,
1471 self._handle, args,
-> 1472 run_metadata_ptr)
1473 if run_metadata:
1474 proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)
InvalidArgumentError: Input to reshape is a tensor with 88064 values, but the requested shape requires a multiple of 38912
[[{{node Reshape_17}}]]
谁能帮帮我,我不明白我哪里做错了
【问题讨论】:
-
请改写问题的主题以描述您要问的内容。粘贴错误消息不会给您太多帮助 - 事实上,这不是问题,不是吗?接下来,问一些具体的问题。记住,S.O.不是“修复我的代码”网站,而是“帮助我理解这个特定的东西”网站。
-
嗨 Benjamin R,对不起,我的问题不完整,我只是这个网站的初学者,但是从下一次开始感谢您的回复,我可以改变我的提问方式,谢谢您建议你能看看here我正在做这个项目,如果可能的话帮助我谢谢你:)
标签: python tensorflow machine-learning keras deep-learning