如何设置GRU / LSTM的输入形状

时间:2017-12-04 00:18:32

标签: python deep-learning keras lstm

我正在尝试训练我的模型以预测整数序列中的下一个数字。我创建的整数序列是随机生成的。 我的X_train = [[1,2,3,4,5,6],[45,45,46,47,48,49],[203,204,205,206,207,208] ...] 和Y_train = [[7,8,9],[50,51,52],[209,210,211]] X_train = (10000,6,511)Y_train = (10000,3,511)的形状 我应该如何为GRU / LSTM模型设置输入形状?

def define_models(n_input = 6, n_output = 3):
    # define training encoder
    sequence = Input(shape=(n_input,), dtype="int32")
    embedded = Embedding(13, 300, mask_zero=True)(sequence)
    gru1 = GRU(1000, return_sequences=True)(embedded)
    after_dp = Dropout(0.5)(gru1)
    gru2 = GRU(1000, return_sequences=True)(after_dp)
    after_dp = Dropout(0.5)(gru2)
    output = TimeDistributed(Dense(13, activation="softmax"))(after_dp)

    model = Model(input=sequence, output=output)

    return model

我收到错误:

ValueError: Error when checking input: expected input_1 to have 2 dimensions, but got array with shape (10000, 6, 511)

我应该如何为我的数据集修复它?

1 个答案:

答案 0 :(得分:0)

您只给出一个维度作为input_shape,而您将3d数组作为输入。对于n-d输入数组,input_shape应该是最后n-1个维度值。因此输入形状应该是  (X_train.shape [1],X_train.shape [2])。

替换此行

 sequence = Input(shape=(n_input,), dtype="int32")

用这个

 sequence = Input(shape=(X_train.shape[1],X_train.shape[2]), dtype="int32")