代碼來源:《Python神經(jīng)網(wǎng)絡(luò)編程》
手寫數(shù)據(jù)集下載地址: 1.訓(xùn)練數(shù)據(jù)集 2.測試數(shù)據(jù)集
摘要
本文代碼主要講解基于Python的簡單神經(jīng)網(wǎng)絡(luò)構(gòu)建用于識別手寫數(shù)據(jù)集,類模塊具有通用性,在分析清楚問題后可以加以改動,運(yùn)用于其他方面。
代碼
import
numpy
# scipy.special for the sigmoid function expit()
import
scipy
.
special
import
matplotlib
.
pyplot
as
plt
# neural network class definition
class
neuralNetwork
:
# initialise the neural network
def
__init__
(
self
,
inputnodes
,
hiddennodes
,
outputnodes
,
learningrate
)
:
# set number of nodes in each input, hidden, output layer
self
.
inodes
=
inputnodes
self
.
hnodes
=
hiddennodes
self
.
onodes
=
outputnodes
# link weight matrices, wih and who
# weight inside the arrays are w_i_j, where link is from node i to node j in the next layer
# w11 w21
# w12 w22 etc
# 創(chuàng)建的兩個鏈接權(quán)重矩陣
# self.wih = (numpy.random.rand(self.hnodes, self.inodes) - 0.5)
# self.who = (numpy.random.rand(self.onodes, self.hnodes) - 0.5)
# 正態(tài)分布初始化值,第一個參數(shù)表示正態(tài)分布中心,第二個參數(shù)表示標(biāo)準(zhǔn)方差,第三個參數(shù)表示形狀
self
.
wih
=
numpy
.
random
.
normal
(
0.0
,
pow
(
self
.
hnodes
,
-
0.5
)
,
(
self
.
hnodes
,
self
.
inodes
)
)
self
.
who
=
numpy
.
random
.
normal
(
0.0
,
pow
(
self
.
onodes
,
-
0.5
)
,
(
self
.
onodes
,
self
.
hnodes
)
)
# learning rate
self
.
lr
=
learningrate
# activation function is the sigmoid function
# 相當(dāng)于創(chuàng)建一個函數(shù),函數(shù)接收x,返回scipy.special.expit(x),調(diào)用時使用self.activation_function(...)即可
self
.
activation_function
=
lambda
x
:
scipy
.
special
.
expit
(
x
)
pass
# train the neural network
def
train
(
self
,
inputs_list
,
targets_list
)
:
# convert inputs list to 2d array
inputs
=
numpy
.
array
(
inputs_list
,
ndmin
=
2
)
.
T
targets
=
numpy
.
array
(
targets_list
,
ndmin
=
2
)
.
T
# calculate signals into hidden layer
hidden_inputs
=
numpy
.
dot
(
self
.
wih
,
inputs
)
# calculate the signals emerging from hidden layer
hidden_outputs
=
self
.
activation_function
(
hidden_inputs
)
# calculate signals into final output layer
final_inputs
=
numpy
.
dot
(
self
.
who
,
hidden_outputs
)
# calculate the signals emerging from final output layer
final_outputs
=
self
.
activation_function
(
final_inputs
)
# error is the (target - actual)
output_errors
=
targets
-
final_outputs
# hidden layer error is the output_errors, split by weights, recombined at hidden nodes
hidden_errors
=
numpy
.
dot
(
self
.
who
.
T
,
output_errors
)
# update the weights for the links between the hidden and output layer
self
.
who
+=
self
.
lr
*
numpy
.
dot
(
(
output_errors
*
final_outputs
*
(
1.0
-
final_outputs
)
)
,
numpy
.
transpose
(
hidden_outputs
)
)
# update the weights for the links between the input and hidden layers
self
.
wih
+=
self
.
lr
*
numpy
.
dot
(
(
hidden_errors
*
hidden_outputs
*
(
1.0
-
hidden_outputs
)
)
,
numpy
.
transpose
(
inputs
)
)
pass
# query the network
def
query
(
self
,
inputs_list
)
:
# convert inputs list to 2d array
# ndmin指定數(shù)組最小維度,ndmin=2表示強(qiáng)制將數(shù)組轉(zhuǎn)換為2維
inputs
=
numpy
.
array
(
inputs_list
,
ndmin
=
2
)
.
T
# calculate signals into hidden layer
hidden_inputs
=
numpy
.
dot
(
self
.
wih
,
inputs
)
# calculate the signals emerging from hidden layer
hidden_outputs
=
self
.
activation_function
(
hidden_inputs
)
# calculate signals into final output layer
final_inputs
=
numpy
.
dot
(
self
.
who
,
hidden_outputs
)
# calculate the signals emerging from final output layer
final_outputs
=
self
.
activation_function
(
final_inputs
)
return
final_outputs
# number of input, hidden and output nodes
input_nodes
=
784
# 784個輸入節(jié)點(diǎn)
hidden_nodes
=
200
# 100個隱藏節(jié)點(diǎn)
output_nodes
=
10
# 10個輸出節(jié)點(diǎn)
# learning_rate is 0.5
learning_rate
=
0.1
# create instance of neural network
n
=
neuralNetwork
(
input_nodes
,
hidden_nodes
,
output_nodes
,
learning_rate
)
# load the mnist training data CSV file into a list
training_data_file
=
open
(
"mnist_data/mnist_train.csv"
,
'r'
)
training_data_list
=
training_data_file
.
readlines
(
)
# 讀入文件,將之變?yōu)橐粋€列表,以行為單位
training_data_file
.
close
(
)
# train the neural network
# epochs is the number of times the training data set is used for training
epochs
=
5
for
e
in
range
(
epochs
)
:
# go through all records in the training data set
for
record
in
training_data_list
:
# split the record by the ',' commas
all_values
=
record
.
split
(
','
)
# scale and shift the inputs
inputs
=
(
numpy
.
asfarray
(
all_values
[
1
:
]
)
/
255.0
*
0.99
)
+
0.01
# 1*784,輸入層有784個節(jié)點(diǎn)
# create the target output values (all 0.01, except the desired label which is 0.99)
targets
=
numpy
.
zeros
(
output_nodes
)
+
0.01
# all_values[0] is the target label for this record
targets
[
int
(
all_values
[
0
]
)
]
=
0.99
n
.
train
(
inputs
,
targets
)
pass
pass
# test the neural network
test_data_file
=
open
(
"mnist_data/mnist_test.csv"
,
'r'
)
test_data_list
=
test_data_file
.
readlines
(
)
test_data_file
.
close
(
)
# scorecard for how well the network performs, initially empty
scorecard
=
[
]
# go through all the records in the test data set
for
record
in
test_data_list
:
# split the record by the ',' commas
all_values
=
record
.
split
(
','
)
# correct answer is first value
correct_label
=
int
(
all_values
[
0
]
)
print
(
correct_label
,
"correct label"
)
# scale and shift the inputs
inputs
=
(
numpy
.
asfarray
(
all_values
[
1
:
]
)
/
255.0
*
0.99
)
+
0.01
# query the network
outputs
=
n
.
query
(
inputs
)
# the index of the highest value correesponds to the label
label
=
numpy
.
argmax
(
outputs
)
# 返回數(shù)組中最大索引值
print
(
label
,
"network's answer"
)
# append correct or incorrect to list
if
label
==
correct_label
:
# network's answer matches correct answer, add 1 to scorecard
scorecard
.
append
(
1
)
else
:
# network's answer doesn't match correct answer, add 0 to scorecard
scorecard
.
append
(
0
)
pass
pass
# calculate the performance score, the fraction of correct answers
scorecard_array
=
numpy
.
asarray
(
scorecard
)
# 將輸入轉(zhuǎn)化為數(shù)組
print
(
"performance = "
,
scorecard_array
.
sum
(
)
/
scorecard_array
.
size
)
總結(jié)
- 網(wǎng)絡(luò)訓(xùn)練中矩陣計算起到重要作用,要特別注意兩個矩陣相乘時的矩陣形狀
- sigmoid函數(shù)的定義域與值域
- 反向誤差傳播中計算部分要深入理解,書中的三層網(wǎng)絡(luò)結(jié)構(gòu)中最后一層輸出層在計算式也要使用sigmoid函數(shù)
- 在一定程度上多次使用數(shù)據(jù)集訓(xùn)練網(wǎng)絡(luò)可以提高識別率
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯(lián)系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長非常感激您!手機(jī)微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
