【问题标题】:InvalidArgumentError: Only one input size may be -1, not both 0 and 1InvalidArgumentError:只有一个输入大小可能是 -1,而不是 0 和 1
【发布时间】:2019-12-21 09:28:54
【问题描述】:

这是我在尝试在 model.fit 上使用注意力(baidhanu)构建 sequence-2-Sequence(编码器-解码器模型)时遇到的错误我面临参数错误谁能纠正我什么是确切的问题

#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

谁能帮我解决我做错的地方

【问题讨论】:

    标签: python-3.x deep-learning tensorflow2.0 tf.keras


    【解决方案1】:

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

    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=(None,),并且必须至少指定输入中的特征数量。

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

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

    【讨论】:

    • 嗨 adamconkey,感谢您根据您的建议进行更改后,我仍然遇到错误
    • 嗨 adamconkey,感谢您在根据您的建议进行更改后,我仍然低于根本错误click here to see my error
    猜你喜欢
    • 2017-05-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 2013-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多