### Initialize and Run Crossformer Model Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Provides a Python example for initializing the Crossformer class and performing a forward pass on a sample tensor. ```python import torch from cross_models.cross_former import Crossformer model = Crossformer( data_dim=7, in_len=168, out_len=24, seg_len=6, win_size=2, factor=10, d_model=256, d_ff=512, n_heads=4, e_layers=3, dropout=0.2, baseline=False, device=torch.device('cuda:0') ) batch_size = 32 x = torch.randn(batch_size, 168, 7).to('cuda:0') predictions = model(x) print(f"Input shape: {x.shape}") print(f"Output shape: {predictions.shape}") ``` -------------------------------- ### Orchestrate Training with Exp_crossformer Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Demonstrates how to configure hyperparameters using argparse, initialize the experiment class, and execute the training and testing pipeline. ```python import argparse from cross_exp.exp_crossformer import Exp_crossformer args = argparse.Namespace( data='ETTh1', root_path='./datasets/', data_path='ETTh1.csv', data_split=[12*30*24, 4*30*24, 4*30*24], checkpoints='./checkpoints/', in_len=168, out_len=24, seg_len=6, win_size=2, factor=10, data_dim=7, d_model=256, d_ff=512, n_heads=4, e_layers=3, dropout=0.2, baseline=False, num_workers=0, batch_size=32, train_epochs=20, patience=3, learning_rate=1e-4, lradj='type1', use_gpu=True, gpu=0, use_multi_gpu=False, device_ids=[0] ) exp = Exp_crossformer(args) setting = 'Crossformer_ETTh1_il168_ol24_sl6_win2_fa10_dm256_nh4_el3_itr0' model = exp.train(setting) exp.test(setting, save_pred=True) ``` -------------------------------- ### Implement EarlyStopping Utility Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Shows how to initialize the EarlyStopping class to monitor validation loss and manage model checkpoints during training. ```python from utils.tools import EarlyStopping early_stopping = EarlyStopping( patience=3, verbose=True, delta=0 ) ``` -------------------------------- ### Train Crossformer Models via CLI Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Demonstrates how to train the Crossformer model using the main_crossformer.py script. Supports standard datasets, custom data paths, and multi-GPU configurations. ```bash python main_crossformer.py --data ETTh1 --in_len 168 --out_len 24 --seg_len 6 --learning_rate 1e-4 --itr 1 python main_crossformer.py --data AirQuality --data_path AirQualityUCI.csv --data_dim 13 --in_len 168 --out_len 24 --seg_len 6 --d_model 256 --n_heads 4 --e_layers 3 --dropout 0.2 --batch_size 32 --train_epochs 20 --patience 3 python main_crossformer.py --data ETTh1 --in_len 168 --out_len 24 --seg_len 6 --use_gpu True --use_multi_gpu --devices 0,1,2,3 ``` -------------------------------- ### Reproduce Paper Results Source: https://github.com/thinklab-sjtu/crossformer/blob/master/readme.md Runs predefined shell scripts to reproduce the experimental results presented in the Crossformer ICLR 2023 paper for various datasets. ```bash bash scripts/ETTh1.sh bash scripts/ETTm1.sh bash scripts/WTH.sh bash scripts/ECL.sh bash scripts/ILI.sh bash scripts/Traffic.sh ``` -------------------------------- ### Custom Crossformer Experiment Configuration Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt This Python command demonstrates how to run a custom experiment with the Crossformer model, specifying parameters such as dataset, input/output lengths, segment length, learning rate, and the number of iterations for statistical significance. Results are saved with a detailed naming convention. ```python python main_crossformer.py --data ETTh1 \ --in_len 168 --out_len 24 --seg_len 6 \ --learning_rate 1e-4 --itr 5 # Results are saved to ./results/ with naming convention: # Crossformer_{data}_il{in_len}_ol{out_len}_sl{seg_len}_win{win}_fa{factor}_dm{d_model}_nh{n_heads}_el{e_layers}_itr{iteration} ``` -------------------------------- ### Evaluate Trained Crossformer Models via CLI Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Shows how to evaluate pre-trained models using eval_crossformer.py. It supports metrics calculation, prediction saving, and custom data splitting. ```bash python eval_crossformer.py --checkpoint_root ./checkpoints --setting_name Crossformer_ETTh1_il168_ol24_sl6_win2_fa10_dm256_nh4_el3_itr0 python eval_crossformer.py --checkpoint_root ./checkpoints --setting_name Crossformer_ETTh1_il168_ol24_sl6_win2_fa10_dm256_nh4_el3_itr0 --save_pred --inverse python eval_crossformer.py --checkpoint_root ./checkpoints --setting_name Crossformer_ETTh1_il168_ol24_sl6_win2_fa10_dm256_nh4_el3_itr0 --different_split --data_split 0.6,0.2,0.2 ``` -------------------------------- ### Batch Experiment Execution with Shell Scripts Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt These shell scripts automate the execution of batch experiments for the Crossformer model across different datasets. Each script configures and runs the model for various prediction lengths and hyperparameters, facilitating reproducible research. ```bash # Run all prediction lengths for ETTh1 dataset bash scripts/ETTh1.sh # Executes: # - predict length 24: in_len=168, seg_len=6, lr=1e-4 # - predict length 48: in_len=168, seg_len=6, lr=1e-4 # - predict length 168: in_len=720, seg_len=24, lr=1e-5 # - predict length 336: in_len=720, seg_len=24, lr=1e-5 # - predict length 720: in_len=720, seg_len=24, lr=1e-5 # Run experiments on other datasets bash scripts/ETTm1.sh # ETTm1 dataset (minutely) bash scripts/WTH.sh # Weather dataset (12 dimensions) bash scripts/ECL.sh # Electricity dataset (321 dimensions) bash scripts/ILI.sh # ILI dataset (influenza) bash scripts/Traffic.sh # Traffic dataset (862 dimensions) ``` -------------------------------- ### Initialize DSW Embedding Layer Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Demonstrates the initialization of the Dimension-Segment-Wise (DSW) embedding module used for processing time series segments. ```python import torch from cross_models.cross_embed import DSW_embedding seg_len = 6 d_model = 256 embedding = DSW_embedding(seg_len=seg_len, d_model=d_model) ``` -------------------------------- ### POST /train Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Trains the Crossformer model using the main_crossformer.py script. ```APIDOC ## POST /train ### Description Initiates the training process for the Crossformer model on a specified dataset. ### Method POST (CLI Execution) ### Endpoint main_crossformer.py ### Parameters #### Query Parameters - **data** (string) - Required - Name of the dataset (e.g., ETTh1) - **in_len** (int) - Required - Input sequence length - **out_len** (int) - Required - Output prediction length - **seg_len** (int) - Required - Segment length for DSW embedding - **learning_rate** (float) - Optional - Learning rate for the optimizer - **use_multi_gpu** (bool) - Optional - Flag to enable multi-GPU training ### Request Example python main_crossformer.py --data ETTh1 --in_len 168 --out_len 24 --seg_len 6 ### Response #### Success Response (200) - **checkpoint** (file) - Model weights saved to ./checkpoints/ - **metrics** (file) - Training logs saved to ./results/ ``` -------------------------------- ### POST /evaluate Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Evaluates a pre-trained Crossformer model against test data. ```APIDOC ## POST /evaluate ### Description Loads a saved model checkpoint and computes performance metrics (MAE, MSE, RMSE, MAPE, MSPE). ### Method POST (CLI Execution) ### Endpoint eval_crossformer.py ### Parameters #### Query Parameters - **checkpoint_root** (string) - Required - Path to checkpoints directory - **setting_name** (string) - Required - Unique identifier for the model setting - **save_pred** (bool) - Optional - Flag to save prediction outputs ### Request Example python eval_crossformer.py --checkpoint_root ./checkpoints --setting_name Crossformer_ETTh1_il168_ol24 ### Response #### Success Response (200) - **metrics** (log) - MAE, MSE, RMSE, MAPE, MSPE values output to metric.log ``` -------------------------------- ### PyTorch Training Loop with Early Stopping Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt This Python snippet demonstrates a typical training loop for a time series forecasting model using PyTorch. It includes simulating training and validation loss, and utilizes an early stopping mechanism to halt training when validation performance ceases to improve, saving the best model checkpoint. ```python import torch.nn as nn # Assume early_stopping is an instantiated EarlyStopping object # Example: early_stopping = EarlyStopping(patience=3, delta=0.01) model = nn.Linear(10, 1) checkpoint_path = './checkpoints/my_experiment' for epoch in range(100): # Simulate training and validation train_loss = 1.0 / (epoch + 1) val_loss = 1.0 / (epoch + 1) + 0.1 * (epoch > 5) # Gets worse after epoch 5 # Call early stopping early_stopping(val_loss, model, checkpoint_path) print(f"Epoch {epoch}: val_loss={val_loss:.4f}") if early_stopping.early_stop: print("Early stopping triggered!") break # Output: # Validation loss decreased (inf --> 1.000000). Saving model ... # Epoch 0: val_loss=1.0000 # Validation loss decreased (1.000000 --> 0.500000). Saving model ... # Epoch 1: val_loss=0.5000 # ... # EarlyStopping counter: 1 out of 3 # EarlyStopping counter: 2 out of 3 # EarlyStopping counter: 3 out of 3 # Early stopping triggered! # Best model saved at: ./checkpoints/my_experiment/checkpoint.pth ``` -------------------------------- ### Evaluate Pre-trained Crossformer Model Source: https://github.com/thinklab-sjtu/crossformer/blob/master/readme.md Evaluates an existing trained model checkpoint using a specific setting name. The model metrics are saved to the results directory. ```bash python eval_crossformer.py --checkpoint_root ./checkpoints --setting_name Crossformer_ETTh1_il168_ol24_sl6_win2_fa10_dm256_nh4_el3_itr0 ``` -------------------------------- ### Train and Evaluate Crossformer Model Source: https://github.com/thinklab-sjtu/crossformer/blob/master/readme.md Executes the training and evaluation process for the Crossformer model on a specified dataset. Requires input length, output length, and segment length parameters. ```bash python main_crossformer.py --data ETTh1 --in_len 168 --out_len 24 --seg_len 6 --itr 1 ``` -------------------------------- ### Calculate Forecasting Metrics Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Provides methods to compute standard evaluation metrics like MAE, MSE, RMSE, MAPE, and MSPE for time series forecasting models. ```python import numpy as np from utils.metrics import metric, MAE, MSE, RMSE, MAPE, MSPE pred = np.random.randn(100, 24, 7) true = np.random.randn(100, 24, 7) mae_value = MAE(pred, true) mse_value = MSE(pred, true) rmse_value = RMSE(pred, true) mape_value = MAPE(pred, true) mspe_value = MSPE(pred, true) mae, mse, rmse, mape, mspe = metric(pred, true) ``` -------------------------------- ### Normalize Data with StandardScaler Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Demonstrates z-score normalization for both numpy arrays and PyTorch tensors, including fitting, transforming, and inverse transforming data. ```python import numpy as np import torch from utils.tools import StandardScaler scaler = StandardScaler() train_data = np.random.randn(1000, 7) scaler.fit(train_data) scaled_data = scaler.transform(np.random.randn(100, 7)) restored_data = scaler.inverse_transform(scaled_data) tensor_data = torch.randn(100, 7) scaled_tensor = scaler.transform(tensor_data) ``` -------------------------------- ### Initialize Embedding Layer Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Initializes the embedding layer for time series data. It takes raw time series input and transforms it into a higher-dimensional representation suitable for downstream processing. The input shape is [batch_size, time_series_length, dimensions], and the output shape is [batch_size, dimensions, num_segments, d_model]. ```python batch_size = 32 ts_len = 168 ts_dim = 7 x = torch.randn(batch_size, ts_len, ts_dim) # Assuming 'embedding' is an initialized torch.nn.Module # embedding = YourEmbeddingModule(...) x_embed = embedding(x) print(f"Input shape: {x.shape}") print(f"Output shape: {x_embed.shape}") ``` -------------------------------- ### Dataset_MTS Data Loader for Multivariate Time Series Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt The Dataset_MTS data loader handles multivariate time series data from CSV files. It supports automatic train/validation/test splitting using either ratios or fixed sample counts, and applies standard scaling. It is designed to be used with PyTorch's DataLoader. ```python from data.data_loader import Dataset_MTS from torch.utils.data import DataLoader # Load dataset with ratio-based split dataset = Dataset_MTS( root_path='./datasets/', data_path='ETTh1.csv', flag='train', size=[168, 24], data_split=[0.7, 0.1, 0.2], scale=True, scale_statistic=None ) # Load with fixed number split dataset_fixed = Dataset_MTS( root_path='./datasets/', data_path='ETTh1.csv', flag='train', size=[168, 24], data_split=[12*30*24, 4*30*24, 4*30*24], scale=True ) # Create DataLoader dataloader = DataLoader( dataset, batch_size=32, shuffle=True, num_workers=0, drop_last=False ) ``` -------------------------------- ### Two-Stage Attention Layer (TSA) Implementation Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt Implements the Two-Stage Attention layer which performs cross-time and cross-dimension attention efficiently. It uses a router mechanism to achieve O(D) complexity. The input and output shapes are both [batch_size, data_dim, seg_num, d_model]. ```python import torch from cross_models.attn import TwoStageAttentionLayer seg_num = 28 factor = 10 d_model = 256 n_heads = 4 d_ff = 512 dropout = 0.1 tsa_layer = TwoStageAttentionLayer( seg_num=seg_num, factor=factor, d_model=d_model, n_heads=n_heads, d_ff=d_ff, dropout=dropout ) batch_size = 32 data_dim = 7 x = torch.randn(batch_size, data_dim, seg_num, d_model) output = tsa_layer(x) print(f"Input shape: {x.shape}") print(f"Output shape: {output.shape}") ``` -------------------------------- ### Multi-Scale Decoder for Forecasting Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt The Multi-Scale Decoder generates predictions at various scales and combines them using additive fusion for the final forecast. It takes learnable position embeddings and encoder outputs as input. The final output shape is [batch_size, out_len, data_dim]. ```python import torch from cross_models.cross_decoder import Decoder seg_len = 6 decoder = Decoder( seg_len=seg_len, d_layers=4, d_model=256, n_heads=4, d_ff=512, dropout=0.2, out_seg_num=4, factor=10 ) batch_size = 32 data_dim = 7 out_seg_num = 4 dec_input = torch.randn(batch_size, data_dim, out_seg_num, 256) encoder_outputs = [ torch.randn(batch_size, data_dim, 28, 256), torch.randn(batch_size, data_dim, 28, 256), torch.randn(batch_size, data_dim, 14, 256), torch.randn(batch_size, data_dim, 7, 256) ] predictions = decoder(dec_input, encoder_outputs) print(f"Prediction shape: {predictions.shape}") ``` -------------------------------- ### Hierarchical Encoder for Multi-Scale Patterns Source: https://context7.com/thinklab-sjtu/crossformer/llms.txt The Hierarchical Encoder progressively merges segments to capture multi-scale temporal patterns. It utilizes scale blocks, segment merging, and TSA layers. The input is [batch_size, data_dim, seg_num, d_model], and the output is a list of encoded representations at each scale. ```python import torch from cross_models.cross_encoder import Encoder encoder = Encoder( e_blocks=3, win_size=2, d_model=256, n_heads=4, d_ff=512, block_depth=1, dropout=0.2, in_seg_num=28, factor=10 ) batch_size = 32 x = torch.randn(batch_size, 7, 28, 256) encode_outputs = encoder(x) print(f"Number of scale outputs: {len(encode_outputs)}") for i, enc in enumerate(encode_outputs): print(f"Scale {i} shape: {enc.shape}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.