Code Monkey home page Code Monkey logo

sagemaker-deep-demand-forecast's Introduction

Deep Demand Forecasting with Amazon SageMaker

Actions Status CodeQL Status License: Apache-2.0 Code style: black
Maintenance AMA OSS Love

This project provides an end-to-end solution for Demand Forecasting task using a new state-of-the-art Deep Learning model LSTNet available in GluonTS and Amazon SageMaker.

Overview

How Does the Input Data Look Like?

The input data is a multi-variate time-series.

An example includes hourly electricity consumption of 321 users over the period of 41 months. Here is a snapshot of the normalized data

sample

How to Prepare Your Data to Feed into the Model?

We have provided example of how to feed your time-series data with GluonTS in the notebook. To convert CSV data or other formats to GluonTS format, please see the customization.

What Are the Outputs?

  1. A trained LSTNet model and
  2. A SageMaker endpoint that can predict the future (multi-variate) values given a prediction interval

For example, we can estimate the hourly electricity consumption of 321 users for the coming week.

What Algorithm is Used?

We have implemented LSTNet which is a state-of-the-art Deep Learning model and is available in GluonTS.

What is the Estimated Cost?

Running the solution end-to-end costs less than $5 USD. Please make sure you have read the cleaning up part here.

How does the Data Flow look like?

data_flow

Solution Details

Demand forecasting uses historical time-series data to help streamline the supply-demand decision-making process across businesses. Examples include predicting the number of

  • Customer representatives to hire for multiple locations in the next month
  • Product sales across multiple regions in the next quarter
  • Cloud server usage for next day for a video streaming service
  • Electricity consumption for multiple regions over the next week
  • IoT devices and sensors such as energy consumption

Deep Learning for Time Series Forecasting

The status quo approaches for time-series forecasting include:

These methods often require tedious data preprocessing and features generation prior to model training. One main advantage of Deep Learning (DL) methods such as LSTNet is automating the feature generation step prior to model training such as incorporating various data normalization, lags, different time scales, some categorical data, dealing with missing values, etc. with better prediction power and fast GPU-enabled training and deployment.

Please check out our blog post for more details.

Getting Started

You will need an AWS account to use this solution. Sign up for an account here.

To run this JumpStart 1P Solution and have the infrastructure deploy to your AWS account you will need to create an active SageMaker Studio instance (see Onboard to Amazon SageMaker Studio). When your Studio instance is Ready, use the instructions in SageMaker JumpStart to 1-Click Launch the solution.

The solution artifacts are included in this GitHub repository for reference.

Note: Solutions are available in most regions including us-west-2, and us-east-1.

Caution: Cloning this GitHub repository and running the code manually could lead to unexpected issues! Use the AWS CloudFormation template. You'll get an Amazon SageMaker Notebook instance that's been correctly setup and configured to access the other resources in the solution.

Contents

  • cloudformation/
    • deep-demand-forecast.yaml: The root cloudformation nested stack which creates the AWS stack for this solution
    • deep-demand-forecast-sagemaker-notebook-instance.yaml: Creates SageMaker notebook instance
    • deep-demand-forecast-permissions.yaml: Manages all the permission necessary to launch the stack
    • deep-demand-forecast-endpoint.yaml: Creates demo endpoint using in demo.ipynb
    • solution-assistant: Deletes the created resources such as endpoint, S3 bucket etc. during cleanup
  • src/
    • preprocess/
      • container/: To build and register the preprocessing ECR job
        • Dockerfile: Docker container config
        • build_and_push.sh: Build and push bash scripts used in deep-demand-forecast.ipynb
        • requirements.txt: Dependencies for preprocess.py
      • container_build/: Uses CodeBuild to the build the container for ECR
      • preprocess.py: Preprocessing script
    • deep_demand_forecast/: Contains the train and inference code
      • train.py: SageMaker train code
      • inference.py: SageMaker inference code
      • data.py: GluonTS data preparation
      • metrics.py: A training metric
      • monitor.py: Preparing results for visualization
      • utils.py: Helper functions
      • requirements.txt: Dependencies for SageMaker MXNet Estimator
    • demo.ipynb: Demo notebook to quickly get some predictions from the demo endpoint
    • deep-demand-forecast.ipynb: See below

What Does deep-demand-forecast.ipynb Offer?

The notebook trains an LSTNet estimator on electricity consumption data which is multivariate time-series dataset capturing the electricity consumption (in kW) with 15min frequency from 2011-01-01 to 2014-05-26. We compare the model performance by visualizing the metrics MASE vs. sMAPE.

Finally, we deploy an endpoint for the trained model and can interactively compare its performance by comparing the train, test data and predictions.

interactive

