Gluon API深度學(xué)習(xí)庫
Gluon 是微軟聯(lián)合亞馬遜推出的一個開源深度學(xué)習(xí)庫,這是一個清晰、簡潔、簡單但功能強大的深度學(xué)習(xí) API,該規(guī)范可以提升開發(fā)人員學(xué)習(xí)深度學(xué)習(xí)的速度,而無需關(guān)心所選擇的深度學(xué)習(xí)框架。Gluon API 提供了靈活的接口來簡化深度學(xué)習(xí)原型設(shè)計、創(chuàng)建、訓(xùn)練以及部署,而且不會犧牲數(shù)據(jù)訓(xùn)練的速度。
Gluon 規(guī)范已經(jīng)在 Apache MXNet 中實現(xiàn),只需要安裝最新的 MXNet 即可使用。推薦使用 Python 3.3 或者更新版本。
主要優(yōu)勢包括:
-
代碼簡單,易于理解
-
靈活,命令式結(jié)構(gòu): 不需要嚴(yán)格定義神經(jīng)網(wǎng)絡(luò)模型,而是將訓(xùn)練算法和模型更緊密地結(jié)合起來,開發(fā)靈活
-
動態(tài)圖: Gluon 可以讓開發(fā)者動態(tài)的定義神經(jīng)網(wǎng)絡(luò)模型,這意味著他們可以在運行時創(chuàng)建模型、結(jié)構(gòu),以及使用任何 Python 原生的控制流
-
高性能: Gluon 所提供的這些優(yōu)勢對底層引擎的訓(xùn)練速度并沒有任何影響
示例代碼:
import mxnet as mx
from mxnet import gluon, autograd, ndarray
import numpy as np
train_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=True,
transform=lambda data, label: (data.astype(np.float32)/255, label)),
batch_size=32, shuffle=True)
test_data = mx.gluon.data.DataLoader(mx.gluon.data.vision.MNIST(train=False,
transform=lambda data, label: (data.astype(np.float32)/255, label)),
batch_size=32, shuffle=False)
# First step is to initialize your model
net = gluon.nn.Sequential()
# Then, define your model architecture
with net.name_scope():
net.add(gluon.nn.Dense(128, activation="relu")) # 1st layer - 128 nodes
net.add(gluon.nn.Dense(64, activation="relu")) # 2nd layer – 64 nodes
net.add(gluon.nn.Dense(10)) # Output layer
# We start with random values for all of the model’s parameters from a
# normal distribution with a standard deviation of 0.05
net.collect_params().initialize(mx.init.Normal(sigma=0.05))
# We opt to use softmax cross entropy loss function to measure how well the # model is able to predict the correct answer
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
# We opt to use the stochastic gradient descent (sgd) training algorithm
# and set the learning rate hyperparameter to .1
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': .1})
epochs = 10
for e in range(epochs):
for i, (data, label) in enumerate(train_data):
data = data.as_in_context(mx.cpu()).reshape((-1, 784))
label = label.as_in_context(mx.cpu())
with autograd.record(): # Start recording the derivatives
output = net(data) # the forward iteration
loss = softmax_cross_entropy(output, label)
loss.backward()
trainer.step(data.shape[0])
# Provide stats on the improvement of the model over each epoch
curr_loss = ndarray.mean(loss).asscalar()
print("Epoch {}. Current Loss: {}.".format(e, curr_loss))評論
圖片
表情
