# input_shape = (137861, 21, 1)
# output_sequence_length = 21
# english_vocab_size = 199
# french_vocab_size = 344
def embed_model(input_shape, output_sequence_length, english_vocab_size, french_vocab_size):
'''
Build and train a RNN model using word embedding on x and y
:param input_shape: Tuple of input shape
:param output_sequence_length: Length of output sequence
:param english_vocab_size: Number of unique English words in the dataset
:param french_vocab_size: Number of unique French words in the dataset
:return: Keras model built, but not trained
'''
learning_rate = 1e-3
model = Sequential()
model.add(Embedding(english_vocab_size, 128, input_length=output_sequence_length, input_shape=input_shape[1:]))
model.add(GRU(units=128, return_sequences=True))
model.add(TimeDistributed(Dense(french_vocab_size)))
model.add(Activation('softmax'))
model.summary()
model.compile(loss=sparse_categorical_crossentropy,
optimizer=Adam(learning_rate),
metrics=['accuracy'])
return model
调用此方法来训练模型时,会得到错误:
ValueError: Input 0 is incompatible with layer gru_1: expected ndim=3, found ndim=4
如何解决嵌入层和GRU层之间的形状错误?
答案 0 :(得分:0)
问题在于嵌入层将2D数组作为输入。但是,输入数组的形状为(137861, 21, 1)
,使其成为3D数组。只需使用numpy中的squeeze()
方法删除最后一个轴:
data = np.squeeze(data, axis=-1)
从侧面来说,这里Dense layer is applied on the last axis by defualt以来,无需使用TimeDistributed
层。