简体   繁体   English

Keras-嵌入层和GRU层形状错误

[英]Keras - Embedding Layer and GRU Layer Shape Error

# 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

When invoking this method to train a model, it gets the error: 调用此方法来训练模型时,会收到错误:

ValueError: Input 0 is incompatible with layer gru_1: expected ndim=3, found ndim=4

How to fix the shape error between Embedding Layer and GRU Layer? 如何修复嵌入层和GRU层之间的形状错误?

The problem is that the Embedding layer takes a 2D array as the input. 问题在于嵌入层将2D数组作为输入。 However, the shape of the input array is (137861, 21, 1) which makes it a 3D array. 但是,输入数组的形状为(137861, 21, 1) ,这使其成为3D数组。 Simply remove the last axis using squeeze() method from numpy: 只需从numpy中使用squeeze()方法删除最后一个轴:

data = np.squeeze(data, axis=-1)

As a side, there is no need to use TimeDistributed layer here, since the Dense layer is applied on the last axis by defualt . TimeDistributed这里不需要使用TimeDistributed层,因为Dense层通过defualt应用于最后一个轴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM