### Command Line Training Script (Bash) Source: https://context7.com/hank0626/timebridge/llms.txt Demonstrates how to train the TimeBridge model using the command line. This script installs dependencies, configures training parameters such as dataset, model ID, sequence lengths, and hyperparameters like learning rate and batch size. It also shows examples for multi-GPU training and inference. ```bash # Install dependencies pip install -r requirements.txt # Train on ETTh1 dataset with 96-step prediction horizon python -u run.py \ --is_training 1 \ --root_path ./dataset/ETT-small/ \ --data_path ETTh1.csv \ --model_id ETTh1_720_96 \ --model TimeBridge \ --data ETTh1 \ --features M \ --seq_len 720 \ --label_len 48 \ --pred_len 96 \ --enc_in 7 \ --ia_layers 3 \ --pd_layers 1 \ --ca_layers 0 \ --d_model 128 \ --d_ff 128 \ --batch_size 64 \ --alpha 0.35 \ --learning_rate 0.0002 \ --train_epochs 100 \ --patience 10 # Train on electricity dataset with multi-GPU support python -u run.py \ --is_training 1 \ --root_path ./dataset/electricity/ \ --data_path electricity.csv \ --model_id electricity_720_96 \ --model TimeBridge \ --data custom \ --features M \ --seq_len 720 \ --pred_len 96 \ --enc_in 321 \ --d_model 512 \ --d_ff 512 \ --n_heads 32 \ --ia_layers 1 \ --pd_layers 1 \ --ca_layers 2 \ --num_p 4 \ --alpha 0.2 \ --batch_size 16 \ --use_multi_gpu \ --devices 0,1,2,3 # Inference/testing only (load pre-trained model) python -u run.py \ --is_training 0 \ --root_path ./dataset/ETT-small/ \ --data_path ETTh1.csv \ --model_id ETTh1_720_96 \ --model TimeBridge \ --data ETTh1 \ --features M \ --seq_len 720 \ --pred_len 96 \ --enc_in 7 \ --d_model 128 ``` -------------------------------- ### TimeBridge Model Initialization and Forward Pass (PyTorch) Source: https://context7.com/hank0626/timebridge/llms.txt Demonstrates how to initialize and use the main TimeBridge model class for time series forecasting. It shows setting up model configurations, defining input tensors for encoding and decoding, and performing a forward pass to obtain predictions. The example includes sample input and output shapes. ```python import torch from model.TimeBridge import Model # Configuration class for TimeBridge model class Configs: def __init__(self): # Model architecture self.enc_in = 7 # Number of input channels (variates) self.seq_len = 720 # Input sequence length self.pred_len = 96 # Prediction horizon self.period = 24 # Patch period length self.d_model = 512 # Model dimension self.d_ff = 2048 # Feed-forward dimension self.n_heads = 8 # Number of attention heads # Layer configuration self.ia_layers = 1 # Integrated attention layers self.pd_layers = 1 # Patch downsampling layers self.ca_layers = 0 # Cointegrated attention layers self.num_p = None # Number of downsampled patches (auto-calculated if None) self.stable_len = 6 # Moving average length for patch norm # Regularization self.dropout = 0.0 self.attn_dropout = 0.15 self.revin = True # Enable RevIN normalization # Embedding self.embed = 'timeF' self.activation = 'gelu' configs = Configs() model = Model(configs) # Example input: [batch_size, seq_len, enc_in] batch_size = 32 x_enc = torch.randn(batch_size, configs.seq_len, configs.enc_in) x_mark_enc = torch.randn(batch_size, configs.seq_len, 4) # Time features x_dec = torch.randn(batch_size, configs.pred_len, configs.enc_in) x_mark_dec = torch.randn(batch_size, configs.pred_len, 4) # Forward pass returns predictions: [batch_size, pred_len, enc_in] predictions = model(x_enc, x_mark_enc, x_dec, x_mark_dec) print(f"Input shape: {x_enc.shape}") print(f"Output shape: {predictions.shape}") # Output: # Input shape: torch.Size([32, 720, 7]) # Output shape: torch.Size([32, 96, 7]) ``` -------------------------------- ### Data Provider Factory for Time Series Datasets (Python) Source: https://context7.com/hank0626/timebridge/llms.txt Illustrates how to use the `data_provider` function to create dataset and dataloader instances for time series forecasting tasks. It shows how to configure arguments for different datasets (e.g., ETT, Solar, PEMS), sequence lengths, prediction horizons, and batch sizes. The example demonstrates obtaining data loaders for training, validation, and testing phases. ```python from data_provider.data_factory import data_provider import argparse # Setup arguments for data loading args = argparse.Namespace( data='ETTh1', # Dataset type: ETTh1, ETTh2, ETTm1, ETTm2, Solar, PEMS, custom root_path='./dataset/ETT-small/', data_path='ETTh1.csv', seq_len=720, # Input sequence length label_len=48, # Label length for decoder pred_len=96, # Prediction length features='M', # M: multivariate, S: univariate, MS: multivariate to univariate target='OT', # Target column for S/MS tasks embed='timeF', # Time encoding: timeF, fixed, learned freq='h', # Frequency: s, t, h, d, b, w, m batch_size=32, num_workers=10, seasonal_patterns='Monthly' ) # Get training data train_data, train_loader = data_provider(args, flag='train') print(f"Training samples: {len(train_data)}") # Get validation data val_data, val_loader = data_provider(args, flag='val') print(f"Validation samples: {len(val_data)}") # Get test data (batch_size=1, no shuffle) test_data, test_loader = data_provider(args, flag='test') print(f"Test samples: {len(test_data)}") ``` -------------------------------- ### Run Long-Term Time Series Forecasting Experiments Source: https://context7.com/hank0626/timebridge/llms.txt This snippet demonstrates how to set up and run long-term time series forecasting experiments using the Exp_Long_Term_Forecast class. It involves configuring experiment parameters via argparse, initializing the experiment, training the model with early stopping, and evaluating its performance on test data. The output shows training progress and final evaluation metrics. ```python from experiments.exp_long_term_forecasting import Exp_Long_Term_Forecast import argparse # Complete configuration for experiment args = argparse.Namespace( # Model config model='TimeBridge', model_id='ETTh1_720_96', # Data config data='ETTh1', root_path='./dataset/ETT-small/', data_path='ETTh1.csv', features='M', target='OT', freq='h', checkpoints='./checkpoints/', # Forecasting config seq_len=720, label_len=48, pred_len=96, seasonal_patterns='Monthly', # Model architecture enc_in=7, d_model=128, d_ff=128, n_heads=8, ia_layers=3, pd_layers=1, ca_layers=0, period=24, num_p=None, stable_len=6, # Training config embed='timeF', activation='gelu', dropout=0.0, attn_dropout=0.15, revin=True, output_attention=False, inverse=False, # Optimization batch_size=64, train_epochs=100, learning_rate=0.0002, patience=10, lradj='type1', pct_start=0.2, alpha=0.35, # Time-frequency loss weight loss='MSE', num_workers=10, itr=1, des='Exp', embedding_epochs=5, embedding_lr=0.0005, # GPU config use_gpu=True, gpu=0, use_multi_gpu=False, devices='0,1,2,3', device_ids=[0] ) # Create experiment exp = Exp_Long_Term_Forecast(args) # Training with early stopping setting = f"{args.model_id}_{args.model}_{args.data}_sl{args.seq_len}_pl{args.pred_len}" trained_model = exp.train(setting) # Output: # >>>>>>>start training : ETTh1_720_96_TimeBridge_ETTh1_sl720_pl96>>>>>>>>>> # Epoch: 1, Steps: 100 | Train Loss: 0.3521 Vali Loss: 0.4123 Test Loss: 0.4256 # EarlyStopping: saving model to ./checkpoints/... # Testing and evaluation exp.test(setting, test=1) # Output: # mse:0.3892, mae:0.4012 # rmse:0.6239, mape:0.1823, mspe:0.0421 ``` -------------------------------- ### Load and Process Time Series Datasets with PyTorch Source: https://context7.com/hank0626/timebridge/llms.txt This section details the usage of custom PyTorch Dataset classes for various time series datasets like ETT, Custom CSV, Solar, and PEMS. It shows how to initialize these datasets with parameters such as root path, data path, sequence length, features, target column, scaling, and time encoding. It also demonstrates accessing a single sample and inverse transforming predictions. ```python from data_provider.data_loader import ( Dataset_ETT_hour, Dataset_ETT_minute, Dataset_Custom, Dataset_Solar, Dataset_PEMS ) from torch.utils.data import DataLoader # ETT Hourly Dataset (ETTh1, ETTh2) ett_h_dataset = Dataset_ETT_hour( root_path='./dataset/ETT-small/', data_path='ETTh1.csv', flag='train', # 'train', 'val', or 'test' size=[720, 48, 96], # [seq_len, label_len, pred_len] features='M', # 'M', 'S', or 'MS' target='OT', scale=True, # StandardScaler normalization timeenc=1, # 0: manual encoding, 1: time_features freq='h' ) print(f"ETT-hour dataset length: {len(ett_h_dataset)}") # ETT Minute Dataset (ETTm1, ETTm2) ett_m_dataset = Dataset_ETT_minute( root_path='./dataset/ETT-small/', data_path='ETTm1.csv', flag='train', size=[720, 48, 96], features='M', target='OT', scale=True, timeenc=1, freq='t' ) # Custom CSV Dataset (electricity, traffic, weather) custom_dataset = Dataset_Custom( root_path='./dataset/electricity/', data_path='electricity.csv', flag='train', size=[720, 48, 96], features='M', target='OT', scale=True, timeenc=1, freq='h' ) # Solar Energy Dataset solar_dataset = Dataset_Solar( root_path='./dataset/Solar/', data_path='solar_AL.txt', flag='train', size=[720, 48, 96], features='M', target='OT', scale=True ) # PEMS Traffic Dataset pems_dataset = Dataset_PEMS( root_path='./dataset/PEMS/', data_path='PEMS03.npz', flag='train', size=[720, 48, 96], features='M', target='OT', scale=True ) # Access single sample seq_x, seq_y, seq_x_mark, seq_y_mark = ett_h_dataset[0] print(f"Sequence X shape: {seq_x.shape}") # [seq_len, channels] print(f"Sequence Y shape: {seq_y.shape}") # [label_len+pred_len, channels] # Inverse transform predictions back to original scale scaled_predictions = seq_y[:96] # Example predictions original_scale = ett_h_dataset.inverse_transform(scaled_predictions) ``` -------------------------------- ### Iterate Through Data Batches Source: https://context7.com/hank0626/timebridge/llms.txt This snippet demonstrates how to iterate through data batches from a DataLoader in PyTorch. It prints the shapes of the input features (batch_x), target values (batch_y), and their corresponding timestamps (batch_x_mark, batch_y_mark) for a single batch. This is useful for understanding the data structure during training. ```python for batch_x, batch_y, batch_x_mark, batch_y_mark in train_loader: print(f"Input X: {batch_x.shape}") # [batch, seq_len, channels] print(f"Target Y: {batch_y.shape}") # [batch, label_len+pred_len, channels] print(f"X timestamps: {batch_x_mark.shape}") # [batch, seq_len, time_features] print(f"Y timestamps: {batch_y_mark.shape}") # [batch, label_len+pred_len, time_features] break ``` -------------------------------- ### TimeBridge Forecasting Evaluation Metrics Source: https://context7.com/hank0626/timebridge/llms.txt Shows how to use the comprehensive evaluation metrics provided by TimeBridge for forecasting model assessment. It includes calculating MSE, MAE, RMSE, MAPE, and MSPE using both a combined function and individual metric functions. ```python import numpy as np from utils.metrics import metric, MAE, MSE, RMSE, MAPE, MSPE # Generate sample predictions and ground truth batch_size = 100 pred_len = 96 channels = 7 predictions = np.random.randn(batch_size, pred_len, channels) ground_truth = np.random.randn(batch_size, pred_len, channels) # Calculate all metrics at once mae, mse, rmse, mape, mspe = metric(predictions, ground_truth) print(f"MAE: {mae:.4f}") # Mean Absolute Error print(f"MSE: {mse:.4f}") # Mean Squared Error print(f"RMSE: {rmse:.4f}") # Root Mean Squared Error print(f"MAPE: {mape:.4f}") # Mean Absolute Percentage Error print(f"MSPE: {mspe:.4f}") # Mean Squared Percentage Error # Individual metric functions mae_val = MAE(predictions, ground_truth) mse_val = MSE(predictions, ground_truth) rmse_val = RMSE(predictions, ground_truth) mape_val = MAPE(predictions, ground_truth) mspe_val = MSPE(predictions, ground_truth) # Typical benchmark results on ETTh1 (pred_len=96): # MSE: ~0.370, MAE: ~0.400 ``` -------------------------------- ### TimeBridge Attention Mechanisms: Integrated, Patch Sampling, Cointegrated Source: https://context7.com/hank0626/timebridge/llms.txt Demonstrates the usage of TimeBridge's specialized attention layers: Integrated Attention for temporal modeling, Patch Sampling for downsampling, and Cointegrated Attention for cross-channel dependencies. These layers utilize underlying attention mechanisms like TSMixer and ResAttention. ```python import torch import torch.nn as nn from layers.SelfAttention_Family import TSMixer, ResAttention from layers.Transformer_EncDec import IntAttention, PatchSampling, CointAttention d_model = 128 d_ff = 128 n_heads = 8 enc_in = 7 stable_len = 6 # Integrated Attention Layer - handles non-stationarity with PeriodNorm int_attention = IntAttention( attention=TSMixer( ResAttention(attention_dropout=0.15), d_model=d_model, n_heads=n_heads ), d_model=d_model, d_ff=d_ff, dropout=0.0, stable_len=stable_len, activation='gelu', stable=True, # Enable PeriodNorm for stationarity enc_in=enc_in ) # Input shape: [batch, channels, patches, d_model] x = torch.randn(32, enc_in, 30, d_model) out, attn = int_attention(x) print(f"IntAttention output: {out.shape}") # [32, 7, 30, 128] # Patch Sampling Layer - downsamples patch dimension patch_sampling = PatchSampling( attention=TSMixer( ResAttention(attention_dropout=0.15), d_model=d_model, n_heads=n_heads ), d_model=d_model, d_ff=d_ff, in_p=30, # Input patches out_p=6, # Output patches (downsampled) dropout=0.0, activation='gelu', stable=False, stable_len=stable_len ) out, attn = patch_sampling(x) print(f"PatchSampling output: {out.shape}") # [32, 7, 6, 128] # Cointegrated Attention Layer - models cross-channel relationships coint_attention = CointAttention( attention=TSMixer( ResAttention(attention_dropout=0.15), d_model=d_model, n_heads=n_heads ), d_model=d_model, d_ff=d_ff, dropout=0.0, activation='gelu', stable=False, enc_in=enc_in, stable_len=stable_len, axial=True # Use axial attention for efficiency ) x_small = torch.randn(32, enc_in, 6, d_model) out, attn = coint_attention(x_small) print(f"CointAttention output: {out.shape}") # [32, 7, 6, 128] ``` -------------------------------- ### TimeBridge Patch Embedding for Time Series Source: https://context7.com/hank0626/timebridge/llms.txt Illustrates the PatchEmbed class, which transforms input time series data into patch representations. It concatenates value and time features before projecting them to the model's dimension. ```python import torch from layers.Embed import PatchEmbed import argparse # Configuration args = argparse.Namespace( seq_len=720, d_model=128, dropout=0.0 ) # Create patch embedding num_patches = 30 # seq_len // period patch_embed = PatchEmbed(args, num_p=num_patches, d_model=128) # Input: time series and time marks batch_size = 32 enc_in = 7 time_features = 4 x = torch.randn(batch_size, args.seq_len, enc_in) x_mark = torch.randn(batch_size, args.seq_len, time_features) # Output: patch embeddings [batch, channels+time_features, num_patches, d_model] embedded = patch_embed(x, x_mark) print(f"Input X shape: {x.shape}") # [32, 720, 7] print(f"Input X_mark shape: {x_mark.shape}") # [32, 720, 4] print(f"Embedded shape: {embedded.shape}") # [32, 11, 30, 128] ``` -------------------------------- ### Time-Frequency MAE Loss Function (Python) Source: https://context7.com/hank0626/timebridge/llms.txt Implements a combined time-domain and frequency-domain Mean Absolute Error (MAE) loss function using PyTorch and FFT. This loss is designed to enhance long-term forecasting performance by considering both temporal and spectral characteristics of the time series. It takes ground truth and predicted tensors as input and returns a weighted combination of time and frequency losses. ```python import torch def time_freq_mae(batch_y, outputs, alpha=0.35): """ Combined time-domain and frequency-domain MAE loss. Args: batch_y: Ground truth tensor [batch, pred_len, channels] outputs: Predictions tensor [batch, pred_len, channels] alpha: Weight for frequency loss (0.0-1.0) Returns: Combined loss value """ # Time-domain MAE loss t_loss = (outputs - batch_y).abs().mean() # Frequency-domain MAE loss using FFT f_loss = torch.fft.rfft(outputs, dim=1) - torch.fft.rfft(batch_y, dim=1) f_loss = f_loss.abs().mean() # Weighted combination return (1 - alpha) * t_loss + alpha * f_loss # Example usage batch_size = 32 pred_len = 96 channels = 7 predictions = torch.randn(batch_size, pred_len, channels) ground_truth = torch.randn(batch_size, pred_len, channels) # Calculate loss with default alpha=0.35 loss = time_freq_mae(ground_truth, predictions, alpha=0.35) print(f"Time-frequency loss: {loss.item():.4f}") # Different alpha values for various datasets: # ETTh1/ETTh2/ETTm1/ETTm2: alpha=0.35 # Weather: alpha=0.1 # Solar: alpha=0.05 # Electricity: alpha=0.2 # Traffic: alpha=0.35 ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.