def backpropagation(self, theta, nn, X, y, lamb): layersNumb=len(nn['structure']) thetaDelta = [0]*(layersNumb) m=len(X) #calculate matrix of outpit values for all input vectors X hLoc = self.runAll(nn, X).copy() yLoc = np.array(y) thetaLoc = nn['theta'].copy() derFunct = np.vectorize( 'float *x, float *res', 'float z = 1/(1+exp(-x[i])); res[i] = z*(1-z)' ) zLoc = nn['z'].copy() aLoc = nn['a'].copy() for n in range(0, len(X)): delta = [0]*(layersNumb+1) #fill list with zeros delta[len(delta)-1] = (hLoc[n] - yLoc[n]).T #calculate delta of error of output layer delta[len(delta)-1] = delta[len(delta)-1].reshape(1, -1) for i in range(layersNumb-1, 0, -1): if i>1: # we can not calculate delta[0] because we don't have theta[0] (and even we don't need it) z = zLoc[i-1][n] z = np.concatenate( ([[1]], z.reshape((1,)*(2-z.ndim) + z.shape),), axis=1) #add one for correct matrix multiplication delta[i] = np.dot(thetaLoc[i].T, delta[i+1]).reshape(-1, 1) * derFunct(z).T delta[i] = delta[i][1:] #print(thetaDelta[i], delta[i+1].shape, aLoc[i-1][n], '\n') #print(np.dot(thetaLoc[i].T, delta[i+1]).shape, derFunct(z).T.shape, '\n') #print(delta[i+1].shape, aLoc[i-1][n].shape ) thetaDelta[i] = thetaDelta[i] + np.dot(delta[i+1].reshape(-1, 1), aLoc[i-1][n].reshape(1, -1)) #delta[i+1]*aLoc[i-1][n] #exit() for i in range(1, len(thetaDelta)): thetaDelta[i]=thetaDelta[i]/m thetaDelta[i][:,1:]=thetaDelta[i][:,1:]+thetaLoc[i][:,1:]*(lamb/m) #regularization if type(theta) == np.ndarray: return np.asarray(self.unroll(thetaDelta)).reshape(-1) # to work also with fmin_cg return thetaDelta
def costTotal(self, theta, nn, X, y, lamb): m = len(X) #following string is for fmin_cg computaton if type(theta) == np.ndarray: nn['theta'] = self.roll(theta, nn['structure']) y = np.array(copy.deepcopy(y)) hAll = self.runAll(nn, X) #feed forward to obtain output of neural network cost = self.cost(hAll, y) return cost/m+(lamb/(2*m))*self.regul(nn['theta']) #apply regularization
def unroll(self, arr): for i in range(0,len(arr)): arr[i]=np.array(arr[i]) if i==0: res=(arr[i]).ravel().T else: res=np.vstack((res,(arr[i]).ravel().T)) res.shape=(1, len(res)) return res
def run(self, nn, input): z=[0] a=[] a.append(copy.deepcopy(input)) a[0]=np.array(a[0]).T # nx1 vector logFunc = self.logisticFunction() for i in range(1, len(nn['structure'])): a[i-1]=np.vstack(([1], a[i-1])) z.append(np.dot(nn['theta'][i], a[i-1])) a.append(logFunc(z[i])) nn['z'] = z nn['a'] = a return a[len(nn['structure'])-1]
def roll(self, arr, structure): rolled=[arr[0]] shift=1 for i in range(1,len(structure)): print(type(structure[i]), " * ", type(structure[i-1]+1)) exit() temparr=arr[shift:shift+structure[i]*(structure[i-1]+1)].copy() temparr.shape=(structure[i],structure[i-1]+1) rolled.append(np.array(temparr)) #NEED COMPARE WITH MATRIX print(rolled[-1].shape) exit() #DEBUG NOT FIRED shift+=structure[i]*(structure[i-1]+1) return rolled
import mynp as np arr = np.array([ -0.4, 0.000028], dtype=np.np.float32) print(arr) val = np.array([-1.], dtype=np.np.float32) idx = np.array([0, 1], dtype=np.np.int32) arr[idx] = val
import mynp as np arr = np.array([ -0.5, 0.2], dtype=np.np.float32) ids = arr > 0 val = np.array([-1.], dtype=np.np.float32) arr[ids] = val
import mynp as np import neurolab as nl from pyopencl import array arr = np.array([[-7.],[ 7.],[ 7.],[-7.],[-7.]]) print(arr[slice(None, None, None),0])