For example, here, re-training with more epochs would be helpful to increase the model performance and we can re-deploy.

Architecture Overview

Here is architecture for the end-to-end training and deployment process

Solution Architecture

  1. The input data located in an Amazon S3 bucket
  2. The provided SageMaker notebook that gets the input data and launches the later stages below
  3. Preprocessing step to normalize the input data. We use SageMaker processing job that is designed as a microservice. This allows users to build and register their own Docker image via Amazon ECR and execute the job using Amazon SageMaker
  4. Training an LSTNet model using the previous preprocessed step and evaluating its results using Amazon SageMaker. If desired, one can deploy the trained model and create a SageMaker endpoint
  5. SageMaker endpoint created from the previous step, is an HTTPS endpoint and is capable of producing predictions
  6. Monitoring the training and deployed model via Amazon CloudWatch

Here is the architecture of the inference

Solution Architecture

  1. The input data, located in an Amazon S3 bucket
  2. From SageMaker notebook, normalize the new input data using the statistics of the training data
  3. Sending the requests to the SageMaker endpoint
  4. Predictions

Cleaning Up

When you've finished with this solution, make sure that you delete all unwanted AWS resources. AWS CloudFormation can be used to automatically delete all standard resources that have been created by the solution and notebook. Go to the AWS CloudFormation Console, and delete the parent stack. Choosing to delete the parent stack will automatically delete the nested stacks.

