def svm_predict_values(self, model, x, dec_values): k = kernel() if model.param.svm_type in ['ONE_CLASS', 'EPSILON_SVR', 'NU_SVR']: sum = 0.0 for i in range(model.l): sum += model.sv_coef[i] * k.k_function(x, model->SV[i], model.param) sum -= model.rho[0] dec_values = sum if model.param.svm_type is 'ONE_CLASS': if sum > 0: sum = 1 else: sum = -1 return sum else: nr_class = model.nr_class l = model.l kvalue = numpy.zeros(l, dtype = float) for i in range(l): kvalue[i] = k.k_function(x, model.SV[i], model.param) start = numpy.zeros(nr_class, dtype = int) start[0] = 0 for i in range(1, nr_class): start[i] = start[i-1] + model.nSV[i-1] vote = numpy.zeros(nr_class, dtype int) p = 0 for i in range(nr_class): for j in range(i + 1, nr_class): sum = 0 si = start[i] sj = start[j] ci = model.nSV[i] cj = model.nSV[j] coef1 = model.sv_coef[j-1] coef2 = model.sv_coef[i] for k in range(ci): sum += coef1[si + k] * kvalue[si + k] for k in range(cj): sum += coef2[sj + k] * kvalue[sj + k] sum -= model.rho[p] dec_values[p] = sum if dec_values[p] > 0: vote[i] += 1 else: vote[j] += 1 vote_max_idx = 0 for i in range(1, nr_class): if vote[i] > vote[vote_max_idx]: vote_max_idx = i return model.label[vote_max_idx]
def predictor_new(kernel, classifier, x): result = classifier.bias for z_i, x_i, y_i in zip(classifier.support_vectors_multipliers, classifier.support_vectors, classifier.support_vector_labels): result += z_i * y_i * kernel(x_i, x) return np.sign(result).item()
def predictor_new(kernel, classifier, x): result = classifier.bias for z_i, x_i, y_i in zip(classifier.support_vectors_multipliers, classifier.support_vectors, classifier.support_vector_labels): result += z_i * y_i * kernel(x_i, x) return np.sign(result).item()
async def friend_message_listener(message: MessageChain, app: Ariadne, friend: Friend): command = normalize(message) result = kernel(command, str(friend.id)) if result: try: await app.sendFriendMessage(friend, MessageChain.create([result])) except Exception as ex: print('## ', ex) await app.sendFriendMessage(friend, MessageChain.create([Plain(str(ex))]))
def __init__(self, kernel=gaussian_kernel, iterations=100, eta=0.01, lamda=0.05, sigma=1): self.kernel = lambda x, y: kernel(x, y, sigma) self.iterations = iterations self.alpha = None self.eta = eta # Step size for gradient descent self.lamda = lamda # Regularization term
async def group_message_handler(message: MessageChain, app: Ariadne, group: Group, member: Member): command = normalize(message) result = kernel(command, f'{group.id}-{member.id}') if debugMode: print(result) return if result: try: await app.sendGroupMessage(group, MessageChain.create([result])) except Exception as ex: print('## ', ex) await app.sendGroupMessage(group, MessageChain.create([Plain(str(ex))]))
def kpca(X, K, kernel): ''' X is an N*D matrix of data (N points in D dimensions) K is the desired maximum target dimensionality (K <= min{N,D}) kernel is a FUNCTION that computes K(x,z) for some desired kernel and vectors x,z should return a tuple (P, alpha, evals), where P and evals are just like in PCA, and alpha is the vector of alphas (mixing parameters) for the kernel PCA decomposition. ''' ### TODO: YOUR CODE HERE #util.raiseNotDefined() N, D = X.shape # Ker0: Kernel Matrix 0 # KerC: Kernel Matrix Centered # KerM: Kernel Matrix Z = X.copy() Ker0 = zeros((N,N)) for i, x in enumerate( X ): for j, z in enumerate( Z ): Ker0[i,j] = kernel(x,z) # Ker0 = kernel( X, X.T ) H = ones((N,N)) / N KerC = Ker0 - dot(H, Ker0) - dot(Ker0, H) + dot( dot(H, Ker0), H ) evals, evecs = eig( KerC ) evals = real( evals ) / N evecs = real( evecs ) # / sqrt( evals ) eorder = argsort( evals )[::-1][:K] evals = evals[ eorder ] alpha = evecs[ eorder ] a_norm = divide(alpha.T, sum(alpha, axis=1) ).T a_norm = divide( a_norm.T, sqrt( evals ) ).T P = dot( KerC, a_norm.T ) return (P, alpha, evals )
def main(): """Main method: read in from three files: main, library and kernel. In the main we find fusion regions and fuse them depending on the type of fusion. Vertical fusion fuses functions vertically removing I/O and is effectively deforestation Horization fusion fuses functions horizatonally effectively allowing for several independent operations to occure on the same hardware in the same kernel. This improve capacity and allows us to leverage concurrency. """ global replacements replacements = dict() if (len(sys.argv) < 3): print "Correct Usage : ./preproc2 <mainfile> <libfile> <kernelfile>" mainfile = sys.argv[1] #"main.cpp" libfile = sys.argv[2] #"img.cpp" kernelfile = sys.argv[3] #"kernels.cl" namespace = "../imagproc-c/lclImage" #handle what the library header file will be temp = libfile.split(".")[0] + ".h" headerFileName = libfile.split(".")[0] + ".h" header = open(temp, "r") lexer = lex.lex() #update types and protected words: global protectedWords protectedWords += additionalProtectedWords #open up the new mainfile temp = mainfile.split(".")[0] + "-out." + mainfile.split(".")[1] mainout = open(temp, "w") #open new libfile temp = libfile.split(".")[0] + "-out." + libfile.split(".")[1] libout = open(temp, "w") #open new kernel file temp = kernelfile.split(".")[0] + "-out." + kernelfile.split(".")[1] kernelout = open(temp, "w") #open new header file temp = libfile.split(".")[0] + "-out.h" headerout = open(temp, "w") #set of some replacements. Basically whenever kernel.cl is referenced in the source code we need to actually reference kernel-out.cl replacements["\"" + libfile.split(".")[0] + ".h" + "\""] = "\"" + libfile.split(".")[0] + "-out.h" + "\"" replacements["\"" + kernelfile + "\""] = "\"" + kernelfile.split( ".")[0] + "-out." + kernelfile.split(".")[1] + "\"" #go through main file state = 0 calls = [] #functions we need to keep track of: #need to write a new init functiono which calls the old one, this bridges the gap initializationFunction = "" #collection of fused calls to be used later fusedfunctions = [] print "Kernel File Analysis" #set up lexer for kernel files lexer = TokenReader(kernelfile, replacements, kernelout) kernels = [] while True: tok = lexer.tw() if not tok: break elif (tok.value == "__kernel"): kernels.append(kernel(lexer)) print "Library Analysis" """" plow through our library to collect library information The key thing here is collect synchronization info Function we want to leverage later (init) are assigned to relevant variables """ state = 0 pre = 0 outfile = libout functions = [] lexer = TokenReader(libfile, replacements, libout) print "opening lib file: ", libfile isInit = False isSyncIn = False isSyncOut = False while True: tok = lexer.tw() if not tok: break elif (tok.type == "PRAGMA"): words = tok.value.split() if ("synchronize" in words): if ("out" in words): isSyncOut = True if ("in" in words): isSyncIn = True if (tok.type == 'TYPEID'): tok2 = lexer.tw() if not tok2: break if (tok2.type == 'ID'): tok3 = lexer.tw() if not tok3: break if (tok3.type == 'LPAREN'): functions.append(function(tok, tok2, lexer)) if (tok2.value == "init"): initializationFunction = functions[-1] functions[-1].isSyncIn = isSyncIn functions[-1].isSyncOut = isSyncOut isSyncIn = False isSyncOut = False print "Main File Analysis and Synthesis" """ State 0 - nothing special look for start fuse, but otherwise print out input State 1 - we are in a fusion region - catalogue called functions and arguments. also look for exit """ lexer = TokenReader(mainfile, replacements, mainout) fusionType = '' while True: tok = lexer.token() if not tok: break if (tok.value in replacements): mainout.write(replacements[tok.value]) elif (tok.type == "PRAGMA"): if (tok.value.split()[1] == "startfuse"): state = 1 print "Starting fusion on line:", tok.lineno fusionType = 'VERTICAL' elif (tok.value.split()[1] == "starthfuse"): state = 1 fusionType = 'HORIZONTAL' print "Starting fusion on line:", tok.lineno elif (tok.value.split()[1] == "endfuse"): state = 0 if (calls): fusedfunctions.append(funfusion(calls, fusionType)) calls = [] print "we now have fused function:", fusedfunctions[ -1], fusedfunctions[-1].type mainout.write(fusedfunctions[-1].fusedcall.__str__()) else: mainout.write(tok.value) elif (state == 1): if (tok.type == 'ID'): tok4 = lexer.token() if (tok4.type == 'LPAREN'): """LOOK A FUNCTION CALL""" call = functionCall(tok) #handle any synchronization requirement. A syncIn must fuse all previous calls, a SyncOut must immediately fuse after the call found = False for fun in functions: if fun.ID.value == call.call.value: found = True if (fun.isSyncIn): if (calls): fusedfunctions.append( funfusion(calls, fusionType)) calls = [] mainout.write( fusedfunctions[-1].fusedcall.__str__()) print "we now have fused function:", fusedfunctions[ len(fusedfunctions) - 1], fusedfunctions[len(fusedfunctions) - 1].type calls.append(call) tok5 = lexer.token() arg = "" while (tok5.type != 'RPAREN'): if (tok5.type == 'COMMA'): #print "arg:",arg calls[-1].addArg(arg) arg = "" else: arg += str(tok5.value) tok5 = lexer.token() if (tok5.type == 'RPAREN'): #print "END:",arg calls[-1].addArg(arg) if (fun.isSyncOut): fusedfunctions.append( funfusion(calls, fusionType)) calls = [] mainout.write( fusedfunctions[-1].fusedcall.__str__()) print "we now have fused function:", fusedfunctions[ len(fusedfunctions) - 1], fusedfunctions[len(fusedfunctions) - 1].type break if (not found): print "Error 1: Function: ", call.call.value, " Not found in library file: ", sys.argv[ 2] exit(1) elif (tok.value == "init"): mainout.write("initFusion") else: mainout.write(tok.value) mainout.close() print "library synthesis" fusions = [] setOutput(libout) for fun in fusedfunctions: print "creating fusion:", str(fun), "type: ", fun.type type = fun.type tofuse = [] for call in fun.funs: for f in functions: if (f.ID.value == call.call.value): cp = copy.deepcopy(f) tofuse.append((call, cp)) print f.ID, cp.ID argDict = dict() childfunctions = [] count = 0 for call, fun in tofuse: for i in xrange(len(call.args)): if (call.args[i] not in argDict): argDict[call.args[i]] = "arg_" + str(count) count += 1 fun.replaceArg(i, argDict[call.args[i]]) fun.contaminate() childfunctions.append(fun) print "Performing ", fun.type, " fusion" newfunction = function("void", "NEW", None, childfunctions, type) print newfunction fusions.append(newfunction) #take care of having to parse new kernels by adding a new init function print "Creating new code to parse newely created kernels" for fun in fusions: libout.write("cl_kernel " + fun.newKernel + ";\n") libout.write(str(fun)) #create new initialization function based on the previous one libout.write("void initFusion") libout.write(initializationFunction.printArguments()) libout.write("{\n") #call the original with the correct arguments string = "\tinit(" for arg in initializationFunction.args: string += arg[-1].value if (arg != initializationFunction.args[-1]): string += "," else: string += ");\n" string += "cl_int result;\n" for fun in fusions: string += "\t" + fun.newKernel + "= clCreateKernel(" + clProgramName + ",\"" + fun.newKernel.split( "kernel")[0].strip() if (fun.ftype == 'HORIZONTAL'): string += 'h' string += "\",&result);" + "\n" string += "\tcheck(result);\n" string += "}\n" libout.write(string) #add used function definitions to the header file #this assumes you use header guards. If you don't it will add an extraneous #endif at the end print "Updating library header file" for line in header: if (line.strip() != "#endif"): headerout.write(line) for fun in fusions: headerout.write(str(fun.call())) headerout.write("extern cl_kernel " + fun.newKernel + ";\n") headerout.write("void initFusion" + initializationFunction.printArguments() + ";\n") headerout.write("#endif\n") headerout.close() #create the new kernels print "Creating new Kernels" setOutput(kernelout) for fun in fusions: tofuse = [] argDict = dict() count = 0 ftype = fun.ftype #print fun.kernelInvocations[-1].args[-1] newArg = Statement(None, -1, 'OTHER') newArg.tokens = [] newArg.tokens.append(makeToken("const int", 'TYPEID')) newArg.tokens.append(makeToken("newSize", 'ID')) cid = 0 for clkernel in fun.kernelInvocations[:-1]: for k in kernels: if (clkernel.kernel.children[1].tokens[0].value.split("_")[0] == k.ID.value): tofuse.append(copy.deepcopy(k)) for i in range(len(clkernel.args)): type, value = clkernel.args[i] value = str(value) if value not in argDict: argDict[value] = "arg_" + str(count) count += 1 tofuse[-1].replaceArg(i, argDict[value]) tofuse[-1].contaminate() tofuse[-1].cid = str(cid) cid += 1 if (ftype == 'HORIZONTAL'): tofuse[-1].args.append(newArg.tokens) #print tofuse[-1] tree = fusionTree(tofuse, ftype) #print tree.node.call kernelout.write(str(tree.node.call))
def __init__(self, kernel=gaussian_kernel,lamda=0.01,sigma=1): self.kernel = lambda x,y: kernel(x,y,sigma) self.alpha = None self.lamda = lamda # Regularization term
j = indices[i] output[j] = x return output space = Space() for i in range(-5, 5): x = gaussian(i) space.append(x) print(space) space.sort() kernel = Kernel('mean') kernel1 = Kernel('max') kernel2 = Kernel('min') x = space y = kernel(x, 2, 2) a = space.angles display(space) print(x, '\n', a) # space1 = Space(y) # x1 = space1 # a1 = space1.angles # print('\n', x, '\n', a) # print('\n', x1, '\n', a1)
def main(): """Main method: read in from three files: main, library and kernel. In the main we find fusion regions and fuse them depending on the type of fusion. Vertical fusion fuses functions vertically removing I/O and is effectively deforestation Horization fusion fuses functions horizatonally effectively allowing for several independent operations to occure on the same hardware in the same kernel. This improve capacity and allows us to leverage concurrency. """ global replacements replacements = dict() if(len(sys.argv) < 3) : print "Correct Usage : ./preproc2 <mainfile> <libfile> <kernelfile>" mainfile = sys.argv[1] #"main.cpp" libfile = sys.argv[2] #"img.cpp" kernelfile = sys.argv[3] #"kernels.cl" namespace = "../imagproc-c/lclImage" #handle what the library header file will be temp = libfile.split(".")[0] + ".h" headerFileName = libfile.split(".")[0] + ".h" header = open(temp,"r") lexer = lex.lex() #update types and protected words: global protectedWords protectedWords += additionalProtectedWords #open up the new mainfile temp = mainfile.split(".")[0] + "-out." + mainfile.split(".")[1] mainout = open(temp,"w") #open new libfile temp = libfile.split(".")[0] + "-out." + libfile.split(".")[1] libout = open(temp,"w") #open new kernel file temp = kernelfile.split(".")[0] + "-out." + kernelfile.split(".")[1] kernelout = open(temp,"w") #open new header file temp= libfile.split(".")[0] + "-out.h" headerout=open(temp,"w") #set of some replacements. Basically whenever kernel.cl is referenced in the source code we need to actually reference kernel-out.cl replacements["\""+libfile.split(".")[0] + ".h" + "\""] = "\""+libfile.split(".")[0] + "-out.h" + "\"" replacements["\""+kernelfile+"\""] = "\""+ kernelfile.split(".")[0] + "-out." + kernelfile.split(".")[1] + "\"" #go through main file state = 0 calls = [] #functions we need to keep track of: #need to write a new init functiono which calls the old one, this bridges the gap initializationFunction = "" #collection of fused calls to be used later fusedfunctions = [] print "Kernel File Analysis" #set up lexer for kernel files lexer = TokenReader(kernelfile,replacements,kernelout) kernels = [] while True: tok = lexer.tw() if not tok: break elif(tok.value == "__kernel"): kernels.append(kernel(lexer)) print "Library Analysis" """" plow through our library to collect library information The key thing here is collect synchronization info Function we want to leverage later (init) are assigned to relevant variables """ state = 0 pre = 0 outfile = libout functions = [] lexer = TokenReader(libfile,replacements,libout) print "opening lib file: ", libfile isInit = False isSyncIn = False isSyncOut = False while True: tok = lexer.tw() if not tok: break elif(tok.type == "PRAGMA"): words = tok.value.split() if("synchronize" in words): if("out" in words): isSyncOut = True if("in" in words): isSyncIn = True if(tok.type == 'TYPEID'): tok2 = lexer.tw() if not tok2: break if(tok2.type == 'ID'): tok3 = lexer.tw() if not tok3: break if(tok3.type == 'LPAREN'): functions.append(function(tok,tok2,lexer)) if(tok2.value == "init"): initializationFunction = functions[-1] functions[-1].isSyncIn = isSyncIn functions[-1].isSyncOut = isSyncOut isSyncIn = False isSyncOut = False print "Main File Analysis and Synthesis" """ State 0 - nothing special look for start fuse, but otherwise print out input State 1 - we are in a fusion region - catalogue called functions and arguments. also look for exit """ lexer = TokenReader(mainfile,replacements,mainout) fusionType = '' while True: tok = lexer.token() if not tok: break if(tok.value in replacements): mainout.write(replacements[tok.value]) elif(tok.type == "PRAGMA"): if(tok.value.split()[1] == "startfuse"): state = 1 print "Starting fusion on line:", tok.lineno fusionType = 'VERTICAL' elif(tok.value.split()[1] == "starthfuse"): state = 1 fusionType = 'HORIZONTAL' print "Starting fusion on line:", tok.lineno elif(tok.value.split()[1] == "endfuse"): state = 0 if(calls): fusedfunctions.append(funfusion(calls,fusionType)) calls = [] print "we now have fused function:", fusedfunctions[-1] , fusedfunctions[-1].type mainout.write(fusedfunctions[-1].fusedcall.__str__()) else: mainout.write(tok.value) elif(state == 1): if(tok.type == 'ID'): tok4 = lexer.token() if(tok4.type == 'LPAREN'): """LOOK A FUNCTION CALL""" call = functionCall(tok) #handle any synchronization requirement. A syncIn must fuse all previous calls, a SyncOut must immediately fuse after the call found = False for fun in functions: if fun.ID.value == call.call.value: found = True if(fun.isSyncIn): if(calls): fusedfunctions.append(funfusion(calls,fusionType)) calls = [] mainout.write(fusedfunctions[-1].fusedcall.__str__()) print "we now have fused function:", fusedfunctions[len(fusedfunctions)-1] , fusedfunctions[len(fusedfunctions)-1].type calls.append(call) tok5 = lexer.token() arg = "" while(tok5.type != 'RPAREN'): if(tok5.type == 'COMMA'): #print "arg:",arg calls[-1].addArg(arg) arg = "" else: arg += str(tok5.value) tok5 = lexer.token() if(tok5.type == 'RPAREN'): #print "END:",arg calls[-1].addArg(arg) if(fun.isSyncOut): fusedfunctions.append(funfusion(calls,fusionType)) calls = [] mainout.write(fusedfunctions[-1].fusedcall.__str__()) print "we now have fused function:", fusedfunctions[len(fusedfunctions)-1] , fusedfunctions[len(fusedfunctions)-1].type break; if(not found): print "Error 1: Function: ", call.call.value , " Not found in library file: ", sys.argv[2] exit(1) elif(tok.value == "init"): mainout.write("initFusion") else: mainout.write(tok.value) mainout.close() print "library synthesis" fusions = [] setOutput(libout) for fun in fusedfunctions: print "creating fusion:",str(fun),"type: ",fun.type type = fun.type tofuse = [] for call in fun.funs: for f in functions: if(f.ID.value == call.call.value): cp = copy.deepcopy(f) tofuse.append((call,cp)) print f.ID,cp.ID argDict = dict() childfunctions = [] count = 0 for call, fun in tofuse: for i in xrange(len(call.args)): if(call.args[i] not in argDict): argDict[call.args[i]] = "arg_" + str(count) count += 1 fun.replaceArg(i,argDict[call.args[i]]) fun.contaminate() childfunctions.append(fun) print "Performing ", fun.type, " fusion" newfunction = function("void","NEW",None,childfunctions,type) print newfunction fusions.append(newfunction) #take care of having to parse new kernels by adding a new init function print "Creating new code to parse newely created kernels" for fun in fusions: libout.write("cl_kernel " + fun.newKernel + ";\n") libout.write(str(fun)) #create new initialization function based on the previous one libout.write("void initFusion") libout.write(initializationFunction.printArguments()) libout.write("{\n") #call the original with the correct arguments string = "\tinit(" for arg in initializationFunction.args: string += arg[-1].value if(arg != initializationFunction.args[-1]): string += "," else: string += ");\n" string += "cl_int result;\n" for fun in fusions: string += "\t" + fun.newKernel + "= clCreateKernel("+ clProgramName +",\""+fun.newKernel.split("kernel")[0].strip() if(fun.ftype == 'HORIZONTAL'): string += 'h' string+="\",&result);" + "\n" string += "\tcheck(result);\n" string +="}\n" libout.write(string) #add used function definitions to the header file #this assumes you use header guards. If you don't it will add an extraneous #endif at the end print "Updating library header file" for line in header: if(line.strip() != "#endif"): headerout.write(line) for fun in fusions: headerout.write(str(fun.call())) headerout.write("extern cl_kernel " + fun.newKernel + ";\n") headerout.write("void initFusion"+initializationFunction.printArguments()+";\n") headerout.write("#endif\n") headerout.close() #create the new kernels print "Creating new Kernels" setOutput(kernelout) for fun in fusions: tofuse = [] argDict = dict() count = 0 ftype = fun.ftype #print fun.kernelInvocations[-1].args[-1] newArg = Statement(None,-1,'OTHER') newArg.tokens = [] newArg.tokens.append(makeToken("const int",'TYPEID')) newArg.tokens.append(makeToken("newSize",'ID')) cid = 0 for clkernel in fun.kernelInvocations[:-1]: for k in kernels: if(clkernel.kernel.children[1].tokens[0].value.split("_")[0] == k.ID.value): tofuse.append(copy.deepcopy(k)) for i in range(len(clkernel.args)): type, value = clkernel.args[i] value = str(value) if value not in argDict: argDict[value] = "arg_" + str(count) count += 1 tofuse[-1].replaceArg(i,argDict[value]) tofuse[-1].contaminate() tofuse[-1].cid = str(cid) cid += 1 if(ftype == 'HORIZONTAL'): tofuse[-1].args.append(newArg.tokens) #print tofuse[-1] tree = fusionTree(tofuse,ftype) #print tree.node.call kernelout.write(str(tree.node.call))
print "\nKernel 2:" print "Name: " + k2.name print "Parms: " print k2.params print "\nKernel 3:" print "Name: " + k3.name print "Parms: " print k3.params # now, generate plots for kernels x = np.arange(-5,5,0.1) y = np.array([2]) # y vals k_gauss = kernel(x,y,k1) k_sigm = kernel(x,y,k2) k_poly = kernel(x,y,k3) # plot Gaussian kernel values # turns on Tex rc('text', usetex=True) rc('font', family='serif') fig = plt.figure() ax = fig.gca() ax.plot(x,k_gauss,'b', linewidth=2.5) ax.set_title(r"Gaussian kernel with $\sigma = 1.2$",fontsize=20) fig2 = plt.figure()
def train_kernel_ridge_regression(X, y, kernel, l2reg): n, _ = X.shape K = kernel(X, X) alpha = np.linalg.lstsq(K + l2reg * np.eye(n), y, rcond=-1)[0] return Kernel_Machine(kernel, X, alpha)