Created
April 12, 2020 06:16
-
-
Save aaaddress1/b9bb3ede6e2f07aa6db4de43ed1bf4aa to your computer and use it in GitHub Desktop.
neuralNetwork.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rewrite by aaaddress1@chroot.org | |
# refer: github.com/makeyourownneuralnetwork/makeyourownneuralnetwork/blob/master/part2_neural_network_mnist_data.ipynb | |
import matplotlib, numpy, os, pickle | |
def sigmoid(x): | |
return 1 / (1 + numpy.exp(-x)) | |
def saveModel(): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
pickle.dump(wih, open('wih.bin', 'wb')) | |
pickle.dump(who, open('who.bin', 'wb')) | |
def loadModel(): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
wih = pickle.load(open('wih.bin', 'rb')) | |
who = pickle.load(open('who.bin', 'rb')) | |
def testWritingPics(): | |
import matplotlib.pyplot as plt | |
import matplotlib.image as mpimg | |
with open('mnist_test.csv') as fp: | |
testSet = fp.read().splitlines() | |
print(f'[+] len(TestData) = {len(testSet)}') | |
index = int(input('[!] choose a handwriting pics? ')) | |
inputWeight = numpy.asfarray( testSet[index].split(',')[1:] ) / 255. * 0.99 + 0.01 | |
plt.imshow(inputWeight.reshape((28, 28))) | |
plt.show() | |
ans = numpy.argmax(query(inputWeight)) | |
print(f'[+] neural network thinks it\'s {ans} ?') | |
def query(inputWeight): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
inputWeight = numpy.array(inputWeight, ndmin=2).T | |
hiddenOut = sigmoid(numpy.dot(wih, inputWeight)) | |
return sigmoid(numpy.dot(who, hiddenOut)) | |
def train(inputWeight, targetWeight): | |
global learningrate | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
# issue ref: github.com/makeyourownneuralnetwork/makeyourownneuralnetwork/issues/27 | |
inputWeight = numpy.array(inputWeight, ndmin=2).T # transform into (28^2, 1) | |
targetWeight = numpy.array(targetWeight, ndmin=2).T # transform into (10, 1) | |
# hidden layer input & output. | |
hiddenOut = sigmoid(numpy.dot(wih, inputWeight)) | |
# output layer input & output. | |
outlayerOut = sigmoid(numpy.dot(who, hiddenOut)) | |
# update weight for output layer. | |
errOutPredict = targetWeight - outlayerOut | |
who += learningrate * numpy.dot( | |
(errOutPredict * outlayerOut * (1.0 - outlayerOut)), | |
numpy.transpose(hiddenOut) | |
) | |
# update weight for hidden layer. | |
errHiddenPredict = numpy.dot(who.T, errOutPredict) | |
wih += learningrate * numpy.dot( | |
(errHiddenPredict * hiddenOut * (1.0 - hiddenOut)), | |
numpy.transpose(inputWeight) | |
) | |
def main(): | |
global learningrate | |
global trainSet | |
global wih # weight of (input -> hidden) layer ... ( 100, 28x28 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
# parse training data. | |
with open('mnist_train.csv') as fp: | |
trainSet = fp.read().splitlines() | |
assert len( trainSet[0].split(',')[1:] ) == 28**2, '[!] no train, no gain.' | |
# init for neural network. | |
learningrate = 0.15 | |
inputNodes = 28 **2 | |
hiddenNodes = 100 | |
outputNodes = 10 | |
wih = numpy.random.normal(0.0, pow(inputNodes, -0.5), (hiddenNodes, inputNodes)) | |
who = numpy.random.normal(0.0, pow(hiddenNodes, -0.5), (outputNodes, hiddenNodes)) | |
epochos = 1 # training times. | |
for _ in range(epochos): | |
for x, record in enumerate(trainSet): | |
print(f'[+] ({x}/{ len(trainSet) }) : training handwriting -> { record[0] }') | |
inputWeight = (numpy.asfarray( record.split(',')[1: ] ) / 255.0 * 0.99) + 0.01 | |
targetWeight = numpy.zeros(outputNodes) + 0.01 | |
targetWeight[int( record[0] )] = 0.99 | |
train( inputWeight, targetWeight ) | |
saveModel() | |
print('[+] done, we cool?') | |
if __name__ == "__main__": | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# rewrite by aaaddress1@chroot.org | |
# refer: github.com/makeyourownneuralnetwork/makeyourownneuralnetwork/blob/master/part2_neural_network_mnist_data.ipynb | |
import matplotlib, numpy, os, pickle | |
def sigmoid(x): | |
return 1 / (1 + numpy.exp(-x)) | |
def saveModel(): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
pickle.dump(wih, open('wih.bin', 'wb')) | |
pickle.dump(who, open('who.bin', 'wb')) | |
def loadModel(): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
wih = pickle.load(open('wih.bin', 'rb')) | |
who = pickle.load(open('who.bin', 'rb')) | |
def testWritingPics(): | |
import matplotlib.pyplot as plt | |
import matplotlib.image as mpimg | |
with open('mnist_test.csv') as fp: | |
testSet = fp.read().splitlines() | |
print(f'[+] len(TestData) = {len(testSet)}') | |
index = int(input('[!] choose a handwriting pics? ')) | |
inputWeight = numpy.asfarray( testSet[index].split(',')[1:] ) / 255. * 0.99 + 0.01 | |
plt.imshow(inputWeight.reshape((28, 28))) | |
plt.show() | |
ans = numpy.argmax(query(inputWeight)) | |
print(f'[+] neural network thinks it\'s {ans} ?') | |
def query(inputWeight): | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
inputWeight = numpy.array(inputWeight, ndmin=2).T | |
hiddenOut = sigmoid(numpy.dot(wih, inputWeight)) | |
return sigmoid(numpy.dot(who, hiddenOut)) | |
def train(inputWeight, targetWeight): | |
global learningrate | |
global wih # weight of (input -> hidden) layer ... ( 100, 28^2 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
# issue ref: github.com/makeyourownneuralnetwork/makeyourownneuralnetwork/issues/27 | |
inputWeight = numpy.array(inputWeight, ndmin=2).T # transform into (28^2, 1) | |
targetWeight = numpy.array(targetWeight, ndmin=2).T # transform into (10, 1) | |
# hidden layer input & output. | |
hiddenOut = sigmoid(numpy.dot(wih, inputWeight)) | |
# output layer input & output. | |
outlayerOut = sigmoid(numpy.dot(who, hiddenOut)) | |
# update weight for output layer. | |
errOutPredict = targetWeight - outlayerOut | |
who += learningrate * numpy.dot( | |
(errOutPredict * outlayerOut * (1.0 - outlayerOut)), | |
numpy.transpose(hiddenOut) | |
) | |
# update weight for hidden layer. | |
errHiddenPredict = numpy.dot(who.T, errOutPredict) | |
wih += learningrate * numpy.dot( | |
(errHiddenPredict * hiddenOut * (1.0 - hiddenOut)), | |
numpy.transpose(inputWeight) | |
) | |
def main(): | |
global learningrate | |
global trainSet | |
global wih # weight of (input -> hidden) layer ... ( 100, 28x28 ) | |
global who # weight of (hidden -> output) layer ... ( 10, 100 ) | |
# parse training data. | |
with open('mnist_train.csv') as fp: | |
trainSet = fp.read().splitlines() | |
assert len( trainSet[0].split(',')[1:] ) == 28**2, '[!] no train, no gain.' | |
# init for neural network. | |
learningrate = 0.15 | |
inputNodes = 28 **2 | |
hiddenNodes = 100 | |
outputNodes = 10 | |
wih = numpy.random.normal(0.0, pow(inputNodes, -0.5), (hiddenNodes, inputNodes)) | |
who = numpy.random.normal(0.0, pow(hiddenNodes, -0.5), (outputNodes, hiddenNodes)) | |
epochos = 1 # training times. | |
for _ in range(epochos): | |
for x, record in enumerate(trainSet): | |
print(f'[+] ({x}/{ len(trainSet) }) : training handwriting -> { record[0] }') | |
inputWeight = (numpy.asfarray( record.split(',')[1: ] ) / 255.0 * 0.99) + 0.01 | |
targetWeight = numpy.zeros(outputNodes) + 0.01 | |
targetWeight[int( record[0] )] = 0.99 | |
train( inputWeight, targetWeight ) | |
saveModel() | |
print('[+] done, we cool?') | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment