MXNET:多層感知機

來源:互聯網
上載者:User

標籤:attach   initial   瞭解   port   rate   net   dde   learn   ati   

從零開始

前面瞭解了多層感知機的原理,我們來實現一個多層感知機。

# -*- coding: utf-8 -*-from mxnet import initfrom mxnet import ndarray as ndfrom mxnet.gluon import loss as glossimport gb# 定義資料來源batch_size = 256train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)# 定義模型參數num_inputs = 784num_outputs = 10num_hiddens = 256W1 = nd.random.normal(scale=0.01, shape=(num_inputs, num_hiddens))b1 = nd.zeros(num_hiddens)W2 = nd.random.normal(scale=0.01, shape=(num_hiddens, num_outputs))b2 = nd.zeros(num_outputs)params = [W1, b1, W2, b2]for param in params:    param.attach_grad()# 定義啟用函數def relu(X):    return nd.maximum(X, 0)# 定義模型def net(X):    X = X.reshape((-1, num_inputs))    H = relu(nd.dot(X, W1) + b1)    return nd.dot(H, W2) + b2# 定義損失函數loss = gloss.SoftmaxCrossEntropyLoss()# 訓練模型num_epochs = 5lr = 0.5gb.train_cpu(net, train_iter, test_iter, loss, num_epochs, batch_size,             params, lr)

添加隱層後,模型的效能大幅提升

# outputepoch 1, loss 0.5029, train acc 0.852, test acc 0.934epoch 2, loss 0.2000, train acc 0.943, test acc 0.956epoch 3, loss 0.1431, train acc 0.959, test acc 0.964epoch 4, loss 0.1138, train acc 0.967, test acc 0.968epoch 5, loss 0.0939, train acc 0.973, test acc 0.973

在定義模型參數和定義模型步驟,仍然有一些繁瑣。

使用Gluon
# -*- coding: utf-8 -*-from mxnet import initfrom mxnet import ndarray as ndfrom mxnet.gluon import loss as glossimport gb# 定義資料來源batch_size = 256train_iter, test_iter = gb.load_data_fashion_mnist(batch_size)# 定義模型from mxnet.gluon import nnnet = nn.Sequential()net.add(nn.Dense(256, activation='relu'))net.add(nn.Dense(10))net.add(nn.Dense(10))net.initialize(init.Normal(sigma=0.01))# 定義損失函數loss = gloss.SoftmaxCrossEntropyLoss()# 訓練模型from mxnet import gluontrainer = gluon.Trainer(net.collect_params(), 'sgd', {'learning_rate': 0.5})num_epochs = 5gb.train_cpu(net, train_iter, test_iter, loss, num_epochs, batch_size,             None, None, trainer)# outputepoch 1, loss 1.3065, train acc 0.525, test acc 0.814epoch 2, loss 0.2480, train acc 0.928, test acc 0.950epoch 3, loss 0.1442, train acc 0.958, test acc 0.961epoch 4, loss 0.1060, train acc 0.969, test acc 0.971epoch 5, loss 0.0807, train acc 0.976, test acc 0.973

MXNET:多層感知機

相關關鍵詞:
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.