分享李沐老師關於深度學習的觀點:1⃣️從實踐的角度入手深度學習可能比單純的研究演算法更好;2⃣️如果想學習深度學習,要只用簡單的資料結構,譬如numpy、ndarray,從0實現乙個深度學習演算法,這樣才能碰到進而解決深度學習中的許多核心問題,也可以更好的理解現在流行的框架;3⃣️從應用的角度,那就直接上現成的框架,結合真實資料不斷練習,調得一手好參;
結合李航《統計學習方法》中的觀點,總結出機器學習(深度學習)的一般**框架,具體看**。
# -*- coding: utf-8 -*-
import d2lzh as d2l
from mxnet import nd
from mxnet import autograd
# data
batch_size =
256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# model
num_inputs, num_hiddens1, num_hiddens2, num_outputs =
784,
256,
256,
10w1 = nd.random.normal(scale=
0.01
, shape=
(num_inputs, num_hiddens1)
)b1 = nd.zeros(num_hiddens1)
w2 = nd.random.normal(scale=
0.01
, shape=
(num_hiddens1, num_hiddens2)
)b2 = nd.zeros(num_hiddens2)
w3 = nd.random.normal(scale=
0.01
, shape=
(num_hiddens2, num_outputs)
)b3 = nd.zeros(num_outputs)
params =
[w1, b1, w2, b2, w3, b3]
for param in params:
param.attach_grad(
)def
relu
(x):
return nd.maximum(x,0)
defsoftmax
(x):
x_exp = x.exp(
) partition = x_exp.
sum(axis=
1, keepdims=
true
)return x_exp / partition
defnet
(x):
x = x.reshape((-
1, num_inputs)
) h1 = relu(nd.dot(x, w1)
+ b1)
h2 = relu(nd.dot(h1, w2)
+ b2)
return softmax(h2)
# strategy
defcross_entropy
(y_hat, y)
:return
-nd.pick(y_hat, y)
.log(
)loss = cross_entropy
# algorithm
defsgd
(params, lr, batch_size)
:for param in params:
param[:]
= param - lr * param.grad / batch_size
# training
defevaluate_accuracy
(data_iter, net)
: acc_sum, n =
0.0,
0for x, y in data_iter:
y = y.astype(
'float32'
) acc_sum +=
(net(x)
.argmax(axis=1)
== y)
.sum()
.asscalar(
) n += y.size
return acc_sum / n
deftrain
(net, train_iter, test_iter, loss, num_epochs, batch_size, params, lr)
:for epoch in
range
(num_epochs)
: train_l_sum, train_acc_sum, n =
0.0,
0.0,
0for x, y in train_iter:
with autograd.record():
y_hat = net(x)
l = loss(y_hat, y)
.sum()
l.backward(
) sgd(params, lr, batch_size)
y = y.astype(
'float32'
) train_l_sum += l.asscalar(
) train_acc_sum +=
(y_hat.argmax(axis=1)
== y)
.sum()
.asscalar(
) n += y.size
test_acc = evaluate_accuracy(test_iter, net)
print
('epoch: %d, loss %.4f, train_acc %.3f, test_acc %.3f'
%(epoch +
1, train_l_sum / n, train_acc_sum / n, test_acc)
)num_epochs, lr =10,
0.3train(net, train_iter, test_iter, loss, num_epochs, batch_size,
params, lr)
# predict
if __name__ ==
'__main__'
:print
('------ok-------'
)
# -*- coding: utf- -*-
import d2lzh as d2l
from mxnet import gluon, init
from mxnet.gluon import loss as gloss, nn
# data
batch_size =
256train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size)
# model
net = nn.sequential(
)net.add(nn.dense(
256, activation=
'relu'),
nn.dense(
256, activation=
'relu'),
nn.dense(10)
)net.initialize(init.normal(sigma=
0.01))
# strategy
loss = gloss.softmaxcrossentropyloss(
)# algorithm
lr =
0.3trainer = gluon.trainer(net.collect_params(),
'sgd',)
# training
num_epochs =
10d2l.train_ch3(net, train_iter, test_iter, loss, num_epochs, batch_size,
none
,none
, trainer)
# predict
if __name__ ==
'__main__'
:print
('-----ok------'
)
三層神經網路
import numpy as np defsigmoid x,deriv false if deriv true return x 1 x x是經過啟用函式後的 return1 1 np.exp x 前向傳播的值x np.array 0,0,1 0,1,1 1,0,1 1,1,1 0,0,1 標籤...
三層神經網路python 簡單的三層神經網路
參照 python神經網路程式設計 寫乙個簡單的三層神經網路 usr bin env python coding utf 8 import numpy sigmoid 函式 import scipy.special 簡單的三層全連線網路,包括乙個輸入層,乙個隱層和乙個輸出層 損失函式用sigmoid...
最簡單的三層神經網路Matlab實現
人工神經網路 artificial neural n etwork,ann 1 具有很廣的應用。理論上來說,採用多層的神經網路能夠逼近任何的連續函式,而不管該函式是否平滑。自從svm出現後,大家都快忘了還有ann這種東東。但是近幾年隨著deep learning技術的興起,大牛們又重新開始關注神經網...