Python 3.x InvalidArgumentError:只有一个输入大小可以为-1,而不是同时为0和1

Python 3.x InvalidArgumentError:只有一个输入大小可以为-1,而不是同时为0和1,python-3.x,deep-learning,tensorflow2.0,tf.keras,Python 3.x,Deep Learning,Tensorflow2.0,Tf.keras,这是当我试图在模型上建立一个带注意(baidhanu)的sequence-2-sequence(编码器-解码器模型)时遇到的错误。fit我面临参数错误,有人能纠正我确切的问题吗 #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

这是当我试图在模型上建立一个带注意(baidhanu)的sequence-2-sequence(编码器-解码器模型)时遇到的错误。fit我面临参数错误,有人能纠正我确切的问题吗

#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 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(state_h) + d2(state_c))
  attention_weights = tf.keras.activations.softmax(score, axis=1)
  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,-1,1024])
  input_to_decoder = tf.keras.layers.concatenate([context_vector,decoder_embedding], axis=-1)

  return input_to_decoder
上面是我的注意层

#Encoder inputs 
encoder_inputs = tf.keras.layers.Input(shape=(None,),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)(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=(None,),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 = 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='adam', loss='categorical_crossentropy')
#model summary
model.summary()
当我试着去适应这个函数时,我被下面的错误卡住了,我不明白它是什么

%%time 
model.fit([encoder_input_data, decoder_input_data], decoder_output_data, batch_size=86, epochs=10, validation_split=0.2) 
------------------------------------------------------------------------------------------------------------------------------
#Output :
Train on 4644 samples, validate on 1162 samples
Epoch 1/10
  86/4644 [..............................] - ETA: 8:18
---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-34-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) ')

14 frames
</usr/local/lib/python3.6/dist-packages/decorator.py:decorator-gen-60> in time(self, line, cell, local_ns)

<timed eval> in <module>()

/usr/local/lib/python3.6/dist-packages/six.py in raise_from(value, from_value)

InvalidArgumentError:  Only one input size may be -1, not both 0 and 1
     [[node model/tf_op_layer_Reshape/Reshape (defined at /usr/local/lib/python3.6/dist-packages/tensorflow_core/python/framework/ops.py:1751) ]] [Op:__inference_distributed_function_16029]

Function call stack:
distributed_function
%%次
模型拟合([编码器输入数据,解码器输入数据],解码器输出数据,批量大小=86,历元=10,验证分割=0.2)
------------------------------------------------------------------------------------------------------------------------------
#输出:
培训4644个样本,验证1162个样本
纪元1/10
86/4644[预计到达时间:8:18]
---------------------------------------------------------------------------
InvalidArgumentError回溯(最后一次最近调用)
在()
---->1获取ipython().运行单元格魔术('time','model.fit([encoder\u input\u data,decoder\u input\u data],decoder\u output\u data,batch\u size=86,epochs=10,validation\u split=0.2'))
14帧
及时(自身、线路、小区、本地)
在()
/raise_from(value,from_value)中的usr/local/lib/python3.6/dist-packages/six.py
InvalidArgumentError:只有一个输入大小可以为-1,而不是同时为0和1
[[node model/tf_op_layer_Reforme/Reforme(定义于/usr/local/lib/python3.6/dist packages/tensorflow_core/python/framework/ops.py:1751)][op:[u推理_分布式函数_16029]
函数调用堆栈:
分布函数

谁能帮我一下哪里出了错

您的问题可能与以下几行有关:

encoder_inputs = tf.keras.layers.Input(shape=(None,),name='encoder_input_layer')
decoder_inputs = tf.keras.layers.Input(shape=(None,),name='decoder_input_layer')
您不能使用
shape=(无,)
,必须至少指定输入中的功能数量

为了详细说明您得到的错误,批次维度将自动计算,它期望0维度为-1(或等效的
None
)-您始终可以选择更改批次大小。但是维度1也不能是
None
(这是您当前设置的),因为只有一个非批次特征维度。模型不知道特征输入的大小


为不同类型的模型输入提供有关有效形状的更多信息。

hi adamconkey,感谢根据您的建议进行更改后我仍在错误下方hi adamconkey,感谢根据您的建议进行更改后我仍在根错误下方