[Python] Why does my LSTM model raise max_seq_length <= 0 when predicting with new input of...

Discussão em 'Python' iniciado por Stack, Maio 17, 2025.

  1. Stack

    Stack Membro Participativo

    I'm training a multi-branch LSTM model on Kaggle using 2x T4 GPUs, with multiple input sequences of different lengths. My target is the "close" column.

    1. Normalization and Data:


    I normalize my data with a custom Normalizer class:

    normalizer = Normalizer(start_data[cols])
    data = normalizer.normalize()
    values = data[cols].values

    2. Model


    times = [5, 15, 30, 90] # Sequence lengths

    def build_lstm_model(times, lstm_units=64, dense_units=64):
    inputs = []
    branches = []

    for t in times:
    input_t = Input(shape=(t, 4))
    x = LSTM(lstm_units, return_sequences=True)(input_t)
    x = Dropout(0.2)(x)
    x = LSTM(lstm_units)(x)
    branches.append(x)
    inputs.append(input_t)

    merged = concatenate(branches)
    dense = Dense(128, activation="relu")(merged)
    dense = Dense(dense_units, activation="relu")(dense)

    residual = Dense(dense_units)(merged)
    added = Add()([dense, residual])

    output = Dense(1)(added)

    return Model(inputs=inputs, outputs=output)

    3. Data Preparation


    I prepare lists with the differents durations :

    min_len = max(times)
    x_inputs = [[] for _ in times]
    y = []

    for i in range(min_len, len(values)):
    for idx, t in enumerate(times):
    x_inputs[idx].append(values[i - t:i])
    y.append(values[cols.index("close")])

    x_inputs = [np.array(x) for x in x_inputs] # e.g., shape (911, 90, 4)
    y = np.array(y)

    4. Training (work)


    strategy = tf.distribute.MirroredStrategy()
    with strategy.scope():
    model = build_lstm_model(times)
    model.compile(optimizer="adam", loss="mse", metrics=["mae"])

    model.fit(x_inputs, y, epochs=2, batch_size=256)

    5. Predictions (error)


    I don't have problem when I use train data. For the last prediction, I do :

    x_inputs = [values[-t:].reshape(1, t, 4) for t in times]
    prediction = model.predict(x_inputs)


    But this throws: InvalidArgumentError: Graph execution error: ... max_seq_length <= 0 ...

    while all inputs have shape (1, t, 4).

    Why is model.predict(x_inputs) throwing max_seq_length <= 0? The model works perfectly for training and predicting on full datasets. The error happens only during single sample prediction using the last t values.

    Thanks for your help

    I have check manually the shape, rebuild the NoteBook, retrain se model, but the error persists.

    Continue reading...

Compartilhe esta Página