/
M5ForecastingDeepAR.java
387 lines (338 loc) · 15.8 KB
/
M5ForecastingDeepAR.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
* Copyright 2022 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"). You may not use this file except in compliance
* with the License. A copy of the License is located at
*
* http://aws.amazon.com/apache2.0/
*
* or in the "license" file accompanying this file. This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES
* OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions
* and limitations under the License.
*/
package ai.djl.examples.inference.timeseries;
import ai.djl.Application;
import ai.djl.ModelException;
import ai.djl.basicdataset.BasicDatasets;
import ai.djl.basicdataset.tabular.utils.DynamicBuffer;
import ai.djl.basicdataset.tabular.utils.Feature;
import ai.djl.inference.Predictor;
import ai.djl.ndarray.NDArray;
import ai.djl.ndarray.NDArrays;
import ai.djl.ndarray.NDList;
import ai.djl.ndarray.NDManager;
import ai.djl.ndarray.types.DataType;
import ai.djl.ndarray.types.Shape;
import ai.djl.repository.Artifact;
import ai.djl.repository.MRL;
import ai.djl.repository.Repository;
import ai.djl.repository.zoo.Criteria;
import ai.djl.repository.zoo.ZooModel;
import ai.djl.timeseries.Forecast;
import ai.djl.timeseries.SampleForecast;
import ai.djl.timeseries.TimeSeriesData;
import ai.djl.timeseries.dataset.FieldName;
import ai.djl.timeseries.translator.DeepARTranslatorFactory;
import ai.djl.training.loss.Loss;
import ai.djl.training.util.ProgressBar;
import ai.djl.translate.TranslateException;
import ai.djl.util.Progress;
import org.apache.commons.csv.CSVFormat;
import org.apache.commons.csv.CSVParser;
import org.apache.commons.csv.CSVRecord;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.URL;
import java.nio.FloatBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public final class M5ForecastingDeepAR {
private static final Logger logger = LoggerFactory.getLogger(M5ForecastingDeepAR.class);
private M5ForecastingDeepAR() {}
public static void main(String[] args) throws IOException, TranslateException, ModelException {
Map<String, Float> metrics = predict();
for (Map.Entry<String, Float> entry : metrics.entrySet()) {
logger.info("{}", String.format("metric: %s:\t%.2f", entry.getKey(), entry.getValue()));
}
}
public static Map<String, Float> predict()
throws IOException, TranslateException, ModelException {
NDManager manager = NDManager.newBaseManager("MXNet");
// To use local dataset, users can load data as follows
// Repository repository = Repository.newInstance("local_dataset",
// Paths.get("YOUR_Path/m5-forecasting-accuracy"));
// Then add the setting `.optRepository(repository)` to the builder below
M5Dataset dataset = M5Dataset.builder().setManager(manager).build();
// Note that, for a DeepAR model directly exported from MXNet, the tensor shape of the
// `begin_state` may be problematic, as indicated in this
// [issue](https://github.com/deepjavalibrary/djl/issues/2106#issuecomment-1295703321). As
// described there, you need to "change every begin_state's shape to (-1, 40)".
// For a DeepAR model exported from PyTorch, please take a look at the sample training
// code, which yields a model that has compatible inputs with the DJL timeseries package.
// https://gist.github.com/Carkham/a5162c9298bc51fec648a458a3437008#file-m5torch-py
// Here you can also use local file: modelUrl = "LOCAL_PATH/deepar.pt";
String modelUrl = "djl://ai.djl.mxnet/deepar/0.0.1/m5forecast";
int predictionLength = 4;
Criteria<TimeSeriesData, Forecast> criteria =
Criteria.builder()
.setTypes(TimeSeriesData.class, Forecast.class)
.optModelUrls(modelUrl)
.optEngine("MXNet")
.optTranslatorFactory(new DeepARTranslatorFactory())
.optArgument("prediction_length", predictionLength)
.optArgument("freq", "W")
.optArgument("use_feat_dynamic_real", "false")
.optArgument("use_feat_static_cat", "false")
.optArgument("use_feat_static_real", "false")
.optProgress(new ProgressBar())
.build();
try (ZooModel<TimeSeriesData, Forecast> model = criteria.loadModel();
Predictor<TimeSeriesData, Forecast> predictor = model.newPredictor()) {
M5Evaluator evaluator = new M5Evaluator(0.5f, 0.67f, 0.95f, 0.99f);
Progress progress = new ProgressBar();
progress.reset("Inferring", dataset.size);
for (NDList data : dataset) {
NDArray array = data.singletonOrThrow();
NDArray gt = array.get("{}:", -predictionLength);
NDArray pastTarget = array.get(":{}", -predictionLength);
TimeSeriesData input = new TimeSeriesData(10);
input.setStartTime(LocalDateTime.parse("2011-01-29T00:00"));
input.setField(FieldName.TARGET, pastTarget);
Forecast forecast = predictor.predict(input);
// We focus on the metric Weighted Root Mean Squared Scaled Error (RMSSE) same as
// https://www.kaggle.com/competitions/m5-forecasting-accuracy/overview/evaluation
// The error is not small compared to the data values (sale amount). This is because
// The model is trained on a sparse data with many zeros. This will be improved by
// aggregating/coarse graining the data. See https://github.com/Carkham/m5_blog
evaluator.aggregateMetrics(evaluator.getMetricsPerTs(gt, pastTarget, forecast));
progress.increment(1);
// save data for plotting. Please see the corresponding python script from
// https://gist.github.com/Carkham/a5162c9298bc51fec648a458a3437008
NDArray samples = ((SampleForecast) forecast).getSortedSamples();
samples.setName("samples");
saveNDArray(samples);
}
manager.close();
return evaluator.computeTotalMetrics();
}
}
private static void saveNDArray(NDArray array) throws IOException {
Path path = Paths.get("build").resolve(array.getName() + ".npz");
try (OutputStream os = Files.newOutputStream(path)) {
new NDList(new NDList(array)).encode(os, NDList.Encoding.NPZ);
}
}
/**
* M5 Forecasting - Accuracy from <a
* href="https://www.kaggle.com/competitions/m5-forecasting-accuracy">https://www.kaggle.com/competitions/m5-forecasting-accuracy</a>
*
* <p>Each csvRecord contains a target from "d_1" to "d_1941".
*/
private static final class M5Dataset implements Iterable<NDList>, Iterator<NDList> {
private NDManager manager;
private List<Feature> target;
private List<CSVRecord> csvRecords;
private long size;
private long current;
M5Dataset(Builder builder) {
manager = builder.manager;
target = builder.target;
try {
prepare(builder);
} catch (Exception e) {
throw new AssertionError("Failed to read files.", e);
}
size = csvRecords.size();
}
private void prepare(Builder builder) throws IOException {
MRL mrl = builder.getMrl();
Artifact artifact = mrl.getDefaultArtifact();
mrl.prepare(artifact, null);
Path root = mrl.getRepository().getResourceDirectory(artifact);
Path csvFile = root.resolve("weekly_sales_train_evaluation.csv");
URL csvUrl = csvFile.toUri().toURL();
try (Reader reader =
new InputStreamReader(
new BufferedInputStream(csvUrl.openStream()), StandardCharsets.UTF_8)) {
CSVParser csvParser = new CSVParser(reader, builder.csvFormat);
csvRecords = csvParser.getRecords();
}
}
@Override
public boolean hasNext() {
return current < size;
}
@Override
public NDList next() {
NDList data = getRowFeatures(manager, current, target);
current++;
return data;
}
public static Builder builder() {
return new Builder();
}
private NDList getRowFeatures(NDManager manager, long index, List<Feature> selected) {
DynamicBuffer bb = new DynamicBuffer();
for (Feature feature : selected) {
String name = feature.getName();
String value = getCell(index, name);
feature.getFeaturizer().featurize(bb, value);
}
FloatBuffer buf = bb.getBuffer();
return new NDList(manager.create(buf, new Shape(bb.getLength())));
}
private String getCell(long rowIndex, String featureName) {
CSVRecord record = csvRecords.get(Math.toIntExact(rowIndex));
return record.get(featureName);
}
@Override
public Iterator<NDList> iterator() {
return this;
}
public static final class Builder {
NDManager manager;
List<Feature> target;
CSVFormat csvFormat;
Repository repository;
String groupId;
String artifactId;
String version;
Builder() {
repository = BasicDatasets.REPOSITORY;
groupId = BasicDatasets.GROUP_ID;
artifactId = "m5forecast-unittest";
version = "1.0";
csvFormat =
CSVFormat.DEFAULT
.builder()
.setHeader()
.setSkipHeaderRecord(true)
.setIgnoreHeaderCase(true)
.setTrim(true)
.build();
target = new ArrayList<>();
for (int i = 1; i <= 277; i++) {
target.add(new Feature("w_" + i, true));
}
}
public Builder optRepository(Repository repository) {
this.repository = repository;
return this;
}
public Builder setManager(NDManager manager) {
this.manager = manager;
return this;
}
public M5Dataset build() {
return new M5Dataset(this);
}
MRL getMrl() {
return repository.dataset(Application.Tabular.ANY, groupId, artifactId, version);
}
}
}
/** An evaluator that calculates performance metrics. */
public static final class M5Evaluator {
private float[] quantiles;
Map<String, Float> totalMetrics;
Map<String, Integer> totalNum;
public M5Evaluator(float... quantiles) {
this.quantiles = quantiles;
totalMetrics = new ConcurrentHashMap<>();
totalNum = new ConcurrentHashMap<>();
init();
}
public Map<String, Float> getMetricsPerTs(
NDArray gtTarget, NDArray pastTarget, Forecast forecast) {
Map<String, Float> retMetrics =
new ConcurrentHashMap<>((8 + quantiles.length * 2) * 3 / 2);
NDArray meanFcst = forecast.mean();
NDArray medianFcst = forecast.median();
NDArray meanSquare = gtTarget.sub(meanFcst).square().mean();
NDArray scaleDenom = gtTarget.get("1:").sub(gtTarget.get(":-1")).square().mean();
NDArray rmsse = meanSquare.div(scaleDenom).sqrt();
rmsse = NDArrays.where(scaleDenom.eq(0), rmsse.onesLike(), rmsse);
retMetrics.put("RMSSE", rmsse.getFloat());
retMetrics.put("MSE", gtTarget.sub(meanFcst).square().mean().getFloat());
retMetrics.put("abs_error", gtTarget.sub(medianFcst).abs().sum().getFloat());
retMetrics.put("abs_target_sum", gtTarget.abs().sum().getFloat());
retMetrics.put("abs_target_mean", gtTarget.abs().mean().getFloat());
retMetrics.put(
"MAPE", gtTarget.sub(medianFcst).abs().div(gtTarget.abs()).mean().getFloat());
retMetrics.put(
"sMAPE",
gtTarget.sub(medianFcst)
.abs()
.div(gtTarget.abs().add(medianFcst.abs()))
.mean()
.mul(2)
.getFloat());
retMetrics.put("ND", retMetrics.get("abs_error") / retMetrics.get("abs_target_sum"));
for (float quantile : quantiles) {
NDArray forecastQuantile = forecast.quantile(quantile);
NDArray quantileLoss =
Loss.quantileL1Loss(quantile)
.evaluate(new NDList(gtTarget), new NDList(forecastQuantile));
NDArray quantileCoverage =
gtTarget.lt(forecastQuantile).toType(DataType.FLOAT32, false).mean();
retMetrics.put(
String.format("QuantileLoss[%.2f]", quantile), quantileLoss.getFloat());
retMetrics.put(
String.format("Coverage[%.2f]", quantile), quantileCoverage.getFloat());
}
return retMetrics;
}
public void aggregateMetrics(Map<String, Float> metrics) {
for (Map.Entry<String, Float> entry : metrics.entrySet()) {
totalMetrics.compute(entry.getKey(), (k, v) -> v + entry.getValue());
totalNum.compute(entry.getKey(), (k, v) -> v + 1);
}
}
public Map<String, Float> computeTotalMetrics() {
for (Map.Entry<String, Integer> entry : totalNum.entrySet()) {
if (!entry.getKey().contains("sum")) {
totalMetrics.compute(entry.getKey(), (k, v) -> v / (float) entry.getValue());
}
}
totalMetrics.put("RMSE", (float) Math.sqrt(totalMetrics.get("MSE")));
totalMetrics.put(
"NRMSE", totalMetrics.get("RMSE") / totalMetrics.get("abs_target_mean"));
return totalMetrics;
}
private void init() {
List<String> metricNames =
new ArrayList<>(
Arrays.asList(
"RMSSE",
"MSE",
"abs_error",
"abs_target_sum",
"abs_target_mean",
"MAPE",
"sMAPE",
"ND"));
for (float quantile : quantiles) {
metricNames.add(String.format("QuantileLoss[%.2f]", quantile));
metricNames.add(String.format("Coverage[%.2f]", quantile));
}
for (String metricName : metricNames) {
totalMetrics.put(metricName, 0f);
totalNum.put(metricName, 0);
}
}
}
}