Skip to content

Commit e806bb7

Browse files
committed
Added the weather dataset
The weather dataset and dataloader added
1 parent a9b77f1 commit e806bb7

12 files changed

+23090
-7
lines changed

experiments/weather/__init__.py

Whitespace-only changes.

experiments/weather/classifier.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import torch as th
2+
3+
from torchmetrics import Accuracy, Precision, Recall, AUROC
4+
from typing import Callable, Union
5+
6+
from classifier_HMM import StateClassifier
7+
from tint.models import Net
8+
9+
10+
class MimicClassifierNet(Net):
11+
def __init__(
12+
self,
13+
feature_size: int,
14+
n_state: int,
15+
hidden_size: int,
16+
rnn: str = "GRU",
17+
dropout: float = 0.5,
18+
regres: bool = True,
19+
bidirectional: bool = False,
20+
loss: Union[str, Callable] = "mse",
21+
optim: str = "adam",
22+
lr: float = 0.001,
23+
lr_scheduler: Union[dict, str] = None,
24+
lr_scheduler_args: dict = None,
25+
l2: float = 0.0,
26+
):
27+
classifier = StateClassifier(
28+
feature_size=feature_size,
29+
n_state=n_state,
30+
hidden_size=hidden_size,
31+
rnn=rnn,
32+
dropout=dropout,
33+
regres=regres,
34+
bidirectional=bidirectional,
35+
)
36+
37+
super().__init__(
38+
layers=classifier,
39+
loss=loss,
40+
optim=optim,
41+
lr=lr,
42+
lr_scheduler=lr_scheduler,
43+
lr_scheduler_args=lr_scheduler_args,
44+
l2=l2,
45+
)
46+
self.save_hyperparameters()
47+
48+
for stage in ["train", "val", "test"]:
49+
setattr(self, stage + "_acc", Accuracy(task="binary"))
50+
setattr(self, stage + "_pre", Precision(task="binary"))
51+
setattr(self, stage + "_rec", Recall(task="binary"))
52+
setattr(self, stage + "_auroc", AUROC(task="binary"))
53+
54+
def forward(self, *args, **kwargs) -> th.Tensor:
55+
return self.net(*args, **kwargs)
56+
57+
def step(self, batch, batch_idx, stage):
58+
x, y = batch
59+
y_hat = self(x)
60+
loss = self.loss(y_hat, y)
61+
62+
for metric in ["acc", "pre", "rec", "auroc"]:
63+
getattr(self, stage + "_" + metric)(y_hat[:, 1], y.long())
64+
self.log(stage + "_" + metric, getattr(self, stage + "_" + metric))
65+
66+
return loss

experiments/weather/classifier_HMM.py

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
import torch as th
2+
import torch.nn as nn
3+
4+
from torchmetrics import Accuracy, Precision, Recall, AUROC
5+
from typing import Callable, Union
6+
7+
from tint.models import Net
8+
9+
10+
class StateClassifier(nn.Module):
11+
def __init__(
12+
self,
13+
feature_size: int,
14+
n_state: int,
15+
hidden_size: int,
16+
rnn: str = "GRU",
17+
dropout: float = 0.5,
18+
regres: bool = True,
19+
bidirectional: bool = False,
20+
):
21+
super().__init__()
22+
self.hidden_size = hidden_size
23+
self.n_state = n_state
24+
self.rnn_type = rnn
25+
self.regres = regres
26+
# Input to torch LSTM should be of size (batch, seq_len, input_size)
27+
if self.rnn_type == "GRU":
28+
self.rnn = nn.GRU(
29+
feature_size,
30+
self.hidden_size,
31+
bidirectional=bidirectional,
32+
batch_first=True,
33+
)
34+
else:
35+
self.rnn = nn.LSTM(
36+
feature_size,
37+
self.hidden_size,
38+
bidirectional=bidirectional,
39+
batch_first=True,
40+
)
41+
42+
self.regressor = nn.Sequential(
43+
nn.BatchNorm1d(num_features=self.hidden_size),
44+
nn.ReLU(),
45+
nn.Dropout(dropout),
46+
nn.Linear(self.hidden_size, self.n_state),
47+
)
48+
49+
def forward(self, x, return_all: bool = False):
50+
if self.rnn_type == "GRU":
51+
all_encodings, encoding = self.rnn(x)
52+
else:
53+
all_encodings, (encoding, state) = self.rnn(x)
54+
55+
if self.regres:
56+
if return_all:
57+
reshaped_encodings = all_encodings.reshape(
58+
all_encodings.shape[0] * all_encodings.shape[1], -1
59+
)
60+
return self.regressor(reshaped_encodings).reshape(
61+
all_encodings.shape[0], all_encodings.shape[1], -1
62+
)
63+
return self.regressor(encoding.reshape(encoding.shape[1], -1))
64+
return encoding.reshape(encoding.shape[1], -1)
65+
66+
67+
class StateClassifierNet(Net):
68+
def __init__(
69+
self,
70+
feature_size: int,
71+
n_state: int,
72+
hidden_size: int,
73+
rnn: str = "GRU",
74+
dropout: float = 0.5,
75+
regres: bool = True,
76+
bidirectional: bool = False,
77+
loss: Union[str, Callable] = "mse",
78+
optim: str = "adam",
79+
lr: float = 0.001,
80+
lr_scheduler: Union[dict, str] = None,
81+
lr_scheduler_args: dict = None,
82+
l2: float = 0.0,
83+
):
84+
classifier = StateClassifier(
85+
feature_size=feature_size,
86+
n_state=n_state,
87+
hidden_size=hidden_size,
88+
rnn=rnn,
89+
dropout=dropout,
90+
regres=regres,
91+
bidirectional=bidirectional,
92+
)
93+
94+
super().__init__(
95+
layers=classifier,
96+
loss=loss,
97+
optim=optim,
98+
lr=lr,
99+
lr_scheduler=lr_scheduler,
100+
lr_scheduler_args=lr_scheduler_args,
101+
l2=l2,
102+
)
103+
self.save_hyperparameters()
104+
105+
for stage in ["train", "val", "test"]:
106+
setattr(self, stage + "_acc", Accuracy(task="binary"))
107+
setattr(self, stage + "_pre", Precision(task="binary"))
108+
setattr(self, stage + "_rec", Recall(task="binary"))
109+
setattr(self, stage + "_auroc", AUROC(task="binary"))
110+
111+
def forward(self, *args, **kwargs) -> th.Tensor:
112+
return self.net(*args, **kwargs)
113+
114+
def step(self, batch, batch_idx, stage):
115+
t = th.randint(batch[1].shape[-1], (1,)).item()
116+
x, y = batch
117+
x = x[:, : t + 1]
118+
y = y[:, t]
119+
y_hat = self(x)
120+
loss = self.loss(y_hat, y)
121+
122+
for metric in ["acc", "pre", "rec", "auroc"]:
123+
getattr(self, stage + "_" + metric)(y_hat[:, 1], y.long())
124+
self.log(stage + "_" + metric, getattr(self, stage + "_" + metric))
125+
126+
return loss
127+
128+
def predict_step(self, batch, batch_idx, dataloader_idx=0):
129+
x, y = batch
130+
return self(x)

0 commit comments

Comments
 (0)