Skip to content

Commit

Permalink
added gluon version of tutorials
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu committed Nov 30, 2017
1 parent 963520a commit 20452fe
Show file tree
Hide file tree
Showing 3 changed files with 490 additions and 0 deletions.
114 changes: 114 additions & 0 deletions gluon-implement/cnn-gluon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@

# coding: utf-8

# # Convolutional Neural Networks in ``gluon``
#

from __future__ import print_function
import mxnet as mx
from mxnet import nd, autograd
from mxnet import gluon
import numpy as np
mx.random.seed(1)


# ## Set the context


ctx = mx.gpu()


# ## Grab the MNIST dataset



batch_size = 64
num_inputs = 784
num_outputs = 10
def transform(data, label):
return nd.transpose(data.astype(np.float32), (2,0,1))/255, label.astype(np.float32)
train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True, transform=transform),
batch_size, shuffle=True)
test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),
batch_size, shuffle=False)


# ## Define a convolutional neural network
#
# Again, a few lines here is all we need in order to change the model. Let's add a couple of convolutional layers using ``gluon.nn``.


num_fc = 512
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Conv2D(channels=20, kernel_size=5, activation='relu'))
net.add(gluon.nn.MaxPool2D(pool_size=2, strides=2))
net.add(gluon.nn.Conv2D(channels=50, kernel_size=5, activation='relu'))
net.add(gluon.nn.MaxPool2D(pool_size=2, strides=2))
# The Flatten layer collapses all axis, except the first one, into one axis.
net.add(gluon.nn.Flatten())
net.add(gluon.nn.Dense(num_fc, activation="relu"))
net.add(gluon.nn.Dense(num_outputs))


# ## Parameter initialization
#


net.collect_params().initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)


# ## Softmax cross-entropy Loss


softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()


# ## Optimizer

trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})


# ## Write evaluation loop to calculate accuracy


def evaluate_accuracy(data_iterator, net):
acc = mx.metric.Accuracy()
for i, (data, label) in enumerate(data_iterator):
data = data.as_in_context(ctx)
label = label.as_in_context(ctx)
output = net(data)
predictions = nd.argmax(output, axis=1)
acc.update(preds=predictions, labels=label)
return acc.get()[1]


# ## Training Loop

# In[9]:


epochs = 10
smoothing_constant = .01

for e in range(epochs):
for i, (data, label) in enumerate(train_data):
data = data.as_in_context(ctx)
label = label.as_in_context(ctx)
with autograd.record():
output = net(data)
loss = softmax_cross_entropy(output, label)
loss.backward()
trainer.step(data.shape[0])

##########################
# Keep a moving average of the losses
##########################
curr_loss = nd.mean(loss).asscalar()
moving_loss = (curr_loss if ((i == 0) and (e == 0))
else (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss)

test_accuracy = evaluate_accuracy(test_data, net)
train_accuracy = evaluate_accuracy(train_data, net)
print("Epoch %s. Loss: %s, Train_acc %s, Test_acc %s" % (e, moving_loss, train_accuracy, test_accuracy))

112 changes: 112 additions & 0 deletions gluon-implement/mlp-gluon.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@

# coding: utf-8

# # Multilayer perceptrons in ``gluon``
#


from __future__ import print_function
import mxnet as mx
import numpy as np
from mxnet import nd, autograd
from mxnet import gluon


# We'll also want to set the compute context for our modeling. Feel free to go ahead and change this to mx.gpu(0) if you're running on an appropriately endowed machine.



ctx = mx.cpu()


# ## The MNIST dataset


mnist = mx.test_utils.get_mnist()
batch_size = 64
num_inputs = 784
num_outputs = 10
def transform(data, label):
return data.astype(np.float32)/255, label.astype(np.float32)
train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True, transform=transform),
batch_size, shuffle=True)
test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False, transform=transform),
batch_size, shuffle=False)


# ## Define the model
#
# *Here's the only real difference. We add two lines!*



num_hidden = 256
net = gluon.nn.Sequential()
with net.name_scope():
net.add(gluon.nn.Dense(num_hidden, activation="relu"))
net.add(gluon.nn.Dense(num_hidden, activation="relu"))
net.add(gluon.nn.Dense(num_outputs))


# ## Parameter initialization
#

net.collect_params().initialize(mx.init.Xavier(magnitude=2.24), ctx=ctx)


# ## Softmax cross-entropy loss


softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()


# ## Optimizer


trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})


# ## Evaluation metric



def evaluate_accuracy(data_iterator, net):
acc = mx.metric.Accuracy()
for i, (data, label) in enumerate(data_iterator):
data = data.as_in_context(ctx).reshape((-1, 784))
label = label.as_in_context(ctx)
output = net(data)
predictions = nd.argmax(output, axis=1)
acc.update(preds=predictions, labels=label)
return acc.get()[1]


# ## Training loop



epochs = 10
smoothing_constant = .01

for e in range(epochs):
for i, (data, label) in enumerate(train_data):
data = data.as_in_context(ctx).reshape((-1, 784))
label = label.as_in_context(ctx)
with autograd.record():
output = net(data)
loss = softmax_cross_entropy(output, label)
loss.backward()
trainer.step(data.shape[0])

##########################
# Keep a moving average of the losses
##########################
curr_loss = nd.mean(loss).asscalar()
moving_loss = (curr_loss if ((i == 0) and (e == 0))
else (1 - smoothing_constant) * moving_loss + (smoothing_constant) * curr_loss)

test_accuracy = evaluate_accuracy(test_data, net)
train_accuracy = evaluate_accuracy(train_data, net)
print("Epoch %s. Loss: %s, Train_acc %s, Test_acc %s" %
(e, moving_loss, train_accuracy, test_accuracy))

Loading

0 comments on commit 20452fe

Please sign in to comment.