|
36 | 36 | "metadata": {},
|
37 | 37 | "outputs": [],
|
38 | 38 | "source": [
|
39 |
| - "bucket = 'NAME_YOUR_BUCKET'\n", |
40 |
| - "prefix = 'sagemaker/DEMO-linear-mnist'\n", |
41 |
| - " \n", |
| 39 | + "bucket = \"NAME_YOUR_BUCKET\"\n", |
| 40 | + "prefix = \"sagemaker/DEMO-linear-mnist\"\n", |
| 41 | + "\n", |
42 | 42 | "# Define IAM role\n",
|
43 | 43 | "import boto3\n",
|
44 | 44 | "import re\n",
|
|
64 | 64 | "import pickle, gzip, numpy, urllib.request, json\n",
|
65 | 65 | "\n",
|
66 | 66 | "# Load the dataset\n",
|
67 |
| - "urllib.request.urlretrieve(\"http://deeplearning.net/data/mnist/mnist.pkl.gz\", \"mnist.pkl.gz\")\n", |
68 |
| - "with gzip.open('mnist.pkl.gz', 'rb') as f:\n", |
69 |
| - " train_set, valid_set, test_set = pickle.load(f, encoding='latin1')" |
| 67 | + "urllib.request.urlretrieve(\n", |
| 68 | + " \"http://deeplearning.net/data/mnist/mnist.pkl.gz\", \"mnist.pkl.gz\"\n", |
| 69 | + ")\n", |
| 70 | + "with gzip.open(\"mnist.pkl.gz\", \"rb\") as f:\n", |
| 71 | + " train_set, valid_set, test_set = pickle.load(f, encoding=\"latin1\")" |
70 | 72 | ]
|
71 | 73 | },
|
72 | 74 | {
|
|
84 | 86 | "source": [
|
85 | 87 | "%matplotlib inline\n",
|
86 | 88 | "import matplotlib.pyplot as plt\n",
|
87 |
| - "plt.rcParams[\"figure.figsize\"] = (2,10)\n", |
| 89 | + "\n", |
| 90 | + "plt.rcParams[\"figure.figsize\"] = (2, 10)\n", |
88 | 91 | "\n",
|
89 | 92 | "\n",
|
90 |
| - "def show_digit(img, caption='', subplot=None):\n", |
91 |
| - " if subplot==None:\n", |
92 |
| - " _,(subplot)=plt.subplots(1,1)\n", |
93 |
| - " imgr=img.reshape((28,28))\n", |
94 |
| - " subplot.axis('off')\n", |
95 |
| - " subplot.imshow(imgr, cmap='gray')\n", |
| 93 | + "def show_digit(img, caption=\"\", subplot=None):\n", |
| 94 | + " if subplot == None:\n", |
| 95 | + " _, (subplot) = plt.subplots(1, 1)\n", |
| 96 | + " imgr = img.reshape((28, 28))\n", |
| 97 | + " subplot.axis(\"off\")\n", |
| 98 | + " subplot.imshow(imgr, cmap=\"gray\")\n", |
96 | 99 | " plt.title(caption)\n",
|
97 | 100 | "\n",
|
98 |
| - "show_digit(train_set[0][30], 'This is a {}'.format(train_set[1][30]))" |
| 101 | + "\n", |
| 102 | + "show_digit(train_set[0][30], \"This is a {}\".format(train_set[1][30]))" |
99 | 103 | ]
|
100 | 104 | },
|
101 | 105 | {
|
|
115 | 119 | "import numpy as np\n",
|
116 | 120 | "import sagemaker.amazon.common as smac\n",
|
117 | 121 | "\n",
|
118 |
| - "vectors = np.array([t.tolist() for t in train_set[0]]).astype('float32')\n", |
119 |
| - "labels = np.where(np.array([t.tolist() for t in train_set[1]]) == 0, 1, 0).astype('float32')\n", |
| 122 | + "vectors = np.array([t.tolist() for t in train_set[0]]).astype(\"float32\")\n", |
| 123 | + "labels = np.where(np.array([t.tolist() for t in train_set[1]]) == 0, 1, 0).astype(\n", |
| 124 | + " \"float32\"\n", |
| 125 | + ")\n", |
120 | 126 | "\n",
|
121 | 127 | "buf = io.BytesIO()\n",
|
122 | 128 | "smac.write_numpy_to_dense_tensor(buf, vectors, labels)\n",
|
|
139 | 145 | "import boto3\n",
|
140 | 146 | "import os\n",
|
141 | 147 | "\n",
|
142 |
| - "key = 'recordio-pb-data'\n", |
143 |
| - "boto3.resource('s3').Bucket(bucket).Object(os.path.join(prefix, 'train', key)).upload_fileobj(buf)\n", |
144 |
| - "s3_train_data = 's3://{}/{}/train/{}'.format(bucket, prefix, key)\n", |
145 |
| - "print('uploaded training data location: {}'.format(s3_train_data))" |
| 148 | + "key = \"recordio-pb-data\"\n", |
| 149 | + "boto3.resource(\"s3\").Bucket(bucket).Object(\n", |
| 150 | + " os.path.join(prefix, \"train\", key)\n", |
| 151 | + ").upload_fileobj(buf)\n", |
| 152 | + "s3_train_data = \"s3://{}/{}/train/{}\".format(bucket, prefix, key)\n", |
| 153 | + "print(\"uploaded training data location: {}\".format(s3_train_data))" |
146 | 154 | ]
|
147 | 155 | },
|
148 | 156 | {
|
|
158 | 166 | "metadata": {},
|
159 | 167 | "outputs": [],
|
160 | 168 | "source": [
|
161 |
| - "output_location = 's3://{}/{}/output'.format(bucket, prefix)\n", |
162 |
| - "print('training artifacts will be uploaded to: {}'.format(output_location))" |
| 169 | + "output_location = \"s3://{}/{}/output\".format(bucket, prefix)\n", |
| 170 | + "print(\"training artifacts will be uploaded to: {}\".format(output_location))" |
163 | 171 | ]
|
164 | 172 | },
|
165 | 173 | {
|
|
176 | 184 | "outputs": [],
|
177 | 185 | "source": [
|
178 | 186 | "from sagemaker.amazon.amazon_estimator import get_image_uri\n",
|
179 |
| - "container = get_image_uri(boto3.Session().region_name, 'linear-learner')" |
| 187 | + "\n", |
| 188 | + "container = get_image_uri(boto3.Session().region_name, \"linear-learner\")" |
180 | 189 | ]
|
181 | 190 | },
|
182 | 191 | {
|
|
190 | 199 | "\n",
|
191 | 200 | "sess = sagemaker.Session()\n",
|
192 | 201 | "\n",
|
193 |
| - "linear = sagemaker.estimator.Estimator(container,\n", |
194 |
| - " role, \n", |
195 |
| - " train_instance_count=1, \n", |
196 |
| - " train_instance_type='ml.c4.xlarge',\n", |
197 |
| - " output_path=output_location,\n", |
198 |
| - " sagemaker_session=sess)\n", |
199 |
| - "linear.set_hyperparameters(feature_dim=784,\n", |
200 |
| - " predictor_type='binary_classifier',\n", |
201 |
| - " mini_batch_size=200)\n", |
| 202 | + "linear = sagemaker.estimator.Estimator(\n", |
| 203 | + " container,\n", |
| 204 | + " role,\n", |
| 205 | + " train_instance_count=1,\n", |
| 206 | + " train_instance_type=\"ml.c4.xlarge\",\n", |
| 207 | + " output_path=output_location,\n", |
| 208 | + " sagemaker_session=sess,\n", |
| 209 | + ")\n", |
| 210 | + "linear.set_hyperparameters(\n", |
| 211 | + " feature_dim=784, predictor_type=\"binary_classifier\", mini_batch_size=200\n", |
| 212 | + ")\n", |
202 | 213 | "\n",
|
203 |
| - "linear.fit({'train': s3_train_data})" |
| 214 | + "linear.fit({\"train\": s3_train_data})" |
204 | 215 | ]
|
205 | 216 | },
|
206 | 217 | {
|
|
214 | 225 | "cell_type": "markdown",
|
215 | 226 | "metadata": {},
|
216 | 227 | "source": [
|
217 |
| - "Define your Comet [REST API](https://www.comet.ml/docs/rest-api/getting-started/) and your [workspace](https://www.comet.ml/docs/user-interface/#workspaces). See the [configuration documentation](http://docs.comet.ml/python-sdk/advanced/#python-configuration) for info on both specifications." |
| 228 | + "Define your Comet [REST API](https://www.comet.com/docs/rest-api/getting-started/) and your [workspace](https://www.comet.com/docs/user-interface/#workspaces). See the [configuration documentation](http://docs.comet.ml/python-sdk/advanced/#python-configuration) for info on both specifications." |
218 | 229 | ]
|
219 | 230 | },
|
220 | 231 | {
|
|
264 | 275 | "source": [
|
265 | 276 | "# .log_sagemaker_job(regressor/estimator object from Sagemaker SDK, Comet Rest API key (optional, can be taken from usual config source), workspace (comet), project (comet))\n",
|
266 | 277 | "# I have used the Sagemaker SDK to train a model. I have the estimator/regressor object. I want to log whatever I just trained\n",
|
267 |
| - "experiment = comet_ml_sagemaker.log_sagemaker_job(linear, api_key=COMET_REST_API, workspace=COMET_WORKSPACE, project_name=\"sagemaker\")\n", |
| 278 | + "experiment = comet_ml_sagemaker.log_sagemaker_job(\n", |
| 279 | + " linear, api_key=COMET_REST_API, workspace=COMET_WORKSPACE, project_name=\"sagemaker\"\n", |
| 280 | + ")\n", |
268 | 281 | "print(experiment.url)"
|
269 | 282 | ]
|
270 | 283 | },
|
|
290 | 303 | "# I have the name of a completed training job I want to lob\n",
|
291 | 304 | "# Same as .log_sagemaker_job, except instead of passing the regressor/estimator object, you pass the job name\n",
|
292 | 305 | "SAGEMAKER_TRAINING_JOB_NAME = \"SAGEMAKER_TRAINING_JOB_NAME\"\n",
|
293 |
| - "experiment = comet_ml_sagemaker.log_sagemaker_job_by_name(SAGEMAKER_TRAINING_JOB_NAME, api_key=COMET_REST_API, workspace=COMET_WORKSPACE, project_name=\"sagemaker\")\n", |
| 306 | + "experiment = comet_ml_sagemaker.log_sagemaker_job_by_name(\n", |
| 307 | + " SAGEMAKER_TRAINING_JOB_NAME,\n", |
| 308 | + " api_key=COMET_REST_API,\n", |
| 309 | + " workspace=COMET_WORKSPACE,\n", |
| 310 | + " project_name=\"sagemaker\",\n", |
| 311 | + ")\n", |
294 | 312 | "print(experiment.url)"
|
295 | 313 | ]
|
296 | 314 | },
|
|
313 | 331 | "outputs": [],
|
314 | 332 | "source": [
|
315 | 333 | "# Logs the last job for your current Amazon Region / S3\n",
|
316 |
| - "experiment = comet_ml_sagemaker.log_last_sagemaker_job(api_key=COMET_REST_API, workspace=COMET_WORKSPACE, project_name=\"sagemaker\")\n", |
| 334 | + "experiment = comet_ml_sagemaker.log_last_sagemaker_job(\n", |
| 335 | + " api_key=COMET_REST_API, workspace=COMET_WORKSPACE, project_name=\"sagemaker\"\n", |
| 336 | + ")\n", |
317 | 337 | "print(experiment.url)"
|
318 | 338 | ]
|
319 | 339 | },
|
|
0 commit comments