|
- #% matplotlib inline
-
- import numpy as np
- from sklearn import datasets, linear_model
- from sklearn.metrics import accuracy_score
- import matplotlib.pyplot as plt
-
-
- # define sigmod
- def sigmod(X):
- return 1.0/(1+np.exp(-X))
-
-
- # generate the NN model
- class NN_Model:
- def __init__(self, nodes=None):
- self.epsilon = 0.01 # learning rate
- self.n_epoch = 1000 # iterative number
-
- if not nodes:
- self.nodes = [2, 4, 2] # default nodes size (from input -> output)
- else:
- self.nodes = nodes
-
- def init_weight(self):
- W = []
- B = []
-
- n_layer = len(self.nodes)
- for i in range(n_layer-1):
- w = np.random.randn(self.nodes[i], self.nodes[i+1]) / np.sqrt(self.nodes[i])
- b = np.random.randn(1, self.nodes[i+1])
-
- W.append(w)
- B.append(b)
-
- self.W = W
- self.B = B
-
- def forward(self, X):
- Z = []
- x0 = X
- for i in range(len(self.nodes)-1):
- z = sigmod(np.dot(x0, self.W[i]) + self.B[i])
- x0 = z
-
- Z.append(z)
-
- self.Z = Z
-
- # back-propagation
- def backpropagation(self, X, y, n_epoch=None, epsilon=None):
- if not n_epoch: n_epoch = self.n_epoch
- if not epsilon: epsilon = self.epsilon
-
- self.X = X
- self.Y = y
-
- for i in range(n_epoch):
- # forward to calculate each node's output
- self.forward(X)
-
- self.evaluate()
-
- # calc weights update
- W = self.W
- B = self.B
- Z = self.Z
-
- D = []
- d0 = y
- n_layer = len(self.nodes)
- for j in range(n_layer-1, 0, -1):
- jj = j - 1
- z = self.Z[jj]
-
- if j == n_layer - 1:
- d = z*(1-z)*(d0 - z)
- else:
- d = z*(1-z)*np.dot(d0, W[j].T)
-
- d0 = d
- D.insert(0, d)
-
- # update weights
- for j in range(n_layer-1, 0, -1):
- jj = j - 1
-
- if jj != 0:
- W[jj] += epsilon * np.dot(Z[jj-1].T, D[jj])
- else:
- W[jj] += epsilon * np.dot(X.T, D[jj])
-
- B[jj] += epsilon * np.sum(D[jj], axis=0)
-
- def evaluate(self):
- z = self.Z[-1]
-
- # print loss, accuracy
- L = np.sum((z - self.Y)**2)
-
- y_pred = np.argmax(z, axis=1)
- y_true = np.argmax(self.Y, axis=1)
- acc = accuracy_score(y_true, y_pred)
-
- print("L = %f, acc = %f" % (L, acc))
-
-
-
- # generate sample data
- np.random.seed(0)
- X, y = datasets.make_moons(200, noise=0.20)
-
- # generate nn output target
- t = np.zeros((X.shape[0], 2))
- t[np.where(y==0), 0] = 1
- t[np.where(y==1), 1] = 1
-
- # plot data
- #plt.scatter(X[:, 0], X[:, 1], c=y, cmap=plt.cm.Spectral)
- #plt.show()
-
-
- nn = NN_Model([2, 3, 2])
- nn.init_weight()
- nn.backpropagation(X, t)
|