mirror of
https://github.com/Azure/MachineLearningNotebooks.git
synced 2025-12-22 02:25:12 -05:00
quickstart added
This commit is contained in:
12
tutorials/get-started-day1/IDE-users/01-create-workspace.py
Normal file
12
tutorials/get-started-day1/IDE-users/01-create-workspace.py
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
# 01-create-workspace.py
|
||||||
|
from azureml.core import Workspace
|
||||||
|
|
||||||
|
# Example locations: 'westeurope' or 'eastus2' or 'westus2' or 'southeastasia'.
|
||||||
|
ws = Workspace.create(name='<my_workspace_name>',
|
||||||
|
subscription_id='<azure-subscription-id>',
|
||||||
|
resource_group='<myresourcegroup>',
|
||||||
|
create_resource_group=True,
|
||||||
|
location='<NAME_OF_REGION>')
|
||||||
|
|
||||||
|
# write out the workspace details to a configuration file: .azureml/config.json
|
||||||
|
ws.write_config(path='.azureml')
|
||||||
23
tutorials/get-started-day1/IDE-users/02-create-compute.py
Normal file
23
tutorials/get-started-day1/IDE-users/02-create-compute.py
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
# 02-create-compute.py
|
||||||
|
from azureml.core import Workspace
|
||||||
|
from azureml.core.compute import ComputeTarget, AmlCompute
|
||||||
|
from azureml.core.compute_target import ComputeTargetException
|
||||||
|
|
||||||
|
ws = Workspace.from_config()
|
||||||
|
|
||||||
|
# Choose a name for your CPU cluster
|
||||||
|
cpu_cluster_name = "cpu-cluster"
|
||||||
|
|
||||||
|
# Verify that cluster does not exist already
|
||||||
|
try:
|
||||||
|
cpu_cluster = ComputeTarget(workspace=ws, name=cpu_cluster_name)
|
||||||
|
print('Found existing cluster, use it.')
|
||||||
|
except ComputeTargetException:
|
||||||
|
cfg = AmlCompute.provisioning_configuration(
|
||||||
|
vm_size='STANDARD_D2_V2',
|
||||||
|
max_nodes=4,
|
||||||
|
idle_seconds_before_scaledown=2400
|
||||||
|
)
|
||||||
|
cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, cfg)
|
||||||
|
|
||||||
|
cpu_cluster.wait_for_completion(show_output=True)
|
||||||
13
tutorials/get-started-day1/IDE-users/03-run-hello.py
Normal file
13
tutorials/get-started-day1/IDE-users/03-run-hello.py
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
# 03-run-hello.py
|
||||||
|
from azureml.core import Workspace, Experiment, ScriptRunConfig
|
||||||
|
|
||||||
|
ws = Workspace.from_config()
|
||||||
|
experiment = Experiment(workspace=ws, name='day1-experiment-hello')
|
||||||
|
|
||||||
|
config = ScriptRunConfig(source_directory='./src',
|
||||||
|
script='hello.py',
|
||||||
|
compute_target='cpu-cluster')
|
||||||
|
|
||||||
|
run = experiment.submit(config)
|
||||||
|
aml_url = run.get_portal_url()
|
||||||
|
print(aml_url)
|
||||||
24
tutorials/get-started-day1/IDE-users/04-run-pytorch.py
Normal file
24
tutorials/get-started-day1/IDE-users/04-run-pytorch.py
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
# 04-run-pytorch.py
|
||||||
|
from azureml.core import Workspace
|
||||||
|
from azureml.core import Experiment
|
||||||
|
from azureml.core import Environment
|
||||||
|
from azureml.core import ScriptRunConfig
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ws = Workspace.from_config()
|
||||||
|
experiment = Experiment(workspace=ws, name='day1-experiment-train')
|
||||||
|
config = ScriptRunConfig(source_directory='./src',
|
||||||
|
script='train.py',
|
||||||
|
compute_target='cpu-cluster')
|
||||||
|
|
||||||
|
# set up pytorch environment
|
||||||
|
env = Environment.from_conda_specification(
|
||||||
|
name='pytorch-env',
|
||||||
|
file_path='./environments/pytorch-env.yml'
|
||||||
|
)
|
||||||
|
config.run_config.environment = env
|
||||||
|
|
||||||
|
run = experiment.submit(config)
|
||||||
|
|
||||||
|
aml_url = run.get_portal_url()
|
||||||
|
print(aml_url)
|
||||||
7
tutorials/get-started-day1/IDE-users/05-upload-data.py
Normal file
7
tutorials/get-started-day1/IDE-users/05-upload-data.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
# 05-upload-data.py
|
||||||
|
from azureml.core import Workspace
|
||||||
|
ws = Workspace.from_config()
|
||||||
|
datastore = ws.get_default_datastore()
|
||||||
|
datastore.upload(src_dir='./data',
|
||||||
|
target_path='datasets/cifar10',
|
||||||
|
overwrite=True)
|
||||||
35
tutorials/get-started-day1/IDE-users/06-run-pytorch-data.py
Normal file
35
tutorials/get-started-day1/IDE-users/06-run-pytorch-data.py
Normal file
@@ -0,0 +1,35 @@
|
|||||||
|
# 06-run-pytorch-data.py
|
||||||
|
from azureml.core import Workspace
|
||||||
|
from azureml.core import Experiment
|
||||||
|
from azureml.core import Environment
|
||||||
|
from azureml.core import ScriptRunConfig
|
||||||
|
from azureml.core import Dataset
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
ws = Workspace.from_config()
|
||||||
|
datastore = ws.get_default_datastore()
|
||||||
|
dataset = Dataset.File.from_files(path=(datastore, 'datasets/cifar10'))
|
||||||
|
|
||||||
|
experiment = Experiment(workspace=ws, name='day1-experiment-data')
|
||||||
|
|
||||||
|
config = ScriptRunConfig(
|
||||||
|
source_directory='./src',
|
||||||
|
script='train.py',
|
||||||
|
compute_target='cpu-cluster',
|
||||||
|
arguments=[
|
||||||
|
'--data_path', dataset.as_named_input('input').as_mount(),
|
||||||
|
'--learning_rate', 0.003,
|
||||||
|
'--momentum', 0.92],
|
||||||
|
)
|
||||||
|
# set up pytorch environment
|
||||||
|
env = Environment.from_conda_specification(
|
||||||
|
name='pytorch-env',
|
||||||
|
file_path='./environments/pytorch-env.yml'
|
||||||
|
)
|
||||||
|
config.run_config.environment = env
|
||||||
|
|
||||||
|
run = experiment.submit(config)
|
||||||
|
aml_url = run.get_portal_url()
|
||||||
|
print("Submitted to compute cluster. Click link below")
|
||||||
|
print("")
|
||||||
|
print(aml_url)
|
||||||
25
tutorials/get-started-day1/IDE-users/README.md
Normal file
25
tutorials/get-started-day1/IDE-users/README.md
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
# Get Started (day 1) with Azure Machine Learning: IDE Users
|
||||||
|
|
||||||
|
This folder has been setup for IDE user (for example, VS Code or Pycharm) following the [Get started (day 1) with Azure Machine Learning tutorial series](https://aka.ms/day1aml).
|
||||||
|
|
||||||
|
The directory is structured as follows:
|
||||||
|
|
||||||
|
```Text
|
||||||
|
IDE-users
|
||||||
|
└──environments
|
||||||
|
| └──pytorch-env.yml
|
||||||
|
└──src
|
||||||
|
| └──hello.py
|
||||||
|
| └──model.py
|
||||||
|
| └──train.py
|
||||||
|
└──01-create-workspace.py
|
||||||
|
└──02-create-compute.py
|
||||||
|
└──03-run-hello.py
|
||||||
|
└──04-run-pytorch.py
|
||||||
|
└──05-upload-data.py
|
||||||
|
└──06-run-pytorch-data.py
|
||||||
|
```
|
||||||
|
|
||||||
|
Please refer to [the documentation](https://aka.ms/day1aml) for more details on these files.
|
||||||
|
|
||||||
|

|
||||||
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
name: pytorch-env
|
||||||
|
channels:
|
||||||
|
- defaults
|
||||||
|
- pytorch
|
||||||
|
dependencies:
|
||||||
|
- python=3.6.2
|
||||||
|
- pytorch
|
||||||
|
- torchvision
|
||||||
2
tutorials/get-started-day1/IDE-users/src/hello.py
Normal file
2
tutorials/get-started-day1/IDE-users/src/hello.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
print("hello world!")
|
||||||
22
tutorials/get-started-day1/IDE-users/src/model.py
Normal file
22
tutorials/get-started-day1/IDE-users/src/model.py
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(3, 6, 5)
|
||||||
|
self.pool = nn.MaxPool2d(2, 2)
|
||||||
|
self.conv2 = nn.Conv2d(6, 16, 5)
|
||||||
|
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
||||||
|
self.fc2 = nn.Linear(120, 84)
|
||||||
|
self.fc3 = nn.Linear(84, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.pool(F.relu(self.conv1(x)))
|
||||||
|
x = self.pool(F.relu(self.conv2(x)))
|
||||||
|
x = x.view(-1, 16 * 5 * 5)
|
||||||
|
x = F.relu(self.fc1(x))
|
||||||
|
x = F.relu(self.fc2(x))
|
||||||
|
x = self.fc3(x)
|
||||||
|
return x
|
||||||
52
tutorials/get-started-day1/IDE-users/src/train.py
Normal file
52
tutorials/get-started-day1/IDE-users/src/train.py
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
import torch
|
||||||
|
import torch.optim as optim
|
||||||
|
import torchvision
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
|
||||||
|
from model import Net
|
||||||
|
|
||||||
|
# download CIFAR 10 data
|
||||||
|
trainset = torchvision.datasets.CIFAR10(
|
||||||
|
root="./data",
|
||||||
|
train=True,
|
||||||
|
download=True,
|
||||||
|
transform=torchvision.transforms.ToTensor(),
|
||||||
|
)
|
||||||
|
trainloader = torch.utils.data.DataLoader(
|
||||||
|
trainset, batch_size=4, shuffle=True, num_workers=2
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
# define convolutional network
|
||||||
|
net = Net()
|
||||||
|
|
||||||
|
# set up pytorch loss / optimizer
|
||||||
|
criterion = torch.nn.CrossEntropyLoss()
|
||||||
|
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
|
||||||
|
|
||||||
|
# train the network
|
||||||
|
for epoch in range(2):
|
||||||
|
|
||||||
|
running_loss = 0.0
|
||||||
|
for i, data in enumerate(trainloader, 0):
|
||||||
|
# unpack the data
|
||||||
|
inputs, labels = data
|
||||||
|
|
||||||
|
# zero the parameter gradients
|
||||||
|
optimizer.zero_grad()
|
||||||
|
|
||||||
|
# forward + backward + optimize
|
||||||
|
outputs = net(inputs)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
# print statistics
|
||||||
|
running_loss += loss.item()
|
||||||
|
if i % 2000 == 1999:
|
||||||
|
loss = running_loss / 2000
|
||||||
|
print(f"epoch={epoch + 1}, batch={i + 1:5}: loss {loss:.2f}")
|
||||||
|
running_loss = 0.0
|
||||||
|
|
||||||
|
print("Finished Training")
|
||||||
2
tutorials/get-started-day1/code/hello/hello.py
Normal file
2
tutorials/get-started-day1/code/hello/hello.py
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
|
||||||
|
print("hello world!")
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(3, 6, 5)
|
||||||
|
self.pool = nn.MaxPool2d(2, 2)
|
||||||
|
self.conv2 = nn.Conv2d(6, 16, 5)
|
||||||
|
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
||||||
|
self.fc2 = nn.Linear(120, 84)
|
||||||
|
self.fc3 = nn.Linear(84, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.pool(F.relu(self.conv1(x)))
|
||||||
|
x = self.pool(F.relu(self.conv2(x)))
|
||||||
|
x = x.view(-1, 16 * 5 * 5)
|
||||||
|
x = F.relu(self.fc1(x))
|
||||||
|
x = F.relu(self.fc2(x))
|
||||||
|
x = self.fc3(x)
|
||||||
|
return x
|
||||||
@@ -0,0 +1,62 @@
|
|||||||
|
import torch
|
||||||
|
import torch.optim as optim
|
||||||
|
import torchvision
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
|
||||||
|
from model import Net
|
||||||
|
from azureml.core import Run
|
||||||
|
|
||||||
|
|
||||||
|
# ADDITIONAL CODE: get AML run from the current context
|
||||||
|
run = Run.get_context()
|
||||||
|
|
||||||
|
# download CIFAR 10 data
|
||||||
|
trainset = torchvision.datasets.CIFAR10(
|
||||||
|
root='./data',
|
||||||
|
train=True,
|
||||||
|
download=True,
|
||||||
|
transform=torchvision.transforms.ToTensor()
|
||||||
|
)
|
||||||
|
trainloader = torch.utils.data.DataLoader(
|
||||||
|
trainset,
|
||||||
|
batch_size=4,
|
||||||
|
shuffle=True,
|
||||||
|
num_workers=2
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
# define convolutional network
|
||||||
|
net = Net()
|
||||||
|
|
||||||
|
# set up pytorch loss / optimizer
|
||||||
|
criterion = torch.nn.CrossEntropyLoss()
|
||||||
|
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
|
||||||
|
|
||||||
|
# train the network
|
||||||
|
for epoch in range(2):
|
||||||
|
|
||||||
|
running_loss = 0.0
|
||||||
|
for i, data in enumerate(trainloader, 0):
|
||||||
|
# unpack the data
|
||||||
|
inputs, labels = data
|
||||||
|
|
||||||
|
# zero the parameter gradients
|
||||||
|
optimizer.zero_grad()
|
||||||
|
|
||||||
|
# forward + backward + optimize
|
||||||
|
outputs = net(inputs)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
# print statistics
|
||||||
|
running_loss += loss.item()
|
||||||
|
if i % 2000 == 1999:
|
||||||
|
loss = running_loss / 2000
|
||||||
|
# ADDITIONAL CODE: log loss metric to AML
|
||||||
|
run.log('loss', loss)
|
||||||
|
print(f'epoch={epoch + 1}, batch={i + 1:5}: loss {loss:.2f}')
|
||||||
|
running_loss = 0.0
|
||||||
|
|
||||||
|
print('Finished Training')
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(3, 6, 5)
|
||||||
|
self.pool = nn.MaxPool2d(2, 2)
|
||||||
|
self.conv2 = nn.Conv2d(6, 16, 5)
|
||||||
|
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
||||||
|
self.fc2 = nn.Linear(120, 84)
|
||||||
|
self.fc3 = nn.Linear(84, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.pool(F.relu(self.conv1(x)))
|
||||||
|
x = self.pool(F.relu(self.conv2(x)))
|
||||||
|
x = x.view(-1, 16 * 5 * 5)
|
||||||
|
x = F.relu(self.fc1(x))
|
||||||
|
x = F.relu(self.fc2(x))
|
||||||
|
x = self.fc3(x)
|
||||||
|
return x
|
||||||
@@ -0,0 +1,52 @@
|
|||||||
|
import torch
|
||||||
|
import torch.optim as optim
|
||||||
|
import torchvision
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
|
||||||
|
from model import Net
|
||||||
|
|
||||||
|
# download CIFAR 10 data
|
||||||
|
trainset = torchvision.datasets.CIFAR10(
|
||||||
|
root="./data",
|
||||||
|
train=True,
|
||||||
|
download=True,
|
||||||
|
transform=torchvision.transforms.ToTensor(),
|
||||||
|
)
|
||||||
|
trainloader = torch.utils.data.DataLoader(
|
||||||
|
trainset, batch_size=4, shuffle=True, num_workers=2
|
||||||
|
)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
|
||||||
|
# define convolutional network
|
||||||
|
net = Net()
|
||||||
|
|
||||||
|
# set up pytorch loss / optimizer
|
||||||
|
criterion = torch.nn.CrossEntropyLoss()
|
||||||
|
optimizer = optim.SGD(net.parameters(), lr=0.001, momentum=0.9)
|
||||||
|
|
||||||
|
# train the network
|
||||||
|
for epoch in range(2):
|
||||||
|
|
||||||
|
running_loss = 0.0
|
||||||
|
for i, data in enumerate(trainloader, 0):
|
||||||
|
# unpack the data
|
||||||
|
inputs, labels = data
|
||||||
|
|
||||||
|
# zero the parameter gradients
|
||||||
|
optimizer.zero_grad()
|
||||||
|
|
||||||
|
# forward + backward + optimize
|
||||||
|
outputs = net(inputs)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
# print statistics
|
||||||
|
running_loss += loss.item()
|
||||||
|
if i % 2000 == 1999:
|
||||||
|
loss = running_loss / 2000
|
||||||
|
print(f"epoch={epoch + 1}, batch={i + 1:5}: loss {loss:.2f}")
|
||||||
|
running_loss = 0.0
|
||||||
|
|
||||||
|
print("Finished Training")
|
||||||
@@ -0,0 +1,22 @@
|
|||||||
|
import torch.nn as nn
|
||||||
|
import torch.nn.functional as F
|
||||||
|
|
||||||
|
|
||||||
|
class Net(nn.Module):
|
||||||
|
def __init__(self):
|
||||||
|
super(Net, self).__init__()
|
||||||
|
self.conv1 = nn.Conv2d(3, 6, 5)
|
||||||
|
self.pool = nn.MaxPool2d(2, 2)
|
||||||
|
self.conv2 = nn.Conv2d(6, 16, 5)
|
||||||
|
self.fc1 = nn.Linear(16 * 5 * 5, 120)
|
||||||
|
self.fc2 = nn.Linear(120, 84)
|
||||||
|
self.fc3 = nn.Linear(84, 10)
|
||||||
|
|
||||||
|
def forward(self, x):
|
||||||
|
x = self.pool(F.relu(self.conv1(x)))
|
||||||
|
x = self.pool(F.relu(self.conv2(x)))
|
||||||
|
x = x.view(-1, 16 * 5 * 5)
|
||||||
|
x = F.relu(self.fc1(x))
|
||||||
|
x = F.relu(self.fc2(x))
|
||||||
|
x = self.fc3(x)
|
||||||
|
return x
|
||||||
@@ -0,0 +1,96 @@
|
|||||||
|
|
||||||
|
import os
|
||||||
|
import argparse
|
||||||
|
import torch
|
||||||
|
import torch.optim as optim
|
||||||
|
import torchvision
|
||||||
|
import torchvision.transforms as transforms
|
||||||
|
|
||||||
|
from model import Net
|
||||||
|
from azureml.core import Run
|
||||||
|
|
||||||
|
run = Run.get_context()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
parser = argparse.ArgumentParser()
|
||||||
|
parser.add_argument(
|
||||||
|
'--data_path',
|
||||||
|
type=str,
|
||||||
|
help='Path to the training data'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--learning_rate',
|
||||||
|
type=float,
|
||||||
|
default=0.001,
|
||||||
|
help='Learning rate for SGD'
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
'--momentum',
|
||||||
|
type=float,
|
||||||
|
default=0.9,
|
||||||
|
help='Momentum for SGD'
|
||||||
|
)
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
print("===== DATA =====")
|
||||||
|
print("DATA PATH: " + args.data_path)
|
||||||
|
print("LIST FILES IN DATA PATH...")
|
||||||
|
print(os.listdir(args.data_path))
|
||||||
|
print("================")
|
||||||
|
|
||||||
|
# prepare DataLoader for CIFAR10 data
|
||||||
|
transform = transforms.Compose([
|
||||||
|
transforms.ToTensor(),
|
||||||
|
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
|
||||||
|
])
|
||||||
|
trainset = torchvision.datasets.CIFAR10(
|
||||||
|
root=args.data_path,
|
||||||
|
train=True,
|
||||||
|
download=False,
|
||||||
|
transform=transform,
|
||||||
|
)
|
||||||
|
trainloader = torch.utils.data.DataLoader(
|
||||||
|
trainset,
|
||||||
|
batch_size=4,
|
||||||
|
shuffle=True,
|
||||||
|
num_workers=2
|
||||||
|
)
|
||||||
|
|
||||||
|
# define convolutional network
|
||||||
|
net = Net()
|
||||||
|
|
||||||
|
# set up pytorch loss / optimizer
|
||||||
|
criterion = torch.nn.CrossEntropyLoss()
|
||||||
|
optimizer = optim.SGD(
|
||||||
|
net.parameters(),
|
||||||
|
lr=args.learning_rate,
|
||||||
|
momentum=args.momentum,
|
||||||
|
)
|
||||||
|
|
||||||
|
# train the network
|
||||||
|
for epoch in range(2):
|
||||||
|
|
||||||
|
running_loss = 0.0
|
||||||
|
for i, data in enumerate(trainloader, 0):
|
||||||
|
# unpack the data
|
||||||
|
inputs, labels = data
|
||||||
|
|
||||||
|
# zero the parameter gradients
|
||||||
|
optimizer.zero_grad()
|
||||||
|
|
||||||
|
# forward + backward + optimize
|
||||||
|
outputs = net(inputs)
|
||||||
|
loss = criterion(outputs, labels)
|
||||||
|
loss.backward()
|
||||||
|
optimizer.step()
|
||||||
|
|
||||||
|
# print statistics
|
||||||
|
running_loss += loss.item()
|
||||||
|
if i % 2000 == 1999:
|
||||||
|
loss = running_loss / 2000
|
||||||
|
run.log('loss', loss) # log loss metric to AML
|
||||||
|
print(f'epoch={epoch + 1}, batch={i + 1:5}: loss {loss:.2f}')
|
||||||
|
running_loss = 0.0
|
||||||
|
|
||||||
|
print('Finished Training')
|
||||||
11
tutorials/get-started-day1/configuration/pytorch-aml-env.yml
Normal file
11
tutorials/get-started-day1/configuration/pytorch-aml-env.yml
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
name: pytorch-aml-env
|
||||||
|
channels:
|
||||||
|
- defaults
|
||||||
|
- pytorch
|
||||||
|
dependencies:
|
||||||
|
- python=3.6.2
|
||||||
|
- pytorch
|
||||||
|
- torchvision
|
||||||
|
- pip
|
||||||
|
- pip:
|
||||||
|
- azureml-sdk
|
||||||
9
tutorials/get-started-day1/configuration/pytorch-env.yml
Normal file
9
tutorials/get-started-day1/configuration/pytorch-env.yml
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
|
||||||
|
name: pytorch-env
|
||||||
|
channels:
|
||||||
|
- defaults
|
||||||
|
- pytorch
|
||||||
|
dependencies:
|
||||||
|
- python=3.6.2
|
||||||
|
- pytorch
|
||||||
|
- torchvision
|
||||||
166
tutorials/get-started-day1/day1-part1-setup.ipynb
Normal file
166
tutorials/get-started-day1/day1-part1-setup.ipynb
Normal file
@@ -0,0 +1,166 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tutorial: Get started (day 1) with Azure Machine Learning (Part 1 of 4)\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"## Introduction <a id='intro'></a>\n",
|
||||||
|
"\n",
|
||||||
|
"In this **four-part tutorial series**, you will learn the fundamentals of Azure Machine Learning and complete jobs-based Python machine learning tasks in the Azure cloud, including:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Set up a compute cluster\n",
|
||||||
|
"2. Run code in the cloud using Azure Machine Learning's Python SDK.\n",
|
||||||
|
"3. Manage the Python environment you use for model training.\n",
|
||||||
|
"4. Upload data to Azure and consume that data in training.\n",
|
||||||
|
"\n",
|
||||||
|
"In this first part of the tutorial series you learn how to create an Azure Machine Learning Compute Cluster that will be used in subsequent parts of the series to submit jobs to. This notebook follows the steps provided on the [Python (day 1) - set up local computer documentation page](https://aka.ms/day1aml).\n",
|
||||||
|
"\n",
|
||||||
|
"## Pre-requisites <a id='pre-reqs'></a>\n",
|
||||||
|
"\n",
|
||||||
|
"- An Azure Subscription. If you don't have an Azure subscription, create a free account before you begin. Try [Azure Machine Learning](https://aka.ms/AMLFree) today.\n",
|
||||||
|
"- Familiarity with Python and Machine Learning concepts. For example, environments, training, scoring, and so on.\n",
|
||||||
|
"- If you are using a compute instance in Azure Machine Learning to run this notebook series, you are all set. Otherwise, please follow the [Configure a development environment for Azure Machine Learning](https://docs.microsoft.com/azure/machine-learning/how-to-configure-environment)\n",
|
||||||
|
"\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Ensure you have the latest Azure Machine Learning Python SDK\n",
|
||||||
|
"\n",
|
||||||
|
"This tutorial series depends on having the Azure Machine Learning SDK version 1.14.0 onwards installed. You can check your version using the code cell below."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import VERSION\n",
|
||||||
|
"\n",
|
||||||
|
"print ('Version: ' + VERSION)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If your version is below 1.14.0, then upgrade the SDK using `pip` (**Note: You may need to restart your kernel for the changes to take effect. Re-run the cell above to ensure you have the right version**).\n",
|
||||||
|
"\n",
|
||||||
|
"```bash\n",
|
||||||
|
"!pip install -U azureml-sdk\n",
|
||||||
|
"```"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Create an Azure Machine Learning compute cluster <a id='createcc'></a>\n",
|
||||||
|
"\n",
|
||||||
|
"As this tutorial focuses on jobs-based machine learning tasks, you will be submitting python code to run on an Azure Machine Learning **Compute cluster**, which is well suited for large jobs and production. Therefore, you create an Azure Machine Learning compute cluster that will auto-scale between zero and four nodes:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"create mlc",
|
||||||
|
"batchai"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace\n",
|
||||||
|
"from azureml.core.compute import ComputeTarget, AmlCompute\n",
|
||||||
|
"from azureml.core.compute_target import ComputeTargetException\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config() # this automatically looks for a directory .azureml\n",
|
||||||
|
"\n",
|
||||||
|
"# Choose a name for your CPU cluster\n",
|
||||||
|
"cpu_cluster_name = \"cpu-cluster\"\n",
|
||||||
|
"\n",
|
||||||
|
"# Verify that cluster does not exist already\n",
|
||||||
|
"try:\n",
|
||||||
|
" cpu_cluster = ComputeTarget(workspace=ws, name=cpu_cluster_name)\n",
|
||||||
|
" print('Found existing cluster, use it.')\n",
|
||||||
|
"except ComputeTargetException:\n",
|
||||||
|
" compute_config = AmlCompute.provisioning_configuration(vm_size='STANDARD_D2_V2',\n",
|
||||||
|
" max_nodes=4, \n",
|
||||||
|
" idle_seconds_before_scaledown=2400)\n",
|
||||||
|
" cpu_cluster = ComputeTarget.create(ws, cpu_cluster_name, compute_config)\n",
|
||||||
|
"\n",
|
||||||
|
"cpu_cluster.wait_for_completion(show_output=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"> <span style=\"color:darkblue;font-weight:bold\"> ! INFORMATION \n",
|
||||||
|
"> When the cluster has been created it will have 0 nodes provisioned. Therefore, the cluster does not incur costs until you submit a job. This cluster will scale down when it has been idle for 2400 seconds (40 minutes).</span>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Next Steps\n",
|
||||||
|
"\n",
|
||||||
|
"In the next tutorial, you walk through submitting a script to the Azure Machine Learning compute cluster.\n",
|
||||||
|
"\n",
|
||||||
|
"[Tutorial: Run \"Hello World\" Python Script on Azure](day1-part2-hello-world.ipynb)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "samkemp"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.5"
|
||||||
|
},
|
||||||
|
"notice": "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License."
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
204
tutorials/get-started-day1/day1-part2-hello-world.ipynb
Normal file
204
tutorials/get-started-day1/day1-part2-hello-world.ipynb
Normal file
@@ -0,0 +1,204 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tutorial: \"Hello World\" (Part 2 of 4)\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"## Introduction\n",
|
||||||
|
"In **part 2 of this get started series**, you will submit a trivial \"hello world\" python script to the cloud by:\n",
|
||||||
|
"\n",
|
||||||
|
"- Running Python code in the cloud with Azure Machine Learning SDK\n",
|
||||||
|
"- Switching between debugging locally on a compute instance.\n",
|
||||||
|
"- Submitting remote runs in the cloud\n",
|
||||||
|
"- Monitoring and recording runs in the Azure Machine Learning studio\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook follows the steps provided on the [Python (day 1) - \"hello world\" documentation page](https://aka.ms/day1aml). This tutorial is part of a **four-part tutorial series** in which you learn the fundamentals of Azure Machine Learning and complete simple jobs-based machine learning tasks in the Azure cloud. It builds off the work you completed in [Tutorial part 1: set up an Azure Machine Learning compute cluster](day1-part1-setup.ipynb).\n",
|
||||||
|
"\n",
|
||||||
|
"## Pre-requisites\n",
|
||||||
|
"\n",
|
||||||
|
"- Complete [Tutorial part 1: set up an Azure Machine Learning compute cluster](day1-part1-setup.ipynb) if you don't already have an Azure Machine Learning compute cluster.\n",
|
||||||
|
"- Familiarity with Python and Machine Learning concepts.\n",
|
||||||
|
"- If you are using a compute instance in Azure Machine Learning to run this notebook series, you are all set. Otherwise, please follow the [Configure a development environment for Azure Machine Learning](https://docs.microsoft.com/azure/machine-learning/how-to-configure-environment)\n",
|
||||||
|
"---"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Your code\n",
|
||||||
|
"\n",
|
||||||
|
"In the `code/hello` subdirectory you will find a trivial python script [hello.py](code/hello/hello.py) that has the following code:\n",
|
||||||
|
"\n",
|
||||||
|
"```Python\n",
|
||||||
|
"# code/hello/hello.py\n",
|
||||||
|
"print(\"hello world!\")\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"In this tutorial you are going to submit this trivial python script to an Azure Machine Learning Compute Cluster."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Test in your development environment\n",
|
||||||
|
"\n",
|
||||||
|
"You can test your code works on a compute instance or locally (for example, a laptop), which has the benefit of interactive debugging of code:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": []
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"!python code/hello/hello.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Submit your code to Azure Machine Learning\n",
|
||||||
|
"\n",
|
||||||
|
"Below you create a __*control script*__ this is where you specify _how_ your code is submitted to Azure Machine Learning. The code you submit to Azure Machine Learning (in this case `hello.py`) does not need anything specific to Azure Machine Learning - it can be any valid Python code. It is only the control script that is Azure Machine Learning specific.\n",
|
||||||
|
"\n",
|
||||||
|
"The code below will show a Jupyter widget that tracks the progress of your run, and displays logs.\n",
|
||||||
|
"\n",
|
||||||
|
"> <span style=\"color:purple; font-weight:bold\">! NOTE <br>\n",
|
||||||
|
"> The very first run will take 5-10minutes to complete. This is because in the background a docker image is built in the cloud, the compute cluster is resized from 0 to 1 node, and the docker image is downloaded to the compute. Subsequent runs are much quicker (~15 seconds) as the docker image is cached on the compute - you can test this by resubmitting the code below after the first run has completed.</span>"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"remote run",
|
||||||
|
"batchai",
|
||||||
|
"configure run",
|
||||||
|
"use notebook widget"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace, Experiment, ScriptRunConfig\n",
|
||||||
|
"from azureml.widgets import RunDetails\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"experiment = Experiment(workspace=ws, name='day1-experiment-hello')\n",
|
||||||
|
"\n",
|
||||||
|
"config = ScriptRunConfig(source_directory='./code/hello', script='hello.py', compute_target='cpu-cluster')\n",
|
||||||
|
"\n",
|
||||||
|
"run = experiment.submit(config)\n",
|
||||||
|
"RunDetails(run).show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Understanding the control code\n",
|
||||||
|
"\n",
|
||||||
|
"| Code |Description | \n",
|
||||||
|
"|---|---|\n",
|
||||||
|
"| `ws = Workspace.from_config()` | [Workspace](https://docs.microsoft.com/python/api/azureml-core/azureml.core.workspace.workspace?view=azure-ml-py&preserve-view=true) connects to your Azure Machine Learning workspace, so that you can communicate with your Azure Machine Learning resources. |\n",
|
||||||
|
"| `experiment = Experiment( ... )` | [Experiment](https://docs.microsoft.com/python/api/azureml-core/azureml.core.experiment.experiment?view=azure-ml-py&preserve-view=true) provides a simple way to organize multiple runs under a single name. <br>Later you can see how experiments make it easy to compare metrics between dozens of runs. |\n",
|
||||||
|
"| `config = ScriptRunConfig( ... )` | [ScriptRunConfig](https://docs.microsoft.com/python/api/azureml-core/azureml.core.scriptrunconfig?view=azure-ml-py&preserve-view=true) wraps your `hello.py` code and passes it to your workspace.<br> As the name suggests, you can use this class to _configure_ how you want your _script_ to _run_ in Azure Machine Learning. <br>Also specifies what compute target the script will run on. <br>In this code, the target is the compute cluster you created in the [setup tutorial](tutorial-1st-experiment-sdk-setup-local.md). |\n",
|
||||||
|
"| `run = experiment.submit(config)` | Submits your script. This submission is called a [Run](https://docs.microsoft.com/python/api/azureml-core/azureml.core.run(class)?view=azure-ml-py&preserve-view=true). <br>A run encapsulates a single execution of your code. Use a run to monitor the script progress, capture the output,<br> analyze the results, visualize metrics and more. |\n",
|
||||||
|
"| `aml_url = run.get_portal_url()` | The `run` object provides a handle on the execution of your code. Monitor its progress from <br> the Azure Machine Learning Studio with the URL that is printed from the python script. |\n",
|
||||||
|
"|`RunDetails(run).show()` | There is an Azure Machine Learning widget that shows the progress of your job along with streaming the log files.\n",
|
||||||
|
"\n",
|
||||||
|
"## View the logs\n",
|
||||||
|
"\n",
|
||||||
|
"The widget has a dropdown box titled **Output logs** select `70_driver_log.txt`, which shows the following standard output: \n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
" 1: [2020-08-04T22:15:44.407305] Entering context manager injector.\n",
|
||||||
|
" 2: [context_manager_injector.py] Command line Options: Namespace(inject=['ProjectPythonPath:context_managers.ProjectPythonPath', 'RunHistory:context_managers.RunHistory', 'TrackUserError:context_managers.TrackUserError', 'UserExceptions:context_managers.UserExceptions'], invocation=['hello.py'])\n",
|
||||||
|
" 3: Starting the daemon thread to refresh tokens in background for process with pid = 31263\n",
|
||||||
|
" 4: Entering Run History Context Manager.\n",
|
||||||
|
" 5: Preparing to call script [ hello.py ] with arguments: []\n",
|
||||||
|
" 6: After variable expansion, calling script [ hello.py ] with arguments: []\n",
|
||||||
|
" 7:\n",
|
||||||
|
" 8: Hello world!\n",
|
||||||
|
" 9: Starting the daemon thread to refresh tokens in background for process with pid = 31263\n",
|
||||||
|
"10:\n",
|
||||||
|
"11:\n",
|
||||||
|
"12: The experiment completed successfully. Finalizing run...\n",
|
||||||
|
"13: Logging experiment finalizing status in history service.\n",
|
||||||
|
"14: [2020-08-04T22:15:46.541334] TimeoutHandler __init__\n",
|
||||||
|
"15: [2020-08-04T22:15:46.541396] TimeoutHandler __enter__\n",
|
||||||
|
"16: Cleaning up all outstanding Run operations, waiting 300.0 seconds\n",
|
||||||
|
"17: 1 items cleaning up...\n",
|
||||||
|
"18: Cleanup took 0.1812913417816162 seconds\n",
|
||||||
|
"19: [2020-08-04T22:15:47.040203] TimeoutHandler __exit__\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"On line 8 above, you see the \"Hello world!\" output. The 70_driver_log.txt file contains the standard output from run and can be useful when debugging remote runs in the cloud. You can also view the run by clicking on the **Click here to see the run in Azure Machine Learning studio** link in the wdiget.\n",
|
||||||
|
"\n",
|
||||||
|
"## Next steps\n",
|
||||||
|
"\n",
|
||||||
|
"In this tutorial, you took a simple \"hello world\" script and ran it on Azure. You saw how to connect to your Azure Machine Learning workspace, create an Experiment, and submit your `hello.py` code to the cloud.\n",
|
||||||
|
"\n",
|
||||||
|
"In the [next tutorial](day1-part3-train-model.ipynb), you build on these learnings by running something more interesting than `print(\"Hello world!\")`.\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "samkemp"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"celltoolbar": "Edit Metadata",
|
||||||
|
"kernel_info": {
|
||||||
|
"name": "python3-azureml"
|
||||||
|
},
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.5"
|
||||||
|
},
|
||||||
|
"notice": "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License.",
|
||||||
|
"nteract": {
|
||||||
|
"version": "nteract-front-end@1.0.0"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
289
tutorials/get-started-day1/day1-part3-train-model.ipynb
Normal file
289
tutorials/get-started-day1/day1-part3-train-model.ipynb
Normal file
@@ -0,0 +1,289 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tutorial: Train your first ML model (Part 3 of 4)\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"## Introduction\n",
|
||||||
|
"In the [previous tutorial](day1-part2-hello-world.ipynb), you ran a trivial \"Hello world!\" script in the cloud using Azure Machine Learning's Python SDK. This time you take it a step further by submitting a script that will train a machine learning model. This example will help you understand how Azure Machine Learning eases consistent behavior between debugging on a compute instance or laptop development environment, and remote runs.\n",
|
||||||
|
"\n",
|
||||||
|
"Learning these concepts means that by the end of this session, you can:\n",
|
||||||
|
"\n",
|
||||||
|
"* Use Conda to define an Azure Machine Learning environment.\n",
|
||||||
|
"* Train a model in the cloud.\n",
|
||||||
|
"* Log metrics to Azure Machine Learning.\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook follows the steps provided on the [Python (day 1) - train a model documentation page](https://aka.ms/day1aml).\n",
|
||||||
|
"\n",
|
||||||
|
"## Prerequisites\n",
|
||||||
|
"\n",
|
||||||
|
"- You have completed the following:\n",
|
||||||
|
" - [Setup on your compute cluster](day1-part1-setup.ipynb)\n",
|
||||||
|
" - [Tutorial: Hello World example](day1-part2-hello-world.md)\n",
|
||||||
|
"- Familiarity with Python and Machine Learning concepts\n",
|
||||||
|
"- If you are using a compute instance in Azure Machine Learning to run this notebook series, you are all set. Otherwise, please follow the [Configure a development environment for Azure Machine Learning](https://docs.microsoft.com/azure/machine-learning/how-to-configure-environment)\n",
|
||||||
|
"---\n",
|
||||||
|
"\n",
|
||||||
|
"## Your machine learning code\n",
|
||||||
|
"\n",
|
||||||
|
"This tutorial shows you how to train a PyTorch model on the CIFAR 10 dataset using an Azure Machine Learning Cluster. In this case you will be using a CPU cluster, but this could equally be a GPU cluster. Whilst this tutorial uses PyTorch, the steps we show you apply to *any* machine learning code. \n",
|
||||||
|
"\n",
|
||||||
|
"In the `code/pytorch-cifar10-train` subdirectory you will see 2 files:\n",
|
||||||
|
"\n",
|
||||||
|
"1. [model.py](code/pytorch-cifar10-train/model.py) - this defines the neural network architecture\n",
|
||||||
|
"1. [train.py](code/pytorch-cifar10-train/train.py) - This is the training script. This script downloads the CIFAR10 dataset using PyTorch `torchvision.dataset` APIs, sets up the network defined in\n",
|
||||||
|
"`model.py`, and trains it for two epochs using standard SGD and cross-entropy loss.\n",
|
||||||
|
"\n",
|
||||||
|
"Note the code is based on [this introductory example from PyTorch](https://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html). "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Define the Python environment for your machine learning code\n",
|
||||||
|
"\n",
|
||||||
|
"For demonstration purposes, we're going to use a Conda environment but the steps for a pip virtual environment are almost identical. This environment has all the dependencies that your model and training script require. \n",
|
||||||
|
"\n",
|
||||||
|
"In the `configuration` directory there is a *conda dependencies* file called [pytorch-env.yml](configuration/pytorch-env.yml) that specifies the dependencies to run the python code. "
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Test in your development environment\n",
|
||||||
|
"\n",
|
||||||
|
"Test your script runs on either your compute instance or laptop using this environment."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"!python code/pytorch-cifar10-train/train.py"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"**You should notice that the script has downloaded the data into a directory called `data`.**\n",
|
||||||
|
"\n",
|
||||||
|
"## Submit your machine learning code to Azure Machine Learning\n",
|
||||||
|
"\n",
|
||||||
|
"The difference to the control script below and the one used to submit \"hello world\" is that you adjust the environment to be set from the conda dependencies file you created earlier.\n",
|
||||||
|
"\n",
|
||||||
|
"> <span style=\"color:purple; font-weight:bold\">! NOTE <br>\n",
|
||||||
|
"> The first time you run this script, Azure Machine Learning will build a new docker image from your PyTorch environment. The whole run could take 5-10 minutes to complete. You can see the docker build logs in the widget by selecting the `20_image_build_log.txt` in the log files dropdown. This image will be reused in future runs making them run much quicker.</span>\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"remote run",
|
||||||
|
"batchai",
|
||||||
|
"configure run",
|
||||||
|
"use notebook widget"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig\n",
|
||||||
|
"from azureml.widgets import RunDetails\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"experiment = Experiment(workspace=ws, name='day1-experiment-train')\n",
|
||||||
|
"config = ScriptRunConfig(source_directory='code/pytorch-cifar10-train/', script='train.py', compute_target='cpu-cluster')\n",
|
||||||
|
"\n",
|
||||||
|
"env = Environment.from_conda_specification(name='pytorch-env', file_path='configuration/pytorch-env.yml')\n",
|
||||||
|
"config.run_config.environment = env\n",
|
||||||
|
"\n",
|
||||||
|
"run = experiment.submit(config)\n",
|
||||||
|
"\n",
|
||||||
|
"RunDetails(run).show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Understand the control code\n",
|
||||||
|
"\n",
|
||||||
|
"Compared to the control script that submitted the \"hello world\" example, this control script introduces the following:\n",
|
||||||
|
"\n",
|
||||||
|
"| Code | Description\n",
|
||||||
|
"| --- | --- |\n",
|
||||||
|
"| `env = Environment.from_conda_specification( ...)` | Azure Machine Learning provides the concept of an `Environment` to represent a reproducible, <br>versioned Python environment for running experiments. Here you have created it from a yaml conda dependencies file.|\n",
|
||||||
|
"| `config.run_config.environment = env` | adds the environment to the ScriptRunConfig. |\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"**There are many ways to create AML environments, including [from a pip requirements.txt](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py&preserve-view=true#from-pip-requirements-name--file-path-), or even [from an existing local Conda environment](https://docs.microsoft.com/python/api/azureml-core/azureml.core.environment.environment?view=azure-ml-py&preserve-view=true#from-existing-conda-environment-name--conda-environment-name-).**\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Once your image is built, select `70_driver_log.txt` to see the output of your training script, which should look like:\n",
|
||||||
|
"\n",
|
||||||
|
"```txt\n",
|
||||||
|
"Downloading https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz to ./data/cifar-10-python.tar.gz\n",
|
||||||
|
"...\n",
|
||||||
|
"Files already downloaded and verified\n",
|
||||||
|
"epoch=1, batch= 2000: loss 2.19\n",
|
||||||
|
"...\n",
|
||||||
|
"epoch=2, batch=12000: loss 1.27\n",
|
||||||
|
"Finished Training\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"Environments can be registered to a workspace with `env.register(ws)`, allowing them to be easily shared, reused, and versioned. Environments make it easy to reproduce previous results and to collaborate with your team.\n",
|
||||||
|
"\n",
|
||||||
|
"Azure Machine Learning also maintains a collection of curated environments. These environments cover common ML scenarios and are backed by cached Docker images. Cached Docker images make the first remote run faster.\n",
|
||||||
|
"\n",
|
||||||
|
"In short, using registered environments can save you time! More details can be found on the [environments documentation](./how-to-use-environments.md)\n",
|
||||||
|
"\n",
|
||||||
|
"## Log training metrics\n",
|
||||||
|
"\n",
|
||||||
|
"Now that you have a model training in Azure Machine Learning, start tracking some performance metrics.\n",
|
||||||
|
"The current training script prints metrics to the terminal. Azure Machine Learning provides a\n",
|
||||||
|
"mechanism for logging metrics with more functionality. By adding a few lines of code, you gain the ability to visualize metrics in the studio and to compare metrics between multiple runs.\n",
|
||||||
|
"\n",
|
||||||
|
"### Machine learning code updates\n",
|
||||||
|
"\n",
|
||||||
|
"In the `code/pytorch-cifar10-train-with-logging` directory you will notice the [train.py](code/pytorch-cifar10-train-with-logging/train.py) script has been modified with two additional lines that will log the loss to the Azure Machine Learning Studio:\n",
|
||||||
|
"\n",
|
||||||
|
"```python\n",
|
||||||
|
"# in train.py\n",
|
||||||
|
"run = Run.get_context()\n",
|
||||||
|
"...\n",
|
||||||
|
"run.log('loss', loss)\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"Metrics in Azure Machine Learning are:\n",
|
||||||
|
"\n",
|
||||||
|
"- Organized by experiment and run so it's easy to keep track of and\n",
|
||||||
|
"compare metrics.\n",
|
||||||
|
"- Equipped with a UI so we can visualize training performance in the studio or in the notebook widget.\n",
|
||||||
|
"- **Designed to scale** You can submit concurrent experiments and the Azure Machine Learning cluster will scale out (up to the maximum node count of the cluster) to run the experiments in parallel."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Update the Environment for your machine learning code\n",
|
||||||
|
"\n",
|
||||||
|
"The `train.py` script just took a new dependency on `azureml.core`. Therefore, the conda dependecies file [pytorch-aml-env](configuration/pytorch-aml-env.yml) reflects this change."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Submit your machine learning code to Azure Machine Learning\n",
|
||||||
|
"Submit your code once more. This time the widget includes the metrics where you can now see live updates on the model training loss!"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"remote run",
|
||||||
|
"batchai",
|
||||||
|
"configure run",
|
||||||
|
"use notebook widget",
|
||||||
|
"get metrics"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig\n",
|
||||||
|
"from azureml.widgets import RunDetails\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"experiment = Experiment(workspace=ws, name='day1-experiment-train')\n",
|
||||||
|
"config = ScriptRunConfig(source_directory='code/pytorch-cifar10-train-with-logging', script='train.py', compute_target='cpu-cluster')\n",
|
||||||
|
"\n",
|
||||||
|
"env = Environment.from_conda_specification(name='pytorch-aml-env', file_path='configuration/pytorch-aml-env.yml')\n",
|
||||||
|
"config.run_config.environment = env\n",
|
||||||
|
"\n",
|
||||||
|
"run = experiment.submit(config)\n",
|
||||||
|
"RunDetails(run).show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Next steps\n",
|
||||||
|
"\n",
|
||||||
|
"In this session, you upgraded from a basic \"Hello world!\" script to a more realistic\n",
|
||||||
|
"training script that required a specific Python environment to run. You saw how\n",
|
||||||
|
"to take a local Conda environment to the cloud with Azure Machine Learning Environments. Finally, you\n",
|
||||||
|
"saw how in a few lines of code you can log metrics to Azure Machine Learning.\n",
|
||||||
|
"\n",
|
||||||
|
"In the next session, you'll see how to work with data in Azure Machine Learning by uploading the CIFAR10\n",
|
||||||
|
"dataset to Azure.\n",
|
||||||
|
"\n",
|
||||||
|
"[Tutorial: Bring your own data](day1-part4-data.ipynb)\n"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "samkemp"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"celltoolbar": "Edit Metadata",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.5"
|
||||||
|
},
|
||||||
|
"notice": "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License."
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
297
tutorials/get-started-day1/day1-part4-data.ipynb
Normal file
297
tutorials/get-started-day1/day1-part4-data.ipynb
Normal file
@@ -0,0 +1,297 @@
|
|||||||
|
{
|
||||||
|
"cells": [
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"Copyright (c) Microsoft Corporation. All rights reserved.\n",
|
||||||
|
"\n",
|
||||||
|
"Licensed under the MIT License."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
""
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"# Tutorial: Bring your own data (Part 4 of 4)\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"## Introduction\n",
|
||||||
|
"\n",
|
||||||
|
"In the previous [Tutorial: Train a model in the cloud](day1-part3-train-model.ipynb) article, the CIFAR10 data was downloaded using the inbuilt `torchvision.datasets.CIFAR10` method in the PyTorch API. However, in many cases you are going to want to use your own data in a remote training run. This article focuses on the workflow you can leverage such that you can work with your own data in Azure Machine Learning. \n",
|
||||||
|
"\n",
|
||||||
|
"By the end of this tutorial you would have a better understanding of:\n",
|
||||||
|
"\n",
|
||||||
|
"- How to upload your data to Azure\n",
|
||||||
|
"- Best practices for working with cloud data in Azure Machine Learning\n",
|
||||||
|
"- Working with command-line arguments\n",
|
||||||
|
"\n",
|
||||||
|
"This notebook follows the steps provided on the [Python (day 1) - bring your own data documentation page](https://aka.ms/day1aml).\n",
|
||||||
|
"\n",
|
||||||
|
"## Prerequisites\n",
|
||||||
|
"\n",
|
||||||
|
"- You have completed:\n",
|
||||||
|
" - Setup on your [Azure Machine Learning Compute Cluster](day1-part1-setup.ipynb)\n",
|
||||||
|
" - [Tutorial: Hello World](day1-part2-hello-world.ipynb)\n",
|
||||||
|
" - [Tutorial: Train a model in the cloud](day1-part3-train-model.ipynb)\n",
|
||||||
|
"- Familiarity with Python and Machine Learning concepts\n",
|
||||||
|
"- If you are using a compute instance in Azure Machine Learning to run this notebook series, you are all set. Otherwise, please follow the [Configure a development environment for Azure Machine Learning](https://docs.microsoft.com/azure/machine-learning/how-to-configure-environment)\n",
|
||||||
|
"\n",
|
||||||
|
"---\n",
|
||||||
|
"\n",
|
||||||
|
"## Your machine learning code\n",
|
||||||
|
"\n",
|
||||||
|
"By now you have your training script running in Azure Machine Learning, and can monitor the model performance. Let's _parametrize_ the training script by introducing\n",
|
||||||
|
"arguments. Using arguments will allow you to easily compare different hyperparmeters.\n",
|
||||||
|
"\n",
|
||||||
|
"Presently our training script is set to download the CIFAR10 dataset on each run. The python code in [code/pytorch-cifar10-your-data/train.py](code/pytorch-cifar10-your-data/train.py) now uses **`argparse` to parametize the script.**"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Understanding your machine learning code changes\n",
|
||||||
|
"\n",
|
||||||
|
"The code used in `train.py` has leveraged the `argparse` library to set up the `data_path`, `learning_rate`, and `momentum`.\n",
|
||||||
|
"\n",
|
||||||
|
"```python\n",
|
||||||
|
"# .... other code\n",
|
||||||
|
"parser = argparse.ArgumentParser()\n",
|
||||||
|
"parser.add_argument('--data_path', type=str, help='Path to the training data')\n",
|
||||||
|
"parser.add_argument('--learning_rate', type=float, default=0.001, help='Learning rate for SGD')\n",
|
||||||
|
"parser.add_argument('--momentum', type=float, default=0.9, help='Momentum for SGD')\n",
|
||||||
|
"args = parser.parse_args()\n",
|
||||||
|
"# ... other code\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"Also the `train.py` script was adapted to update the optimizer to use the user-defined parameters:\n",
|
||||||
|
"\n",
|
||||||
|
"```python\n",
|
||||||
|
"optimizer = optim.SGD(\n",
|
||||||
|
" net.parameters(),\n",
|
||||||
|
" lr=args.learning_rate, # get learning rate from command-line argument\n",
|
||||||
|
" momentum=args.momentum, # get momentum from command-line argument\n",
|
||||||
|
")\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"## Test your machine learning code locally\n",
|
||||||
|
"\n",
|
||||||
|
"To run the modified training script locally, run the python command below.\n",
|
||||||
|
"\n",
|
||||||
|
"You avoid having to download the CIFAR10 dataset by passing in a local path to the\n",
|
||||||
|
"data. Also you can experiment with different values for _learning rate_ and\n",
|
||||||
|
"_momentum_ hyperparameters without having to hard-code them in the training script.\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"!python code/pytorch-cifar10-your-data/train.py --data_path ./data --learning_rate 0.003 --momentum 0.92"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"## Upload your data to Azure\n",
|
||||||
|
"\n",
|
||||||
|
"In order to run this script in Azure Machine Learning, you need to make your training data available in Azure. Your Azure Machine Learning workspace comes equipped with a _default_ **Datastore** - an Azure Blob storage account - that you can use to store your training data.\n",
|
||||||
|
"\n",
|
||||||
|
"> <span style=\"color:purple; font-weight:bold\">! NOTE <br>\n",
|
||||||
|
"> Azure Machine Learning allows you to connect other cloud-based datastores that store your data. For more details, see [datastores documentation](./concept-data.md).</span>\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"datastore = ws.get_default_datastore()\n",
|
||||||
|
"datastore.upload(src_dir='./data', target_path='datasets/cifar10', overwrite=True)"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"The `target_path` specifies the path on the datastore where the CIFAR10 data will be uploaded.\n",
|
||||||
|
"\n",
|
||||||
|
"## Submit your machine learning code to Azure Machine Learning\n",
|
||||||
|
"\n",
|
||||||
|
"As you have done previously, create a new Python control script:"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {
|
||||||
|
"tags": [
|
||||||
|
"remote run",
|
||||||
|
"batchai",
|
||||||
|
"configure run",
|
||||||
|
"use notebook widget",
|
||||||
|
"get metrics",
|
||||||
|
"use datastore"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace, Experiment, Environment, ScriptRunConfig, Dataset\n",
|
||||||
|
"from azureml.widgets import RunDetails\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"\n",
|
||||||
|
"datastore = ws.get_default_datastore()\n",
|
||||||
|
"dataset = Dataset.File.from_files(path=(datastore, 'datasets/cifar10'))\n",
|
||||||
|
"\n",
|
||||||
|
"experiment = Experiment(workspace=ws, name='day1-experiment-data')\n",
|
||||||
|
"\n",
|
||||||
|
"config = ScriptRunConfig(source_directory='./code/pytorch-cifar10-your-data',\n",
|
||||||
|
" script='train.py',\n",
|
||||||
|
" compute_target='cpu-cluster',\n",
|
||||||
|
" arguments=[\n",
|
||||||
|
" '--data_path', dataset.as_named_input('input').as_mount(),\n",
|
||||||
|
" '--learning_rate', 0.003,\n",
|
||||||
|
" '--momentum', 0.92])\n",
|
||||||
|
"\n",
|
||||||
|
"# set up pytorch environment\n",
|
||||||
|
"env = Environment.from_conda_specification(name='pytorch-aml-env',file_path='configuration/pytorch-aml-env.yml')\n",
|
||||||
|
"config.run_config.environment = env\n",
|
||||||
|
"\n",
|
||||||
|
"run = experiment.submit(config)\n",
|
||||||
|
"RunDetails(run).show()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"### Understand the control code\n",
|
||||||
|
"\n",
|
||||||
|
"The above control code has the following additional code compared to the control code written in [previous tutorial](03-train-model.ipynb)\n",
|
||||||
|
"\n",
|
||||||
|
"**`dataset = Dataset.File.from_files(path=(datastore, 'datasets/cifar10'))`**: A Dataset is used to reference the data you uploaded to the Azure Blob Store. Datasets are an abstraction layer on top of your data that are designed to improve reliability and trustworthiness.\n",
|
||||||
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"**`config = ScriptRunConfig(...)`**: We modified the `ScriptRunConfig` to include a list of arguments that will be passed into `train.py`. We also specified `dataset.as_named_input('input').as_mount()`, which means the directory specified will be _mounted_ to the compute target.\n",
|
||||||
|
"\n",
|
||||||
|
"## Inspect the 70_driver_log log file\n",
|
||||||
|
"\n",
|
||||||
|
"In the navigate to the 70_driver_log.txt file - you should see the following output:\n",
|
||||||
|
"\n",
|
||||||
|
"```\n",
|
||||||
|
"Processing 'input'.\n",
|
||||||
|
"Processing dataset FileDataset\n",
|
||||||
|
"{\n",
|
||||||
|
" \"source\": [\n",
|
||||||
|
" \"('workspaceblobstore', 'datasets/cifar10')\"\n",
|
||||||
|
" ],\n",
|
||||||
|
" \"definition\": [\n",
|
||||||
|
" \"GetDatastoreFiles\"\n",
|
||||||
|
" ],\n",
|
||||||
|
" \"registration\": {\n",
|
||||||
|
" \"id\": \"XXXXX\",\n",
|
||||||
|
" \"name\": null,\n",
|
||||||
|
" \"version\": null,\n",
|
||||||
|
" \"workspace\": \"Workspace.create(name='XXXX', subscription_id='XXXX', resource_group='X')\"\n",
|
||||||
|
" }\n",
|
||||||
|
"}\n",
|
||||||
|
"Mounting input to /tmp/tmp9kituvp3.\n",
|
||||||
|
"Mounted input to /tmp/tmp9kituvp3 as folder.\n",
|
||||||
|
"Exit __enter__ of DatasetContextManager\n",
|
||||||
|
"Entering Run History Context Manager.\n",
|
||||||
|
"Current directory: /mnt/batch/tasks/shared/LS_root/jobs/dsvm-aml/azureml/tutorial-session-3_1600171983_763c5381/mounts/workspaceblobstore/azureml/tutorial-session-3_1600171983_763c5381\n",
|
||||||
|
"Preparing to call script [ train.py ] with arguments: ['--data_path', '$input', '--learning_rate', '0.003', '--momentum', '0.92']\n",
|
||||||
|
"After variable expansion, calling script [ train.py ] with arguments: ['--data_path', '/tmp/tmp9kituvp3', '--learning_rate', '0.003', '--momentum', '0.92']\n",
|
||||||
|
"\n",
|
||||||
|
"Script type = None\n",
|
||||||
|
"===== DATA =====\n",
|
||||||
|
"DATA PATH: /tmp/tmp9kituvp3\n",
|
||||||
|
"LIST FILES IN DATA PATH...\n",
|
||||||
|
"['cifar-10-batches-py', 'cifar-10-python.tar.gz']\n",
|
||||||
|
"```\n",
|
||||||
|
"\n",
|
||||||
|
"Notice:\n",
|
||||||
|
"\n",
|
||||||
|
"1. Azure Machine Learning has mounted the blob store to the compute cluster automatically for you.\n",
|
||||||
|
"2. The ``dataset.as_named_input('input').as_mount()`` used in the control script resolves to the mount point\n",
|
||||||
|
"3. In the machine learning code we include a line to list the directorys under the data directory - you can see the list above.\n",
|
||||||
|
"\n",
|
||||||
|
"## Clean up resources\n",
|
||||||
|
"\n",
|
||||||
|
"The compute cluster will scale down to zero after 40minutes of idle time. When the compute is idle you will not be charged. If you want to delete the cluster use:\n"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"execution_count": null,
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": [],
|
||||||
|
"source": [
|
||||||
|
"from azureml.core import Workspace\n",
|
||||||
|
"\n",
|
||||||
|
"ws = Workspace.from_config()\n",
|
||||||
|
"ct = ws.compute_targets['cpu-cluster']\n",
|
||||||
|
"ct.delete()"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "markdown",
|
||||||
|
"metadata": {},
|
||||||
|
"source": [
|
||||||
|
"If you're not going to use what you've created here, delete the resources you just created with this quickstart so you don't incur any charges for storage. In the Azure portal, select and delete your resource group.\n",
|
||||||
|
"\n",
|
||||||
|
"## Next Steps\n",
|
||||||
|
"\n",
|
||||||
|
"To learn more about the capabilities of Azure Machine Learning please refer to the following documentation:\n",
|
||||||
|
"\n",
|
||||||
|
"* [Azure Machine Learning Pipelines](https://docs.microsoft.com/en-us/azure/machine-learning/concept-ml-pipelines#building-pipelines-with-the-python-sdk)\n",
|
||||||
|
"* [Deploy models for real-time scoring](https://docs.microsoft.com/en-us/azure/machine-learning/tutorial-deploy-models-with-aml)\n",
|
||||||
|
"* [Hyper parameter tuning with Azure Machine Learning](https://docs.microsoft.com/en-us/azure/machine-learning/how-to-tune-hyperparameters)\n",
|
||||||
|
"* [Prep your code for production](https://docs.microsoft.com/azure/machine-learning/tutorial-convert-ml-experiment-to-production)"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"metadata": {
|
||||||
|
"authors": [
|
||||||
|
{
|
||||||
|
"name": "samkemp"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
"celltoolbar": "Edit Metadata",
|
||||||
|
"kernelspec": {
|
||||||
|
"display_name": "Python 3.6",
|
||||||
|
"language": "python",
|
||||||
|
"name": "python36"
|
||||||
|
},
|
||||||
|
"language_info": {
|
||||||
|
"codemirror_mode": {
|
||||||
|
"name": "ipython",
|
||||||
|
"version": 3
|
||||||
|
},
|
||||||
|
"file_extension": ".py",
|
||||||
|
"mimetype": "text/x-python",
|
||||||
|
"name": "python",
|
||||||
|
"nbconvert_exporter": "python",
|
||||||
|
"pygments_lexer": "ipython3",
|
||||||
|
"version": "3.6.5"
|
||||||
|
},
|
||||||
|
"notice": "Copyright (c) Microsoft Corporation. All rights reserved. Licensed under the MIT License."
|
||||||
|
},
|
||||||
|
"nbformat": 4,
|
||||||
|
"nbformat_minor": 4
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user