Skip to content

Commit

Permalink
Add autoregressive neural network models
Browse files Browse the repository at this point in the history
  • Loading branch information
klane committed Dec 30, 2021
1 parent 8d8a099 commit e04fd31
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions torchts/nn/models/autoreg.py
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

0 comments on commit e04fd31

Please sign in to comment.