Caution: You need to manually delete any extra resources that you may have created in this notebook. Some examples include, extra Amazon S3 buckets (to the solution's default bucket), extra Amazon SageMaker endpoints (using a custom name), and extra Amazon ECR repositories.

Customization

To use your own data, please take a look at

Useful Resources

License

This project is licensed under the Apache-2.0 License.

sagemaker-deep-demand-forecast's People

Contributors

amazon-auto avatar ehsanmok avatar hehehe47 avatar voitau avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

sagemaker-deep-demand-forecast's Issues

Error when using Exchange_rate data

Hi Ehsan. I was hoping to get your insight on an error I'm getting. I'm not so sure it's a bug as much as something I might be doing wrong.

I swapped out the 'electricity' data with the GluonTS provided 'exchange_rate' data. Everything works fine up to the point of testing the endpoint. I get the below error message, which I'm having trouble understanding. I'm very new to MXNet, BTW.
The following Model error seems to be the issue:

Check failed: dshape[axes[i]] == 1 (314 vs. 1) : cannot select an axis to squeeze out which has size=314 not equal to one

Anyway, I was curious if you have any ideas? Any help would be greatly appreciated!

  • Andy

Original Error Message:

`ModelError Traceback (most recent call last)
in
11 random_test_data['source'] = []
12
---> 13 random_ret = predictor.predict(random_test_data)

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/sagemaker/predictor.py in predict(self, data, initial_args, target_model, target_variant)
123
124 request_args = self._create_request_args(data, initial_args, target_model, target_variant)
--> 125 response = self.sagemaker_session.sagemaker_runtime_client.invoke_endpoint(**request_args)
126 return self._handle_response(response)
127

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/botocore/client.py in _api_call(self, *args, **kwargs)
355 "%s() only accepts keyword arguments." % py_operation_name)
356 # The "self" in this scope is referring to the BaseClient.
--> 357 return self._make_api_call(operation_name, kwargs)
358
359 _api_call.name = str(py_operation_name)

~/anaconda3/envs/mxnet_p36/lib/python3.6/site-packages/botocore/client.py in _make_api_call(self, operation_name, api_params)
674 error_code = parsed_response.get("Error", {}).get("Code")
675 error_class = self.exceptions.from_code(error_code)
--> 676 raise error_class(parsed_response, operation_name)
677 else:
678 return parsed_response

ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from model with message "[15:48:53] src/operator/tensor/./matrix_op-inl.h:2359: Check failed: dshape[axes[i]] == 1 (314 vs. 1) : cannot select an axis to squeeze out which has size=314 not equal to one
Stack trace:
[bt] (0) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x3172a2) [0x7fcf408742a2]
[bt] (1) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x1ad2e0a) [0x7fcf4202fe0a]
[bt] (2) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(mxnet::imperative::SetShapeType(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray*, std::allocatormxnet::NDArray* > const&, std::vector<mxnet::NDArray*, std::allocatormxnet::NDArray* > const&, mxnet::DispatchMode*)+0x22d5) [0x7fcf43bb2b05]
[bt] (3) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(mxnet::Imperative::Invoke(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray*, std::allocatormxnet::NDArray* > const&, std::vector<mxnet::NDArray*, std::allocatormxnet::NDArray* > const&)+0x35c) [0x7fcf43bc8e0c]
[bt] (4) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x3d84d2b) [0x7fcf442e1d2b]
[bt] (5) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(MXImperativeInvokeEx+0x5b) [0x7fcf442e30bb]
[bt] (6) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call_unix64+0x4c) [0x7fcf50ccd4e6]
[bt] (7) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call+0x3f1) [0x7fcf50ccc241]
[bt] (8) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x2cf) [0x7fcf50cc38ff]

Traceback (most recent call last):
File "/usr/local/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 126, in transform
result = self._transform_fn(self._model, input_data, content_type, accept)
File "/opt/ml/model/code/inference.py", line 41, in transform_fn
forecasts, tss, agg_metrics, _ = evaluate(model, request_list_data, num_samples=1)
File "/opt/ml/model/code/utils.py", line 26, in evaluate
forecasts = list(forecast_it)
File "/usr/local/lib/python3.6/site-packages/gluonts/model/predictor.py", line 330, in predict
num_samples=num_samples,
File "/usr/local/lib/python3.6/site-packages/gluonts/model/forecast_generator.py", line 197, in call
outputs = prediction_net(inputs).asnumpy()
File "/usr/local/lib/python3.6/site-packages/mxnet/gluon/block.py", line 758, in call
out = self.forward(args)
File "/usr/local/lib/python3.6/site-packages/mxnet/gluon/block.py", line 1223, in forward
return self.hybrid_forward(ndarray, x, args, params)
File "/usr/local/lib/python3.6/site-packages/gluonts/model/lstnet/_network.py", line 355, in hybrid_forward
F, past_target, past_observed_values
File "/usr/local/lib/python3.6/site-packages/gluonts/model/lstnet/_network.py", line 234, in hybrid_forward
c = F.squeeze(c, axis=2) # NCT
File "", line 44, in squeeze
File "/usr/local/lib/python3.6/site-packages/mxnet/_ctypes/ndarray.py", line 107, in _imperative_invoke
ctypes.byref(out_stypes)))
File "/usr/local/lib/python3.6/site-packages/mxnet/base.py", line 255, in check_call
raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: [15:48:53] src/operator/tensor/./matrix_op-inl.h:2359: Check failed: dshape[axes[i]] == 1 (314 vs. 1) : cannot select an axis to squeeze out which has size=314 not equal to one
Stack trace:
[bt] (0) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x3172a2) [0x7fcf408742a2]
[bt] (1) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x1ad2e0a) [0x7fcf4202fe0a]
[bt] (2) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(mxnet::imperative::SetShapeType(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray
, std::allocatormxnet::NDArray* > const&, std::vector<mxnet::NDArray
, std::allocatormxnet::NDArray* > const&, mxnet::DispatchMode
)+0x22d5) [0x7fcf43bb2b05]
[bt] (3) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(mxnet::Imperative::Invoke(mxnet::Context const&, nnvm::NodeAttrs const&, std::vector<mxnet::NDArray
, std::allocatormxnet::NDArray* > const&, std::vector<mxnet::NDArray
, std::allocatormxnet::NDArray* > const&)+0x35c) [0x7fcf43bc8e0c]
[bt] (4) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(+0x3d84d2b) [0x7fcf442e1d2b]
[bt] (5) /usr/local/lib/python3.6/site-packages/mxnet/libmxnet.so(MXImperativeInvokeEx+0x5b) [0x7fcf442e30bb]
[bt] (6) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call_unix64+0x4c) [0x7fcf50ccd4e6]
[bt] (7) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(ffi_call+0x3f1) [0x7fcf50ccc241]
[bt] (8) /usr/local/lib/python3.6/lib-dynload/_ctypes.cpython-36m-x86_64-linux-gnu.so(_ctypes_callproc+0x2cf) [0x7fcf50cc38ff]

". See https://us-west-2.console.aws.amazon.com/cloudwatch/home?region=us-west-2#logEventViewer:group=/aws/sagemaker/Endpoints/sagemaker-soln-ddf-demand-endpoint in account 940412083416 for more information.`

AWS Stack

Hi Folks,
I'm trying to get this deep learning algorithm with aws sage maker to work but I am not able to generate the needed aws instance. When I use the link to create the stack, aws page opens and I can clikc the button Create Stack. But once the stack is created it deletes itself automatically (ROLLBACK_COMPLETE).
I am new working with the SageMaker. Can anyone help me? Thanks
Dom

[Bug]

In src/preprocess/container/build_and_push.sh:

The get-login command is deprecated and must be replaced by the get-login-password command.

Here's the (tested) fix:

# Get the login command from ECR and execute it directly
aws ecr get-login-password --region ${region}| docker login \
     --username AWS \
     --password-stdin ${account}.dkr.ecr.${region}.amazonaws.com

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.