MXNet is a deep learning framework designed for both efficiency and flexibility. It allows you to mix the flavours of symbolic programming and imperative programming to maximize efficiency and productivity. In its core, a dynamic dependency scheduler that automatically parallelizes both symbolic and imperative operations on the fly. A graph optimization layer on top of that makes symbolic execution fast and memory efficient. The library is portable and lightweight, and it scales to multiple GPUs and multiple machines.
In [1]:
ENV["MXNET_HOME"] = "/home/ubuntu/mxnet/"
Base.compilecache("MXNet")
using MXNet
In [2]:
data = mx.Variable(:data)
Out[2]:
In [3]:
fc1 = mx.FullyConnected(data = data, name=:fc1, num_hidden=128)
act1 = mx.Activation(data = fc1, name=:relu1, act_type=:relu)
fc2 = mx.FullyConnected(data = act1, name=:fc2, num_hidden=64)
act2 = mx.Activation(data = fc2, name=:relu2, act_type=:relu)
fc3 = mx.FullyConnected(data = act2, name=:fc3, num_hidden=10)
Out[3]:
In [4]:
mlp = mx.SoftmaxOutput(data = fc3, name=:softmax)
Out[4]:
In [5]:
mlp = @mx.chain mx.Variable(:data) =>
mx.FullyConnected(name=:fc1, num_hidden=128) =>
mx.Activation(name=:relu1, act_type=:relu) =>
mx.FullyConnected(name=:fc2, num_hidden=64) =>
mx.Activation(name=:relu2, act_type=:relu) =>
mx.FullyConnected(name=:fc3, num_hidden=10) =>
mx.SoftmaxOutput(name=:softmax)
Out[5]:
In [6]:
batch_size = 100
include(Pkg.dir("MXNet", "examples", "mnist", "mnist-data.jl"))
train_provider, eval_provider = get_mnist_providers(batch_size)
Out[6]:
In [7]:
model = mx.FeedForward(mlp, context=mx.cpu())
Out[7]:
In [8]:
optimizer = mx.SGD(lr=0.1, momentum=0.9, weight_decay=0.00001)
Out[8]:
In [9]:
@time mx.fit(model, optimizer, train_provider, n_epoch=20, eval_data=eval_provider)
In [21]:
probs = mx.predict(model, eval_provider)
Out[21]:
In [22]:
# collect all labels from eval data
labels = Array[]
for batch in eval_provider
push!(labels, copy(mx.get(eval_provider, batch, :softmax_label)))
end
labels = cat(1, labels...)
Out[22]:
In [23]:
# Now we use compute the accuracy
correct = 0
for i = 1:length(labels)
# labels are 0...9
if indmax(probs[:,i]) == labels[i]+1
correct += 1
end
end
accuracy = 100correct/length(labels)
println(mx.format("Accuracy on eval set: {1:.2f}%", accuracy))
In [24]:
labels
Out[24]:
In [16]:
features = trainfeatures(60000)
Out[16]:
In [25]:
testfeatures(1)
Out[25]:
In [ ]: