【问题标题】:Keras - Embedding Layer and GRU Layer Shape ErrorKeras - 嵌入层和 GRU 层形状错误
【发布时间】:2019-04-06 02:20:02
【问题描述】:
# 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

Embedding Layer 和 GRU Layer 之间的形状错误如何解决?

【问题讨论】:

标签: python keras embedding keras-layer


【解决方案1】:

问题在于 Embedding 层将 2D 数组作为输入。但是,输入数组的形状是(137861, 21, 1),这使它成为一个 3D 数组。只需使用 numpy 中的 squeeze() 方法删除最后一个轴:

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

作为一个侧面,这里没有必要使用TimeDistributed层,因为Dense layer is applied on the last axis by defualt

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-13
    • 2019-01-21
    • 2021-05-20
    • 1970-01-01
    • 2019-01-08
    • 1970-01-01
    • 1970-01-01
    • 2020-04-29
    相关资源
    最近更新 更多