Advertisement
Guest User

Untitled

a guest
Feb 21st, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. from copy import deepcopy
  2. import numpy as np
  3. step = 0.5
  4. e1 = 1
  5. w11 = [0.2, 0.3, 1]
  6. w12 = [0.4, 0.5, 1]
  7. w13 = [0.7, 0.6, 1]
  8. w14 = [0.5, 0.9, 1]
  9. w1 = [w11, w12, w13, w14]
  10. w2 = [0.2, 0.4, 0.6, 0.8, 1]
  11. dataset = [
  12. [0, 0, 1],
  13. [0, 1, 1],
  14. [0, 2, 1],
  15. [0, 3, 1],
  16. [1, 0, 1],
  17. [1, 1, 1],
  18. [1, 2, 1],
  19. [1, 3, 1],
  20. [2, 0, 1],
  21. [2, 1, 1],
  22. [2, 2, 1],
  23. [2, 3, 1],
  24. [3, 0, 1],
  25. [3, 1, 1],
  26. [3, 2, 1],
  27. [3, 3, 1]
  28. ]
  29. y_pr_dataset = [1,1,1,1,0,1,0,1,0,0,1,1,0,0,0,1]
  30. for ii in range(16):
  31. x = dataset[ii]
  32. y_pr = y_pr_dataset[ii]
  33. e1 = 1
  34. iterations = 0
  35. while e1 >= 0.1 and iterations < 1000:
  36. z = np.dot(w1, x)
  37. y_hid = 1 / (1 + np.exp(-z))
  38. y_hid = np.append(y_hid, 1)
  39. z_out = np.dot(y_hid, w2)
  40. y_out = 1 / (1 + np.exp(-z_out))
  41.  
  42. e1 = ((y_pr - y_out) ** 2) / 2
  43. delta1 = e1 * y_out * (1 - y_out)
  44.  
  45. w2_new = []
  46. for i in range(len(y_hid)):
  47. w2_new.append(w2[i] - step * delta1 * y_hid[i])
  48.  
  49. e2 = [e1 * w for w in w2_new]
  50. delta2 = [e2[i] * y_hid[i] for i in range(len(e2) - 1) ]
  51. w1_new = []
  52. for i in range(len(w1)):
  53. w11_new = []
  54. for j in range(len(x)):
  55. w11_new.append(w1[i][j] - step * delta2[i] * x[j])
  56. w1_new.append(w11_new)
  57. w1 = deepcopy(w1_new)
  58. w2 = deepcopy(w2_new)
  59. z_out = np.dot(y_hid, w2)
  60. y_out = 1 / (1 + np.exp(-z_out))
  61. iterations += 1
  62. #print(e1)
  63.  
  64. for ii in range(16):
  65. x = dataset[ii]
  66. y_pr = y_pr_dataset[ii]
  67. z = np.dot(w1, x)
  68. y_hid = 1 / (1 + np.exp(-z))
  69. y_hid = np.append(y_hid, 1)
  70. z_out = np.dot(y_hid, w2)
  71. y_out = 1 / (1 + np.exp(-z_out))
  72. print(y_out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement