[Python] Tensorflow timeseries cant predict Sine

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

  1. Stack

    Stack Membro Participativo

    i am trying to validate a tensorflow code for simple timeseries prediction in where:

    X = np.arange(0, 2000, 0.5)
    y = 2 * np.sin(X) + 0.8 * np.random.rand(X.shape[0])


    and the following parameters for the timeseries:

    LOOK_BACK = 100

    FORECAST_HORIZON = 100


    any model I use or try gets very bad performance, LSTM, MLP, CNN... loss is not improved in any epoch.

    def create_dataset(X_data, y_data, shuffle=False, repeat=False):
    # Ensure input and output are tensors
    X_data = tf.convert_to_tensor(X_data, dtype=tf.float32)
    y_data = tf.convert_to_tensor(y_data, dtype=tf.float32)

    # Create windowed dataset
    dataset = tf.keras.preprocessing.timeseries_dataset_from_array(
    data=tf.concat([X_data, y_data], axis=-1), # combine to keep alignment
    targets=None,
    sequence_length=look_back + forecast,
    sequence_stride=1,
    shuffle=shuffle,
    batch_size=1,
    )

    def split_input_target(sequence):
    input_seq = sequence[:, :look_back, : X_data.shape[-1]]
    target_seq = sequence[:, look_back:, X_data.shape[-1] :]
    return input_seq, target_seq

    dataset = dataset.map(split_input_target)

    return dataset

    train_dataset = create_dataset(X_train, y_train, shuffle=True, repeat=True)
    val_dataset = create_dataset(X_val, y_val)
    test_dataset = create_dataset(X_test, y_test)

    return train_dataset, val_dataset, test_dataset

    Continue reading...

Compartilhe esta Página