elisim commited on
Commit
2bc420d
1 Parent(s): ee60feb

wip added multivariate

Browse files
.idea/.gitignore ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ # Default ignored files
2
+ /shelf/
3
+ /workspace.xml
4
+ # Editor-based HTTP Client requests
5
+ /httpRequests/
6
+ # Datasource local storage ignored files
7
+ /dataSources/
8
+ /dataSources.local.xml
.idea/Time-Series-Transformers-Comparison.iml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <module type="PYTHON_MODULE" version="4">
3
+ <component name="NewModuleRootManager">
4
+ <content url="file://$MODULE_DIR$" />
5
+ <orderEntry type="jdk" jdkName="hf2" jdkType="Python SDK" />
6
+ <orderEntry type="sourceFolder" forTests="false" />
7
+ </component>
8
+ </module>
.idea/inspectionProfiles/profiles_settings.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <component name="InspectionProjectProfileManager">
2
+ <settings>
3
+ <option name="USE_PROJECT_PROFILE" value="false" />
4
+ <version value="1.0" />
5
+ </settings>
6
+ </component>
.idea/misc.xml ADDED
@@ -0,0 +1,4 @@
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectRootManager" version="2" project-jdk-name="hf2" project-jdk-type="Python SDK" />
4
+ </project>
.idea/modules.xml ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="ProjectModuleManager">
4
+ <modules>
5
+ <module fileurl="file://$PROJECT_DIR$/.idea/Time-Series-Transformers-Comparison.iml" filepath="$PROJECT_DIR$/.idea/Time-Series-Transformers-Comparison.iml" />
6
+ </modules>
7
+ </component>
8
+ </project>
.idea/vcs.xml ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
1
+ <?xml version="1.0" encoding="UTF-8"?>
2
+ <project version="4">
3
+ <component name="VcsDirectoryMappings">
4
+ <mapping directory="" vcs="Git" />
5
+ </component>
6
+ </project>
multivariate/train.py ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from gluonts.dataset.multivariate_grouper import MultivariateGrouper
2
+ from gluonts.time_feature import time_features_from_frequency_str
3
+
4
+ from datasets import load_dataset
5
+ from functools import lru_cache
6
+
7
+ import pandas as pd
8
+ import numpy as np
9
+
10
+ from functools import partial
11
+ from transformers import InformerConfig, InformerForPrediction
12
+
13
+ freq = "1H"
14
+ prediction_length = 48
15
+
16
+
17
+ def get_train_test_datasets():
18
+
19
+ @lru_cache(10_000)
20
+ def convert_to_pandas_period(date, freq):
21
+ return pd.Period(date, freq)
22
+
23
+ def transform_start_field(batch, freq):
24
+ batch["start"] = [convert_to_pandas_period(date, freq) for date in batch["start"]]
25
+ return batch
26
+
27
+ dataset = load_dataset("monash_tsf", "traffic_hourly")
28
+ train_dataset = dataset["train"]
29
+ test_dataset = dataset["test"]
30
+
31
+ train_dataset.set_transform(partial(transform_start_field, freq=freq))
32
+ test_dataset.set_transform(partial(transform_start_field, freq=freq))
33
+
34
+ return train_dataset, test_dataset
35
+
36
+
37
+ def get_train_test_multivariate_grouper(train_dataset, test_dataset):
38
+ num_of_variates = len(train_dataset)
39
+
40
+ train_grouper = MultivariateGrouper(max_target_dim=num_of_variates)
41
+ test_grouper = MultivariateGrouper(
42
+ max_target_dim=num_of_variates,
43
+ num_test_dates=len(test_dataset) // num_of_variates, # number of rolling test windows
44
+ )
45
+ return train_grouper, test_grouper
46
+
47
+
48
+ def get_informer_model(num_of_variates, time_features):
49
+ config = InformerConfig(
50
+ # in the multivariate setting, input_size is the number of variates in the time series per time step
51
+ input_size=num_of_variates,
52
+ # prediction length:
53
+ prediction_length=prediction_length,
54
+ # context length:
55
+ context_length=prediction_length * 2,
56
+ # lags value copied from 1 week before:
57
+ lags_sequence=[1, 24 * 7],
58
+ # we'll add 5 time features ("hour_of_day", ..., and "age"):
59
+ num_time_features=len(time_features) + 1,
60
+
61
+ # informer params:
62
+ dropout=0.1,
63
+ encoder_layers=6,
64
+ decoder_layers=4,
65
+ # project input from num_of_variates*len(lags_sequence)+num_time_features to:
66
+ d_model=64,
67
+ )
68
+
69
+ model = InformerForPrediction(config)
70
+ return model
71
+
72
+
73
+ def main():
74
+ train_dataset, test_dataset = get_train_test_datasets()
75
+ train_grouper, test_grouper = get_train_test_multivariate_grouper(train_dataset, test_dataset)
76
+ multi_variate_train_dataset = train_grouper(train_dataset)
77
+ multi_variate_test_dataset = test_grouper(test_dataset)
78
+
79
+ multi_variate_train_example = multi_variate_train_dataset[0]
80
+ train_example = train_dataset[0]
81
+ print('train_example["target"].shape =', len(train_example["target"]))
82
+ print('multi_variate_train_example["target"].shape =', multi_variate_train_example["target"].shape)
83
+
84
+ time_features = time_features_from_frequency_str(freq)
85
+ print(time_features)
86
+
87
+ informer = get_informer_model(num_of_variates=62, time_features=time_features)
88
+
89
+
90
+ if __name__ == '__main__':
91
+ main()