### Clone MSPatch Repository Source: https://github.com/zjucaoyz/mspatch/blob/main/README.md This command clones the MSPatch repository from GitHub to your local machine. It is the first step to install and run the framework. ```bash git clone https://github.com/zjucaoyz/MSPatch.git cd MSPatch ``` -------------------------------- ### Start Experiment for Long-Term Forecasting (Python) Source: https://context7.com/zjucaoyz/mspatch/llms.txt This Python code snippet initializes the experiment runner for long-term forecasting tasks using the MSPatch framework. It imports the necessary class `Exp_Long_Term_Forecast` and prepares to set up and execute the training and evaluation loop. ```python from exp.exp_long_term_forecasting import Exp_Long_Term_Forecast import torch ``` -------------------------------- ### Configure Multi-GPU Training Source: https://context7.com/zjucaoyz/mspatch/llms.txt This snippet demonstrates the command-line interface or script configuration for enabling multi-GPU training. It specifies the use of multiple GPUs and lists the device IDs to be utilized for distributed training. ```bash # Example usage for multi-GPU training (adjust device IDs as needed) # python -m torch.distributed.launch --nproc_per_node=4 --master_port=29500 your_script.py --use_multi_gpu --devices "0,1,2,3" ``` -------------------------------- ### Train MSPatch with Multiple GPUs Source: https://context7.com/zjucaoyz/mspatch/llms.txt This command initiates the training process for the MSPatch model, specifically configured for long-term forecasting on the Traffic dataset. It enables multi-GPU training across devices 0, 1, 2, and 3, specifying various hyperparameters for the model and training process. ```bash python run.py \ --task_name long_term_forecast \ --is_training 1 \ --model MSPatch \ --data Traffic \ --root_path ./data/traffic/ \ --data_path traffic.csv \ --features M \ --seq_len 96 \ --pred_len 192 \ --e_layers 2 \ --d_model 512 \ --n_heads 8 \ --train_epochs 10 \ --batch_size 16 \ --learning_rate 0.0001 \ --use_multi_gpu \ --devices 0,1,2,3 \ --des 'traffic_exp' ``` -------------------------------- ### Test MSPatch Model and Load Results Source: https://context7.com/zjucaoyz/mspatch/llms.txt Configures and tests the MSPatch model, loading a pre-trained checkpoint. It sets up arguments for testing, initializes the experiment, specifies the checkpoint setting, and then runs the test function. Predictions, ground truth, and metrics are loaded and printed. ```python import torch import numpy as np from exp.exp_long_term_forecasting import Exp_Long_Term_Forecast # Configure for testing mode class TestArgs: task_name = 'long_term_forecast' is_training = 0 model_id = 'test' model = 'MSPatch' data = 'ETTh1' root_path = './data/ETT/' data_path = 'ETTh1.csv' features = 'MS' seq_len = 96 label_len = 48 pred_len = 96 enc_in = 7 d_model = 512 n_heads = 8 e_layers = 2 d_layers = 1 d_ff = 2048 dropout = 0.1 batch_size = 1 # batch_size=1 for testing use_gpu = True gpu = 0 inverse = False # whether to inverse output data use_dtw = True # compute Dynamic Time Warping metric checkpoints = './checkpoints/' args = TestArgs() # Initialize experiment exp = Exp_Long_Term_Forecast(args) # Define setting to load checkpoint setting = 'long_term_forecast_test_MSPatch_ETTh1_ftMS_sl96_ll48_pl96_dm512_nh8_el2_dl1_df2048_fc1_ebtimeF_dtTrue_experiment_0' # Test with checkpoint exp.test(setting, test=1) # Load results predictions = np.load(f'./results/{setting}/pred.npy') ground_truth = np.load(f'./results/{setting}/true.npy') metrics = np.load(f'./results/{setting}/metrics.npy') mae, mse, rmse, mape, mspe = metrics print(f'MAE: {mae:.4f}, MSE: {mse:.4f}, RMSE: {rmse:.4f}') print(f'MAPE: {mape:.4f}, MSPE: {mspe:.4f}') ``` -------------------------------- ### Load and Preprocess Time Series Data (Python) Source: https://context7.com/zjucaoyz/mspatch/llms.txt This Python script shows how to load and preprocess time series data for MSPatch using the data_provider module. It configures parameters for data source, features, frequency, sequence lengths, and batching. The code prepares training, validation, and test data loaders and illustrates iterating through data batches. ```python from data_provider.data_factory import data_provider import argparse # Configure data loading parameters class Args: data = 'ETTh1' root_path = './data/ETT/' data_path = 'ETTh1.csv' features = 'MS' # M: multivariate predict multivariate, # S: univariate predict univariate, # MS: multivariate predict univariate target = 'OT' freq = 'h' # h: hourly, t: minutely, d: daily, etc. seq_len = 96 label_len = 48 pred_len = 96 batch_size = 32 num_workers = 10 embed = 'timeF' task_name = 'long_term_forecast' seasonal_patterns = 'Monthly' args = Args() # Load training data train_dataset, train_loader = data_provider(args, flag='train') # Load validation data val_dataset, val_loader = data_provider(args, flag='val') # Load test data test_dataset, 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: [batch_size, seq_len, features] - input sequence # batch_y: [batch_size, label_len + pred_len, features] - target sequence # batch_x_mark: [batch_size, seq_len, time_features] - time stamps for input # batch_y_mark: [batch_size, label_len + pred_len, time_features] - time stamps for target pass ``` -------------------------------- ### Custom Dataset Integration for Forecasting Source: https://context7.com/zjucaoyz/mspatch/llms.txt This Python code demonstrates how to integrate a custom dataset into the MSPatch framework using PyTorch's DataLoader. It shows the instantiation of the custom dataset and the creation of a DataLoader for batch processing, along with the expected output shapes for input and target sequences. ```python from data_provider.data_loader import Dataset_Custom from torch.utils.data import DataLoader # Load custom dataset dataset = Dataset_Custom( root_path='./data/custom/', data_path='my_data.csv', flag='train', # 'train', 'val', or 'test' size=[96, 48, 96], # [seq_len, label_len, pred_len] features='M', # feature type target='OT', # target column name timeenc=1, # time encoding (0 or 1) freq='h', # frequency seasonal_patterns=None ) # Create data loader dataloader = DataLoader( dataset, batch_size=32, shuffle=True, num_workers=10, drop_last=True ) # Dataset returns: # - seq_x: historical observations [batch, seq_len, features] # - seq_y: future values [batch, label_len + pred_len, features] # - seq_x_mark: time features for seq_x [batch, seq_len, time_dim] # - seq_y_mark: time features for seq_y [batch, label_len + pred_len, time_dim] for seq_x, seq_y, seq_x_mark, seq_y_mark in dataloader: print(f"Input shape: {seq_x.shape}") print(f"Target shape: {seq_y.shape}") break ``` -------------------------------- ### Initialize MSPatch Model and Perform Forward Pass (Python) Source: https://context7.com/zjucaoyz/mspatch/llms.txt This Python code demonstrates how to initialize the MSPatch model with specific configurations for multi-scale patch lengths and strides. It then performs a forward pass with sample input data to generate predictions. The input is a tensor of shape [batch_size, seq_len, features], and the output is a tensor of predicted timesteps. ```python import torch from models.MSPatch import Model # Configure model parameters class Config: task_name = 'long_term_forecast' seq_len = 96 pred_len = 96 enc_in = 7 # number of input features dec_in = 7 # decoder input size c_out = 7 # output size d_model = 512 n_heads = 8 e_layers = 2 d_layers = 1 d_ff = 2048 dropout = 0.1 moving_avg = 25 activation = 'gelu' output_attention = False factor = 1 block_layers = 2 # Initialize model with multi-scale patches config = Config() model = Model( config, patch_len=[96, 48, 16], # three scales of patch lengths stride=[96, 24, 8] # corresponding strides ) # Forward pass x_enc = torch.randn(32, 96, 7) # [batch_size, seq_len, features] predictions = model(x_enc) # [32, 96, 7] - predicts next 96 timesteps ``` -------------------------------- ### Forecasting Different Horizons with MSPatch Source: https://context7.com/zjucaoyz/mspatch/llms.txt These bash commands illustrate how to configure the MSPatch model for time series forecasting across different prediction horizons (lengths). By adjusting the `--pred_len` argument, users can specify whether they need short-term (96 timesteps), medium-term (192 timesteps), long-term (336 timesteps), or very long-term (720 timesteps) predictions. ```bash # Short-term forecasting (96 timesteps) python run.py --task_name long_term_forecast --model MSPatch --data ETTh1 --pred_len 96 # Medium-term forecasting (192 timesteps) python run.py --task_name long_term_forecast --model MSPatch --data ETTh1 --pred_len 192 # Long-term forecasting (336 timesteps) python run.py --task_name long_term_forecast --model MSPatch --data ETTh1 --pred_len 336 # Very long-term forecasting (720 timesteps) python run.py --task_name long_term_forecast --model MSPatch --data ETTh1 --pred_len 720 ``` -------------------------------- ### Train MSPatch Model Source: https://github.com/zjucaoyz/mspatch/blob/main/README.md This command initiates the training process for the MSPatch model. It specifies the model to be used ('MSPatch'), the dataset ('ETTh1'), and the prediction length ('96'). ```python python run.py --model MSPatch --data ETTh1 --pred_len 96 ``` -------------------------------- ### Train MSPatch Model Source: https://context7.com/zjucaoyz/mspatch/llms.txt Configures and trains the MSPatch model for long-term forecasting. It initializes the experiment with provided arguments, generates a setting name for checkpointing, and then proceeds with the training process. Results and checkpoints are saved based on the setting name. ```python class Args: task_name = 'long_term_forecast' is_training = 1 model_id = 'test' model = 'MSPatch' data = 'ETTh1' root_path = './data/ETT/' data_path = 'ETTh1.csv' features = 'MS' target = 'OT' freq = 'h' checkpoints = './checkpoints/' seq_len = 96 label_len = 48 pred_len = 96 enc_in = 7 dec_in = 7 c_out = 7 d_model = 512 n_heads = 8 e_layers = 2 d_layers = 1 d_ff = 2048 dropout = 0.1 train_epochs = 10 batch_size = 32 patience = 3 learning_rate = 0.0001 loss = 'MSE' lradj = 'type1' use_amp = False use_gpu = True gpu = 0 use_multi_gpu = False devices = '0,1,2,3' use_dtw = False args = Args() # Initialize experiment exp = Exp_Long_Term_Forecast(args) # Generate setting name for checkpointing setting = 'long_term_forecast_test_MSPatch_ETTh1_ftMS_sl96_ll48_pl96_dm512_nh8_el2_dl1_df2048_fc1_ebtimeF_dtTrue_experiment_0' # Train the model print('>>>>>>>start training>>>>>>>>>>>>>>>') trained_model = exp.train(setting) # Test the model print('>>>>>>>testing<<<<<<<<<<<<<<<<<<<<<') exp.test(setting) torch.cuda.empty_cache() ``` -------------------------------- ### Train MSPatch for Long-Term Forecasting (Bash) Source: https://context7.com/zjucaoyz/mspatch/llms.txt This command trains a MSPatch model for long-term time series forecasting using the ETTh1 dataset. It configures parameters such as data path, sequence length, prediction length, model architecture details, and training epochs. The output is a trained model ready for evaluation. ```bash python run.py \ --task_name long_term_forecast \ --is_training 1 \ --model_id ETTh1_96_96 \ --model MSPatch \ --data ETTh1 \ --root_path ./data/ETT/ \ --data_path ETTh1.csv \ --features MS \ --seq_len 96 \ --label_len 48 \ --pred_len 96 \ --e_layers 2 \ --d_layers 1 \ --d_model 512 \ --n_heads 8 \ --d_ff 2048 \ --dropout 0.1 \ --fc_dropout 0.05 \ --head_dropout 0.0 \ --patch_len 16 \ --stride 8 \ --des 'experiment_1' \ --train_epochs 10 \ --patience 3 \ --itr 1 \ --batch_size 32 \ --learning_rate 0.0001 ``` -------------------------------- ### Compute Evaluation Metrics Source: https://context7.com/zjucaoyz/mspatch/llms.txt Calculates various evaluation metrics for time series forecasting tasks. It supports computing all standard metrics (MAE, MSE, RMSE, MAPE, MSPE) at once or individually using dedicated functions. Sample predictions and ground truth data are used for demonstration. ```python import numpy as np from utils.metrics import metric, MAE, MSE, RMSE, MAPE, MSPE # Generate sample predictions and ground truth pred = np.random.randn(1000, 96, 7) # [samples, pred_len, features] true = np.random.randn(1000, 96, 7) # Compute all metrics at once mae, mse, rmse, mape, mspe = metric(pred, true) print(f"Mean Absolute Error: {mae:.6f}") print(f"Mean Squared Error: {mse:.6f}") print(f"Root Mean Squared Error: {rmse:.6f}") print(f"Mean Absolute Percentage Error: {mape:.6f}") print(f"Mean Squared Percentage Error: {mspe:.6f}") # Compute individual metrics mae_value = MAE(pred, true) mse_value = MSE(pred, true) rmse_value = RMSE(pred, true) # Output: # Mean Absolute Error: 1.128456 # Mean Squared Error: 2.003812 # Root Mean Squared Error: 1.415627 # Mean Absolute Percentage Error: 15.234567 # Mean Squared Percentage Error: 3.456789 ``` -------------------------------- ### Multi-scale Patch Embedding in Python Source: https://context7.com/zjucaoyz/mspatch/llms.txt Applies multi-scale patch embedding to input data using the `MultiPatchEmbedding` layer. This layer processes input sequences at different scales (patch lengths) and returns a list of embedded representations. It's useful for capturing features at various temporal resolutions. ```python from layers.Embed import MultiPatchEmbedding import torch # Configure multi-scale patch embedding d_model = 512 patch_len = [96, 48, 16] # three scales stride = [96, 24, 8] padding = [0, 24, 8] dropout = 0.1 # Initialize embedding layer patch_embedding = MultiPatchEmbedding( d_model=d_model, patch_len=patch_len, stride=stride, padding=padding, dropout=dropout ) # Input: [batch_size, n_vars, seq_len] x = torch.randn(32, 7, 96) # Apply multi-scale patch embedding x_list, n_vars = patch_embedding(x) # x_list contains 3 tensors (one per scale): # x_list[0]: [batch_size * n_vars, num_patches_scale_0, d_model] # x_list[1]: [batch_size * n_vars, num_patches_scale_1, d_model] # x_list[2]: [batch_size * n_vars, num_patches_scale_2, d_model] print(f"Number of variables: {n_vars}") print(f"Scale 0 shape: {x_list[0].shape}") print(f"Scale 1 shape: {x_list[1].shape}") print(f"Scale 2 shape: {x_list[2].shape}") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.