手作業で深く学ぶ(二)——多層感知機(gluon)
3955 ワード
作者:Tyan博客:noahsnail.com|CSDN|簡書
注:本文は李沐大神の《手を出して深く勉強します》の課程のノートです!
注:本文は李沐大神の《手を出して深く勉強します》の課程のノートです!
# mxnet
import mxnet as mx
#
mx.random.seed(2)
from mxnet import gluon
from mxnet import ndarray as nd
from mxnet import autograd
from mxnet import image
from utils import load_data_fashion_mnist, accuracy, evaluate_accuracy
データの取得と読み込み
#
batch_size = 256
#
train_data, test_data = load_data_fashion_mnist(batch_size)
モデルの定義
#
net = gluon.nn.Sequential()
# name_scope ,
with net.name_scope():
net.add(gluon.nn.Flatten())
net.add(gluon.nn.Dense(256, activation='relu'))
net.add(gluon.nn.Dense(10))
#
net.initialize()
Softmaxとクロスエントロピー損失関数
#
softmax_cross_entropy = gluon.loss.SoftmaxCrossEntropyLoss()
最適化
#
trainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.1})
トレーニング
#
epochs = 5
#
for epoch in range(epochs):
#
train_loss = 0.0
#
train_acc = 0.0
#
for data, label in train_data:
#
with autograd.record():
#
output = net(data)
#
loss = softmax_cross_entropy(output, label)
#
loss.backward()
#
trainer.step(batch_size)
#
train_loss += nd.mean(loss).asscalar()
#
train_acc += accuracy(output, label)
#
test_acc = evaluate_accuracy(test_data, net)
print("Epoch %d. Loss: %f, Train acc %f, Test acc %f" % (
epoch, train_loss / len(train_data), train_acc / len(train_data), test_acc))
Epoch 0. Loss: 0.819048, Train acc 0.728666, Test acc 0.768530
Epoch 1. Loss: 0.550646, Train acc 0.808644, Test acc 0.823618
Epoch 2. Loss: 0.488554, Train acc 0.829210, Test acc 0.845553
Epoch 3. Loss: 0.457407, Train acc 0.839493, Test acc 0.842448
Epoch 4. Loss: 0.438059, Train acc 0.845486, Test acc 0.852063