fork download
  1. # neural network v2, 3 years later
  2. import numpy as np
  3. import math as mt
  4. def neuron(weights, inputs, bias):
  5. return (sum(np.multiply(np.array(weights), np.array(inputs)), bias))
  6. def relu(neuron):
  7. return (1/(1+mt.exp(neuron)))
  8. def reluderiv(neuron):
  9. return neuron*(1 - neuron)
  10. connections = []
  11. structure = [2, 3, 1]
  12. for i in structure:
  13. toadd = []
  14. for m in range(i):
  15. toadd.append(m)
  16. toadd.append(i)
  17. for v in range(i):
  18. connections.append(toadd)
  19. print(connections)
  20. traindata = [[[0, 0], [0]], [[1, 1], [0]], [[0, 1], [1]], [[1, 0], [1]]]
  21. history = []
  22. confidence = 0.5
  23. for u in traindata:
  24. layer = u[0]
  25. for f in connections:
  26. last = layer
  27. layer = []
  28. for k in f:
  29. layer.append(relu(neuron(k[0], last, float(k[1]))))
  30. history.append(layer)
  31. print(history)
  32. train = [1, 0] if u[1] == true else [0, 1]
  33. layerarr = np.array(layer)
  34. trainarr = np.array(train)
  35. totalerror = abs(sum(layerarr-trainarr))
  36. totalerrorsquared = sum(np.square(layerarr-trainarr))/2
  37. mse = totalerrorsquared/(len(traindata))
  38. backhist = history.reverse()
  39. backconn = connections.reverse()
  40. for k in backconn:
  41. for i in k:
  42. erroroutderiv = (i - train)
  43. outnetderiv = reluderiv(i)
  44. netweightderiv = backhist[backconn.index(k) + 1][backconn.index(i)]
  45. errorweightderiv = erroroutderiv*outnetderiv*netweightderiv
  46. backconn[backconn.index(k)][backconn.index(i)] += confidence*errorweightderiv
  47. connections = backconn.reverse()
  48. print(connections)
Runtime error #stdin #stdout #stderr 1.85s 50616KB
stdin
Standard input is empty
stdout
[[0, 1, 2], [0, 1, 2], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1, 2, 3], [0, 1]]
stderr
Traceback (most recent call last):
  File "./prog.py", line 29, in <module>
TypeError: 'int' object is not subscriptable