forked from deepjavalibrary/djl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[timeseries] add some basic block and deepAR model (deepjavalibrary#2027
) * feature: add TimeSeriesDataset and training transform * feature: some basic block and deepar model * feature: add train example * feature: add m5-demo and air passengers demo Co-authored-by: Carkham <1302112560@qq.com> Co-authored-by: Frank Liu <frankfliu2000@gmail.com> Co-authored-by: KexinFeng <fengx463@umn.edu>
- Loading branch information
Showing
32 changed files
with
3,264 additions
and
105 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
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
66 changes: 66 additions & 0 deletions
66
.../src/test/resources/mlrepo/model/timeseries/forecasting/ai/djl/mxnet/deepar/metadata.json
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,66 @@ | ||
{ | ||
"metadataVersion": "0.2", | ||
"resourceType": "model", | ||
"application": "timeseries/forecasting", | ||
"groupId": "ai.djl.mxnet", | ||
"artifactId": "resnest", | ||
"name": "deepar", | ||
"description": "DeepAR model for timeseries forecasting", | ||
"website": "http://www.djl.ai/engines/mxnet/model-zoo", | ||
"licenses": { | ||
"apache": { | ||
"name": "The Apache License, Version 2.0", | ||
"url": "https://www.apache.org/licenses/LICENSE-2.0" | ||
} | ||
}, | ||
"artifacts": [ | ||
{ | ||
"version": "0.0.1", | ||
"snapshot": false, | ||
"name": "airpassengers", | ||
"properties": { | ||
"dataset": "airpassengers" | ||
}, | ||
"arguments": { | ||
"prediction_length": 12, | ||
"freq": "M", | ||
"use_feat_dynamic_real": false, | ||
"use_feat_static_cat": false, | ||
"use_feat_static_real": false, | ||
"translatorFactory": "ai.djl.timeseries.translator.DeepARTranslatorFactory" | ||
}, | ||
"files": { | ||
"model": { | ||
"uri": "0.0.1/airpassengers.zip", | ||
"sha1Hash": "1c99cdaefb79c3e63bc7ff1965b0fb2ba45e96c3", | ||
"name": "", | ||
"size": 106895 | ||
} | ||
} | ||
}, | ||
{ | ||
"version": "0.0.1", | ||
"snapshot": false, | ||
"name": "m5forecast", | ||
"properties": { | ||
"dataset": "m5forecast" | ||
}, | ||
"arguments": { | ||
"prediction_length": 4, | ||
"freq": "W", | ||
"use_feat_dynamic_real": false, | ||
"use_feat_static_cat": false, | ||
"use_feat_static_real": false, | ||
"translatorFactory": "ai.djl.timeseries.translator.DeepARTranslatorFactory" | ||
}, | ||
"files": { | ||
"model": { | ||
"uri": "0.0.1/m5forecast.zip", | ||
"sha1Hash": "e251628df3a246911479de0ed36762515a5df241", | ||
"name": "", | ||
"size": 96363 | ||
} | ||
} | ||
} | ||
] | ||
} |
128 changes: 128 additions & 0 deletions
128
examples/src/main/java/ai/djl/examples/inference/timeseries/AirPassengersDeepAR.java
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,128 @@ | ||
/* | ||
* 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.ModelException; | ||
import ai.djl.inference.Predictor; | ||
import ai.djl.ndarray.NDArray; | ||
import ai.djl.ndarray.NDList; | ||
import ai.djl.ndarray.NDManager; | ||
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.training.util.ProgressBar; | ||
import ai.djl.translate.DeferredTranslatorFactory; | ||
import ai.djl.translate.TranslateException; | ||
|
||
import com.google.gson.GsonBuilder; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStreamReader; | ||
import java.io.OutputStream; | ||
import java.io.Reader; | ||
import java.net.URL; | ||
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.time.ZoneId; | ||
import java.util.Date; | ||
|
||
public final class AirPassengersDeepAR { | ||
|
||
private static final Logger logger = LoggerFactory.getLogger(AirPassengersDeepAR.class); | ||
|
||
private AirPassengersDeepAR() {} | ||
|
||
public static void main(String[] args) throws IOException, TranslateException, ModelException { | ||
float[] results = predict(); | ||
logger.info("{}", results); | ||
} | ||
|
||
public static float[] predict() throws IOException, TranslateException, ModelException { | ||
Criteria<TimeSeriesData, Forecast> criteria = | ||
Criteria.builder() | ||
.setTypes(TimeSeriesData.class, Forecast.class) | ||
.optModelUrls("djl://ai.djl.mxnet/deepar/0.0.1/airpassengers") | ||
.optEngine("MXNet") | ||
.optTranslatorFactory(new DeferredTranslatorFactory()) | ||
.optArgument("prediction_length", 12) | ||
.optArgument("freq", "M") | ||
.optArgument("use_feat_dynamic_real", false) | ||
.optArgument("use_feat_static_cat", false) | ||
.optArgument("use_feat_static_real", false) | ||
.optProgress(new ProgressBar()) | ||
.build(); | ||
|
||
String url = "https://resources.djl.ai/test-models/mxnet/timeseries/air_passengers.json"; | ||
|
||
try (ZooModel<TimeSeriesData, Forecast> model = criteria.loadModel(); | ||
Predictor<TimeSeriesData, Forecast> predictor = model.newPredictor(); | ||
NDManager manager = NDManager.newBaseManager(null, "MXNet")) { | ||
TimeSeriesData data = getTimeSeriesData(manager, new URL(url)); | ||
|
||
// save data for plotting | ||
NDArray target = data.get(FieldName.TARGET); | ||
target.setName("target"); | ||
saveNDArray(target); | ||
|
||
Forecast forecast = predictor.predict(data); | ||
|
||
// 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); | ||
return forecast.mean().toFloatArray(); | ||
} | ||
} | ||
|
||
private static TimeSeriesData getTimeSeriesData(NDManager manager, URL url) throws IOException { | ||
try (Reader reader = new InputStreamReader(url.openStream(), StandardCharsets.UTF_8)) { | ||
AirPassengers passengers = | ||
new GsonBuilder() | ||
.setDateFormat("yyyy-MM") | ||
.create() | ||
.fromJson(reader, AirPassengers.class); | ||
|
||
LocalDateTime start = | ||
passengers.start.toInstant().atZone(ZoneId.systemDefault()).toLocalDateTime(); | ||
NDArray target = manager.create(passengers.target); | ||
TimeSeriesData data = new TimeSeriesData(10); | ||
data.setStartTime(start); | ||
data.setField(FieldName.TARGET, target); | ||
return data; | ||
} | ||
} | ||
|
||
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, true); | ||
} | ||
} | ||
|
||
private static final class AirPassengers { | ||
|
||
Date start; | ||
float[] target; | ||
} | ||
} |
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
15 changes: 15 additions & 0 deletions
15
examples/src/main/java/ai/djl/examples/inference/timeseries/package-info.java
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,15 @@ | ||
/* | ||
* 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. | ||
*/ | ||
|
||
/** Contains examples of time series forecasting. */ | ||
package ai.djl.examples.inference.timeseries; |
Oops, something went wrong.