Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from copy import deepcopy
- import numpy as np
- step = 0.5
- e1 = 1
- w11 = [0.2, 0.3, 1]
- w12 = [0.4, 0.5, 1]
- w13 = [0.7, 0.6, 1]
- w14 = [0.5, 0.9, 1]
- w1 = [w11, w12, w13, w14]
- w2 = [0.2, 0.4, 0.6, 0.8, 1]
- dataset = [
- [0, 0, 1],
- [0, 1, 1],
- [0, 2, 1],
- [0, 3, 1],
- [1, 0, 1],
- [1, 1, 1],
- [1, 2, 1],
- [1, 3, 1],
- [2, 0, 1],
- [2, 1, 1],
- [2, 2, 1],
- [2, 3, 1],
- [3, 0, 1],
- [3, 1, 1],
- [3, 2, 1],
- [3, 3, 1]
- ]
- y_pr_dataset = [1,1,1,1,0,1,0,1,0,0,1,1,0,0,0,1]
- for ii in range(16):
- x = dataset[ii]
- y_pr = y_pr_dataset[ii]
- e1 = 1
- iterations = 0
- while e1 >= 0.1 and iterations < 1000:
- z = np.dot(w1, x)
- y_hid = 1 / (1 + np.exp(-z))
- y_hid = np.append(y_hid, 1)
- z_out = np.dot(y_hid, w2)
- y_out = 1 / (1 + np.exp(-z_out))
- e1 = ((y_pr - y_out) ** 2) / 2
- delta1 = e1 * y_out * (1 - y_out)
- w2_new = []
- for i in range(len(y_hid)):
- w2_new.append(w2[i] - step * delta1 * y_hid[i])
- e2 = [e1 * w for w in w2_new]
- delta2 = [e2[i] * y_hid[i] for i in range(len(e2) - 1) ]
- w1_new = []
- for i in range(len(w1)):
- w11_new = []
- for j in range(len(x)):
- w11_new.append(w1[i][j] - step * delta2[i] * x[j])
- w1_new.append(w11_new)
- w1 = deepcopy(w1_new)
- w2 = deepcopy(w2_new)
- z_out = np.dot(y_hid, w2)
- y_out = 1 / (1 + np.exp(-z_out))
- iterations += 1
- #print(e1)
- for ii in range(16):
- x = dataset[ii]
- y_pr = y_pr_dataset[ii]
- z = np.dot(w1, x)
- y_hid = 1 / (1 + np.exp(-z))
- y_hid = np.append(y_hid, 1)
- z_out = np.dot(y_hid, w2)
- y_out = 1 / (1 + np.exp(-z_out))
- print(y_out)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement