Skip to content

Make the MXNet Gluon MNIST Example scale #134

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions sagemaker-python-sdk/mxnet_gluon_mnist/mnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
# ------------------------------------------------------------ #


def train(channel_input_dirs, hyperparameters, **kwargs):
def train(channel_input_dirs, hyperparameters, hosts, num_gpus, **kwargs):
# SageMaker passes num_cpus, num_gpus and other args we can use to tailor training to
# the current container environment, but here we just use simple cpu context.
ctx = mx.cpu()
Expand All @@ -41,8 +41,15 @@ def train(channel_input_dirs, hyperparameters, **kwargs):
# Collect all parameters from net and its children, then initialize them.
net.initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)
# Trainer is for updating parameters with gradient.

if len(hosts) == 1:
kvstore = 'device' if num_gpus > 0 else 'local'
else:
kvstore = 'dist_device_sync' if num_gpus > 0 else 'dist_sync'

trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': learning_rate, 'momentum': momentum})
{'learning_rate': learning_rate, 'momentum': momentum},
kvstore=kvstore)
metric = mx.metric.Accuracy()
loss = gluon.loss.SoftmaxCrossEntropyLoss()

Expand Down
5 changes: 3 additions & 2 deletions sagemaker-python-sdk/mxnet_gluon_sentiment/sentiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def train(current_host, hosts, num_cpus, num_gpus, channel_input_dirs, model_dir
if len(hosts) == 1:
kvstore = 'device' if num_gpus > 0 else 'local'
else:
kvstore = 'dist_sync'
kvstore = 'dist_device_sync' if num_gpus > 0 else 'dist_sync'

ctx = mx.gpu() if num_gpus > 0 else mx.cpu()

Expand All @@ -56,7 +56,8 @@ def train(current_host, hosts, num_cpus, num_gpus, channel_input_dirs, model_dir
net.initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)
# Trainer is for updating parameters with gradient.
trainer = gluon.Trainer(net.collect_params(), 'adam',
{'learning_rate': learning_rate})
{'learning_rate': learning_rate},
kvstore=kvstore)
metric = mx.metric.Accuracy()
loss = gluon.loss.SoftmaxCrossEntropyLoss()

Expand Down