TSMixer: Time Series Mixer for Forecasting
Overview
TSMixer is an unofficial PyTorch-based implementation of the TSMixer architecture as described TSMixer Paper. It leverages mixer layers for processing time series data, offering a robust approach for both standard and extended forecasting tasks.
Installation
You can install the package using pip:
pip install pytorch-tsmixer
or after cloning the repository, you can install it directly from the source code:
pip install .
Modules
tsmixer.py: Contains theTSMixerclass, a model using mixer layers for time series forecasting.tsmixer_ext.py: Implements theTSMixerExtclass, an extended version of TSMixer that integrates additional inputs and contextual information.
Usage
TSMixer
from torchtsmixer import TSMixer
import torch
m = TSMixer(sequence_length=10, prediction_length=5, input_channels=2, output_channels=4)
x = torch.randn(3, 10, 2)
y = m(x)
TSMixerExt
from torchtsmixer import TSMixerExt
import torch
m = TSMixerExt(
sequence_length=10,
prediction_length=5,
input_channels=2,
extra_channels=3,
hidden_channels=8,
static_channels=4,
output_channels=4
)
x_hist = torch.randn(3, 10, 2, requires_grad=True)
x_extra_hist = torch.randn(3, 10, 3, requires_grad=True)
x_extra_future = torch.randn(3, 5, 3, requires_grad=True)
x_static = torch.randn(3, 4, requires_grad=True)
y = m.forward(
x_hist=x_hist,
x_extra_hist=x_extra_hist,
x_extra_future=x_extra_future,
x_static=x_static
)
Example: Training Loop with TSMixer
Here's a basic example of how to use TSMixer in a simple training loop. This example assumes a regression task with a mean squared error loss and an Adam optimizer.
import torch
import torch.nn as nn
import torch.optim as optim
from torchtsmixer import TSMixer
# Model parameters
sequence_length = 10
prediction_length = 5
input_channels = 2
output_channels = 1
# Create the TSMixer model
model = TSMixer(sequence_length, prediction_length, input_channels, output_channels)
# Loss function and optimizer
criterion = nn.MSELoss()
optimizer = optim.Adam(model.parameters(), lr=0.0001)
# Dummy dataset (replace with real data)
# Assuming batch_size, seq_len, num_features format
X_train = torch.randn(10,32, sequence_length, input_channels)
y_train = torch.randn(10,32, prediction_length, output_channels)
# Training loop
num_epochs = 10
for epoch in range(num_epochs):
model.train()
for X,y in zip(X_train, y_train):
# Zero the parameter gradients
optimizer.zero_grad()
# Forward pass
outputs = model(X)
loss = criterion(outputs, y)
# Backward pass and optimize
loss.backward()
optimizer.step()
print(f'Epoch [{epoch+1}/{num_epochs}], Loss: {loss.item():.4f}')
print("Training complete")
This example is quite basic and should be adapted to your specific dataset and task. For instance, you might want to add data loading with DataLoader, validation steps, and more sophisticated training logic.
Testing
Run tests using:
python -m unittest
License
This project is licensed under the MIT License - see the LICENSE file for details.
Acknowledgments
This implementation is based on the TSMixer model as described in TSMixer Paper.
Metadata
Release files for pytorch-tsmixer 0.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| pytorch-tsmixer-0.2.0.tar.gz | 10.1 kB | Without provenance | Details for pytorch-tsmixer-0.2.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| pytorch_tsmixer-0.2.0-py3-none-any.whl | Python 3 | none | any | Details for pytorch_tsmixer-0.2.0-py3-none-any.whl |
Total release size: 20.6 kB
Release files / pytorch-tsmixer-0.2.0.tar.gz
| Download URL | pytorch-tsmixer-0.2.0.tar.gz |
|---|---|
| Size | 10.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3cdae659ff37b1ce2d839508e94e304955a3ce56c4b952758801a05cbc808758
|
|
BLAKE2b-256 checksum How to use checksums |
59c00706e4e0bf0c62c64757f7bf1bb0699233e8f98d8a65ac7dcb152a768f6b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|
Release files / pytorch_tsmixer-0.2.0-py3-none-any.whl
| Download URL | pytorch_tsmixer-0.2.0-py3-none-any.whl |
|---|---|
| Size | 10.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7db21124777b204488ba2a0b72b788924a8c95437ad4de277077fdb2f19b4629
|
|
BLAKE2b-256 checksum How to use checksums |
fed30fc7bc3efd3a2ff4f5684855117f1b2d0d5d5368d64fb4e800f00c82d14f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.2 CPython/3.8.18
|
Security for pytorch-tsmixer 0.2.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.