### Install TimeMachine Requirements Source: https://github.com/atik-ahamed/timemachine/blob/main/README.md Install the necessary dependencies for the project. ```bash pip install -r requirements.txt ``` -------------------------------- ### Utilize training management and data utilities Source: https://context7.com/atik-ahamed/timemachine/llms.txt Implement early stopping, learning rate scheduling, visualization, and data normalization using the utils module. ```python import torch import numpy as np from utils.tools import EarlyStopping, adjust_learning_rate, visual, StandardScaler # Early Stopping for training early_stopping = EarlyStopping(patience=10, verbose=True, delta=0) # Simulate training loop model = torch.nn.Linear(10, 1) best_model_path = './checkpoints/experiment/' for epoch in range(100): val_loss = np.random.rand() # Replace with actual validation loss early_stopping(val_loss, model, best_model_path) if early_stopping.early_stop: print(f"Early stopping triggered at epoch {epoch}") break # Learning rate adjustment optimizer = torch.optim.Adam(model.parameters(), lr=0.001) scheduler = None # Optional scheduler for TST mode class Args: lradj = 'type3' learning_rate = 0.001 args = Args() adjust_learning_rate(optimizer, scheduler, epoch=5, args=args) # Visualization of predictions ground_truth = np.sin(np.linspace(0, 4*np.pi, 100)) predictions = ground_truth + np.random.normal(0, 0.1, 100) visual(ground_truth, predictions, name='./results/forecast_plot.pdf') # Standard Scaler for data normalization scaler = StandardScaler(mean=0.5, std=0.2) data = np.random.randn(100, 7) normalized = scaler.transform(data) denormalized = scaler.inverse_transform(normalized) ``` -------------------------------- ### Configure and Run TimeMachine Experiments Source: https://context7.com/atik-ahamed/timemachine/llms.txt Defines model, data, training, and hardware parameters using argparse.Namespace, then executes training, testing, and prediction workflows. ```python args = argparse.Namespace( # Model configuration model='TimeMachine', seq_len=96, label_len=48, pred_len=96, enc_in=7, n1=256, n2=128, d_state=256, dconv=2, e_fact=1, dropout=0.05, revin=1, ch_ind=1, residual=1, # Data configuration data='custom', root_path='./data/ETT/', data_path='ETTh1.csv', features='M', # M: multivariate, S: univariate, MS: multivariate->univariate target='OT', freq='h', embed='timeF', # Training configuration train_epochs=100, batch_size=32, learning_rate=0.0001, patience=10, lradj='type3', pct_start=0.3, loss='mse', use_amp=False, # Hardware configuration use_gpu=True, gpu=0, use_multi_gpu=False, devices='0,1,2,3', num_workers=10, # Output configuration checkpoints='./checkpoints/', random_seed=2021, itr=1, des='Exp', do_predict=False, test_flop=False ) # Initialize experiment exp = Exp_Main(args) # Training creates checkpoints at ./checkpoints/{setting}/ setting = f'{args.model}_{args.data_path[:-4]}_sl{args.seq_len}_pl{args.pred_len}' trained_model = exp.train(setting) # Testing evaluates on test set, saves results to ./results/ and ./csv_results/ exp.test(setting) # Output: mse:0.xxx, mae:0.xxx, rse:0.xxx # Generate predictions on unseen data exp.predict(setting, load=True) # Predictions saved to ./results/{setting}/real_prediction.npy ``` -------------------------------- ### Manage Data Loading and Custom Datasets Source: https://context7.com/atik-ahamed/timemachine/llms.txt Utilizes the data_provider factory to generate loaders and demonstrates direct instantiation of the Dataset_Custom class. ```python from data_provider.data_factory import data_provider from data_provider.data_loader import Dataset_Custom, Dataset_ETT_hour import argparse # Configure data loading args = argparse.Namespace( data='custom', # 'ETTh1', 'ETTh2', 'ETTm1', 'ETTm2', 'custom' root_path='./data/weather/', data_path='weather.csv', seq_len=96, label_len=48, pred_len=96, features='M', target='OT', embed='timeF', freq='h', batch_size=32, num_workers=4 ) # Get train/val/test data loaders train_data, train_loader = data_provider(args, flag='train') val_data, val_loader = data_provider(args, flag='val') test_data, test_loader = data_provider(args, flag='test') # Iterate through batches for batch_x, batch_y, batch_x_mark, batch_y_mark in train_loader: # batch_x: input sequence (batch, seq_len, features) # batch_y: target sequence (batch, label_len + pred_len, features) # batch_x_mark: input time features # batch_y_mark: target time features print(f"Input: {batch_x.shape}, Target: {batch_y.shape}") break # Direct dataset usage for custom data dataset = Dataset_Custom( root_path='./data/', data_path='my_data.csv', # CSV with 'date' column + feature columns flag='train', size=[96, 48, 96], # [seq_len, label_len, pred_len] features='M', target='target_column', scale=True, timeenc=1, freq='h' ) print(f"Dataset size: {len(dataset)}") ``` -------------------------------- ### Execute benchmark reproduction scripts Source: https://context7.com/atik-ahamed/timemachine/llms.txt Run pre-configured shell scripts to reproduce benchmark results for various datasets. ```bash sh ./scripts/TimeMachine/weather.sh sh ./scripts/TimeMachine/etth1.sh sh ./scripts/TimeMachine/electricity.sh sh ./scripts/TimeMachine/traffic.sh ``` -------------------------------- ### Run TimeMachine training and inference via CLI Source: https://context7.com/atik-ahamed/timemachine/llms.txt Execute training or inference using run_longExp.py with specific model configurations and dataset parameters. ```bash python run_longExp.py \ --is_training 1 \ --model TimeMachine \ --model_id weather_96_96 \ --data custom \ --root_path ./data/weather/ \ --data_path weather.csv \ --features M \ --seq_len 96 \ --pred_len 96 \ --enc_in 21 \ --n1 128 \ --n2 16 \ --d_state 256 \ --dconv 2 \ --e_fact 1 \ --dropout 0.1 \ --revin 1 \ --ch_ind 1 \ --residual 1 \ --batch_size 512 \ --learning_rate 0.001 \ --train_epochs 100 \ --patience 10 \ --lradj constant \ --itr 1 \ --random_seed 2024 ``` ```bash python run_longExp.py \ --is_training 1 \ --model TimeMachine \ --model_id etth1_96_192 \ --data ETTh1 \ --root_path ./data/ETT/ \ --data_path ETTh1.csv \ --features M \ --seq_len 96 \ --pred_len 192 \ --enc_in 7 \ --n1 256 \ --n2 128 \ --batch_size 32 \ --learning_rate 0.0001 ``` ```bash python run_longExp.py \ --is_training 0 \ --model TimeMachine \ --model_id weather_96_96 \ --data custom \ --root_path ./data/weather/ \ --data_path weather.csv \ --features M \ --seq_len 96 \ --pred_len 96 \ --enc_in 21 ``` -------------------------------- ### Initialize and Run TimeMachine Model Source: https://context7.com/atik-ahamed/timemachine/llms.txt Instantiates the TimeMachine model with specified configurations and performs a forward pass on a batch of multivariate time series data. Ensure Mamba and TimeMachine model components are correctly imported. ```python import torch from mamba_ssm import Mamba from models.TimeMachine import Model # Configuration using argparse namespace or dotdict class Config: def __init__(self): self.seq_len = 96 # Input sequence length self.pred_len = 96 # Prediction horizon self.enc_in = 7 # Number of input channels/features self.n1 = 256 # First embedding dimension self.n2 = 128 # Second embedding dimension self.d_state = 256 # Mamba state dimension self.dconv = 2 # Mamba convolution kernel size self.e_fact = 1 # Mamba expansion factor self.dropout = 0.05 # Dropout rate self.revin = 1 # Use RevIN normalization (1=True, 0=False) self.ch_ind = 1 # Channel independence (1=True, 0=False) self.residual = 1 # Use residual connections (1=True, 0=False) configs = Config() model = Model(configs) # Forward pass with batch of multivariate time series # Input shape: (batch_size, seq_len, num_features) batch_size = 32 x = torch.randn(batch_size, configs.seq_len, configs.enc_in) # Get predictions # Output shape: (batch_size, pred_len, num_features) predictions = model(x) print(f"Input shape: {x.shape}") # torch.Size([32, 96, 7]) print(f"Output shape: {predictions.shape}") # torch.Size([32, 96, 7]) ``` -------------------------------- ### Initialize and Use RevIN for Normalization/Denormalization Source: https://context7.com/atik-ahamed/timemachine/llms.txt Initializes the Reversible Instance Normalization (RevIN) layer for a specified number of features. Use 'norm' mode before model input and 'denorm' mode after model output for robust time series processing. ```python import torch from RevIN.RevIN import RevIN # Initialize RevIN for 7 feature channels num_features = 7 revin = RevIN(num_features=num_features, eps=1e-5, affine=True) # Input: batch of time series (batch_size, seq_len, features) x = torch.randn(16, 96, num_features) # Normalize input before model processing x_normalized = revin(x, mode='norm') print(f"Normalized mean: {x_normalized.mean(dim=1).mean():.4f}") # ~0.0 print(f"Normalized std: {x_normalized.std(dim=1).mean():.4f}") # ~1.0 # After model prediction, denormalize output # Assumes predictions have same feature dimension predictions = torch.randn(16, 96, num_features) # Model output predictions_denorm = revin(predictions, mode='denorm') print(f"Denormalized shape: {predictions_denorm.shape}") ``` -------------------------------- ### Run Multivariate Forecasting Source: https://github.com/atik-ahamed/timemachine/blob/main/README.md Execute the shell script to perform multivariate forecasting on the weather dataset. ```bash sh ./scripts/TimeMachine/weather.sh ``` -------------------------------- ### Import Experiment Training Pipeline Source: https://context7.com/atik-ahamed/timemachine/llms.txt Imports the main experiment class responsible for orchestrating the training pipeline, including data handling, model training, validation, and testing. ```python import argparse from exp.exp_main import Exp_Main ``` -------------------------------- ### Calculate Forecasting Evaluation Metrics Source: https://context7.com/atik-ahamed/timemachine/llms.txt Computes standard forecasting metrics like MSE, MAE, and RSE using the utils.metrics module. ```python import numpy as np from utils.metrics import metric, MAE, MSE, RMSE, RSE, CORR # Sample predictions and ground truth # Shape: (num_samples, pred_len, num_features) predictions = np.random.randn(100, 96, 7) ground_truth = np.random.randn(100, 96, 7) # Calculate all metrics at once mae, mse, rmse, mape, mspe, rse, corr = metric(predictions, ground_truth) print(f"MSE: {mse:.6f}") print(f"MAE: {mae:.6f}") print(f"RMSE: {rmse:.6f}") print(f"RSE: {rse:.6f}") print(f"Correlation: {corr:.6f}") # Individual metric calculations mae_value = MAE(predictions, ground_truth) mse_value = MSE(predictions, ground_truth) rse_value = RSE(predictions, ground_truth) ``` -------------------------------- ### Cite TimeMachine Source: https://github.com/atik-ahamed/timemachine/blob/main/README.md BibTeX citation format for the TimeMachine research paper. ```bibtex @article{timemachine, title = {TimeMachine: A Time Series is Worth 4 Mambas for Long-term Forecasting}, author = {Ahamed, Md Atik and Cheng, Qiang}, journal = {arXiv preprint arXiv:2403.09898}, year = {2024} } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.