├── daal
├── web-usage
│ ├── knn-web-usage.pdf
│ ├── pca-web-usage.pdf
│ ├── kmeans-web-usage.pdf
│ ├── log-reg-web-usage.pdf
│ └── decision-forest-web-usage.pdf
├── k-nearest-neighbors-example.ipynb
├── pca-example.ipynb
├── kmeans-example.ipynb
├── decision-forest-example.ipynb
└── logistic-regression-example.ipynb
├── README.md
└── LICENSE
/daal/web-usage/knn-web-usage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intel/aws-sagemaker-marketplace/master/daal/web-usage/knn-web-usage.pdf
--------------------------------------------------------------------------------
/daal/web-usage/pca-web-usage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intel/aws-sagemaker-marketplace/master/daal/web-usage/pca-web-usage.pdf
--------------------------------------------------------------------------------
/daal/web-usage/kmeans-web-usage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intel/aws-sagemaker-marketplace/master/daal/web-usage/kmeans-web-usage.pdf
--------------------------------------------------------------------------------
/daal/web-usage/log-reg-web-usage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intel/aws-sagemaker-marketplace/master/daal/web-usage/log-reg-web-usage.pdf
--------------------------------------------------------------------------------
/daal/web-usage/decision-forest-web-usage.pdf:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/intel/aws-sagemaker-marketplace/master/daal/web-usage/decision-forest-web-usage.pdf
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # DISCONTINUATION OF PROJECT #
2 | This project will no longer be maintained by Intel.
3 | Intel has ceased development and contributions including, but not limited to, maintenance, bug fixes, new releases, or updates, to this project.
4 | Intel no longer accepts patches to this project.
5 |
6 |
7 | # Intel AI Software on AWS SageMaker Marketplace
8 |
9 | This repo contain example notebooks with instructions on using Intel AI Software listed in AWS SageMaker Marketplace.
10 |
11 | Intel and [AWS](https://aws.amazon.com/), a subsidiary of Amazon that provides on-demand cloud computing services, have worked [together for over a decade](https://aws.amazon.com/intel/) to ensure AWS services run on a platform optimized for customer workloads at the best value. At the annual [AWS re:Invent](https://reinvent.awsevents.com/) conference from November 26-30, AWS is highlighting advancements in this ongoing collaboration, including the new [AWS Marketplace for Machine Learning](https://aws.amazon.com/mp/ai/), developed by AWS for its Machine Learning as a Service (MLaaS) platform, [Amazon SageMaker](https://aws.amazon.com/sagemaker/). Read more at the [Intel AI Blog](https://ai.intel.com/intel-software-development-tools-on-the-new-aws-marketplace-for-machine-learning/).
12 |
13 | Intel has listed the following AI software tools and libraries on [AWS Marketplace](https://aws.amazon.com/marketplace/search/results?page=1&filters=vendor_id,fulfillment_options&vendor_id=a35b6cd1-6ad5-47c4-ac34-3e7b30c6d3a9&fulfillment_options=SAGEMAKER) (more coming soon):
14 | - [Intel® Data Analytics Acceleration Library (DAAL)](https://software.intel.com/en-us/intel-daal)
15 | - Principal Component Analysis (PCA) - [SDK usage](daal/pca-example.ipynb), [AWS Portal usage](daal/web-usage/pca-web-usage.pdf)
16 | - Logistic Regression - [SDK usage](daal/logistic-regression-example.ipynb), [AWS Portal usage](daal/web-usage/log-reg-web-usage.pdf)
17 | - Decision Forest Regression - [SDK usage](daal/decision-forest-example.ipynb), [AWS Portal usage](daal/web-usage/decision-forest-web-usage.pdf)
18 | - Decision Forest Classification - [SDK usage](daal/decision-forest-example.ipynb), [AWS Portal usage](daal/web-usage/decision-forest-web-usage.pdf)
19 | - K-Means- [SDK usage](daal/kmeans-example.ipynb), [AWS Portal usage](daal/web-usage/kmeans-web-usage.pdf)
20 | - k-Nearest Neighbors (kNN) Classifier - [SDK usage](daal/k-nearest-neighbors-example.ipynb), [AWS Portal usage](daal/web-usage/knn-web-usage.pdf)
21 |
22 | - [Intel® Optimized Deep Learning Libraries](https://www.intel.ai/framework-optimizations/)
23 | - Intel Optimized MXNet ResNet50 Inference
24 | - [BigDL](https://software.intel.com/en-us/ai-academy/frameworks/bigdl), a distributed deep learning library for Apache Spark.
25 | - BigDL Text Classifier on Analytics Zoo
26 |
27 |
28 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | Apache License
2 | Version 2.0, January 2004
3 | http://www.apache.org/licenses/
4 |
5 | TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6 |
7 | 1. Definitions.
8 |
9 | "License" shall mean the terms and conditions for use, reproduction,
10 | and distribution as defined by Sections 1 through 9 of this document.
11 |
12 | "Licensor" shall mean the copyright owner or entity authorized by
13 | the copyright owner that is granting the License.
14 |
15 | "Legal Entity" shall mean the union of the acting entity and all
16 | other entities that control, are controlled by, or are under common
17 | control with that entity. For the purposes of this definition,
18 | "control" means (i) the power, direct or indirect, to cause the
19 | direction or management of such entity, whether by contract or
20 | otherwise, or (ii) ownership of fifty percent (50%) or more of the
21 | outstanding shares, or (iii) beneficial ownership of such entity.
22 |
23 | "You" (or "Your") shall mean an individual or Legal Entity
24 | exercising permissions granted by this License.
25 |
26 | "Source" form shall mean the preferred form for making modifications,
27 | including but not limited to software source code, documentation
28 | source, and configuration files.
29 |
30 | "Object" form shall mean any form resulting from mechanical
31 | transformation or translation of a Source form, including but
32 | not limited to compiled object code, generated documentation,
33 | and conversions to other media types.
34 |
35 | "Work" shall mean the work of authorship, whether in Source or
36 | Object form, made available under the License, as indicated by a
37 | copyright notice that is included in or attached to the work
38 | (an example is provided in the Appendix below).
39 |
40 | "Derivative Works" shall mean any work, whether in Source or Object
41 | form, that is based on (or derived from) the Work and for which the
42 | editorial revisions, annotations, elaborations, or other modifications
43 | represent, as a whole, an original work of authorship. For the purposes
44 | of this License, Derivative Works shall not include works that remain
45 | separable from, or merely link (or bind by name) to the interfaces of,
46 | the Work and Derivative Works thereof.
47 |
48 | "Contribution" shall mean any work of authorship, including
49 | the original version of the Work and any modifications or additions
50 | to that Work or Derivative Works thereof, that is intentionally
51 | submitted to Licensor for inclusion in the Work by the copyright owner
52 | or by an individual or Legal Entity authorized to submit on behalf of
53 | the copyright owner. For the purposes of this definition, "submitted"
54 | means any form of electronic, verbal, or written communication sent
55 | to the Licensor or its representatives, including but not limited to
56 | communication on electronic mailing lists, source code control systems,
57 | and issue tracking systems that are managed by, or on behalf of, the
58 | Licensor for the purpose of discussing and improving the Work, but
59 | excluding communication that is conspicuously marked or otherwise
60 | designated in writing by the copyright owner as "Not a Contribution."
61 |
62 | "Contributor" shall mean Licensor and any individual or Legal Entity
63 | on behalf of whom a Contribution has been received by Licensor and
64 | subsequently incorporated within the Work.
65 |
66 | 2. Grant of Copyright License. Subject to the terms and conditions of
67 | this License, each Contributor hereby grants to You a perpetual,
68 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69 | copyright license to reproduce, prepare Derivative Works of,
70 | publicly display, publicly perform, sublicense, and distribute the
71 | Work and such Derivative Works in Source or Object form.
72 |
73 | 3. Grant of Patent License. Subject to the terms and conditions of
74 | this License, each Contributor hereby grants to You a perpetual,
75 | worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76 | (except as stated in this section) patent license to make, have made,
77 | use, offer to sell, sell, import, and otherwise transfer the Work,
78 | where such license applies only to those patent claims licensable
79 | by such Contributor that are necessarily infringed by their
80 | Contribution(s) alone or by combination of their Contribution(s)
81 | with the Work to which such Contribution(s) was submitted. If You
82 | institute patent litigation against any entity (including a
83 | cross-claim or counterclaim in a lawsuit) alleging that the Work
84 | or a Contribution incorporated within the Work constitutes direct
85 | or contributory patent infringement, then any patent licenses
86 | granted to You under this License for that Work shall terminate
87 | as of the date such litigation is filed.
88 |
89 | 4. Redistribution. You may reproduce and distribute copies of the
90 | Work or Derivative Works thereof in any medium, with or without
91 | modifications, and in Source or Object form, provided that You
92 | meet the following conditions:
93 |
94 | (a) You must give any other recipients of the Work or
95 | Derivative Works a copy of this License; and
96 |
97 | (b) You must cause any modified files to carry prominent notices
98 | stating that You changed the files; and
99 |
100 | (c) You must retain, in the Source form of any Derivative Works
101 | that You distribute, all copyright, patent, trademark, and
102 | attribution notices from the Source form of the Work,
103 | excluding those notices that do not pertain to any part of
104 | the Derivative Works; and
105 |
106 | (d) If the Work includes a "NOTICE" text file as part of its
107 | distribution, then any Derivative Works that You distribute must
108 | include a readable copy of the attribution notices contained
109 | within such NOTICE file, excluding those notices that do not
110 | pertain to any part of the Derivative Works, in at least one
111 | of the following places: within a NOTICE text file distributed
112 | as part of the Derivative Works; within the Source form or
113 | documentation, if provided along with the Derivative Works; or,
114 | within a display generated by the Derivative Works, if and
115 | wherever such third-party notices normally appear. The contents
116 | of the NOTICE file are for informational purposes only and
117 | do not modify the License. You may add Your own attribution
118 | notices within Derivative Works that You distribute, alongside
119 | or as an addendum to the NOTICE text from the Work, provided
120 | that such additional attribution notices cannot be construed
121 | as modifying the License.
122 |
123 | You may add Your own copyright statement to Your modifications and
124 | may provide additional or different license terms and conditions
125 | for use, reproduction, or distribution of Your modifications, or
126 | for any such Derivative Works as a whole, provided Your use,
127 | reproduction, and distribution of the Work otherwise complies with
128 | the conditions stated in this License.
129 |
130 | 5. Submission of Contributions. Unless You explicitly state otherwise,
131 | any Contribution intentionally submitted for inclusion in the Work
132 | by You to the Licensor shall be under the terms and conditions of
133 | this License, without any additional terms or conditions.
134 | Notwithstanding the above, nothing herein shall supersede or modify
135 | the terms of any separate license agreement you may have executed
136 | with Licensor regarding such Contributions.
137 |
138 | 6. Trademarks. This License does not grant permission to use the trade
139 | names, trademarks, service marks, or product names of the Licensor,
140 | except as required for reasonable and customary use in describing the
141 | origin of the Work and reproducing the content of the NOTICE file.
142 |
143 | 7. Disclaimer of Warranty. Unless required by applicable law or
144 | agreed to in writing, Licensor provides the Work (and each
145 | Contributor provides its Contributions) on an "AS IS" BASIS,
146 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
147 | implied, including, without limitation, any warranties or conditions
148 | of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
149 | PARTICULAR PURPOSE. You are solely responsible for determining the
150 | appropriateness of using or redistributing the Work and assume any
151 | risks associated with Your exercise of permissions under this License.
152 |
153 | 8. Limitation of Liability. In no event and under no legal theory,
154 | whether in tort (including negligence), contract, or otherwise,
155 | unless required by applicable law (such as deliberate and grossly
156 | negligent acts) or agreed to in writing, shall any Contributor be
157 | liable to You for damages, including any direct, indirect, special,
158 | incidental, or consequential damages of any character arising as a
159 | result of this License or out of the use or inability to use the
160 | Work (including but not limited to damages for loss of goodwill,
161 | work stoppage, computer failure or malfunction, or any and all
162 | other commercial damages or losses), even if such Contributor
163 | has been advised of the possibility of such damages.
164 |
165 | 9. Accepting Warranty or Additional Liability. While redistributing
166 | the Work or Derivative Works thereof, You may choose to offer,
167 | and charge a fee for, acceptance of support, warranty, indemnity,
168 | or other liability obligations and/or rights consistent with this
169 | License. However, in accepting such obligations, You may act only
170 | on Your own behalf and on Your sole responsibility, not on behalf
171 | of any other Contributor, and only if You agree to indemnify,
172 | defend, and hold each Contributor harmless for any liability
173 | incurred by, or claims asserted against, such Contributor by reason
174 | of your accepting any such warranty or additional liability.
175 |
176 | END OF TERMS AND CONDITIONS
177 |
178 | APPENDIX: How to apply the Apache License to your work.
179 |
180 | To apply the Apache License to your work, attach the following
181 | boilerplate notice, with the fields enclosed by brackets "[]"
182 | replaced with your own identifying information. (Don't include
183 | the brackets!) The text should be enclosed in the appropriate
184 | comment syntax for the file format. We also recommend that a
185 | file or class name and description of purpose be included on the
186 | same "printed page" as the copyright notice for easier
187 | identification within third-party archives.
188 |
189 | Copyright [yyyy] [name of copyright owner]
190 |
191 | Licensed under the Apache License, Version 2.0 (the "License");
192 | you may not use this file except in compliance with the License.
193 | You may obtain a copy of the License at
194 |
195 | http://www.apache.org/licenses/LICENSE-2.0
196 |
197 | Unless required by applicable law or agreed to in writing, software
198 | distributed under the License is distributed on an "AS IS" BASIS,
199 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
200 | See the License for the specific language governing permissions and
201 | limitations under the License.
202 |
--------------------------------------------------------------------------------
/daal/k-nearest-neighbors-example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# k-Nearest Neighbors (kNN) Classifier with Intel® Data Analytics Acceleration Library in Amazon SageMaker\n",
8 | "\n",
9 | "## Introduction\n",
10 | "\n",
11 | "Intel® Data Analytics Acceleration Library (Intel® DAAL) is the library of Intel® architecture optimized building blocks covering all stages of data analytics: data acquisition from a data source, preprocessing, transformation, data mining, modeling, validation, and decision making. One of its algorithms is kNN.\n",
12 | "\n",
13 | "k-Nearest Neighbors (kNN) classification is a non-parametric classification algorithm. The model of the kNN classifier is based on feature vectors and class labels from the training data set. This classifier induces the class of the query vector from the labels of the feature vectors in the training data set to which the query vector is similar. A similarity between feature vectors is determined by the type of distance (for example, Euclidian) in a multidimensional feature space.\n",
14 | "\n",
15 | "Intel® DAAL developer guide: https://software.intel.com/en-us/daal-programming-guide\n",
16 | "\n",
17 | "Intel® DAAL documentation for kNN: https://software.intel.com/en-us/daal-programming-guide-k-nearest-neighbors-knn-classifier"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## kNN Usage with SageMaker Estimator\n",
25 | "Firstly, you need to import SageMaker package, get execution role and create session."
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 1,
31 | "metadata": {},
32 | "outputs": [],
33 | "source": [
34 | "import sagemaker\n",
35 | "\n",
36 | "role = sagemaker.get_execution_role()\n",
37 | "sess = sagemaker.Session()"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "Secondly, you can specify parameters of kNN.\n",
45 | "#### Hyperparameters\n",
46 | "
\n",
47 | " \n",
48 | " | Parameter name | \n",
49 | " Type | \n",
50 | " Default value | \n",
51 | " Description | \n",
52 | "
\n",
53 | " \n",
54 | " | nClasses | \n",
55 | " int | \n",
56 | " 2 | \n",
57 | " Number of classes in data | \n",
58 | "
\n",
59 | " \n",
60 | " | fptype | \n",
61 | " str | \n",
62 | " \"double\" | \n",
63 | " The floating-point type that the algorithm uses for intermediate computations. Can be \"float\" or \"double\" | \n",
64 | "
\n",
65 | " \n",
66 | " | method | \n",
67 | " str | \n",
68 | " \"defaultDense\" | \n",
69 | " The computation method used by the K-D tree based kNN classification. The only training method supported so far is the default dense method. | \n",
70 | "
\n",
71 | " \n",
72 | " | k | \n",
73 | " int | \n",
74 | " 1 | \n",
75 | " The number of neighbors | \n",
76 | "
\n",
77 | " \n",
78 | " | dataUseInModel | \n",
79 | " str | \n",
80 | " \"doNotUse\" | \n",
81 | " A parameter to enable/disable use of the input data set in the kNN model. Possible values: \"doNotUse\" - the algorithm does not include the input data and labels in the trained kNN model but creates a copy of the input data set \"doUse\" - the algorithm includes the input data and labels in the trained kNN model | \n",
82 | "
\n",
83 | " \n",
84 | " | seed | \n",
85 | " int | \n",
86 | " 777 | \n",
87 | " Seed for random number generator engine that is used internally to perform sampling needed to choose dimensions and cut-points for the K-D tree. | \n",
88 | "
\n",
89 | "
\n",
90 | "\n",
91 | "Example of hyperparameters dictionary:"
92 | ]
93 | },
94 | {
95 | "cell_type": "code",
96 | "execution_count": 3,
97 | "metadata": {},
98 | "outputs": [],
99 | "source": [
100 | "knn_params = {\n",
101 | " \"nClasses\":2,\n",
102 | " \"fptype\":\"double\",\n",
103 | " \"method\":\"defaultDense\",\n",
104 | " \"dataUseInModel\":\"doNotUse\",\n",
105 | " \"seed\": 777,\n",
106 | " \"k\":1\n",
107 | "}"
108 | ]
109 | },
110 | {
111 | "cell_type": "markdown",
112 | "metadata": {},
113 | "source": [
114 | "Then, you need to create SageMaker Estimator instance with following parameters:\n",
115 | "\n",
116 | " \n",
117 | " | Parameter name | \n",
118 | " Description | \n",
119 | "
\n",
120 | " \n",
121 | " | image_name | \n",
122 | " The container image to use for training | \n",
123 | "
\n",
124 | " \n",
125 | " | role | \n",
126 | " An AWS IAM role. The SageMaker training jobs and APIs that create SageMaker endpoints use this role to access training data and models | \n",
127 | "
\n",
128 | " \n",
129 | " | train_instance_count | \n",
130 | " Number of Amazon EC2 instances to use for training. Should be 1, because it is not distributed version of algorithm | \n",
131 | "
\n",
132 | " \n",
133 | " | train_instance_type | \n",
134 | " Type of EC2 instance to use for training. See available types on Amazon Marketplace page of algorithm | \n",
135 | "
\n",
136 | " \n",
137 | " | input_mode | \n",
138 | " The input mode that the algorithm supports. May be \"File\" or \"Pipe\" | \n",
139 | "
\n",
140 | " \n",
141 | " | output_path | \n",
142 | " S3 location for saving the trainig result (model artifacts and output files) | \n",
143 | "
\n",
144 | " \n",
145 | " | sagemaker_session | \n",
146 | " Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed | \n",
147 | "
\n",
148 | " \n",
149 | " | hyperparameters | \n",
150 | " Dictionary containing the hyperparameters to initialize this estimator with | \n",
151 | "
\n",
152 | "
\n",
153 | "Full SageMaker Estimator documentation: https://sagemaker.readthedocs.io/en/latest/estimators.html"
154 | ]
155 | },
156 | {
157 | "cell_type": "code",
158 | "execution_count": 6,
159 | "metadata": {},
160 | "outputs": [],
161 | "source": [
162 | "daal_knn_arn = \"\" # you can find it on algorithm page in your subscriptions\n",
163 | "\n",
164 | "daal_knn = sagemaker.algorithm.AlgorithmEstimator(\n",
165 | " algorithm_arn=daal_knn_arn,\n",
166 | " role=role,\n",
167 | " base_job_name=\"\",\n",
168 | " train_instance_count=1,\n",
169 | " train_instance_type='ml.m4.xlarge',\n",
170 | " input_mode=\"File\",\n",
171 | " output_path=\"s3:///\",\n",
172 | " sagemaker_session=sess,\n",
173 | " hyperparameters=knn_params\n",
174 | ")"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "### Training stage\n",
182 | "On training stage, kNN algorithm consume input data from S3 location.\n",
183 | "This container supports only .csv (\"comma-separated values\") files."
184 | ]
185 | },
186 | {
187 | "cell_type": "code",
188 | "execution_count": 7,
189 | "metadata": {
190 | "scrolled": false
191 | },
192 | "outputs": [
193 | {
194 | "name": "stderr",
195 | "output_type": "stream",
196 | "text": [
197 | "INFO:sagemaker:Creating training-job with name: daal-knn-sm-2019-02-24-15-31-09-721\n"
198 | ]
199 | },
200 | {
201 | "name": "stdout",
202 | "output_type": "stream",
203 | "text": [
204 | "2019-02-24 15:31:09 Starting - Starting the training job...\n",
205 | "2019-02-24 15:31:11 Starting - Launching requested ML instances......\n",
206 | "2019-02-24 15:32:12 Starting - Preparing the instances for training...\n",
207 | "2019-02-24 15:33:02 Downloading - Downloading input data\n",
208 | "2019-02-24 15:33:02 Training - Downloading the training image......\n",
209 | "2019-02-24 15:34:09 Uploading - Uploading generated training model\n",
210 | "\u001b[31m2019-02-24 15:34:04 INFO Container setup completed, In Docker entrypoint - train... \u001b[0m\n",
211 | "\u001b[31m2019-02-24 15:34:04 INFO Default Hyperparameters loaded: \u001b[0m\n",
212 | "\u001b[31m2019-02-24 15:34:04 INFO \u001b[0m\n",
213 | "\u001b[31m{'dataUseInModel': 'doNotUse',\n",
214 | " 'fptype': 'double',\n",
215 | " 'k': '1',\n",
216 | " 'method': 'defaultDense',\n",
217 | " 'nClasses': '2'}\u001b[0m\n",
218 | "\u001b[31m2019-02-24 15:34:04 INFO Updated with user hyperparameters, Final Hyperparameters: \u001b[0m\n",
219 | "\u001b[31m2019-02-24 15:34:04 INFO \u001b[0m\n",
220 | "\u001b[31m{'dataUseInModel': 'doNotUse',\n",
221 | " 'fptype': 'double',\n",
222 | " 'k': '1',\n",
223 | " 'method': 'defaultDense',\n",
224 | " 'nClasses': '2',\n",
225 | " 'seed': '777'}\u001b[0m\n",
226 | "\u001b[31m2019-02-24 15:34:04 INFO Reading training data... \u001b[0m\n",
227 | "\u001b[31m2019-02-24 15:34:04 INFO Train data shape: (20000, 6)\u001b[0m\n",
228 | "\u001b[31m2019-02-24 15:34:04 INFO Files loading time: 0.024965763092041016\u001b[0m\n",
229 | "\u001b[31m2019-02-24 15:34:04 INFO Training Data Shape: (20000, 5)\u001b[0m\n",
230 | "\u001b[31m2019-02-24 15:34:04 INFO Starting DAAL KNN Kdtree training...\u001b[0m\n",
231 | "\u001b[31m2019-02-24 15:34:04 INFO Training time in sec = 0.024128198623657227\u001b[0m\n",
232 | "\u001b[31m2019-02-24 15:34:04 INFO Training complete.\u001b[0m\n",
233 | "\u001b[31m2019-02-24 15:34:04 INFO Saving model results...\u001b[0m\n",
234 | "\u001b[31m2019-02-24 15:34:04 INFO Parameters saved at /opt/ml/model/parameters.json\u001b[0m\n",
235 | "\n",
236 | "2019-02-24 15:34:15 Completed - Training job completed\n",
237 | "Billable seconds: 80\n"
238 | ]
239 | }
240 | ],
241 | "source": [
242 | "daal_knn.fit({\"training\": \"s3:///\"})"
243 | ]
244 | },
245 | {
246 | "cell_type": "markdown",
247 | "metadata": {},
248 | "source": [
249 | "### Real-time prediction\n",
250 | "Firstly, you need to deploy SageMaker endpoint that consumes data."
251 | ]
252 | },
253 | {
254 | "cell_type": "code",
255 | "execution_count": 8,
256 | "metadata": {
257 | "scrolled": true
258 | },
259 | "outputs": [
260 | {
261 | "name": "stderr",
262 | "output_type": "stream",
263 | "text": [
264 | "INFO:sagemaker:Creating model package with name: daal-knn-2019-02-24-15-35-14-387\n"
265 | ]
266 | },
267 | {
268 | "name": "stdout",
269 | "output_type": "stream",
270 | "text": [
271 | ".........."
272 | ]
273 | },
274 | {
275 | "name": "stderr",
276 | "output_type": "stream",
277 | "text": [
278 | "INFO:sagemaker:Creating model with name: daal-knn-2019-02-24-15-35-14-387-2019-02-24-15-35-59-854\n"
279 | ]
280 | },
281 | {
282 | "name": "stdout",
283 | "output_type": "stream",
284 | "text": [
285 | "\n"
286 | ]
287 | },
288 | {
289 | "name": "stderr",
290 | "output_type": "stream",
291 | "text": [
292 | "INFO:sagemaker:Creating endpoint with name daal-knn-sm-2019-02-24-15-31-09-721\n"
293 | ]
294 | },
295 | {
296 | "name": "stdout",
297 | "output_type": "stream",
298 | "text": [
299 | "--------------------------------------------------------------------------!"
300 | ]
301 | }
302 | ],
303 | "source": [
304 | "predictor = daal_knn.deploy(1, \"ml.m4.xlarge\", serializer=sagemaker.predictor.csv_serializer)"
305 | ]
306 | },
307 | {
308 | "cell_type": "markdown",
309 | "metadata": {},
310 | "source": [
311 | "Secondly, you should pass data as numpy array to endpoint and get predictions.\n",
312 | "\n",
313 | "In this example we are passing random data, but you can use any numpy 2D array"
314 | ]
315 | },
316 | {
317 | "cell_type": "code",
318 | "execution_count": 11,
319 | "metadata": {},
320 | "outputs": [
321 | {
322 | "name": "stdout",
323 | "output_type": "stream",
324 | "text": [
325 | "1\n",
326 | "1\n",
327 | "2\n",
328 | "4\n",
329 | "4\n",
330 | "3\n",
331 | "3\n",
332 | "1\n",
333 | "2\n",
334 | "4\n",
335 | "\n"
336 | ]
337 | }
338 | ],
339 | "source": [
340 | "import numpy as np\n",
341 | "\n",
342 | "predict_data = np.random.random(size=(10,5))\n",
343 | "print(predictor.predict(predict_data).decode(\"utf-8\"))"
344 | ]
345 | },
346 | {
347 | "cell_type": "markdown",
348 | "metadata": {},
349 | "source": [
350 | "Don't forget to delete endpoint if you don't need it anymore."
351 | ]
352 | },
353 | {
354 | "cell_type": "code",
355 | "execution_count": 12,
356 | "metadata": {},
357 | "outputs": [
358 | {
359 | "name": "stderr",
360 | "output_type": "stream",
361 | "text": [
362 | "INFO:sagemaker:Deleting endpoint with name: daal-knn-sm-2019-02-24-15-31-09-721\n"
363 | ]
364 | }
365 | ],
366 | "source": [
367 | "sess.delete_endpoint(predictor.endpoint)"
368 | ]
369 | },
370 | {
371 | "cell_type": "markdown",
372 | "metadata": {},
373 | "source": [
374 | "### Batch transform job\n",
375 | "If you don't need real-time prediction, you can use transform job. It uses saved model, compute predictions one time and saves it in specified or auto-generated output path.\n",
376 | "\n",
377 | "More about transform jobs: https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html\n",
378 | "\n",
379 | "Transformer API: https://sagemaker.readthedocs.io/en/latest/transformer.html"
380 | ]
381 | },
382 | {
383 | "cell_type": "code",
384 | "execution_count": 14,
385 | "metadata": {},
386 | "outputs": [
387 | {
388 | "name": "stderr",
389 | "output_type": "stream",
390 | "text": [
391 | "INFO:sagemaker:Creating model package with name: daal-knn-2019-02-24-15-53-00-053\n"
392 | ]
393 | },
394 | {
395 | "name": "stdout",
396 | "output_type": "stream",
397 | "text": [
398 | ".........."
399 | ]
400 | },
401 | {
402 | "name": "stderr",
403 | "output_type": "stream",
404 | "text": [
405 | "INFO:sagemaker:Creating model with name: daal-knn-2019-02-24-15-53-00-053-2019-02-24-15-53-45-504\n"
406 | ]
407 | },
408 | {
409 | "name": "stdout",
410 | "output_type": "stream",
411 | "text": [
412 | "\n"
413 | ]
414 | },
415 | {
416 | "name": "stderr",
417 | "output_type": "stream",
418 | "text": [
419 | "INFO:sagemaker:Creating transform job with name: daal-knn-sm-2019-02-24-15-53-45-783\n"
420 | ]
421 | },
422 | {
423 | "name": "stdout",
424 | "output_type": "stream",
425 | "text": [
426 | "........................................!\n",
427 | "s3://sagemaker-us-east-2-123123123123/daal-knn-sm-2019-02-24-15-53-45-783\n"
428 | ]
429 | }
430 | ],
431 | "source": [
432 | "transformer = daal_knn.transformer(1, 'ml.m4.xlarge')\n",
433 | "transformer.transform(\"s3:///\", content_type='text/csv')\n",
434 | "transformer.wait()\n",
435 | "print(transformer.output_path)"
436 | ]
437 | }
438 | ],
439 | "metadata": {
440 | "kernelspec": {
441 | "display_name": "conda_python3",
442 | "language": "python",
443 | "name": "conda_python3"
444 | },
445 | "language_info": {
446 | "codemirror_mode": {
447 | "name": "ipython",
448 | "version": 3
449 | },
450 | "file_extension": ".py",
451 | "mimetype": "text/x-python",
452 | "name": "python",
453 | "nbconvert_exporter": "python",
454 | "pygments_lexer": "ipython3",
455 | "version": "3.6.5"
456 | }
457 | },
458 | "nbformat": 4,
459 | "nbformat_minor": 2
460 | }
461 |
--------------------------------------------------------------------------------
/daal/pca-example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Principal Component Analysis with Intel® Data Analytics Acceleration Library in Amazon SageMaker\n",
8 | "\n",
9 | "## Introduction\n",
10 | "\n",
11 | "Intel® Data Analytics Acceleration Library (Intel® DAAL) is the library of Intel® architecture optimized building blocks covering all stages of data analytics: data acquisition from a data source, preprocessing, transformation, data mining, modeling, validation, and decision making. One of its algorithms is PCA.\n",
12 | "\n",
13 | "Principal Component Analysis (PCA) is a method for exploratory data analysis. PCA transforms a set of observations of possibly correlated variables to a new set of uncorrelated variables, called principal components. Principal components are the directions of the largest variance, that is, the directions where the data is mostly spread out.\n",
14 | "\n",
15 | "Because all principal components are orthogonal to each other, there is no redundant information. This is a way of replacing a group of variables with a smaller set of new variables. PCA is one of powerful techniques for dimension reduction.\n",
16 | "\n",
17 | "Intel® DAAL developer guide: https://software.intel.com/en-us/daal-programming-guide\n",
18 | "\n",
19 | "Intel® DAAL documentation for PCA: https://software.intel.com/en-us/daal-programming-guide-principal-component-analysis"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "## PCA Usage with SageMaker Estimator\n",
27 | "Firstly, you need to import SageMaker package, get execution role and create session."
28 | ]
29 | },
30 | {
31 | "cell_type": "code",
32 | "execution_count": 13,
33 | "metadata": {},
34 | "outputs": [],
35 | "source": [
36 | "import sagemaker\n",
37 | "\n",
38 | "role = sagemaker.get_execution_role()\n",
39 | "sess = sagemaker.Session()"
40 | ]
41 | },
42 | {
43 | "cell_type": "markdown",
44 | "metadata": {},
45 | "source": [
46 | "Secondly, you can specify parameters of PCA.\n",
47 | "#### Hyperparameters\n",
48 | "All hyperparameters of PCA algorithm are optional.\n",
49 | "\n",
50 | " \n",
51 | " | Parameter name | \n",
52 | " Type | \n",
53 | " Default value | \n",
54 | " Description | \n",
55 | "
\n",
56 | " \n",
57 | " | fptype | \n",
58 | " str | \n",
59 | " \"double\" | \n",
60 | " The floating-point type that the algorithm uses for intermediate computations. Can be \"float\" or \"double\" | \n",
61 | "
\n",
62 | " \n",
63 | " | method | \n",
64 | " str | \n",
65 | " \"correlationDense\" | \n",
66 | " Available methods for PCA computation: \"correlationDense\" (\"defaultDense\") or \"svdDense\" | \n",
67 | "
\n",
68 | " \n",
69 | " | resultsToCompute | \n",
70 | " str | \n",
71 | " \"none\" | \n",
72 | " Provide one of the following values to request a single characteristic or use bitwise OR to request a combination of the characteristics: mean, variance, eigenvalue. For example, combination of all is \"mean|variance|eigenvalue\" | \n",
73 | "
\n",
74 | " \n",
75 | " | nComponents | \n",
76 | " int | \n",
77 | " 0 | \n",
78 | " Number of principal components. If it is zero, the algorithm will compute the result for number of principal components = number of features. Remember that number of components must be equal or less than number of features for PCA algorithm | \n",
79 | "
\n",
80 | " \n",
81 | " | isDeterministic | \n",
82 | " bool | \n",
83 | " False | \n",
84 | " If True, the algorithm applies the \"sign flip\" technique to the results | \n",
85 | "
\n",
86 | " \n",
87 | " | transformOnTrain | \n",
88 | " bool | \n",
89 | " False | \n",
90 | " If True, training data will be transformed and saved in model package on training stage | \n",
91 | "
\n",
92 | "
\n",
93 | "\n",
94 | "Example of hyperparameters dictionary:"
95 | ]
96 | },
97 | {
98 | "cell_type": "code",
99 | "execution_count": 14,
100 | "metadata": {},
101 | "outputs": [],
102 | "source": [
103 | "pca_params = {\n",
104 | " \"fptype\": \"float\",\n",
105 | " \"method\": \"svdDense\",\n",
106 | " \"resultsToCompute\": \"mean|eigenvalue\",\n",
107 | " \"nComponents\": 4,\n",
108 | " \"isDeterministic\": True\n",
109 | "}"
110 | ]
111 | },
112 | {
113 | "cell_type": "markdown",
114 | "metadata": {},
115 | "source": [
116 | "Then, you need to create SageMaker Estimator instance with following parameters:\n",
117 | "\n",
118 | " \n",
119 | " | Parameter name | \n",
120 | " Description | \n",
121 | "
\n",
122 | " \n",
123 | " | image_name | \n",
124 | " The container image to use for training | \n",
125 | "
\n",
126 | " \n",
127 | " | role | \n",
128 | " An AWS IAM role. The SageMaker training jobs and APIs that create SageMaker endpoints use this role to access training data and models | \n",
129 | "
\n",
130 | " \n",
131 | " | train_instance_count | \n",
132 | " Number of Amazon EC2 instances to use for training. Should be 1, because it is not distributed version of algorithm | \n",
133 | "
\n",
134 | " \n",
135 | " | train_instance_type | \n",
136 | " Type of EC2 instance to use for training. See available types on Amazon Marketplace page of algorithm | \n",
137 | "
\n",
138 | " \n",
139 | " | input_mode | \n",
140 | " The input mode that the algorithm supports. May be \"File\" or \"Pipe\" | \n",
141 | "
\n",
142 | " \n",
143 | " | output_path | \n",
144 | " S3 location for saving the trainig result (model artifacts and output files) | \n",
145 | "
\n",
146 | " \n",
147 | " | sagemaker_session | \n",
148 | " Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed | \n",
149 | "
\n",
150 | " \n",
151 | " | hyperparameters | \n",
152 | " Dictionary containing the hyperparameters to initialize this estimator with | \n",
153 | "
\n",
154 | "
\n",
155 | "Full SageMaker Estimator documentation: https://sagemaker.readthedocs.io/en/latest/estimators.html"
156 | ]
157 | },
158 | {
159 | "cell_type": "code",
160 | "execution_count": 9,
161 | "metadata": {},
162 | "outputs": [],
163 | "source": [
164 | "daal_pca_arn = \"\" # you can find it on algorithm page in your subscriptions\n",
165 | "\n",
166 | "daal_pca = sagemaker.algorithm.AlgorithmEstimator(\n",
167 | " algorithm_arn=daal_pca_arn,\n",
168 | " role=role,\n",
169 | " base_job_name=\"\",\n",
170 | " train_instance_count=1,\n",
171 | " train_instance_type='ml.m4.xlarge',\n",
172 | " input_mode=\"File\",\n",
173 | " output_path=\"s3:///\",\n",
174 | " sagemaker_session=sess,\n",
175 | " hyperparameters=pca_params\n",
176 | ")"
177 | ]
178 | },
179 | {
180 | "cell_type": "markdown",
181 | "metadata": {},
182 | "source": [
183 | "### Training stage\n",
184 | "On training stage, PCA algorithm consume input data from S3 location and computes eigen vectors and other results (if they are specified in \"resultsToCompute\" parameter).\n",
185 | "This container supports only .csv (\"comma-separated values\") files."
186 | ]
187 | },
188 | {
189 | "cell_type": "code",
190 | "execution_count": 10,
191 | "metadata": {},
192 | "outputs": [
193 | {
194 | "name": "stderr",
195 | "output_type": "stream",
196 | "text": [
197 | "INFO:sagemaker:Creating training-job with name: daal-pca-alg-test-2018-11-30-06-50-06-484\n"
198 | ]
199 | },
200 | {
201 | "name": "stdout",
202 | "output_type": "stream",
203 | "text": [
204 | "2018-11-30 06:50:06 Starting - Starting the training job...\n",
205 | "2018-11-30 06:50:07 Starting - Launching requested ML instances......\n",
206 | "2018-11-30 06:51:15 Starting - Preparing the instances for training...\n",
207 | "2018-11-30 06:52:07 Downloading - Downloading input data...\n",
208 | "2018-11-30 06:52:30 Training - Downloading the training image...\n",
209 | "2018-11-30 06:53:01 Uploading - Uploading generated training model\n",
210 | "2018-11-30 06:53:01 Completed - Training job completed\n",
211 | "\n",
212 | "\u001b[31m2018-11-30 06:52:49 INFO Training stage started\u001b[0m\n",
213 | "\u001b[31m2018-11-30 06:52:49 INFO Default Paramaters:\u001b[0m\n",
214 | "\u001b[31m2018-11-30 06:52:49 INFO {'fptype': 'double', 'method': 'correlationDense', 'resultsToCompute': '', 'nComponents': '0', 'isDeterministic': 'False', 'transformOnTrain': 'False'}\u001b[0m\n",
215 | "\u001b[31m2018-11-30 06:52:49 INFO Updated with user hyperparameters, uncorrect parameters changed or deleted\u001b[0m\n",
216 | "\u001b[31m2018-11-30 06:52:49 INFO Final Hyperparameters:\u001b[0m\n",
217 | "\u001b[31m2018-11-30 06:52:49 INFO {'fptype': 'float', 'method': 'defaultDense', 'resultsToCompute': 'mean|eigenvalue', 'nComponents': 3, 'isDeterministic': True, 'transformOnTrain': False}\u001b[0m\n",
218 | "\u001b[31m2018-11-30 06:52:54 INFO Train data shape: (1600000, 10)\u001b[0m\n",
219 | "\u001b[31m2018-11-30 06:52:54 INFO Files loading time: 4.69575047492981\u001b[0m\n",
220 | "\u001b[31m2018-11-30 06:52:54 INFO Starting DAAL PCA algorithm...\u001b[0m\n",
221 | "\u001b[31m2018-11-30 06:52:54 INFO Training time, sec: 0.10286879539489746\u001b[0m\n",
222 | "\u001b[31m2018-11-30 06:52:54 INFO Means table saved at /opt/ml/model/mean.csv\u001b[0m\n",
223 | "\u001b[31m2018-11-30 06:52:54 INFO Eigenvalues table saved at /opt/ml/model/eigenvalue.csv\u001b[0m\n",
224 | "\u001b[31m2018-11-30 06:52:54 INFO Eigen vectors saved at /opt/ml/model/vectors.csv\u001b[0m\n",
225 | "\u001b[31m2018-11-30 06:52:54 INFO Parameters saved at /opt/ml/model/parameters.json\u001b[0m\n",
226 | "Billable seconds: 54\n"
227 | ]
228 | }
229 | ],
230 | "source": [
231 | "daal_pca.fit({\"training\": \"s3:///\"})"
232 | ]
233 | },
234 | {
235 | "cell_type": "markdown",
236 | "metadata": {},
237 | "source": [
238 | "### Real-time prediction\n",
239 | "On prediction stage, PCA algorithm transforms input data using previously computed results.\n",
240 | "Firstly, you need to deploy SageMaker endpoint that consumes data."
241 | ]
242 | },
243 | {
244 | "cell_type": "code",
245 | "execution_count": 6,
246 | "metadata": {},
247 | "outputs": [
248 | {
249 | "name": "stderr",
250 | "output_type": "stream",
251 | "text": [
252 | "INFO:sagemaker:Creating model package with name: intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-29-15-37-47-871\n"
253 | ]
254 | },
255 | {
256 | "name": "stdout",
257 | "output_type": "stream",
258 | "text": [
259 | ".........."
260 | ]
261 | },
262 | {
263 | "name": "stderr",
264 | "output_type": "stream",
265 | "text": [
266 | "INFO:sagemaker:Creating model with name: intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-29-15-38-33-468\n"
267 | ]
268 | },
269 | {
270 | "name": "stdout",
271 | "output_type": "stream",
272 | "text": [
273 | "\n"
274 | ]
275 | },
276 | {
277 | "name": "stderr",
278 | "output_type": "stream",
279 | "text": [
280 | "INFO:sagemaker:Creating endpoint with name intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-29-15-31-41-921\n"
281 | ]
282 | },
283 | {
284 | "name": "stdout",
285 | "output_type": "stream",
286 | "text": [
287 | "-------------------------------------------------------------!"
288 | ]
289 | }
290 | ],
291 | "source": [
292 | "predictor = daal_pca.deploy(1, \"ml.m4.xlarge\", serializer=sagemaker.predictor.csv_serializer)"
293 | ]
294 | },
295 | {
296 | "cell_type": "markdown",
297 | "metadata": {},
298 | "source": [
299 | "Secondly, you should pass data as numpy array to predictor instance and get transformed data as space-separated values.\n",
300 | "\n",
301 | "In this example we are passing random data, but you can use any numpy 2D array with one specific condition for PCA: training data and data to transform must have same numbers of features."
302 | ]
303 | },
304 | {
305 | "cell_type": "code",
306 | "execution_count": 7,
307 | "metadata": {},
308 | "outputs": [
309 | {
310 | "name": "stdout",
311 | "output_type": "stream",
312 | "text": [
313 | "1.185592651367187500e+00 2.620933353900909424e-01 3.085311949253082275e-01\n",
314 | "6.714667081832885742e-01 8.762556910514831543e-01 -8.037568628787994385e-02\n",
315 | "1.111641526222229004e+00 3.375906124711036682e-02 -1.244278624653816223e-01\n",
316 | "1.294844508171081543e+00 3.067855909466743469e-02 -9.089314937591552734e-02\n",
317 | "8.781186342239379883e-01 2.732140570878982544e-02 -5.232766270637512207e-01\n",
318 | "5.412490963935852051e-01 3.470270335674285889e-01 -8.399704098701477051e-02\n",
319 | "1.048457860946655273e+00 -1.020107120275497437e-01 6.852779537439346313e-02\n",
320 | "8.287011384963989258e-01 2.592234015464782715e-01 -1.914716511964797974e-01\n",
321 | "1.426655769348144531e+00 1.917291432619094849e-02 -1.039648428559303284e-01\n",
322 | "1.183746933937072754e+00 -8.203709125518798828e-02 3.905816972255706787e-01\n",
323 | "\n"
324 | ]
325 | }
326 | ],
327 | "source": [
328 | "import numpy as np\n",
329 | "\n",
330 | "predict_data = np.random.random(size=(10,10))\n",
331 | "print(predictor.predict(predict_data).decode(\"utf-8\"))"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "metadata": {},
337 | "source": [
338 | "Don't forget to delete endpoint if you don't need it anymore."
339 | ]
340 | },
341 | {
342 | "cell_type": "code",
343 | "execution_count": 8,
344 | "metadata": {},
345 | "outputs": [
346 | {
347 | "name": "stderr",
348 | "output_type": "stream",
349 | "text": [
350 | "INFO:sagemaker:Deleting endpoint with name: intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-29-15-31-41-921\n"
351 | ]
352 | }
353 | ],
354 | "source": [
355 | "sess.delete_endpoint(predictor.endpoint)"
356 | ]
357 | },
358 | {
359 | "cell_type": "markdown",
360 | "metadata": {},
361 | "source": [
362 | "### Batch transform job\n",
363 | "If you don't need real-time prediction, you can use transform job. It uses saved model, compute transformed data one time and saves it in specified or auto-generated output path.\n",
364 | "\n",
365 | "More about transform jobs: https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html\n",
366 | "\n",
367 | "Transformer API: https://sagemaker.readthedocs.io/en/latest/transformer.html"
368 | ]
369 | },
370 | {
371 | "cell_type": "code",
372 | "execution_count": 12,
373 | "metadata": {},
374 | "outputs": [
375 | {
376 | "name": "stderr",
377 | "output_type": "stream",
378 | "text": [
379 | "INFO:sagemaker:Creating model package with name: intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-30-07-42-48-772\n"
380 | ]
381 | },
382 | {
383 | "name": "stdout",
384 | "output_type": "stream",
385 | "text": [
386 | ".........."
387 | ]
388 | },
389 | {
390 | "name": "stderr",
391 | "output_type": "stream",
392 | "text": [
393 | "INFO:sagemaker:Creating model with name: intel-daal-pca1542385402-d0d25e75ca6ef4-2018-11-30-07-43-34-517\n"
394 | ]
395 | },
396 | {
397 | "name": "stdout",
398 | "output_type": "stream",
399 | "text": [
400 | "\n"
401 | ]
402 | },
403 | {
404 | "name": "stderr",
405 | "output_type": "stream",
406 | "text": [
407 | "INFO:sagemaker:Creating transform job with name: daal-pca-alg-test-2018-11-30-07-43-35-199\n"
408 | ]
409 | },
410 | {
411 | "name": "stdout",
412 | "output_type": "stream",
413 | "text": [
414 | "......................................!\n",
415 | "s3://\n"
416 | ]
417 | }
418 | ],
419 | "source": [
420 | "transformer = daal_pca.transformer(1, 'ml.m4.xlarge')\n",
421 | "transformer.transform(\"s3:///\", content_type='text/csv')\n",
422 | "transformer.wait()\n",
423 | "print(transformer.output_path)"
424 | ]
425 | }
426 | ],
427 | "metadata": {
428 | "kernelspec": {
429 | "display_name": "Python 3",
430 | "language": "python",
431 | "name": "python3"
432 | },
433 | "language_info": {
434 | "codemirror_mode": {
435 | "name": "ipython",
436 | "version": 3
437 | },
438 | "file_extension": ".py",
439 | "mimetype": "text/x-python",
440 | "name": "python",
441 | "nbconvert_exporter": "python",
442 | "pygments_lexer": "ipython3",
443 | "version": "3.6.5"
444 | }
445 | },
446 | "nbformat": 4,
447 | "nbformat_minor": 2
448 | }
449 |
--------------------------------------------------------------------------------
/daal/kmeans-example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# K-Means with Intel® Data Analytics Acceleration Library in Amazon SageMaker\n",
8 | "\n",
9 | "## Introduction\n",
10 | "\n",
11 | "Intel® Data Analytics Acceleration Library (Intel® DAAL) is the library of Intel® architecture optimized building blocks covering all stages of data analytics: data acquisition from a data source, preprocessing, transformation, data mining, modeling, validation, and decision making. One of its algorithms is K-Means.\n",
12 | "\n",
13 | "K-Means is among the most popular and simplest clustering methods. It is intended to partition a data set into a small number of clusters such that feature vectors within a cluster have greater similarity with one another than with feature vectors from other clusters. Each cluster is characterized by a representative point, called a centroid, and a cluster radius.\n",
14 | "\n",
15 | "In other words, the clustering methods enable reducing the problem of analysis of the entire data set to the analysis of clusters.\n",
16 | "\n",
17 | "There are numerous ways to define the measure of similarity and centroids. For K-Means, the centroid is defined as the mean of feature vectors within the cluster.\n",
18 | "\n",
19 | "Intel® DAAL developer guide: https://software.intel.com/en-us/daal-programming-guide\n",
20 | "\n",
21 | "Intel® DAAL documentation for K-Means: https://software.intel.com/en-us/daal-programming-guide-k-means-clustering "
22 | ]
23 | },
24 | {
25 | "cell_type": "markdown",
26 | "metadata": {},
27 | "source": [
28 | "## K-Means Usage with SageMaker Estimator\n",
29 | "Firstly, you need to import SageMaker package, get execution role and create session."
30 | ]
31 | },
32 | {
33 | "cell_type": "code",
34 | "execution_count": 1,
35 | "metadata": {},
36 | "outputs": [],
37 | "source": [
38 | "import sagemaker\n",
39 | "\n",
40 | "role = sagemaker.get_execution_role()\n",
41 | "sess = sagemaker.Session()"
42 | ]
43 | },
44 | {
45 | "cell_type": "markdown",
46 | "metadata": {},
47 | "source": [
48 | "Secondly, you can specify parameters of K-Means.\n",
49 | "#### Hyperparameters\n",
50 | "\"nClusters\" and \"maxIterations\" hyperparameters of K-Means algorithm are required, all other - optional.\n",
51 | "\n",
52 | " \n",
53 | " | Parameter name | \n",
54 | " Type | \n",
55 | " Default value | \n",
56 | " Description | \n",
57 | "
\n",
58 | " \n",
59 | " | fptype | \n",
60 | " str | \n",
61 | " \"double\" | \n",
62 | " The floating-point type that the algorithm uses for intermediate computations. Can be \"float\" or \"double\" | \n",
63 | "
\n",
64 | " \n",
65 | " | nClusters | \n",
66 | " int | \n",
67 | " 2 | \n",
68 | " The number of clusters | \n",
69 | "
\n",
70 | " \n",
71 | " | initMethod | \n",
72 | " str | \n",
73 | " \"defaultDense\" | \n",
74 | " Available initialization methods for K-Means clustering: defaultDense - uses first nClusters points as initial clusters, randomDense - uses random nClusters points as initial clusters, plusPlusDense - uses K-Means++ algorithm; parallelPlusDense - uses parallel K-Means++ algorithm | \n",
75 | "
\n",
76 | " \n",
77 | " | oversamplingFactor | \n",
78 | " float | \n",
79 | " 0.5 | \n",
80 | " A fraction of nClusters in each of nRounds of parallel K-Means++. L=nClusters*oversamplingFactor points are sampled in a round | \n",
81 | "
\n",
82 | " \n",
83 | " | nRounds | \n",
84 | " int | \n",
85 | " 5 | \n",
86 | " The number of rounds for parallel K-Means++. (L*nRounds) must be greater than nClusters | \n",
87 | "
\n",
88 | " \n",
89 | " | seed | \n",
90 | " int | \n",
91 | " 777 | \n",
92 | " The seed for random number generator | \n",
93 | "
\n",
94 | " \n",
95 | " | method | \n",
96 | " str | \n",
97 | " \"lloydDense\" | \n",
98 | " Computation method for K-Means clustering | \n",
99 | "
\n",
100 | " \n",
101 | " | maxIterations | \n",
102 | " int | \n",
103 | " 100 | \n",
104 | " The number of iterations | \n",
105 | "
\n",
106 | " \n",
107 | " | accuracyThreshold | \n",
108 | " float | \n",
109 | " 0 | \n",
110 | " The threshold for termination of the algorithm | \n",
111 | "
\n",
112 | " \n",
113 | " | gamma | \n",
114 | " float | \n",
115 | " 1 | \n",
116 | " The weight to be used in distance calculation for binary categorical features | \n",
117 | "
\n",
118 | " \n",
119 | " | distanceType | \n",
120 | " str | \n",
121 | " \"euclidean\" | \n",
122 | " The measure of closeness between points (observations) being clustered. The only distance type supported so far is the Euclidian distance | \n",
123 | "
\n",
124 | " \n",
125 | " | assignFlag | \n",
126 | " bool | \n",
127 | " True | \n",
128 | " A flag that enables computation of assignments, that is, assigning cluster indices to respective observations | \n",
129 | "
\n",
130 | "
\n",
131 | "\n",
132 | "Example of hyperparameters dictionary:"
133 | ]
134 | },
135 | {
136 | "cell_type": "code",
137 | "execution_count": 3,
138 | "metadata": {},
139 | "outputs": [],
140 | "source": [
141 | "kmeans_params = {\n",
142 | " \"fptype\": \"float\",\n",
143 | " \"nClusters\": 5,\n",
144 | " \"initMethod\": \"plusPlusDense\",\n",
145 | " \"maxIterations\": 1000\n",
146 | "}"
147 | ]
148 | },
149 | {
150 | "cell_type": "markdown",
151 | "metadata": {},
152 | "source": [
153 | "Then, you need to create SageMaker Estimator instance with following parameters:\n",
154 | "\n",
155 | " \n",
156 | " | Parameter name | \n",
157 | " Description | \n",
158 | "
\n",
159 | " \n",
160 | " | image_name | \n",
161 | " The container image to use for training | \n",
162 | "
\n",
163 | " \n",
164 | " | role | \n",
165 | " An AWS IAM role. The SageMaker training jobs and APIs that create SageMaker endpoints use this role to access training data and models | \n",
166 | "
\n",
167 | " \n",
168 | " | train_instance_count | \n",
169 | " Number of Amazon EC2 instances to use for training. Should be 1, because it is not distributed version of algorithm | \n",
170 | "
\n",
171 | " \n",
172 | " | train_instance_type | \n",
173 | " Type of EC2 instance to use for training. See available types on Amazon Marketplace page of algorithm | \n",
174 | "
\n",
175 | " \n",
176 | " | input_mode | \n",
177 | " The input mode that the algorithm supports. May be \"File\" or \"Pipe\" | \n",
178 | "
\n",
179 | " \n",
180 | " | output_path | \n",
181 | " S3 location for saving the trainig result (model artifacts and output files) | \n",
182 | "
\n",
183 | " \n",
184 | " | sagemaker_session | \n",
185 | " Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed | \n",
186 | "
\n",
187 | " \n",
188 | " | hyperparameters | \n",
189 | " Dictionary containing the hyperparameters to initialize this estimator with | \n",
190 | "
\n",
191 | "
\n",
192 | "Full SageMaker Estimator documentation: https://sagemaker.readthedocs.io/en/latest/estimators.html"
193 | ]
194 | },
195 | {
196 | "cell_type": "code",
197 | "execution_count": 4,
198 | "metadata": {},
199 | "outputs": [],
200 | "source": [
201 | "daal_kmeans_arn = \"\" # you can find it on algorithm page in your subscriptions\n",
202 | "\n",
203 | "daal_kmeans = sagemaker.algorithm.AlgorithmEstimator(\n",
204 | " algorithm_arn=daal_kmeans_arn,\n",
205 | " role=role,\n",
206 | " base_job_name=\"\",\n",
207 | " train_instance_count=1,\n",
208 | " train_instance_type='ml.m4.xlarge',\n",
209 | " input_mode=\"File\",\n",
210 | " output_path=\"s3:///\",\n",
211 | " sagemaker_session=sess,\n",
212 | " hyperparameters=kmeans_params\n",
213 | ")"
214 | ]
215 | },
216 | {
217 | "cell_type": "markdown",
218 | "metadata": {},
219 | "source": [
220 | "### Training stage\n",
221 | "On training stage, K-Means algorithm consume input data from S3 location and computes centroids.\n",
222 | "This container supports only .csv (\"comma-separated values\") files."
223 | ]
224 | },
225 | {
226 | "cell_type": "code",
227 | "execution_count": 5,
228 | "metadata": {},
229 | "outputs": [
230 | {
231 | "name": "stderr",
232 | "output_type": "stream",
233 | "text": [
234 | "INFO:sagemaker:Creating training-job with name: daal-kmeans-test-2019-02-15-15-47-15-619\n"
235 | ]
236 | },
237 | {
238 | "name": "stdout",
239 | "output_type": "stream",
240 | "text": [
241 | "2019-02-15 15:47:15 Starting - Starting the training job...\n",
242 | "2019-02-15 15:47:17 Starting - Launching requested ML instances......\n",
243 | "2019-02-15 15:48:17 Starting - Preparing the instances for training...\n",
244 | "2019-02-15 15:49:09 Downloading - Downloading input data\n",
245 | "2019-02-15 15:49:09 Training - Downloading the training image......\n",
246 | "2019-02-15 15:50:14 Uploading - Uploading generated training model\n",
247 | "2019-02-15 15:50:14 Completed - Training job completed\n",
248 | "\n",
249 | "\u001b[31m2019-02-15 15:50:04 INFO Training stage started\u001b[0m\n",
250 | "\u001b[31m2019-02-15 15:50:04 INFO Final Hyperparameters:\u001b[0m\n",
251 | "\u001b[31m2019-02-15 15:50:04 INFO {'fptype': 'float', 'initMethod': 'plusPlusDense', 'seed': '777', 'oversamplingFactor': '0.5', 'nRounds': '5', 'kmeansMethod': 'lloydDense', 'accuracyThreshold': '0', 'gamma': '1', 'distanceType': 'euclidean', 'assignFlag': True, 'maxIterations': '1000', 'method': 'lloydDense', 'assignFlag ': 'True', 'nClusters': '5'}\u001b[0m\n",
252 | "\u001b[31m2019-02-15 15:50:04 INFO Train data shape: (30000, 10)\u001b[0m\n",
253 | "\u001b[31m2019-02-15 15:50:04 INFO Files loading time: 0.10956525802612305\u001b[0m\n",
254 | "\u001b[31m2019-02-15 15:50:04 INFO Starting DAAL k-Means algorithm...\u001b[0m\n",
255 | "\u001b[31m2019-02-15 15:50:04 INFO Training time, sec: 0.34311580657958984\u001b[0m\n",
256 | "\u001b[31m2019-02-15 15:50:04 INFO Parameters saved at /opt/ml/model/parameters.json\u001b[0m\n",
257 | "\u001b[31m2019-02-15 15:50:04 INFO Centroids saved at /opt/ml/model/centroids.csv\u001b[0m\n",
258 | "\u001b[31m2019-02-15 15:50:05 INFO Assignments saved at /opt/ml/model/assignments.csv\u001b[0m\n",
259 | "\u001b[31m2019-02-15 15:50:05 INFO Number of computed iterations: 1000\u001b[0m\n",
260 | "\u001b[31m2019-02-15 15:50:05 INFO Objective function value: 286906.5\u001b[0m\n",
261 | "\u001b[31m2019-02-15 15:50:05 INFO Number of computed iterations and objective function value saved at /opt/ml/model/other.txt\u001b[0m\n",
262 | "Billable seconds: 75\n"
263 | ]
264 | }
265 | ],
266 | "source": [
267 | "daal_kmeans.fit({\"training\": \"s3:///\"})"
268 | ]
269 | },
270 | {
271 | "cell_type": "markdown",
272 | "metadata": {},
273 | "source": [
274 | "### Real-time prediction\n",
275 | "On prediction stage, K-Means algorithm determines assignments for input data using previously computed centroids.\n",
276 | "Firstly, you need to deploy SageMaker endpoint that consumes data."
277 | ]
278 | },
279 | {
280 | "cell_type": "code",
281 | "execution_count": 6,
282 | "metadata": {},
283 | "outputs": [
284 | {
285 | "name": "stderr",
286 | "output_type": "stream",
287 | "text": [
288 | "INFO:sagemaker:Creating model package with name: daal-kmeans-new-2019-02-15-15-56-26-951\n"
289 | ]
290 | },
291 | {
292 | "name": "stdout",
293 | "output_type": "stream",
294 | "text": [
295 | ".........."
296 | ]
297 | },
298 | {
299 | "name": "stderr",
300 | "output_type": "stream",
301 | "text": [
302 | "INFO:sagemaker:Creating model with name: daal-kmeans-new-2019-02-15-15-56-26-951-2019-02-15-15-57-12-443\n"
303 | ]
304 | },
305 | {
306 | "name": "stdout",
307 | "output_type": "stream",
308 | "text": [
309 | "\n"
310 | ]
311 | },
312 | {
313 | "name": "stderr",
314 | "output_type": "stream",
315 | "text": [
316 | "INFO:sagemaker:Creating endpoint with name daal-kmeans-test-2019-02-15-15-47-15-619\n"
317 | ]
318 | },
319 | {
320 | "name": "stdout",
321 | "output_type": "stream",
322 | "text": [
323 | "------------------------------------------------------------------!"
324 | ]
325 | }
326 | ],
327 | "source": [
328 | "predictor = daal_kmeans.deploy(1, \"ml.m4.xlarge\", serializer=sagemaker.predictor.csv_serializer)"
329 | ]
330 | },
331 | {
332 | "cell_type": "markdown",
333 | "metadata": {},
334 | "source": [
335 | "Secondly, you should pass data as numpy array to predictor instance and get assignments.\n",
336 | "\n",
337 | "In this example we are passing random data, but you can use any numpy 2D array."
338 | ]
339 | },
340 | {
341 | "cell_type": "code",
342 | "execution_count": 8,
343 | "metadata": {
344 | "scrolled": true
345 | },
346 | "outputs": [
347 | {
348 | "name": "stdout",
349 | "output_type": "stream",
350 | "text": [
351 | "1\n",
352 | "0\n",
353 | "2\n",
354 | "3\n",
355 | "3\n",
356 | "4\n",
357 | "1\n",
358 | "2\n",
359 | "3\n",
360 | "0\n"
361 | ]
362 | }
363 | ],
364 | "source": [
365 | "import numpy as np\n",
366 | "\n",
367 | "predict_data = np.random.random(size=(10,10))\n",
368 | "print(predictor.predict(predict_data).decode(\"utf-8\"))"
369 | ]
370 | },
371 | {
372 | "cell_type": "markdown",
373 | "metadata": {},
374 | "source": [
375 | "Don't forget to delete endpoint if you don't need it anymore."
376 | ]
377 | },
378 | {
379 | "cell_type": "code",
380 | "execution_count": 9,
381 | "metadata": {},
382 | "outputs": [
383 | {
384 | "name": "stderr",
385 | "output_type": "stream",
386 | "text": [
387 | "INFO:sagemaker:Deleting endpoint with name: daal-kmeans-test-2019-02-15-15-47-15-619\n"
388 | ]
389 | }
390 | ],
391 | "source": [
392 | "sess.delete_endpoint(predictor.endpoint)"
393 | ]
394 | },
395 | {
396 | "cell_type": "markdown",
397 | "metadata": {},
398 | "source": [
399 | "### Batch transform job\n",
400 | "If you don't need real-time prediction, you can use transform job. It uses saved model with centroids, compute assignments one time and saves it in specified or auto-generated output path.\n",
401 | "\n",
402 | "More about transform jobs: https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html\n",
403 | "\n",
404 | "Transformer API: https://sagemaker.readthedocs.io/en/latest/transformer.html"
405 | ]
406 | },
407 | {
408 | "cell_type": "code",
409 | "execution_count": 10,
410 | "metadata": {},
411 | "outputs": [
412 | {
413 | "name": "stderr",
414 | "output_type": "stream",
415 | "text": [
416 | "INFO:sagemaker:Creating model package with name: daal-kmeans-new-2019-02-15-16-06-34-857\n"
417 | ]
418 | },
419 | {
420 | "name": "stdout",
421 | "output_type": "stream",
422 | "text": [
423 | ".........."
424 | ]
425 | },
426 | {
427 | "name": "stderr",
428 | "output_type": "stream",
429 | "text": [
430 | "INFO:sagemaker:Creating model with name: daal-kmeans-new-2019-02-15-16-06-34-857-2019-02-15-16-07-20-327\n"
431 | ]
432 | },
433 | {
434 | "name": "stdout",
435 | "output_type": "stream",
436 | "text": [
437 | "\n"
438 | ]
439 | },
440 | {
441 | "name": "stderr",
442 | "output_type": "stream",
443 | "text": [
444 | "INFO:sagemaker:Creating transform job with name: daal-kmeans-test-2019-02-15-16-07-20-877\n"
445 | ]
446 | },
447 | {
448 | "name": "stdout",
449 | "output_type": "stream",
450 | "text": [
451 | "........................................!\n",
452 | "s3://sagemaker-us-east-2-123123123123/daal-kmeans-test-2019-02-15-16-07-20-877\n"
453 | ]
454 | }
455 | ],
456 | "source": [
457 | "transformer = daal_kmeans.transformer(1, 'ml.m4.xlarge')\n",
458 | "transformer.transform(\"s3:///\", content_type='text/csv')\n",
459 | "transformer.wait()\n",
460 | "print(transformer.output_path)"
461 | ]
462 | },
463 | {
464 | "cell_type": "code",
465 | "execution_count": null,
466 | "metadata": {},
467 | "outputs": [],
468 | "source": []
469 | }
470 | ],
471 | "metadata": {
472 | "kernelspec": {
473 | "display_name": "Python 3",
474 | "language": "python",
475 | "name": "python3"
476 | },
477 | "language_info": {
478 | "codemirror_mode": {
479 | "name": "ipython",
480 | "version": 3
481 | },
482 | "file_extension": ".py",
483 | "mimetype": "text/x-python",
484 | "name": "python",
485 | "nbconvert_exporter": "python",
486 | "pygments_lexer": "ipython3",
487 | "version": "3.6.5"
488 | }
489 | },
490 | "nbformat": 4,
491 | "nbformat_minor": 2
492 | }
493 |
--------------------------------------------------------------------------------
/daal/decision-forest-example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Decision Forest Classification and Regression with Intel® Data Analytics Acceleration Library in Amazon SageMaker\n",
8 | "\n",
9 | "## Introduction\n",
10 | "\n",
11 | "Intel® Data Analytics Acceleration Library (Intel® DAAL) is the library of Intel® architecture optimized building blocks covering all stages of data analytics: data acquisition from a data source, preprocessing, transformation, data mining, modeling, validation, and decision making. One of its algorithms is Decision Forest.\n",
12 | "\n",
13 | "The library provides decision forest classification and regression algorithms based on an ensemble of tree-structured classifiers (decision trees) built using the general technique of bootstrap aggregation (bagging) and random choice of features. Decision tree is a binary tree graph. Its internal (split) nodes represent a decision function used to select the following (child) node at the prediction stage. Its leaf (terminal) nodes represent the corresponding response values, which are the result of the prediction from the tree.\n",
14 | "\n",
15 | "Intel® DAAL developer guide: https://software.intel.com/en-us/daal-programming-guide\n",
16 | "\n",
17 | "Intel® DAAL documentation for Decision Forest: https://software.intel.com/en-us/daal-programming-guide-decision-forest"
18 | ]
19 | },
20 | {
21 | "cell_type": "markdown",
22 | "metadata": {},
23 | "source": [
24 | "## Decision Forest Usage with SageMaker Estimator\n",
25 | "Firstly, you need to import SageMaker package, get execution role and create session."
26 | ]
27 | },
28 | {
29 | "cell_type": "code",
30 | "execution_count": 1,
31 | "metadata": {},
32 | "outputs": [],
33 | "source": [
34 | "import sagemaker\n",
35 | "\n",
36 | "role = sagemaker.get_execution_role()\n",
37 | "sess = sagemaker.Session()"
38 | ]
39 | },
40 | {
41 | "cell_type": "markdown",
42 | "metadata": {},
43 | "source": [
44 | "Secondly, you can specify parameters of Decision Forest.\n",
45 | "#### Hyperparameters\n",
46 | "\n",
47 | " \n",
48 | " | Parameter name | \n",
49 | " Type | \n",
50 | " Default value | \n",
51 | " Description | \n",
52 | "
\n",
53 | " \n",
54 | " | nClasses | \n",
55 | " int | \n",
56 | " 2 | \n",
57 | " Number of classes in data (only for classification) | \n",
58 | "
\n",
59 | " \n",
60 | " | fptype | \n",
61 | " str | \n",
62 | " \"double\" | \n",
63 | " The floating-point type that the algorithm uses for intermediate computations. Can be \"float\" or \"double\" | \n",
64 | "
\n",
65 | " \n",
66 | " | method | \n",
67 | " str | \n",
68 | " \"defaultDense\" | \n",
69 | " The only training method supported so far is the default dense method | \n",
70 | "
\n",
71 | " \n",
72 | " | nTrees | \n",
73 | " int | \n",
74 | " 100 | \n",
75 | " The number of trees in the forest | \n",
76 | "
\n",
77 | " \n",
78 | " | observationsPerTreeFraction | \n",
79 | " int | \n",
80 | " 1 | \n",
81 | " Fraction of the training set S used to form the bootstrap set for a single tree training, observationsPerTreeFraction in (0, 1]. The observations are sampled randomly with replacement | \n",
82 | "
\n",
83 | " \n",
84 | " | featuresPerNode | \n",
85 | " int | \n",
86 | " 0 | \n",
87 | " The number of features tried as possible splits per node. If the parameter is set to 0, the library uses the square root of the number of features for classification and (the number of features)/3 for regression | \n",
88 | "
\n",
89 | " \n",
90 | " | maxTreeDepth | \n",
91 | " int | \n",
92 | " 0 | \n",
93 | " Maximal tree depth. Default is 0 (unlimited). | \n",
94 | "
\n",
95 | " \n",
96 | " | minObservationsInLeafNode | \n",
97 | " int | \n",
98 | " 1 for classification, 5 for regression | \n",
99 | " The number of neighbors | \n",
100 | "
\n",
101 | " \n",
102 | " | seed | \n",
103 | " int | \n",
104 | " 777 | \n",
105 | " The seed for random number generator, which is used to choose the bootstrap set, split features in every split node in a tree, and generate permutation required in computations of MDA variable importance | \n",
106 | "
\n",
107 | " \n",
108 | " | impurityThreshold | \n",
109 | " float | \n",
110 | " 0 | \n",
111 | " The threshold value used as stopping criteria: if the impurity value in the node is smaller than the threshold, the node is not split anymore | \n",
112 | "
\n",
113 | " \n",
114 | " | varImportance | \n",
115 | " str | \n",
116 | " \"None\" | \n",
117 | " The variable importance computation mode. Possible values: none – variable importance is not calculated MDI - Mean Decrease of Impurity, also known as the Gini importance or Mean Decrease Gini MDA_Raw - Mean Decrease of Accuracy (permutation importance) MDA_Scaled - the MDA_Raw value scaled by its standard deviation | \n",
118 | "
\n",
119 | " \n",
120 | " | resultsToCompute | \n",
121 | " str | \n",
122 | " \"None\" | \n",
123 | " Provide one of the following values to request a single characteristic or use bitwise OR to request a combination of the characteristics: computeOutOfBagError, computeOutOfBagErrorPerObservation | \n",
124 | "
\n",
125 | " \n",
126 | " | memorySavingMode | \n",
127 | " bool | \n",
128 | " False | \n",
129 | " If True, memory saving mode is enabled | \n",
130 | "
\n",
131 | " \n",
132 | " | bootstrap | \n",
133 | " bool | \n",
134 | " False for classification, True for regression | \n",
135 | " If True, bootstrap is enabled | \n",
136 | "
\n",
137 | "
\n",
138 | "\n",
139 | "Example of hyperparameters dictionary:"
140 | ]
141 | },
142 | {
143 | "cell_type": "code",
144 | "execution_count": 2,
145 | "metadata": {},
146 | "outputs": [],
147 | "source": [
148 | "decision_forest_params = {\n",
149 | " \"nClasses\": 5,\n",
150 | " \"fptype\":\"double\",\n",
151 | " \"method\":\"defaultDense\",\n",
152 | " \"nTrees\":\"100\",\n",
153 | " \"observationsPerTreeFraction\":\"1\",\n",
154 | " \"featuresPerNode\":\"0\",\n",
155 | " \"maxTreeDepth\":\"0\",\n",
156 | " \"minObservationsInLeafNode\":\"1\",\n",
157 | " \"seed\":\"777\",\n",
158 | " \"impurityThreshold\":\"0\",\n",
159 | " \"varImportance\":\"None\",\n",
160 | " \"resultsToCompute\":\"None\",\n",
161 | " \"memorySavingMode\":\"False\",\n",
162 | " \"bootstrap\":\"False\"\n",
163 | "}"
164 | ]
165 | },
166 | {
167 | "cell_type": "markdown",
168 | "metadata": {},
169 | "source": [
170 | "Then, you need to create SageMaker Estimator instance with following parameters:\n",
171 | "\n",
172 | " \n",
173 | " | Parameter name | \n",
174 | " Description | \n",
175 | "
\n",
176 | " \n",
177 | " | image_name | \n",
178 | " The container image to use for training | \n",
179 | "
\n",
180 | " \n",
181 | " | role | \n",
182 | " An AWS IAM role. The SageMaker training jobs and APIs that create SageMaker endpoints use this role to access training data and models | \n",
183 | "
\n",
184 | " \n",
185 | " | train_instance_count | \n",
186 | " Number of Amazon EC2 instances to use for training. Should be 1, because it is not distributed version of algorithm | \n",
187 | "
\n",
188 | " \n",
189 | " | train_instance_type | \n",
190 | " Type of EC2 instance to use for training. See available types on Amazon Marketplace page of algorithm | \n",
191 | "
\n",
192 | " \n",
193 | " | input_mode | \n",
194 | " The input mode that the algorithm supports. May be \"File\" or \"Pipe\" | \n",
195 | "
\n",
196 | " \n",
197 | " | output_path | \n",
198 | " S3 location for saving the trainig result (model artifacts and output files) | \n",
199 | "
\n",
200 | " \n",
201 | " | sagemaker_session | \n",
202 | " Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed | \n",
203 | "
\n",
204 | " \n",
205 | " | hyperparameters | \n",
206 | " Dictionary containing the hyperparameters to initialize this estimator with | \n",
207 | "
\n",
208 | "
\n",
209 | "Full SageMaker Estimator documentation: https://sagemaker.readthedocs.io/en/latest/estimators.html"
210 | ]
211 | },
212 | {
213 | "cell_type": "code",
214 | "execution_count": 3,
215 | "metadata": {},
216 | "outputs": [],
217 | "source": [
218 | "daal_decision_forest_arn = \"\" # you can find it on algorithm page in your subscriptions\n",
219 | "\n",
220 | "daal_decision_forest = sagemaker.algorithm.AlgorithmEstimator(\n",
221 | " algorithm_arn=daal_decision_forest_arn,\n",
222 | " role=role,\n",
223 | " base_job_name=\"\",\n",
224 | " train_instance_count=1,\n",
225 | " train_instance_type='ml.m4.xlarge',\n",
226 | " input_mode=\"File\",\n",
227 | " output_path=\"s3:///\",\n",
228 | " sagemaker_session=sess,\n",
229 | " hyperparameters=decision_forest_params\n",
230 | ")"
231 | ]
232 | },
233 | {
234 | "cell_type": "markdown",
235 | "metadata": {},
236 | "source": [
237 | "### Training stage\n",
238 | "On training stage, Decision Forest algorithm consume input data from S3 location.\n",
239 | "This container supports only .csv (\"comma-separated values\") files."
240 | ]
241 | },
242 | {
243 | "cell_type": "code",
244 | "execution_count": 5,
245 | "metadata": {
246 | "scrolled": false
247 | },
248 | "outputs": [
249 | {
250 | "name": "stderr",
251 | "output_type": "stream",
252 | "text": [
253 | "INFO:sagemaker:Creating training-job with name: testdaaldfcls2\n"
254 | ]
255 | },
256 | {
257 | "name": "stdout",
258 | "output_type": "stream",
259 | "text": [
260 | "2019-02-24 15:09:50 Starting - Starting the training job...\n",
261 | "2019-02-24 15:09:51 Starting - Launching requested ML instances......\n",
262 | "2019-02-24 15:11:18 Starting - Preparing the instances for training...\n",
263 | "2019-02-24 15:11:49 Downloading - Downloading input data...\n",
264 | "2019-02-24 15:11:56 Training - Downloading the training image.....\n",
265 | "\u001b[31m2019-02-24 15:12:57 INFO Container setup completed, In Docker entrypoint - train... \u001b[0m\n",
266 | "\u001b[31m2019-02-24 15:12:57 INFO Default Hyperparameters loaded: \u001b[0m\n",
267 | "\u001b[31m2019-02-24 15:12:57 INFO {'nClasses': '2', 'task': 'none', 'fptype': 'double', 'method': 'defaultDense', 'nTrees': '100', 'observationsPerTreeFraction': '1', 'featuresPerNode': '0', 'maxTreeDepth': '0', 'minObservationsInLeafNode': '0', 'seed': '777', 'impurityThreshold': '0', 'varImportance': 'none', 'resultsToCompute': '', 'memorySavingMode': 'False', 'bootstrap': 'False'}\u001b[0m\n",
268 | "\u001b[31m2019-02-24 15:12:57 INFO classification\u001b[0m\n",
269 | "\u001b[31m2019-02-24 15:12:57 INFO Updated with user hyperparameters, Final Hyperparameters: \u001b[0m\n",
270 | "\u001b[31m2019-02-24 15:12:57 INFO {'nClasses': '5', 'task': 'classification', 'fptype': 'double', 'method': 'defaultDense', 'nTrees': '100', 'observationsPerTreeFraction': '1.0', 'featuresPerNode': '0', 'maxTreeDepth': '0', 'minObservationsInLeafNode': '1', 'seed': '777', 'impurityThreshold': '0.0', 'varImportance': 'none', 'resultsToCompute': 'none', 'memorySavingMode': 'False', 'bootstrap': 'False'}\u001b[0m\n",
271 | "\u001b[31m2019-02-24 15:12:57 INFO Reading training data... \u001b[0m\n",
272 | "\u001b[31m2019-02-24 15:12:57 INFO Train data shape: (16000, 6)\u001b[0m\n",
273 | "\u001b[31m2019-02-24 15:12:57 INFO Files loading time: 0.021045207977294922\u001b[0m\n",
274 | "\u001b[31m2019-02-24 15:12:57 INFO Starting DAAL Decision Forest training...\u001b[0m\n",
275 | "\u001b[31m2019-02-24 15:12:58 INFO Training time, sec: 0.35150837898254395\u001b[0m\n",
276 | "\u001b[31m2019-02-24 15:12:58 INFO Saving model results...\u001b[0m\n",
277 | "\u001b[31m2019-02-24 15:12:58 INFO To get outOfBagErrorPerObservation use the method 'outOfBagErrorPerObservation' on the training model object\u001b[0m\n",
278 | "\u001b[31m2019-02-24 15:12:58 INFO Parameters saved at /opt/ml/model/parameters.json\u001b[0m\n",
279 | "\n",
280 | "2019-02-24 15:13:08 Uploading - Uploading generated training model\n",
281 | "2019-02-24 15:13:08 Completed - Training job completed\n",
282 | "Billable seconds: 79\n"
283 | ]
284 | }
285 | ],
286 | "source": [
287 | "daal_decision_forest.fit({\"training\": \"s3:///\"})"
288 | ]
289 | },
290 | {
291 | "cell_type": "markdown",
292 | "metadata": {},
293 | "source": [
294 | "### Real-time prediction\n",
295 | "Firstly, you need to deploy SageMaker endpoint that consumes data."
296 | ]
297 | },
298 | {
299 | "cell_type": "code",
300 | "execution_count": 6,
301 | "metadata": {
302 | "scrolled": true
303 | },
304 | "outputs": [
305 | {
306 | "name": "stderr",
307 | "output_type": "stream",
308 | "text": [
309 | "INFO:sagemaker:Creating model package with name: daal-df-cls-2019-02-24-15-13-43-176\n"
310 | ]
311 | },
312 | {
313 | "name": "stdout",
314 | "output_type": "stream",
315 | "text": [
316 | ".........."
317 | ]
318 | },
319 | {
320 | "name": "stderr",
321 | "output_type": "stream",
322 | "text": [
323 | "INFO:sagemaker:Creating model with name: daal-df-cls-2019-02-24-15-13-43-176-2019-02-24-15-14-28-639\n"
324 | ]
325 | },
326 | {
327 | "name": "stdout",
328 | "output_type": "stream",
329 | "text": [
330 | "\n"
331 | ]
332 | },
333 | {
334 | "name": "stderr",
335 | "output_type": "stream",
336 | "text": [
337 | "INFO:sagemaker:Creating endpoint with name testdaaldfcls2\n"
338 | ]
339 | },
340 | {
341 | "name": "stdout",
342 | "output_type": "stream",
343 | "text": [
344 | "--------------------------------------------------------------!"
345 | ]
346 | }
347 | ],
348 | "source": [
349 | "predictor = daal_decision_forest.deploy(1, \"ml.m4.xlarge\", serializer=sagemaker.predictor.csv_serializer)"
350 | ]
351 | },
352 | {
353 | "cell_type": "markdown",
354 | "metadata": {},
355 | "source": [
356 | "Secondly, you should pass data as numpy array to endpoint and get predictions.\n",
357 | "\n",
358 | "In this example we are passing random data, but you can use any numpy 2D array"
359 | ]
360 | },
361 | {
362 | "cell_type": "code",
363 | "execution_count": 7,
364 | "metadata": {},
365 | "outputs": [
366 | {
367 | "name": "stdout",
368 | "output_type": "stream",
369 | "text": [
370 | "4\n",
371 | "2\n",
372 | "2\n",
373 | "1\n",
374 | "3\n",
375 | "2\n",
376 | "3\n",
377 | "3\n",
378 | "3\n",
379 | "3\n",
380 | "\n"
381 | ]
382 | }
383 | ],
384 | "source": [
385 | "import numpy as np\n",
386 | "\n",
387 | "predict_data = np.random.random(size=(10,10))\n",
388 | "print(predictor.predict(predict_data).decode(\"utf-8\"))"
389 | ]
390 | },
391 | {
392 | "cell_type": "markdown",
393 | "metadata": {},
394 | "source": [
395 | "Don't forget to delete endpoint if you don't need it anymore."
396 | ]
397 | },
398 | {
399 | "cell_type": "code",
400 | "execution_count": 8,
401 | "metadata": {},
402 | "outputs": [
403 | {
404 | "name": "stderr",
405 | "output_type": "stream",
406 | "text": [
407 | "INFO:sagemaker:Deleting endpoint with name: testdaaldfcls2\n"
408 | ]
409 | }
410 | ],
411 | "source": [
412 | "sess.delete_endpoint(predictor.endpoint)"
413 | ]
414 | },
415 | {
416 | "cell_type": "markdown",
417 | "metadata": {},
418 | "source": [
419 | "### Batch transform job\n",
420 | "If you don't need real-time prediction, you can use transform job. It uses saved model, compute predictions one time and saves it in specified or auto-generated output path.\n",
421 | "\n",
422 | "More about transform jobs: https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html\n",
423 | "\n",
424 | "Transformer API: https://sagemaker.readthedocs.io/en/latest/transformer.html"
425 | ]
426 | },
427 | {
428 | "cell_type": "code",
429 | "execution_count": 10,
430 | "metadata": {},
431 | "outputs": [
432 | {
433 | "name": "stderr",
434 | "output_type": "stream",
435 | "text": [
436 | "INFO:sagemaker:Creating model package with name: daal-df-cls-2019-02-24-15-22-38-863\n"
437 | ]
438 | },
439 | {
440 | "name": "stdout",
441 | "output_type": "stream",
442 | "text": [
443 | ".........."
444 | ]
445 | },
446 | {
447 | "name": "stderr",
448 | "output_type": "stream",
449 | "text": [
450 | "INFO:sagemaker:Creating model with name: daal-df-cls-2019-02-24-15-22-38-863-2019-02-24-15-23-24-300\n"
451 | ]
452 | },
453 | {
454 | "name": "stdout",
455 | "output_type": "stream",
456 | "text": [
457 | "\n"
458 | ]
459 | },
460 | {
461 | "name": "stderr",
462 | "output_type": "stream",
463 | "text": [
464 | "INFO:sagemaker:Creating transform job with name: testdfclst1\n"
465 | ]
466 | },
467 | {
468 | "name": "stdout",
469 | "output_type": "stream",
470 | "text": [
471 | "........................................!\n",
472 | "s3://sagemaker-us-east-2-123123123123/testdfclst1\n"
473 | ]
474 | }
475 | ],
476 | "source": [
477 | "transformer = daal_decision_forest.transformer(1, 'ml.m4.xlarge')\n",
478 | "transformer.transform(\"s3:///\", content_type='text/csv', job_name=\"\")\n",
479 | "transformer.wait()\n",
480 | "print(transformer.output_path)"
481 | ]
482 | }
483 | ],
484 | "metadata": {
485 | "kernelspec": {
486 | "display_name": "conda_python3",
487 | "language": "python",
488 | "name": "conda_python3"
489 | },
490 | "language_info": {
491 | "codemirror_mode": {
492 | "name": "ipython",
493 | "version": 3
494 | },
495 | "file_extension": ".py",
496 | "mimetype": "text/x-python",
497 | "name": "python",
498 | "nbconvert_exporter": "python",
499 | "pygments_lexer": "ipython3",
500 | "version": "3.6.5"
501 | }
502 | },
503 | "nbformat": 4,
504 | "nbformat_minor": 2
505 | }
506 |
--------------------------------------------------------------------------------
/daal/logistic-regression-example.ipynb:
--------------------------------------------------------------------------------
1 | {
2 | "cells": [
3 | {
4 | "cell_type": "markdown",
5 | "metadata": {},
6 | "source": [
7 | "# Logistic Regression with Intel® Data Analytics Acceleration Library in Amazon SageMaker\n",
8 | "\n",
9 | "# Introduction\n",
10 | "\n",
11 | "Intel® Data Analytics Acceleration Library (Intel® DAAL) is the library of Intel® architecture optimized building blocks covering all stages of data analytics: data acquisition from a data source, preprocessing, transformation, data mining, modeling, validation, and decision making. One of its classification algorithms is Logistic Regression.\n",
12 | "\n",
13 | "Logistic Regression is a method for modeling the relationships between one or more explanatory variables and a categorical variable by expressing the posterior statistical distribution of the categorical variable via linear functions on observed data. If the categorical variable is binary, that is it takes only two values, \"0\" and \"1\", the Logistic Regression is simple, otherwise, it is multinomial.\n",
14 | "DAAL Logistic Regression algorithm support L1 and L2 regularizations.\n",
15 | "\n",
16 | "\n",
17 | "Intel® DAAL developer guide: https://software.intel.com/en-us/daal-programming-guide\n",
18 | "\n",
19 | "Intel® DAAL documentation for Logistic Regression: https://software.intel.com/en-us/daal-programming-guide-logistic-regression"
20 | ]
21 | },
22 | {
23 | "cell_type": "markdown",
24 | "metadata": {},
25 | "source": [
26 | "* [Hyperparameters description](#1-bullet)\n",
27 | "* [Usage of the algorithm](#2-bullet)\n",
28 | " * [Upload the data for training](#3-bullet)\n",
29 | " * [Creating Training Job using Algorithm ARN](#4-bullet)\n",
30 | " * [Live Inference Endpoint for Prediction stage](#5-bullet)\n",
31 | " * [Batch transform job](#6-bullet)"
32 | ]
33 | },
34 | {
35 | "cell_type": "markdown",
36 | "metadata": {},
37 | "source": [
38 | "# Hyperparameters description: \n",
39 | "\n",
40 | "\n",
41 | " \n",
42 | " | Required parameters | \n",
43 | " Type | \n",
44 | " Default value | \n",
45 | " Description | \n",
46 | "
\n",
47 | " \n",
48 | " | nClasses | \n",
49 | " integer | \n",
50 | " None | \n",
51 | " Number of classes in training dataset | \n",
52 | "
\n",
53 | "
\n",
54 | "\n",
55 | "\n",
56 | "\n",
57 | "\n",
58 | " \n",
59 | " | Optional parameters | \n",
60 | " Type | \n",
61 | " Default value | \n",
62 | " Description | \n",
63 | "
\n",
64 | " \n",
65 | " | penaltyL1 | \n",
66 | " float | \n",
67 | " 0 | \n",
68 | " Penalty coefficient for L1 regularization | \n",
69 | "
\n",
70 | " \n",
71 | " | penaltyL2 | \n",
72 | " float | \n",
73 | " 0 | \n",
74 | " Penalty coefficient for L2 regularization | \n",
75 | "
\n",
76 | " \n",
77 | " | interceptFlag | \n",
78 | " bool | \n",
79 | " False | \n",
80 | " A flag that indicates a need to compute θ0j | \n",
81 | "
\n",
82 | " \n",
83 | " | solverName | \n",
84 | " str | \n",
85 | " 'sgd' | \n",
86 | " Name of solver that will be used for training stage available values: 'lbfgs', 'adagrad', 'saga', 'sgd' | \n",
87 | "
\n",
88 | " \n",
89 | " | solverMethod | \n",
90 | " str | \n",
91 | " 'defaultDense' | \n",
92 | " Method of the solver. Available values for 'sgd': \n",
93 | " 'momentum', 'minibatch', 'defaultDense' \n",
94 | " available values for others solver: 'defaultDense' | \n",
95 | "
\n",
96 | " \n",
97 | " | solverMaxIterations | \n",
98 | " integer | \n",
99 | " 100 | \n",
100 | " Max number of iterations for training stage | \n",
101 | "
\n",
102 | " \n",
103 | " | solverAccuracyThreshold | \n",
104 | " float | \n",
105 | " 1.0-e4 | \n",
106 | " Accuracy of the algorithm. The algorithm terminates when \n",
107 | " this accuracy is achieved. | \n",
108 | "
\n",
109 | " \n",
110 | " | solverBatchSize | \n",
111 | " integer | \n",
112 | " number of rows in training dataset | \n",
113 | " Number of batch indices to compute the stochastic gradient. | \n",
114 | "
\n",
115 | " \n",
116 | " | solverLearningRate | \n",
117 | " float | \n",
118 | " 1.0-e3 | \n",
119 | " learning rate for optimization problem applicable for 'sgd','adagrad', 'saga' only | \n",
120 | "
\n",
121 | " \n",
122 | " | solverStepLength | \n",
123 | " float | \n",
124 | " 1.0-e3 | \n",
125 | " step size for optimization problem applicable for 'lbfgs' only | \n",
126 | "
\n",
127 | " \n",
128 | " | solverCorrectionPairBatchSize | \n",
129 | " integer | \n",
130 | " number of rows in training dataset | \n",
131 | " Number of batch indices to compute Hessian aproximation. applicable for 'lbfgs' only | \n",
132 | "
\n",
133 | " \n",
134 | " | solverL | \n",
135 | " integer | \n",
136 | " 1 | \n",
137 | " The number of iterations between calculations of the curvature estimates \n",
138 | " applicable for 'lbfgs' only\n",
139 | " | \n",
140 | "
\n",
141 | "
\n"
142 | ]
143 | },
144 | {
145 | "cell_type": "markdown",
146 | "metadata": {},
147 | "source": [
148 | " For more detailes please visit:\n",
149 | " https://software.intel.com/en-us/daal-programming-guide\n",
150 | " \n",
151 | " All parameters that start from 'solver' have a name without 'solver' prefix in DAAL documentation"
152 | ]
153 | },
154 | {
155 | "cell_type": "markdown",
156 | "metadata": {},
157 | "source": [
158 | "# Usage of the algorithm \n",
159 | "At the first we need to import SageMaker Python package, get execution role and create session."
160 | ]
161 | },
162 | {
163 | "cell_type": "code",
164 | "execution_count": 1,
165 | "metadata": {},
166 | "outputs": [],
167 | "source": [
168 | "import numpy as np\n",
169 | "import pandas as pd\n",
170 | "from sagemaker import get_execution_role\n",
171 | "import sagemaker as sage\n",
172 | "\n",
173 | "role = get_execution_role()\n",
174 | "sess = sage.Session()"
175 | ]
176 | },
177 | {
178 | "cell_type": "markdown",
179 | "metadata": {},
180 | "source": [
181 | "## Upload the data for training \n",
182 | "When training large models with huge amounts of data, you'll typically use big data tools, like Amazon Athena, AWS Glue, or Amazon EMR, to create your data in S3. For the purposes of this example, we're using some the classic Iris dataset\n",
183 | "\n",
184 | "We can use use the tools provided by the SageMaker Python SDK to upload the data to a default bucket."
185 | ]
186 | },
187 | {
188 | "cell_type": "markdown",
189 | "metadata": {},
190 | "source": [
191 | "At first download iris-data via sklearn and split it into 'training' and 'test' data"
192 | ]
193 | },
194 | {
195 | "cell_type": "code",
196 | "execution_count": 2,
197 | "metadata": {},
198 | "outputs": [],
199 | "source": [
200 | "from sklearn import datasets\n",
201 | "iris = datasets.load_iris()\n",
202 | "\n",
203 | "from sklearn.model_selection import train_test_split\n",
204 | "X_train, X_test, y_train, y_test = train_test_split(iris.data, iris.target, test_size=0.33, random_state=777)"
205 | ]
206 | },
207 | {
208 | "cell_type": "markdown",
209 | "metadata": {},
210 | "source": [
211 | "Upload the training data to S3"
212 | ]
213 | },
214 | {
215 | "cell_type": "code",
216 | "execution_count": 31,
217 | "metadata": {},
218 | "outputs": [
219 | {
220 | "name": "stdout",
221 | "output_type": "stream",
222 | "text": [
223 | "Training artifacts will be uploaded at: s3://daal-log-reg-test/log_reg_iris_data_test\n",
224 | "And data_location will be a parameter for fit method (see training stage below).\n"
225 | ]
226 | },
227 | {
228 | "data": {
229 | "text/plain": [
230 | "'s3://daal-log-reg-test/log_reg_iris_data_test/training_data.csv'"
231 | ]
232 | },
233 | "execution_count": 31,
234 | "metadata": {},
235 | "output_type": "execute_result"
236 | }
237 | ],
238 | "source": [
239 | "import numpy as np\n",
240 | "\n",
241 | "reshaped_Y_train = y_train.reshape(y_train.shape[0],1)\n",
242 | "\n",
243 | "#Last column in training dataset is labels. \n",
244 | "training_data = np.concatenate((X_train,reshaped_Y_train),axis=1)\n",
245 | "\n",
246 | "#save the training data\n",
247 | "train_data_file = 'training_data.csv'\n",
248 | "# NO comma at the end of each line in training data.\n",
249 | "np.savetxt(train_data_file,training_data,delimiter=',')\n",
250 | "\n",
251 | "# S3 prefix\n",
252 | "bucket_name = 'daal-log-reg-test'\n",
253 | "data_key = 'log_reg_iris_data_test'\n",
254 | "\n",
255 | "output_location = 's3://{}/{}'.format(bucket_name, 'output')\n",
256 | "data_location = output_location = 's3://{}/{}'.format(bucket_name, data_key)\n",
257 | "print (\"Training artifacts will be uploaded at: \" + output_location)\n",
258 | "print (\"And data_location will be a parameter for fit method (see training stage below).\")\n",
259 | "\n",
260 | "sess.upload_data(train_data_file, bucket=bucket_name, key_prefix=data_key)"
261 | ]
262 | },
263 | {
264 | "cell_type": "markdown",
265 | "metadata": {},
266 | "source": [
267 | "Example of hyperparameters list:"
268 | ]
269 | },
270 | {
271 | "cell_type": "code",
272 | "execution_count": 32,
273 | "metadata": {},
274 | "outputs": [],
275 | "source": [
276 | "hyperparameters={\"nClasses\": 3,\n",
277 | " \"penaltyL1\": 0,\n",
278 | " \"penaltyL2\": 0,\n",
279 | " \"interceptFlag\": False,\n",
280 | " \"solverBatchSize\":100, #as training data has 100 samples only, but default value is 150\n",
281 | " #\"solverName\": \"lbfgs\", \n",
282 | " #\"solverMaxIterations\": 1000,\n",
283 | " #\"solverAccuracyThreshold\": 0.0001,\n",
284 | " #\"solverL\": 1\n",
285 | " }"
286 | ]
287 | },
288 | {
289 | "cell_type": "markdown",
290 | "metadata": {},
291 | "source": [
292 | "Then, you need to create SageMaker Estimator instance with following parameters:\n",
293 | "\n",
294 | " \n",
295 | " | Parameter name | \n",
296 | " Description | \n",
297 | "
\n",
298 | " \n",
299 | " | image_name | \n",
300 | " The container image to use for training | \n",
301 | "
\n",
302 | " \n",
303 | " | role | \n",
304 | " An AWS IAM role. The SageMaker training jobs and APIs that create SageMaker endpoints use this role to access training data and models | \n",
305 | "
\n",
306 | " \n",
307 | " | train_instance_count | \n",
308 | " Number of Amazon EC2 instances to use for training. Should be 1, because it is not distributed version of algorithm | \n",
309 | "
\n",
310 | " \n",
311 | " | train_instance_type | \n",
312 | " Type of EC2 instance to use for training. See available types on Amazon Marketplace page of algorithm | \n",
313 | "
\n",
314 | " \n",
315 | " | input_mode | \n",
316 | " The input mode that the algorithm supports. May be \"File\" or \"Pipe\" | \n",
317 | "
\n",
318 | " \n",
319 | " | output_path | \n",
320 | " S3 location for saving the trainig result (model artifacts and output files) | \n",
321 | "
\n",
322 | " \n",
323 | " | sagemaker_session | \n",
324 | " Session object which manages interactions with Amazon SageMaker APIs and any other AWS services needed | \n",
325 | "
\n",
326 | " \n",
327 | " | hyperparameters | \n",
328 | " Dictionary containing the hyperparameters to initialize this estimator with | \n",
329 | "
\n",
330 | "
\n",
331 | "SageMaker Estimator documentation: https://sagemaker.readthedocs.io/en/latest/estimators.html"
332 | ]
333 | },
334 | {
335 | "cell_type": "markdown",
336 | "metadata": {},
337 | "source": [
338 | "## Creating Training Job using Algorithm ARN\n",
339 | "Please put in the algorithm arn you want to use below. This can either be an AWS Marketplace algorithm you subscribed to (or) one of the algorithms you created in your own account.\n",
340 | "The algorithm arn listed below belongs to the Intel DAAL Logistic Regression."
341 | ]
342 | },
343 | {
344 | "cell_type": "code",
345 | "execution_count": 33,
346 | "metadata": {},
347 | "outputs": [],
348 | "source": [
349 | "daal_log_reg_arn = \"arn:aws:sagemaker:us-east-2:057799348421:algorithm/intel-daal-logistic-regression-ce8a1f38da2f8a234e4205a356021dbf\" # you can find it on algorithm page in your subscriptions\n",
350 | "#daal_log_reg_arn = \"\"\n",
351 | "daal_log_reg = sage.algorithm.AlgorithmEstimator(\n",
352 | " algorithm_arn=daal_log_reg_arn,\n",
353 | " base_job_name=\"daal-log-reg-alg-test\",\n",
354 | " role=role,\n",
355 | " train_instance_count=1,\n",
356 | " train_instance_type='ml.m4.xlarge',\n",
357 | " input_mode=\"File\",\n",
358 | " output_path=output_location,\n",
359 | " sagemaker_session=sess,\n",
360 | " hyperparameters=hyperparameters\n",
361 | ")"
362 | ]
363 | },
364 | {
365 | "cell_type": "markdown",
366 | "metadata": {},
367 | "source": [
368 | "## Run training stage\n",
369 | "On training stage, Logistic Rergession algorithm consume input data from S3 location and train the model."
370 | ]
371 | },
372 | {
373 | "cell_type": "code",
374 | "execution_count": 35,
375 | "metadata": {},
376 | "outputs": [
377 | {
378 | "name": "stderr",
379 | "output_type": "stream",
380 | "text": [
381 | "INFO:sagemaker:Creating training-job with name: daal-log-reg-alg-test-2018-11-30-10-58-20-329\n"
382 | ]
383 | },
384 | {
385 | "name": "stdout",
386 | "output_type": "stream",
387 | "text": [
388 | "2018-11-30 10:58:20 Starting - Starting the training job...\n",
389 | "2018-11-30 10:58:25 Starting - Launching requested ML instances......\n",
390 | "2018-11-30 10:59:21 Starting - Preparing the instances for training......\n",
391 | "2018-11-30 11:00:40 Downloading - Downloading input data\n",
392 | "2018-11-30 11:00:40 Training - Downloading the training image..\n",
393 | "\u001b[31m2018-11-30 11:00:59 INFO Container setup completed, In Docker entrypoint - train... \u001b[0m\n",
394 | "\u001b[31m2018-11-30 11:00:59 INFO Default Hyperparameters loaded: \u001b[0m\n",
395 | "\u001b[31m2018-11-30 11:00:59 INFO {'dtype': 'float',\n",
396 | " 'interceptFlag': True,\n",
397 | " 'nClasses': 0,\n",
398 | " 'penaltyL1': 0,\n",
399 | " 'penaltyL2': 0}\u001b[0m\n",
400 | "\u001b[31m2018-11-30 11:00:59 INFO Reading training data... \u001b[0m\n",
401 | "\u001b[31m2018-11-30 11:00:59 INFO Training data with labels shape: (100, 5)\u001b[0m\n",
402 | "\u001b[31m2018-11-30 11:00:59 INFO Updated with user hyperparameters, Final Hyperparameters: \u001b[0m\n",
403 | "\u001b[31m2018-11-30 11:00:59 INFO {'dtype': 'float',\n",
404 | " 'interceptFlag': 'False',\n",
405 | " 'nClasses': '3',\n",
406 | " 'penaltyL1': '0.0',\n",
407 | " 'penaltyL2': '0.0'}\u001b[0m\n",
408 | "\u001b[31m2018-11-30 11:00:59 INFO {'solverAccuracyThreshold': '0.0001',\n",
409 | " 'solverBatchSize': '100',\n",
410 | " 'solverCorrectionPairBatchSize': 100,\n",
411 | " 'solverL': 1,\n",
412 | " 'solverLearningRate': '0.001',\n",
413 | " 'solverMaxIterations': '1000',\n",
414 | " 'solverMethod': 'momentum',\n",
415 | " 'solverName': 'sgd',\n",
416 | " 'solverStepLength': 0.001}\u001b[0m\n",
417 | "\u001b[31m2018-11-30 11:00:59 INFO If optional parameters were not specified default values will be used.\u001b[0m\n",
418 | "\u001b[31m2018-11-30 11:00:59 INFO Training Data Shape: (100, 4)\u001b[0m\n",
419 | "\u001b[31m2018-11-30 11:00:59 INFO Training Labels Shape: (100, 1)\u001b[0m\n",
420 | "\u001b[31m2018-11-30 11:00:59 INFO Starting DAAL Logistic Regression training...\u001b[0m\n",
421 | "\u001b[31m2018-11-30 11:00:59 INFO Training time in sec = 0.040670156478881836\u001b[0m\n",
422 | "\u001b[31m2018-11-30 11:00:59 INFO number of classes saved at /opt/ml/model/daal-log-reg-train-features-classes.csv\u001b[0m\n",
423 | "\u001b[31m2018-11-30 11:00:59 INFO dtype saved at /opt/ml/model/daal-log-reg-dtype.txt\u001b[0m\n",
424 | "\u001b[31m2018-11-30 11:00:59 INFO Model saved at <_io.BufferedWriter name='/opt/ml/model/daal-log-reg-train-model.pkl'>\u001b[0m\n",
425 | "\n",
426 | "2018-11-30 11:01:05 Uploading - Uploading generated training model\n",
427 | "2018-11-30 11:01:05 Completed - Training job completed\n",
428 | "Billable seconds: 43\n"
429 | ]
430 | }
431 | ],
432 | "source": [
433 | "daal_log_reg.fit({\"training\": data_location})"
434 | ]
435 | },
436 | {
437 | "cell_type": "markdown",
438 | "metadata": {},
439 | "source": [
440 | "## Live Inference Endpoint for Prediction stage\n",
441 | "On prediction stage, Logistic Regression algorithm compute probabulity of classes and retern the class for each input samples.\n",
442 | "Firstly, you need to deploy SageMaker endpoint that consumes data.\n"
443 | ]
444 | },
445 | {
446 | "cell_type": "code",
447 | "execution_count": 23,
448 | "metadata": {},
449 | "outputs": [
450 | {
451 | "name": "stderr",
452 | "output_type": "stream",
453 | "text": [
454 | "INFO:sagemaker:Creating model package with name: intel-daal-logistic-regression-ce8a1f38-2018-11-30-10-05-13-703\n"
455 | ]
456 | },
457 | {
458 | "name": "stdout",
459 | "output_type": "stream",
460 | "text": [
461 | ".........."
462 | ]
463 | },
464 | {
465 | "name": "stderr",
466 | "output_type": "stream",
467 | "text": [
468 | "INFO:sagemaker:Creating model with name: intel-daal-logistic-regression-ce8a1f38-2018-11-30-10-05-59-280\n"
469 | ]
470 | },
471 | {
472 | "name": "stdout",
473 | "output_type": "stream",
474 | "text": [
475 | "\n"
476 | ]
477 | },
478 | {
479 | "name": "stderr",
480 | "output_type": "stream",
481 | "text": [
482 | "INFO:sagemaker:Creating endpoint with name daal-log-reg-alg-test-2018-11-30-10-00-36-782\n"
483 | ]
484 | },
485 | {
486 | "name": "stdout",
487 | "output_type": "stream",
488 | "text": [
489 | "--------------------------------------------------------------------------!"
490 | ]
491 | }
492 | ],
493 | "source": [
494 | "from sagemaker.predictor import csv_serializer\n",
495 | "predictor = daal_log_reg.deploy(1, 'ml.m4.xlarge', serializer=csv_serializer)"
496 | ]
497 | },
498 | {
499 | "cell_type": "markdown",
500 | "metadata": {},
501 | "source": [
502 | "Define functions to handle response from predictor instance at first:"
503 | ]
504 | },
505 | {
506 | "cell_type": "code",
507 | "execution_count": 24,
508 | "metadata": {},
509 | "outputs": [],
510 | "source": [
511 | "def output_to_np(prediction, numberOfSamples, nClasses):\n",
512 | " if nClasses == 2:\n",
513 | " return np.fromstring(prediction, dtype=np.float64, sep=' ').reshape(2,numberOfSamples)\n",
514 | " if nClasses > 2:\n",
515 | " return np.fromstring(prediction, dtype=np.float64, sep=' ').reshape(nClasses+1,numberOfSamples)\n",
516 | "\n",
517 | "def output_to_pd(prediction, nClasses):\n",
518 | " C=[]\n",
519 | " C.append('lables')\n",
520 | " if nClasses > 2:\n",
521 | " for i in range(nClasses):\n",
522 | " C.append('probability of class ' + str(i))\n",
523 | " if nClasses == 2:\n",
524 | " C.append('probability of class 1')\n",
525 | " return pd.DataFrame(np.transpose(prediction), columns=C)"
526 | ]
527 | },
528 | {
529 | "cell_type": "markdown",
530 | "metadata": {},
531 | "source": [
532 | "After deployment, you should pass data as numpy array to predictor instance and get predicted lables and probabilities."
533 | ]
534 | },
535 | {
536 | "cell_type": "code",
537 | "execution_count": 26,
538 | "metadata": {},
539 | "outputs": [
540 | {
541 | "name": "stdout",
542 | "output_type": "stream",
543 | "text": [
544 | "2.0\n",
545 | "0.0\n",
546 | "2.0\n",
547 | "2.0\n",
548 | "1.0\n",
549 | "0.0\n",
550 | "2.0\n",
551 | "2.0\n",
552 | "0.0\n",
553 | "0.0\n",
554 | "2.0\n",
555 | "1.0\n",
556 | "1.0\n",
557 | "2.0\n",
558 | "2.0\n",
559 | "2.0\n",
560 | "0.0\n",
561 | "2.0\n",
562 | "0.0\n",
563 | "1.0\n",
564 | "1.0\n",
565 | "1.0\n",
566 | "2.0\n",
567 | "0.0\n",
568 | "2.0\n",
569 | "0.0\n",
570 | "1.0\n",
571 | "0.0\n",
572 | "2.0\n",
573 | "2.0\n",
574 | "0.0\n",
575 | "2.0\n",
576 | "0.0\n",
577 | "2.0\n",
578 | "1.0\n",
579 | "0.0\n",
580 | "0.0\n",
581 | "0.0\n",
582 | "1.0\n",
583 | "0.0\n",
584 | "0.0\n",
585 | "2.0\n",
586 | "1.0\n",
587 | "1.0\n",
588 | "0.0\n",
589 | "2.0\n",
590 | "2.0\n",
591 | "0.0\n",
592 | "2.0\n",
593 | "1.0\n",
594 | "\n",
595 | "\n"
596 | ]
597 | }
598 | ],
599 | "source": [
600 | "#usage from slplited data\n",
601 | "payload = X_test\n",
602 | "\n",
603 | "ground_truth = y_test\n",
604 | "prediction = predictor.predict(payload).decode('utf-8')\n",
605 | "print(prediction)\n",
606 | "#np_res = output_to_np(prediction=prediction,numberOfSamples=payload.shape[0],nClasses=3)\n",
607 | "#pd_res = output_to_pd(prediction=np_res, nClasses=3)"
608 | ]
609 | },
610 | {
611 | "cell_type": "markdown",
612 | "metadata": {},
613 | "source": [
614 | "Print the first 5 rows of obtained prediction:"
615 | ]
616 | },
617 | {
618 | "cell_type": "code",
619 | "execution_count": 32,
620 | "metadata": {},
621 | "outputs": [
622 | {
623 | "data": {
624 | "text/html": [
625 | "\n",
626 | "\n",
639 | "
\n",
640 | " \n",
641 | " \n",
642 | " | \n",
643 | " lables | \n",
644 | " probability of class 0 | \n",
645 | " probability of class 1 | \n",
646 | " probability of class 2 | \n",
647 | "
\n",
648 | " \n",
649 | " \n",
650 | " \n",
651 | " | 0 | \n",
652 | " 2.0 | \n",
653 | " 9.311358e-10 | \n",
654 | " 0.010659 | \n",
655 | " 9.893406e-01 | \n",
656 | "
\n",
657 | " \n",
658 | " | 1 | \n",
659 | " 0.0 | \n",
660 | " 9.948357e-01 | \n",
661 | " 0.005164 | \n",
662 | " 1.804341e-12 | \n",
663 | "
\n",
664 | " \n",
665 | " | 2 | \n",
666 | " 2.0 | \n",
667 | " 2.409668e-09 | \n",
668 | " 0.018753 | \n",
669 | " 9.812474e-01 | \n",
670 | "
\n",
671 | " \n",
672 | " | 3 | \n",
673 | " 2.0 | \n",
674 | " 3.761642e-09 | \n",
675 | " 0.003549 | \n",
676 | " 9.964515e-01 | \n",
677 | "
\n",
678 | " \n",
679 | " | 4 | \n",
680 | " 1.0 | \n",
681 | " 2.466924e-05 | \n",
682 | " 0.858058 | \n",
683 | " 1.419178e-01 | \n",
684 | "
\n",
685 | " \n",
686 | "
\n",
687 | "
"
688 | ],
689 | "text/plain": [
690 | " lables probability of class 0 probability of class 1 \\\n",
691 | "0 2.0 9.311358e-10 0.010659 \n",
692 | "1 0.0 9.948357e-01 0.005164 \n",
693 | "2 2.0 2.409668e-09 0.018753 \n",
694 | "3 2.0 3.761642e-09 0.003549 \n",
695 | "4 1.0 2.466924e-05 0.858058 \n",
696 | "\n",
697 | " probability of class 2 \n",
698 | "0 9.893406e-01 \n",
699 | "1 1.804341e-12 \n",
700 | "2 9.812474e-01 \n",
701 | "3 9.964515e-01 \n",
702 | "4 1.419178e-01 "
703 | ]
704 | },
705 | "execution_count": 32,
706 | "metadata": {},
707 | "output_type": "execute_result"
708 | }
709 | ],
710 | "source": [
711 | "pd_res.head()"
712 | ]
713 | },
714 | {
715 | "cell_type": "markdown",
716 | "metadata": {},
717 | "source": [
718 | "Compute the accuracy of trained model on test dataset 'y_test'."
719 | ]
720 | },
721 | {
722 | "cell_type": "code",
723 | "execution_count": 35,
724 | "metadata": {
725 | "scrolled": false
726 | },
727 | "outputs": [
728 | {
729 | "name": "stdout",
730 | "output_type": "stream",
731 | "text": [
732 | "DAAL Accuracy on Iris Train Set: 1.0\n"
733 | ]
734 | }
735 | ],
736 | "source": [
737 | "from sklearn.metrics.cluster import v_measure_score\n",
738 | "\n",
739 | "prediction_arr = np_res[0]\n",
740 | "ground_truth = y_test\n",
741 | "#print(prediction_arr)\n",
742 | "#print(ground_truth)\n",
743 | "\n",
744 | "print(\"DAAL Accuracy on Iris Train Set: \", str(v_measure_score(prediction_arr, ground_truth)))"
745 | ]
746 | },
747 | {
748 | "cell_type": "markdown",
749 | "metadata": {},
750 | "source": [
751 | "Don't forget to delete endpoint if you don't need it anymore. Otherwise it will run as a daemon process.\n"
752 | ]
753 | },
754 | {
755 | "cell_type": "code",
756 | "execution_count": 27,
757 | "metadata": {},
758 | "outputs": [
759 | {
760 | "name": "stderr",
761 | "output_type": "stream",
762 | "text": [
763 | "INFO:sagemaker:Deleting endpoint with name: daal-log-reg-alg-test-2018-11-30-10-00-36-782\n"
764 | ]
765 | }
766 | ],
767 | "source": [
768 | "sess.delete_endpoint(predictor.endpoint)"
769 | ]
770 | },
771 | {
772 | "cell_type": "markdown",
773 | "metadata": {},
774 | "source": [
775 | "## Batch transform job\n",
776 | "If you don't need real-time prediction, you can use transform job. It uses saved model, compute transformed data one time and saves it in specified or auto-generated output path.\n",
777 | "\n",
778 | "More about transform jobs: https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html\n",
779 | "\n",
780 | "Transformer API: https://sagemaker.readthedocs.io/en/latest/transformer.html"
781 | ]
782 | },
783 | {
784 | "cell_type": "code",
785 | "execution_count": 28,
786 | "metadata": {},
787 | "outputs": [
788 | {
789 | "name": "stderr",
790 | "output_type": "stream",
791 | "text": [
792 | "INFO:sagemaker:Creating model package with name: intel-daal-logistic-regression-ce8a1f38-2018-11-30-10-25-22-669\n"
793 | ]
794 | },
795 | {
796 | "name": "stdout",
797 | "output_type": "stream",
798 | "text": [
799 | ".........."
800 | ]
801 | },
802 | {
803 | "name": "stderr",
804 | "output_type": "stream",
805 | "text": [
806 | "INFO:sagemaker:Creating model with name: intel-daal-logistic-regression-ce8a1f38-2018-11-30-10-26-08-303\n"
807 | ]
808 | },
809 | {
810 | "name": "stdout",
811 | "output_type": "stream",
812 | "text": [
813 | "\n"
814 | ]
815 | }
816 | ],
817 | "source": [
818 | "transformer = daal_log_reg.transformer(1, \"ml.m4.xlarge\")"
819 | ]
820 | },
821 | {
822 | "cell_type": "code",
823 | "execution_count": 29,
824 | "metadata": {
825 | "scrolled": true
826 | },
827 | "outputs": [
828 | {
829 | "name": "stderr",
830 | "output_type": "stream",
831 | "text": [
832 | "INFO:sagemaker:Creating transform job with name: daal-log-reg-alg-test-2018-11-30-10-26-55-276\n"
833 | ]
834 | },
835 | {
836 | "name": "stdout",
837 | "output_type": "stream",
838 | "text": [
839 | ".......................................!\n"
840 | ]
841 | }
842 | ],
843 | "source": [
844 | "transformer.transform(\"s3://daal-log-reg-test/input/data/test_data.csv\", content_type=\"text/csv\")\n",
845 | "transformer.wait()"
846 | ]
847 | },
848 | {
849 | "cell_type": "code",
850 | "execution_count": 30,
851 | "metadata": {},
852 | "outputs": [
853 | {
854 | "name": "stdout",
855 | "output_type": "stream",
856 | "text": [
857 | "2.0\n",
858 | "1.0\n",
859 | "2.0\n",
860 | "2.0\n",
861 | "1.0\n",
862 | "0.0\n",
863 | "0.0\n",
864 | "0.0\n",
865 | "2.0\n",
866 | "2.0\n",
867 | "1.0\n",
868 | "2.0\n",
869 | "0.0\n",
870 | "0.0\n",
871 | "0.0\n",
872 | "2.0\n",
873 | "1.0\n",
874 | "2.0\n",
875 | "0.0\n",
876 | "0.0\n",
877 | "1.0\n",
878 | "0.0\n",
879 | "2.0\n",
880 | "1.0\n",
881 | "0.0\n",
882 | "2.0\n",
883 | "1.0\n",
884 | "2.0\n",
885 | "0.0\n",
886 | "0.0\n",
887 | "0.0\n",
888 | "1.0\n",
889 | "2.0\n",
890 | "1.0\n",
891 | "1.0\n",
892 | "2.0\n",
893 | "2.0\n",
894 | "2.0\n",
895 | "2.0\n",
896 | "1.0\n",
897 | "1.0\n",
898 | "2.0\n",
899 | "1.0\n",
900 | "0.0\n",
901 | "2.0\n",
902 | "0.0\n",
903 | "0.0\n",
904 | "0.0\n",
905 | "1.0\n",
906 | "1.0\n",
907 | "1.0\n",
908 | "0.0\n",
909 | "1.0\n",
910 | "1.0\n",
911 | "0.0\n",
912 | "2.0\n",
913 | "2.0\n",
914 | "0.0\n",
915 | "0.0\n",
916 | "1.0\n",
917 | "0.0\n",
918 | "0.0\n",
919 | "1.0\n",
920 | "1.0\n",
921 | "2.0\n",
922 | "0.0\n",
923 | "0.0\n",
924 | "2.0\n",
925 | "1.0\n",
926 | "1.0\n",
927 | "1.0\n",
928 | "2.0\n",
929 | "1.0\n",
930 | "1.0\n",
931 | "2.0\n",
932 | "2.0\n",
933 | "1.0\n",
934 | "1.0\n",
935 | "2.0\n",
936 | "1.0\n",
937 | "2.0\n",
938 | "0.0\n",
939 | "2.0\n",
940 | "0.0\n",
941 | "1.0\n",
942 | "2.0\n",
943 | "1.0\n",
944 | "1.0\n",
945 | "1.0\n",
946 | "0.0\n",
947 | "0.0\n",
948 | "0.0\n",
949 | "0.0\n",
950 | "2.0\n",
951 | "2.0\n",
952 | "1.0\n",
953 | "1.0\n",
954 | "1.0\n",
955 | "0.0\n",
956 | "2.0\n",
957 | "\n",
958 | "\n"
959 | ]
960 | }
961 | ],
962 | "source": [
963 | "from urllib.parse import urlparse\n",
964 | "\n",
965 | "parsed_url = urlparse(transformer.output_path)\n",
966 | "bucket_name = parsed_url.netloc\n",
967 | "file_key = '{}/{}.out'.format(parsed_url.path[1:], \"test_data.csv\") # size of data is equal to 100\n",
968 | "\n",
969 | "s3_client = sess.boto_session.client('s3')\n",
970 | "\n",
971 | "response = s3_client.get_object(Bucket = sess.default_bucket(), Key = file_key)\n",
972 | "response_bytes = response['Body'].read().decode('utf-8')\n",
973 | "print(response_bytes)\n",
974 | "#size_data = 100\n",
975 | "#np_res = output_to_np(prediction=response_bytes,numberOfSamples=size_data,nClasses=3)\n",
976 | "#pd_res = output_to_pd(prediction=np_res, nClasses=3)\n"
977 | ]
978 | },
979 | {
980 | "cell_type": "code",
981 | "execution_count": 47,
982 | "metadata": {},
983 | "outputs": [
984 | {
985 | "data": {
986 | "text/html": [
987 | "\n",
988 | "\n",
1001 | "
\n",
1002 | " \n",
1003 | " \n",
1004 | " | \n",
1005 | " lables | \n",
1006 | " probability of class 0 | \n",
1007 | " probability of class 1 | \n",
1008 | " probability of class 2 | \n",
1009 | "
\n",
1010 | " \n",
1011 | " \n",
1012 | " \n",
1013 | " | 0 | \n",
1014 | " 2.0 | \n",
1015 | " 2.848256e-10 | \n",
1016 | " 0.008035 | \n",
1017 | " 0.991965 | \n",
1018 | "
\n",
1019 | " \n",
1020 | " | 1 | \n",
1021 | " 1.0 | \n",
1022 | " 7.074607e-06 | \n",
1023 | " 0.810579 | \n",
1024 | " 0.189414 | \n",
1025 | "
\n",
1026 | " \n",
1027 | " | 2 | \n",
1028 | " 2.0 | \n",
1029 | " 2.953847e-09 | \n",
1030 | " 0.005116 | \n",
1031 | " 0.994884 | \n",
1032 | "
\n",
1033 | " \n",
1034 | " | 3 | \n",
1035 | " 2.0 | \n",
1036 | " 8.515568e-09 | \n",
1037 | " 0.014403 | \n",
1038 | " 0.985597 | \n",
1039 | "
\n",
1040 | " \n",
1041 | " | 4 | \n",
1042 | " 1.0 | \n",
1043 | " 1.201045e-02 | \n",
1044 | " 0.987550 | \n",
1045 | " 0.000439 | \n",
1046 | "
\n",
1047 | " \n",
1048 | "
\n",
1049 | "
"
1050 | ],
1051 | "text/plain": [
1052 | " lables probability of class 0 probability of class 1 \\\n",
1053 | "0 2.0 2.848256e-10 0.008035 \n",
1054 | "1 1.0 7.074607e-06 0.810579 \n",
1055 | "2 2.0 2.953847e-09 0.005116 \n",
1056 | "3 2.0 8.515568e-09 0.014403 \n",
1057 | "4 1.0 1.201045e-02 0.987550 \n",
1058 | "\n",
1059 | " probability of class 2 \n",
1060 | "0 0.991965 \n",
1061 | "1 0.189414 \n",
1062 | "2 0.994884 \n",
1063 | "3 0.985597 \n",
1064 | "4 0.000439 "
1065 | ]
1066 | },
1067 | "execution_count": 47,
1068 | "metadata": {},
1069 | "output_type": "execute_result"
1070 | }
1071 | ],
1072 | "source": [
1073 | "pd_res.head()"
1074 | ]
1075 | }
1076 | ],
1077 | "metadata": {
1078 | "kernelspec": {
1079 | "display_name": "Python 3",
1080 | "language": "python",
1081 | "name": "python3"
1082 | },
1083 | "language_info": {
1084 | "codemirror_mode": {
1085 | "name": "ipython",
1086 | "version": 3
1087 | },
1088 | "file_extension": ".py",
1089 | "mimetype": "text/x-python",
1090 | "name": "python",
1091 | "nbconvert_exporter": "python",
1092 | "pygments_lexer": "ipython3",
1093 | "version": "3.6.5"
1094 | }
1095 | },
1096 | "nbformat": 4,
1097 | "nbformat_minor": 2
1098 | }
1099 |
--------------------------------------------------------------------------------