-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add autoregressive neural network models
- Loading branch information
Showing
1 changed file
with
33 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import torch | ||
from torch import nn | ||
|
||
from torchts.nn.model import TimeSeriesModel | ||
|
||
|
||
class SimpleAR(TimeSeriesModel): | ||
def __init__(self, p, bias=True, **kwargs): | ||
super().__init__(**kwargs) | ||
self.linear = nn.Linear(p, 1, bias=bias) | ||
|
||
def forward(self, x): | ||
return self.linear(x) | ||
|
||
|
||
class MultiAR(TimeSeriesModel): | ||
def __init__(self, p, k, bias=True, **kwargs): | ||
super().__init__(**kwargs) | ||
self.p = p | ||
self.k = k | ||
self.layers = nn.ModuleList(nn.Linear(k, k, bias=False) for _ in range(p)) | ||
self.bias = nn.Parameter(torch.zeros(k)) if bias else None | ||
|
||
def forward(self, x): | ||
y = torch.zeros(x.shape[0], self.k) | ||
|
||
for i in range(self.p): | ||
y += self.layers[i](x[:, i, :]) | ||
|
||
if self.bias is not None: | ||
y += self.bias | ||
|
||
return y |