Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- import random
- import matplotlib.pyplot as plt
- def generateDataset():
- np.random.seed(120)
- n=100
- #Generate 100 points per class
- classA = np.random.randn(n, 2) * 0.5 + [0, 0]
- classB = np.random.randn(n, 2) * 0.5 + [-2.0, -3.1]
- # concatenate class
- inputs = np.concatenate(
- ( classA , classB )
- )
- #Generate targets (-1 for class A, 1 for class B)
- targets = np.concatenate (
- (np.ones(classA.shape[0]), -np.ones(classB.shape[0])))
- # shuffle dataset
- N = inputs.shape[0]
- permute=list(range(N))
- random.shuffle(permute)
- inputs = inputs [permute, : ]
- targets = targets [permute ]
- ones=np.ones(np.shape(inputs)[0])
- inputs=inputs.T
- # inputs = np.vstack((ones, inputs))
- return np.matrix(inputs), np.matrix(targets.T)
- def generateNonLinearDataset():
- np.random.seed(120)
- n=100
- #Generate 100 points per class
- classA = np. concatenate (
- (
- np.random.randn(int(n/2), 2) * 0.2 + [2.5, 1.8],
- np.random.randn(int(n/2), 2) * 0.2 + [-2.5, -1.8])
- )
- classB = np. concatenate (
- (
- np.random.randn(n, 2) * 0.3 + [0, -0.1],
- )
- )
- # concatenate class
- inputs = np.concatenate(
- ( classA , classB )
- )
- #Generate targets (-1 for class A, 1 for class B)
- targets = np.concatenate (
- (np.ones(classA.shape[0]), -np.ones(classB.shape[0])))
- # shuffle dataset
- N = inputs.shape[0]
- permute=list(range(N))
- random.shuffle(permute)
- inputs = inputs [permute, : ]
- targets = targets [permute ]
- ones=np.ones(np.shape(inputs)[0])
- inputs=inputs.T
- #binputs = np.vstack((ones, inputs))
- return np.matrix(inputs), np.matrix(targets.T)
- def generateDatasetSQW(n, datatype, add_ones = False):
- if datatype == '2D':
- """add_ones is False by default"""
- np.random.seed(120)
- random.seed(123)
- #Generate n points per class
- classA = np.random.randn(n, 2) * 0.5 + [4.0, 4.5]
- classB = np.random.randn(n, 2) * 0.5 + [-3.0, -3]
- # concatenate class
- inputs = np.concatenate(
- ( classA , classB )
- )
- # Generate targets (-1 for class A, 1 for class B)
- targets = np.concatenate (
- (np.ones(classA.shape[0]), -np.ones(classB.shape[0])))
- # shuffle dataset
- N = inputs.shape[0]
- permute = list(range(N))
- random.shuffle(permute)
- inputs = inputs [permute, : ]
- targets = targets [permute ]
- if add_ones:
- # print(inputs)
- # print(np.matrix(inputs.T), inputs.T.shape)
- ones=np.ones(np.shape(inputs)[0])
- print(inputs)
- inputs = inputs.T
- inputs = np.vstack((ones, inputs))
- else:
- inputs=inputs.T
- return np.matrix(inputs), np.matrix(targets.T)
- if datatype == 'encoder':
- np.random.seed(100)
- random.seed(123)
- inputs = - np.ones(n)
- idx_active = random.randrange(len(inputs))
- inputs[idx_active] = 1
- targets = np.copy(inputs)
- # np.random.shuffle(targets)
- # inputs = inputs.T
- return np.matrix(inputs.T) , np.matrix(targets.T)
- if datatype == 'gaussian':
- # x = np.arange(-5, 5.1, 0.5)
- x = np.linspace(-5 ,5 , n)
- # y = np.arange(-5, 5.1, 0.5)
- y = np.linspace(-5 ,5 , n)
- ndata = len(x)
- z = (np.exp(np.multiply(-x, x*0.1)) * np.exp(np.multiply(-y, y*0.1).T)) - 0.5
- fig = plt.figure()
- ax = fig.add_subplot(111, projection='3d')
- ax.plot(x, y, z)
- plt.show()
- targets = np.reshape(z, (1, ndata))
- xx, yy = np.meshgrid (x, y)
- patterns = np.vstack((np.reshape(xx, (1, ndata*ndata)), np.reshape(yy, (1, ndata*ndata))))
- return patterns, targets
- def get_weights(n_nodes):
- """n_nodes: numpy array with values"""
- weights = []
- for layer in range(len(n_nodes) - 1):
- if layer != (len(n_nodes) - 1):
- weights.append(np.matrix(np.random.randn(n_nodes[layer + 1], n_nodes[layer] + 1)*np.sqrt(1/n_nodes[layer])))
- else:
- weights.append(np.matrix(np.random.randn(n_nodes[layer + 1], n_nodes[layer])*np.sqrt(1/n_nodes[layer])))
- return weights
- def forward_pass(X, Ws, bias = False):
- if not bias:
- ones = np.ones(np.shape(X)[1])
- H_star = np.dot(Ws[0], (np.vstack((X, ones))))
- ones = np.ones(np.shape(H_star)[1])
- H = np.vstack((np.divide(2, (1 + np.exp(-H_star)) )-1, ones))
- O_star = np.dot(Ws[1], H)
- O = np.divide(2, (1 + np.exp(-O_star))) - 1
- return O,O_star, H
- else:
- H_star = np.dot(Ws[0], X)
- H = np.vstack(np.divide(2, (1 + np.exp(-H_star)) )- 1, np.ones(np.shape(H_star)[0]))
- O_star = Ws[1] * H
- O = np.divide(2, (1 + np.exp( - O_star)))- 1
- pass
- def backward_pass(O, H, T, weight):
- delta_o = np.multiply((O - T) , np.multiply((1 + O) , (1 - O))) * 0.5
- delta_h = np.multiply((weight.T @ delta_o) , np.multiply((1 + H) , (1 - H)))* 0.5
- delta_h = delta_h[:-1]
- #print(delta_h)
- return delta_h , delta_o
- def weight_update(X, H, delta_h, delta_o, dw, dv, weights, eta, alpha):
- ones = np.ones(np.shape(X)[1])
- dw = np.multiply(dw , alpha) - np.multiply((delta_h * (np.vstack((X, ones))).T) , (1-alpha))
- dv = np.multiply(dv , alpha) - np.multiply((delta_o * H.T) , (1-alpha))
- weights[0] = weights[0] + np.multiply(dw , eta)
- weights[1] = weights[1] + np.multiply(dv , eta)
- return weights, dw, dv
- def model(X, T, weights, n_nodes, epochs, eta, alpha, linear = True):
- for i in range(epochs):
- O,O_star, H = forward_pass(X, weights )
- dh, do, = backward_pass(O, H, T, weights[1])
- if i == 0:
- dw = 0
- dv = 0
- weights, dw, dv = weight_update(X, H, dh, do, dw, dv, weights, eta, alpha)
- return O_star
- def partition(X, fraction):
- breakPoint = int(len(X) * fraction)
- return X[:breakPoint], X[breakPoint:]
- def filterDataset1(inputs,targets,ratio=0.75):
- N = inputs.shape[1]
- idx=random.sample(list(range(N)),int(N*ratio))
- x_train = inputs [ :, idx]
- y_train = targets [:,idx ]
- x_test = inputs.delete(idx)
- y_test = inputs.delete(idx)
- return x_train,y_train, x_test, y_test
- def filterDataset2(inputs,targets,ratio=0.50,filter=1):
- N = inputs.shape[1]
- #print(np.array(targets[0,:])[0])
- #print(list(np.where(np.array(targets[0,:])[0]==filter)[0]))
- idx=random.sample(list((np.where(np.array(targets[0,:])[0]==filter)[0])),int((N/2)*ratio)) #index to be removed
- #random.shuffle(permute)
- #print(idx)
- x_train = inputs [ :, idx]
- y_train = targets [:,idx ]
- x_test = inputs.delete(idx)
- y_test = inputs.delete(idx)
- return x_train,y_train, x_test, y_test
- def filterDataset3(inputs,targets,ratio=0.50,filter=1):
- N = inputs.shape[1]
- #print(np.array(targets[0,:])[0])
- #print(list(np.where(np.array(targets[0,:])[0]==filter)[0]))
- color=list(np.where(np.array(targets[0,:])[0]==filter)[0])
- #print(np.array(inputs[1,color])[0])
- #print(list((np.where(np.array(inputs[1,color])[0]<0)[0])))
- sub_set=list((np.where(np.array(inputs[1,color])[0]<0)[0]))
- idx1=random.sample(sub_set,int(len(sub_set)*0.2)) #index to be removed
- color=list(np.where(np.array(targets[0,:])[0]==filter)[0])
- sub_set=list((np.where(np.array(inputs[1,color])[0]>0)[0]))
- idx2=random.sample(sub_set,int(len(sub_set)*0.8))
- #print(idx)
- idx=np.concatenate((idx1,idx2))
- x_test = inputs [ :, idx]
- y_test = targets [:,idx ]
- x_train = inputs.delete(idx)
- y_train = inputs.delete(idx)
- return inputs,targets
- # TO BE ADDED IN 'model' FOR SIZEING THE NN
- layers = 2
- # function = get_function(x)
- #patterns, targets = generateDatasetSQW(100,"2D")
- patterns,targets=generateNonLinearDataset()
- #X_train,Y_train,X_test,Y_test = filterDataset1(patterns,targets)
- #X_train,X_test = partition(patterns,0.75)
- #Y_train,Y_test = partition(targets,0.75)
- eta = 0.001
- epochs_list = range(50,800,50)
- alpha = 0.9
- plt.figure()
- plt.title("Missclassified points after x epochs")
- for i in range(1,11):
- acc_list=[]
- for epochs in epochs_list:
- nodes = [patterns.shape[0], i, targets.shape[0]]
- weights = get_weights(nodes)
- output=model(patterns, targets, weights, nodes,epochs, eta, alpha, linear = False)
- output=np.array(output)[0]
- accuracy=np.sum(np.where(np.sign(output)==np.array(targets)[0],1,0))
- acc_list.append(200-accuracy)
- plt.plot(epochs_list,acc_list,label="N.Hidden nodes="+str(i))
- #plt.yscale('log')
- plt.xlabel("Epochs")
- plt.ylabel("Missclassified points")
- #plt.xlim((-0.2, 1))
- #plt.ylim((0,2))
- #plt.xticks(np.arange(0, iterations+1, 1))
- plt.legend()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement