import numpy
as np
import scipy
.special
import matplotlib
.pyplot
as plt
class neuralNetwork:
def __init__(self
, inputNodes
, hiddenNodes1
,heddenNodes2
, outputNodes
, learningRate
):
self
.inodes
= inputNodes
self
.hnodes1
= hiddenNodes1
self
.hnodes2
= heddenNodes2
self
.onodes
= outputNodes
self
.lr
= learningRate
self
.ihWeightMatrix
= np
.random
.normal\
(0.0, pow(self
.hnodes1
, -0.5), (self
.hnodes1
, self
.inodes
))
self
.iiWeightMatrix
= np
.random
.normal\
(0.0, pow(self
.hnodes2
, -0.5), (self
.hnodes2
, self
.hnodes1
))
self
.hoWeightMatrix
= np
.random
.normal\
(0.0, pow(self
.onodes
, -0.5), (self
.onodes
, self
.hnodes2
))
self
.activateFunc
= lambda x
: scipy
.special
.expit
(x
)
pass
def train(self
, inputsList
, targetsList
):
inputs
= np
.array
(inputsList
, ndmin
=2).T
hiddenInputs1
= np
.dot
(self
.ihWeightMatrix
, inputs
)
hiddenOutputs1
= self
.activateFunc
(hiddenInputs1
)
hiddenInputs2
= np
.dot
(self
.iiWeightMatrix
, hiddenOutputs1
)
hiddenOutputs2
= self
.activateFunc
(hiddenInputs2
)
finalInputs
= np
.dot
(self
.hoWeightMatrix
, hiddenOutputs2
)
finalOutputs
= self
.activateFunc
(finalInputs
)
targets
= np
.array
(targetsList
, ndmin
=2).T
outputErrors
= targets
- finalOutputs
hiddenErrors2
= np
.dot
(self
.hoWeightMatrix
.T
, outputErrors
)
hiddenErrors1
= np
.dot
(self
.iiWeightMatrix
.T
, hiddenErrors2
)
self
.hoWeightMatrix
+= self
.lr
* np
.dot
((outputErrors
* finalOutputs
* (1 - finalOutputs
)),
np
.transpose
(hiddenOutputs2
))
self
.iiWeightMatrix
+= self
.lr
* np
.dot
((hiddenErrors2
* hiddenOutputs2
* (1 - hiddenOutputs2
)),
np
.transpose
(hiddenOutputs1
))
self
.ihWeightMatrix
+= self
.lr
* np
.dot
((hiddenErrors1
* hiddenOutputs1
* (1 - hiddenOutputs1
)),
np
.transpose
(inputs
))
pass
def query(self
, inputsList
):
inputs
= np
.array
(inputsList
, ndmin
=2).T
hiddenInputs1
= np
.dot
(self
.ihWeightMatrix
, inputs
)
hiddenOutputs1
= self
.activateFunc
(hiddenInputs1
)
hiddenInputs2
= np
.dot
(self
.iiWeightMatrix
, hiddenOutputs1
)
hiddenOutputs2
= self
.activateFunc
(hiddenInputs2
)
finalInputs
= np
.dot
(self
.hoWeightMatrix
, hiddenOutputs2
)
finalOutputs
= self
.activateFunc
(finalInputs
)
return finalOutputs
def MSE(predict
,fact
,n
):
return np
.sum((predict
-fact
)**2)/n
input_nodes
= 784
hidden_nodes1
= 30
hidden_nodes2
= 60
output_nodes
= 10
learning_rate
= 0.05
n
= neuralNetwork
(input_nodes
, hidden_nodes1
,hidden_nodes2
,output_nodes
, learning_rate
)
mnistTrain
=open("mnist_train.csv",'r')
trainList
=mnistTrain
.readlines
()
mnistTrain
.close
()
epochs
=1
for i
in range(epochs
):
for record
in trainList
:
allValues
=record
.split
(',')
reducedInputs
= (np
.asfarray
(allValues
[1:])) / 255 * 0.99 + 0.01
targets
=np
.zeros
(output_nodes
)+0.01
targets
[int(allValues
[0])]=0.99
n
.train
(reducedInputs
,targets
)
pass
pass
mnistTest
=open("mnist_test.csv",'r')
testList
=mnistTest
.readlines
()
mnistTest
.close
()
score
=[]
sum=0.0
for record
in testList
:
allValues
=record
.split
(',')
expectedResult
=int(allValues
[0])
print(expectedResult
,"正确的结果")
reducedInputs
= (np
.asfarray
(allValues
[1:])) / 255 * 0.99 + 0.01
outputs
=n
.query
(reducedInputs
)
factResult
=np
.argmax
(outputs
)
print(factResult
,"网络输出的结果\n")
sum+=((factResult
-expectedResult
)**2)
if(factResult
==expectedResult
):
score
.append
(1)
else:
score
.append
(0)
pass
pass
scoreArr
=np
.asarray
(score
)
mse
=sum/len(testList
)
print("accuracy:",scoreArr
.sum()/scoreArr
.size
*100,'%')
print("均方误差MSE:",mse
)
i
=int(input("可视化例子请输入要测试的记录编号:"))
allValues
=testList
[i
].split
(',')
print(allValues
[0])
imgArr
=np
.asfarray
(allValues
[1:]).reshape
((28,28))
plt
.imshow
(imgArr
,cmap
='Blues',interpolation
='None')
plt
.show
()
print(n
.query
((np
.asfarray
(allValues
[1:])/ 255 * 0.99) + 0.01))
转载请注明原文地址: https://lol.8miu.com/read-1316.html