neuralia.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-"
  3. """
  4. NeuraLiA 2020 - by psy (epsylon@riseup.net)
  5. """
  6. import numpy as np
  7. print(75*"=")
  8. print(" _ _ ____ _ ___ _ ")
  9. print("| \ | | ___ _ _| _ \ __ _| | |_ _| / \ ")
  10. print("| \| |/ _ \ | | | |_) / _` | | | | / _ \ ")
  11. print("| |\ | __/ |_| | _ < (_| | |___ | | / ___ \ ")
  12. print("|_| \_|\___|\__,_|_| \_\__,_|_____|___/_/ \_| by psy")
  13. print(" ")
  14. print(75*"=","\n")
  15. print('"Advanced -SYA (Sigmoid + YFactor_Algorithm)- Neural Network"\n')
  16. print(75*"=")
  17. ########################### PoC ###################################
  18. dataset = np.array(([1, 1], [1, 2], [1, 3], [1, 4]), dtype=float) # training set
  19. y_factor = 0.000003141537462295 # in-time reply Y-factor
  20. y = np.array(([2], [3], [4]), dtype=float) # score results
  21. print("\n + Training Set:\n") # printing output
  22. print(" 1 + 1 = 2")
  23. print(" 1 + 2 = 3")
  24. print(" 1 + 3 = 4")
  25. print("\n + Question:\n")
  26. print(" 1 + 4 = ?")
  27. print("\n + Answer (expected):\n")
  28. print(" 5\n")
  29. print(75*"=")
  30. ########################### PoC ###################################
  31. simulation = input("\nDo you wanna start? (Y/n): ")
  32. if simulation == "n" or simulation == "N":
  33. import sys
  34. sys.exit()
  35. dataset = dataset/np.amax(dataset, axis=0)
  36. y = y/100
  37. X = np.split(dataset, [3])[0]
  38. xPredicted = np.split(dataset, [3])[1]
  39. class Neural_Network(object):
  40. def __init__(self):
  41. self.inputSize = 2
  42. self.hiddenSize = 3
  43. self.outputSize = 1
  44. self.W1 = np.random.randn(self.inputSize, self.hiddenSize)
  45. self.W2 = np.random.randn(self.hiddenSize, self.outputSize)
  46. def forward(self, X):
  47. self.z = np.dot(X, self.W1)
  48. self.z2 = self.sigmoid(self.z)
  49. self.z3 = np.dot(self.z2, self.W2)
  50. o = self.sigmoid(self.z3)
  51. return o
  52. def sigmoid(self, s):
  53. return 1/(1+np.exp(-s))
  54. def sigmoidPrime(self, s):
  55. return s*(1-s)
  56. def backward(self, X, y, o):
  57. self.o_error = y - o
  58. self.o_delta = self.o_error*self.sigmoidPrime(o)
  59. self.z2_error = self.o_delta.dot(self.W2.T)
  60. self.z2_delta = self.z2_error*self.sigmoidPrime(self.z2)
  61. self.W1 += X.T.dot(self.z2_delta)
  62. self.W2 += self.z2.T.dot(self.o_delta)
  63. def train(self, X, y):
  64. o = self.forward(X)
  65. self.backward(X, y, o)
  66. def predict(self):
  67. print("="*75)
  68. total_neurons = self.inputSize + self.hiddenSize + self.outputSize
  69. print("-> NEURONS: ["+str(total_neurons)+"] (Input: ["+str(self.inputSize)+"] | Hidden: ["+str(self.hiddenSize)+"] | Output: ["+str(self.outputSize)+"])")
  70. print("="*75)
  71. print("\n + Input (scaled): \n\n " + str(xPredicted))
  72. print("\n + Prediction (scaled): \n\n " + str(self.forward(xPredicted)))
  73. print("\n + Answer (predicted): \n\n " + str(round(int(self.forward(xPredicted)*100), 2)))
  74. print("\n"+"-"*50+"\n")
  75. NN = Neural_Network()
  76. t = 0
  77. while True:
  78. loss = np.mean(np.square(y - NN.forward(X)))
  79. if loss > y_factor:
  80. t = t + 1
  81. print("="*75)
  82. print("-> ROUNDS (Learning): "+str(t))
  83. print("="*75)
  84. print("\n + Input (scaled): \n\n " + str(X).replace(" ",""))
  85. print("\n + Actual Output: \n\n " + str(y))
  86. print("\n + Predicted Output: \n\n " + str(NN.forward(X)))
  87. print("\n + Loss: \n\n [" + str(loss)+"]\n")
  88. NN.train(X, y)
  89. else:
  90. break
  91. NN.predict()