Comments (3)
Adding a section on the documentation about how to implement a custom model is on the roadmap but not a high priority at the moment. But I'll try to give you the key elements for deep-learning models.
First, you need to determine if the model will support past, future and static covariates (check illustration here).
Based on this, you will be able to decide form which class it should inherit. At the moment, the options are : PastCovariatesTorchModel
, FutureCovariatesTorchModel
, DualCovariatesTorchModel
, MixedCovariatesTorchModel
, SplitCovariatesTorchModel
and they are implemented here. Now, you can create your class with class NameModel([...]CovariatesTorchModel)
, which will requite the following methods : __init__()
and _create_model()
as well as a bunch of properties (check existing models).
Then, you can implement the actual neural network architecture in a separate class, class _NameModel(PL[...]CovariatesModule)
which inherit for the Pytorch-Lightning module corresponding to the class selected above (implemented here). This class requires the __init__()
and forward()
methods to be defined.
That's pretty much it, for deterministic forecast. Probabilistic is a tad more complicated, and I would rather not into details here but I encourage you to check the implementation of any existing model as a reference for your work.
You might need to rely on the documentation for the dataset associated with the model parent class (here) to get the shape and order of the samples that the model will receive as well as the Pytorch-Lightning documentation to learn more about what is happening under the hood with the trainer (here).
from darts.
Recently, I tried to add a simple CNN model for time-series forecasting task based on Darts. And I cannot find any turtorial or guidance either. But I find some information from CHATGPT.
from darts import TimeSeries
from darts.models import TorchForecastingModel
from darts.utils.data import SequentialDataset
import torch
import torch.nn as nn
from torch.utils.data import DataLoader
import pandas as pd
class CustomCNN(nn.Module):
def __init__(self, input_size):
super(CustomCNN, self).__init__()
self.conv1 = nn.Conv1d(1, 16, kernel_size=3, padding=1)
self.act1 = nn.ReLU()
self.fc1 = nn.Linear(input_size * 16, 50)
self.act2 = nn.ReLU()
self.fc2 = nn.Linear(50, 1)
def forward(self, x):
x = x.view(x.size(0), 1, -1) # Reshape input for CNN
x = self.conv1(x)
x = self.act1(x)
x = x.view(x.size(0), -1) # Flatten
x = self.fc1(x)
x = self.act2(x)
x = self.fc2(x)
return x
class CNNForecastingModel(TorchForecastingModel):
def __init__(self, input_chunk_length, output_chunk_length):
self.model = CustomCNN(input_chunk_length)
super().__init__(model=self.model, input_chunk_length=input_chunk_length, output_chunk_length=output_chunk_length)
def train(self, training_series, val_series=None, epochs=15, batch_size=32):
train_dataset = SequentialDataset(training_series, input_chunk_length=self.input_chunk_length, output_chunk_length=self.output_chunk_length)
train_loader = DataLoader(train_dataset, batch_size=batch_size, shuffle=True)
optimizer = torch.optim.Adam(self.model.parameters(), lr=0.001)
loss_fn = nn.MSELoss()
self.model.train()
for epoch in range(epochs):
for x_batch, y_batch in train_loader:
optimizer.zero_grad()
output = self.model(x_batch)
loss = loss_fn(output, y_batch)
loss.backward()
optimizer.step()
# Example: Loading data from a CSV file
df = pd.read_csv('path_to_your_data.csv', parse_dates=['date_column'])
series = TimeSeries.from_dataframe(df, 'date_column', 'value_column')
# Split the series into train and validation sets
train, val = series.split_before(pd.Timestamp("YYYY-MM-DD"))
input_chunk_length = 30 # How many past time steps the model looks at
output_chunk_length = 1 # How many time steps ahead the model predicts
model = CNNForecastingModel(input_chunk_length, output_chunk_length)
model.train(train, val_series=val, epochs=10, batch_size=16)
predicted = model.predict(n=24) # Predict the next 24 time steps
print(predicted)`
but not quite sure this will work :( can someone help me?
from darts.
Hi @GuYith and @noahvand, 你好,、
Adding a section on the documentation about how to implement a custom model is on the roadmap but not a high priority at the moment. But I'll try to give you the key elements for deep-learning models.在文档中添加有关如何实现自定义模型的部分已列入路线图,但目前还不是最优先的事项。不过,我会尽力为您提供深度学习模型的关键要素。
First, you need to determine if the model will support past, future and static covariates (check illustration here).首先,您需要确定模型是否支持过去、未来和静态协变量(在此查看图例)。
Based on this, you will be able to decide form which class it should inherit. At the moment, the options are :
PastCovariatesTorchModel
,FutureCovariatesTorchModel
,DualCovariatesTorchModel
,MixedCovariatesTorchModel
,SplitCovariatesTorchModel
and they are implemented here. Now, you can create your class withclass NameModel([...]CovariatesTorchModel)
, which will requite the following methods :__init__()
and_create_model()
as well as a bunch of properties (check existing models).在此基础上,您可以决定它应该继承哪个类。目前,可供选择的有 :PastCovariatesTorchModel
,FutureCovariatesTorchModel
,DualCovariatesTorchModel
,MixedCovariatesTorchModel
,SplitCovariatesTorchModel
在此实现。现在,你可以用class NameModel([...]CovariatesTorchModel)
创建你的类,这将需要以下方法:__init__()
和_create_model()
以及一系列属性(查看现有模型)。Then, you can implement the actual neural network architecture in a separate class,
class _NameModel(PL[...]CovariatesModule)
which inherit for the Pytorch-Lightning module corresponding to the class selected above (implemented here). This class requires the__init__()
andforward()
methods to be defined.然后,你可以在一个单独的类class _NameModel(PL[...]CovariatesModule)
中实现实际的神经网络架构,该类继承了 Pytorch-Lightning 模块,与上面选择的类(在此实现)相对应。该类需要定义__init__()
和forward()
方法。That's pretty much it, for deterministic forecast. Probabilistic is a tad more complicated, and I would rather not into details here but I encourage you to check the implementation of any existing model as a reference for your work.对于确定性预测来说,差不多就是这样。概率预测稍微复杂一些,我不想在此赘述,但我鼓励你们检查任何现有模型的实施情况,作为你们工作的参考。
You might need to rely on the documentation for the dataset associated with the model parent class (here) to get the shape and order of the samples that the model will receive as well as the Pytorch-Lightning documentation to learn more about what is happening under the hood with the trainer (here).您可能需要依靠与模型父类相关的数据集文档(此处)来获取模型将接收的样本的形状和顺序,以及 Pytorch-Lightning 文档(此处)来了解更多有关训练器的内部情况。
I'm glad to hear that. Thank you very much!
from darts.
Related Issues (20)
- Can we add past_covariates and future_covariates to support in darts/darts/models/forecasting /exponential_smoothing.py ? HOT 2
- [Question] Multi covariates setting and scaling problem using own data HOT 3
- Difficulty in Implementing Model Re-Training for Time-Series Data with Pre-Trained Model HOT 9
- [BUG] ValueError in metric calculation at series intersect slicing with custom frequency HOT 3
- GPU Optimization with Num_Workers not working HOT 8
- Does Darts provide methods for unsupervised anomaly detection models? HOT 5
- FFT 2d forecasting HOT 1
- [Question] Are features scaled when using add_encoders? HOT 2
- [New Model] Pre-trained, "foundational" models HOT 2
- hyperparameter TiDE and TFT no complete trials HOT 3
- [Question] Is it possible to override the models' data loading methods? HOT 4
- Saving/loading model with a checkpoint callback HOT 2
- [BUG] TimeSeries should support the fill_missing_dates=False without freq HOT 1
- [BUG] Error when loading a saved TFTModel checkpoint HOT 2
- What is the difference between variable importance for the encoder and decoder in TFTExplainer? HOT 1
- Rolling prediction ;validation model HOT 1
- Tsmixer incorrect loss_fn/likelihood documentation HOT 1
- [BUG] darts should provide way to input multiprocessing_context parameter for dataloader when fit HOT 3
- [BUG] darts should support re-training base on the old checkpoint, not reset the model HOT 2
- Add KAN (Kolmogorov-Arnold Network) to Darts HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from darts.