From 59945fb8dee604d701a808b08fcb8de0560d8ed6 Mon Sep 17 00:00:00 2001 From: s452693 Date: Sun, 20 Jun 2021 16:56:42 +0200 Subject: [PATCH 1/2] neural_networks --- src/cyfry/1.png | Bin 0 -> 246 bytes src/cyfry/2.png | Bin 0 -> 381 bytes src/cyfry/3.png | Bin 0 -> 577 bytes src/cyfry/4.png | Bin 0 -> 455 bytes src/cyfry/5.png | Bin 0 -> 367 bytes src/cyfry/6.png | Bin 0 -> 346 bytes src/cyfry/7.png | Bin 0 -> 337 bytes src/cyfry/8.png | Bin 0 -> 619 bytes src/cyfry/9.png | Bin 0 -> 541 bytes src/neural_network.py | 150 ++++++++++++++++++++++++++++++++++++++++++ 10 files changed, 150 insertions(+) create mode 100644 src/cyfry/1.png create mode 100644 src/cyfry/2.png create mode 100644 src/cyfry/3.png create mode 100644 src/cyfry/4.png create mode 100644 src/cyfry/5.png create mode 100644 src/cyfry/6.png create mode 100644 src/cyfry/7.png create mode 100644 src/cyfry/8.png create mode 100644 src/cyfry/9.png create mode 100644 src/neural_network.py diff --git a/src/cyfry/1.png b/src/cyfry/1.png new file mode 100644 index 0000000000000000000000000000000000000000..ef3fe8a2ec010d1dfded7ef5449e9f996b025b32 GIT binary patch literal 246 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1|*Ak?@s|zjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBufiR<}hF1enu-ntcF~r0B?X=Up42m2siP{@jqBpR3Zw!jw z)cjW^rd-+hSc>pHR}0?*j8{LqnHAmG*((s!7rkL?nqb@QrK_}hHr^FoHf70_q?z&= zd^5HFKMY#7_CuEQ?O*-Vj9MOO_Xi)Bn0A(#``f*x*RpQfTIOXWY{buiN oFX^7Oj5cA9T{ykqWs-~D&oYxoXBzA00-eR+>FVdQ&MBb@06QgHfB*mh literal 0 HcmV?d00001 diff --git a/src/cyfry/2.png b/src/cyfry/2.png new file mode 100644 index 0000000000000000000000000000000000000000..98f8b7f51d4bb05594db2185694bb1fed9090114 GIT binary patch literal 381 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1|*Ak?@s|zjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBufiR<}hF1engOI0-V~B_M+i4dCn-zH477AN5nY!QuCY zW8DL`U5rn+d&&p&FrItuyx-cd`Gq=*%WIowj&UV5J@-Y+V$;`bz29WP`Q$^_w%m!y z;h~~eYaDv2Vy-K2EiG?~P!>(sZ1c}3TFtBcY{R;2D{beAr4d`&m!1?$s{GOEJke*G zz^2s5GrQJ>C{Nb8v&~nR`Q)KQi8Cg?BAN$ut~RyG)m6S*^e*vLfRUDB%43UVysFN& zq91LGYVYr^oVQY~H`?i3?`DQ%HmrYJua_>B&C}x%tX~})_cO?_Qa#4G+iR;BT zwdpb~e+uqJ9KNGC?Z}Qc&az{UFTU^p+adkogM^{s{*<2Erzopr078wIY5)KL literal 0 HcmV?d00001 diff --git a/src/cyfry/3.png b/src/cyfry/3.png new file mode 100644 index 0000000000000000000000000000000000000000..0b3fa344692486d71f493bf80a90eaab0fd3ec82 GIT binary patch literal 577 zcmV-H0>1r;P)Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!T6%Mz(nN-P!&IwBk+VNe#VYPI@VEWBVu3Wb7HCW$Ag1B}_c~A6K*yHXM zCX-3(mE#lQH;&5}7D^k9hAYOGH;&$3<2d{AHK#%v3oc*(h5NGEtglZ;hOjy0zUZ=w z$K!OC8div)lv}^x8V9Few)_Y~Yh8LeonWPx#1ZP1_K>z@;j|==^1poj532;bRa{vGi!TamC=Tl;PIB~6k9s;a_>A5_7*uF+$MrIWeM zA!)Ateh=5{6*JRt5nR&An*~W3f5cQ(g&+t9pT=gvFbvTH5Cj2^$0OYDcg&1o7QEeV z;5g1NGYkXDvcxX{nx=uaZNW56%uImeG2(K$JZF&AbX~{YbXJ#CQ54wib{KtQ+ctW1 zzHl8u%0l~rBuUU;d*J&%c%Fw@M)0Jo_*-_C>yOJEF5xY>2u;(_qr*+`a5%vEe8z|l x7eOM{zk4v60lRe-d7gLuU5XJKFMjF+JOCLVmvVHoW3vDN002ovPDHLkV1j33yRiTO literal 0 HcmV?d00001 diff --git a/src/cyfry/5.png b/src/cyfry/5.png new file mode 100644 index 0000000000000000000000000000000000000000..107cfd03aac17176bb7b05cd2c2282c2b778a01b GIT binary patch literal 367 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1|*Ak?@s|zjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBufiR<}hF1en1B<7NV~B_M+i4ehn-v7w7XErPd+`f4tFFmb zP1-vc^Ai5PP2_oKlc9Ia(dM`)(3g zULwsnwPc4#lJ2p>C6k0^1oeG>U3^5TR(k6i0a-EELXJZZJ{+8^-N&_hRhL)N0^dFp zEuGVoiZUx!7WZXU=v_Bx&zyB_-D{VXvjiNpeAoZ_sv;|Le)qgBm2CpNI)J?fM^?@bYh%MDGwtw66cc)I$ztaD0e0sxXIhARL7 literal 0 HcmV?d00001 diff --git a/src/cyfry/7.png b/src/cyfry/7.png new file mode 100644 index 0000000000000000000000000000000000000000..8262a0fce08516c47e545283e0478b2d1040e436 GIT binary patch literal 337 zcmeAS@N?(olHy`uVBq!ia0vp^G9b*s1|*Ak?@s|zjKx9jP7LeL$-D$|SkfJR9T^xl z_H+M9WCij$3p^r=85sBufiR<}hF1en@R_HJV~B_M+bORFT?|CrCa-niEOB5DS;w;U zg5$NBJO4%W|Gx3@^h`sQ6VE-0iX%PwKTZ6cyr(g1TB>L4^`(gtOP;fcofWLTKYd&7 zX`AzxK68JsXiKzMwMy%<$;rFPe$0*2B~zpe>T?)bJ~x&p7D%pO_4bu$`L0-e-NZLa zcj@z49~LD}?0KxQw%VyJ%$h?o>g|Et7aN(kFFek7r_C~b+mtO|cJOE>zFszc8t?Rl z*ZI!030fOSyS+J(BC(Zua^e*}r>V!9ttOlj`a5?4-Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!Thd*e!C$a5$u2@fd|_2`$UA zJDrYfJ06dAqtVcc&*!ro#}O*5)oRX7#^W)yzCp2H+G@3^mq&Xn_WM0tE*GltwA*dy z_xn_PQ7VBp&sH-=Ww}PhHif@OyW?XLgY$2nM|?* zU7(ng8il5m=FUR?;LdL7>nWGZ!R>aFW!&y!d7)aZ$~rO_3}Cn0$#yX#YBfOO09;B_ z;D*9*!(|Px#1ZP1_K>z@;j|==^1poj532;bRa{vGi!T72H$NzAG#Zb7wOWl@c2Dn}`NUK~X!w}KaoMc&S#o*V(UBDq*Dmo!pNr4Z4TcDr2~!J{QaIBCD% zf0V0UC!#Hs5r$z&t|NTwv`EvGO8WethY&>(h)o+dn+;{&Ln*`qc03-#ZnuLZNhr(H zYPG1u!wOM0o6V>OAdV#}$q~B__`Xjy9_e4(`FsX(9OX||WHcIq*r=%na6BI2a=Adg zUWeQ5M%fDTk3+(0wJLpMTnHJB(1U%Y;(VhLPp{Y0e=?Du=B f>GRkByaB!ew6ck=U6d-`00000NkvXXu0mjf`%LJI literal 0 HcmV?d00001 diff --git a/src/neural_network.py b/src/neural_network.py new file mode 100644 index 0000000..a474cca --- /dev/null +++ b/src/neural_network.py @@ -0,0 +1,150 @@ + +from emnist import list_datasets +from emnist import extract_test_samples +from emnist import extract_training_samples +import numpy as np +import torch +from torch import nn +from torch import optim +import scipy.special +from matplotlib.pyplot import imshow +import glob +import imageio + +dig_train_images, dig_train_labels = extract_training_samples('digits') +dig_test_images, dig_test_labels = extract_test_samples('digits') +let_train_images, let_train_labels = extract_training_samples('letters') +let_test_images, let_test_labels = extract_test_samples('letters') +#print(dig_train_images.shape) + +#def plotdigit(image): +# img = np.reshape(image, (-1, 28)) +# imshow(img, cmap='Greys', vmin=0, vmax=255) +print(dig_train_images.shape) +""" +dig_train_images = dig_train_images / 255 +dig_test_images = dig_test_images / 255 +let_train_images = let_train_images / 255 +let_test_images = let_test_images / 255 + +dig_train_images = [torch.tensor(image, dtype=torch.float32) for image in dig_train_images] +""" +#print(dig_train_images[0]) +dig_train_images = dig_train_images.reshape(len(dig_train_images),28*28) +d_train = dig_train_images[:1000] +d_labels = dig_train_labels[:1000] + +dig_test_images = dig_test_images.reshape(len(dig_test_images),28*28) +d_test = dig_test_images[:600] +d_labelstest = dig_test_labels[:600] + +print(d_test.shape) +print(d_labelstest) +#print(dig_train_images[0]) +#print(dig_train_images.shape) + + +class NeuralNetwork: + def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade): + self.inodes = inputNodes + self.hnodes = hiddenNodes + self.onodes = outputNodes + + self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5) + self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5) + + #print( 'Matrix1 \n', self.weights) + #print( 'Matrix2 \n', self.hidden) + + self.lr = learningGrade + + self.activationFunction = lambda x: scipy.special.expit(x) + + pass + + def train(self, inputsList, targetsList): + + inputs = np.array(inputsList,ndmin=2).T + targets = np.array(targetsList,ndmin=2).T + + #forward pass + hiddenInputs = np.dot(self.weights, inputs) + 2 + hiddenOutputs = self.activationFunction(hiddenInputs) + + finalInputs = np.dot(self.hidden, hiddenOutputs) + 1 + finalOutputs = self.activationFunction(finalInputs) + + outputErrors = targets - finalOutputs + #print(outputErrors.shape) + x =self.weights.T + #print(x.shape) + hiddenErrors = np.dot(self.hidden.T, outputErrors) + + #print('OutputErrors', outputErrors.shape) + #print('finalOutputs',finalOutputs.shape) + #print(x.shape) + self.hidden += self.lr * np.dot((outputErrors * finalOutputs * (1.0 - finalOutputs)) , np.transpose(hiddenOutputs)) + self.weights += self.lr * np.dot((hiddenErrors * hiddenOutputs * (1.0 - hiddenOutputs)) , np.transpose(inputs)) + + + pass + + def query(self, inputsList): + + inputs = np.array(inputsList, ndmin=2).T + + + hiddenInputs = np.dot(self.weights, inputs) + hiddenOutputs = self.activationFunction(hiddenInputs) + + finalInputs = np.dot(self.hidden, hiddenOutputs) + finalOutputs = self.activationFunction(finalInputs) + + return finalOutputs + + + +""" + def getAccurancy(predictons,Y): + print(predictons,Y) + return np.sum(predictons=Y)/Y.size + + def getPredictions(A2): + return np.argmax(A2,0) +""" + + +#n = NeuralNetwork(inputNodes=3, hiddenNodes=5, outputNodes=2, learningGrade=0.2) +n = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=10, learningGrade=0.1) + +def trainNetwork(n): + epochs = 10 + outputNodes = 10 + for e in range(epochs): + m=0 + print('Epoch', e+1) + + for record in d_train: + inputs = (np.asfarray(record[0:])/255 * 0.99) + 0.01 + #print(inputs.shape) + + targets = np.zeros(outputNodes) + 0.01 + targets[d_labels[m]] = 0.99 + #print(targets) + n.train(inputs,targets) + + m+=1 + pass + pass + + +trainNetwork(n) + +record = d_test[0] +#print('Label', d_labelstest[0]) +inputs = np.asfarray(record[0:])/ 255 * 0.99 + 0.01 +#print(n.query(inputs)) + + + + From 9ca08c7afbbf7f7b058a88b905310b36f3e5a7cf Mon Sep 17 00:00:00 2001 From: s452693 Date: Sun, 20 Jun 2021 17:59:01 +0200 Subject: [PATCH 2/2] neural_networks2 --- src/neural_network.py | 60 ++++++++++++++++++------------------------- 1 file changed, 25 insertions(+), 35 deletions(-) diff --git a/src/neural_network.py b/src/neural_network.py index a474cca..0a7f88d 100644 --- a/src/neural_network.py +++ b/src/neural_network.py @@ -15,20 +15,10 @@ dig_train_images, dig_train_labels = extract_training_samples('digits') dig_test_images, dig_test_labels = extract_test_samples('digits') let_train_images, let_train_labels = extract_training_samples('letters') let_test_images, let_test_labels = extract_test_samples('letters') -#print(dig_train_images.shape) -#def plotdigit(image): -# img = np.reshape(image, (-1, 28)) -# imshow(img, cmap='Greys', vmin=0, vmax=255) -print(dig_train_images.shape) -""" -dig_train_images = dig_train_images / 255 -dig_test_images = dig_test_images / 255 -let_train_images = let_train_images / 255 -let_test_images = let_test_images / 255 -dig_train_images = [torch.tensor(image, dtype=torch.float32) for image in dig_train_images] -""" + + #print(dig_train_images[0]) dig_train_images = dig_train_images.reshape(len(dig_train_images),28*28) d_train = dig_train_images[:1000] @@ -45,13 +35,16 @@ print(d_labelstest) class NeuralNetwork: - def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade): + def __init__(self, inputNodes, hiddenNodes, outputNodes, learningGrade, fileWeight, fileHidden): self.inodes = inputNodes self.hnodes = hiddenNodes self.onodes = outputNodes - self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5) - self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5) + """te pierwsze dwa użyj przy nauce, potem zostaw cały czas te 2""" + #self.weights = (np.random.rand(self.hnodes, self.inodes) - 0.5) + #self.hidden = (np.random.rand(self.onodes, self.hnodes) - 0.5) + self.weights = np.load(fileWeight) + self.hidden = np.load(fileHidden) #print( 'Matrix1 \n', self.weights) #print( 'Matrix2 \n', self.hidden) @@ -89,6 +82,10 @@ class NeuralNetwork: pass + def saveTraining(self, fileWeight, fileHidden): + np.save(fileWeight, self.weights) + np.save(fileHidden, self.hidden) + def query(self, inputsList): inputs = np.array(inputsList, ndmin=2).T @@ -102,29 +99,20 @@ class NeuralNetwork: return finalOutputs - - -""" - def getAccurancy(predictons,Y): - print(predictons,Y) - return np.sum(predictons=Y)/Y.size - - def getPredictions(A2): - return np.argmax(A2,0) -""" - -#n = NeuralNetwork(inputNodes=3, hiddenNodes=5, outputNodes=2, learningGrade=0.2) -n = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=10, learningGrade=0.1) -def trainNetwork(n): +""" dodaj tablicę literek""" +#n = NeuralNetwork(inputNodes=3, hiddenNodes=5, outputNodes=2, learningGrade=0.2) +digitNetwork = NeuralNetwork(inputNodes=784, hiddenNodes=200, outputNodes=10, learningGrade=0.1, fileWeight="Dweights.npy", fileHidden="Dhidden.npy") + +def trainNetwork(n, fWeight, fHidden, trainingSamples): epochs = 10 outputNodes = 10 for e in range(epochs): m=0 print('Epoch', e+1) - for record in d_train: + for record in trainingSamples: inputs = (np.asfarray(record[0:])/255 * 0.99) + 0.01 #print(inputs.shape) @@ -136,15 +124,17 @@ def trainNetwork(n): m+=1 pass pass + n.saveTraining(fileWeight=fWeight, fileHidden=fHidden) -trainNetwork(n) -record = d_test[0] +##################################### ODPALANIE TRAINING +#trainNetwork(digitNetwork, "Dweights.npy", "Dhidden.npy", d_train) + +#record = d_test[0] #print('Label', d_labelstest[0]) -inputs = np.asfarray(record[0:])/ 255 * 0.99 + 0.01 +#inputs = np.asfarray(record[0:])/ 255 * 0.99 + 0.01 #print(n.query(inputs)) - - +#testing \ No newline at end of file