Beispiel #1
0
	def smooth_resize(img, n_rows, n_cols, smooth_type):
		##----CAUTION---
		## THIS FUNCTION DOES NOT UPSCALE ON ONE AXIS AND DOWNSCALE IN THE OTHER
		# smooth_type= gaussian, bilinear interpolation, median or whatever
		# flag: 0 down else up
		if n_rows==img.shape[0]:
			return img
		if n_cols==img.shape[1]:
			return img
		flag = n_rows > img.shape[0]
		ratio_rows = (n_rows)*1.0 / img.shape[0]
		ratio_cols = (n_cols)*1.0 / img.shape[1]

		if flag:
			kernel_size = (math.ceil((ratio_rows + ratio_cols) / 2))*2-1 
		else:
			kernel_size = (math.ceil(math.log(math.ceil((math.pow(ratio_rows,-1) + math.pow(ratio_cols,-1)) / 2),2)))* 2 + 1
		resized_img = Sampling.simple_resize(img, n_rows, n_cols) if flag else Sampling.simple_resize(img, n_rows, n_cols)

		if smooth_type == Sampling.FILTER_MEDIAN:
			return conv.median_filter_nd(resized_img, kernel_size)
		elif smooth_type == Sampling.FILTER_BILINEAR_INTERPOLATION:
			return conv.convolution_nd(resized_img, ker.bilinear_interpolation(kernel_size))
		elif smooth_type == Sampling.FILTER_GAUSSIAN:
			return conv.convolution_nd(resized_img, ker.gaussian(kernel_size))
Beispiel #2
0
def addmoney():
    kernel=Kernel()
    role=chkroleloggedin('adult')
    if request.method=="POST":
        if role != "!!":
            if role:
                child=request.form["child"]
                sqlstr='select * from users'
                cur=conn.execute(sqlstr)
                rows=cur.fetchall()
                nowdollar=kernel.getmoney(child)
                if nowdollar == "D":
                    flash("The child account you entered either is not an valid account or is not a child.")
                    return redirect(url_for("addmoney"))
                else:
                    enckey=EncryptPass()
                    addmoney1=request.form["dollars"]
                    session["add"]=enckey.encrypt(sessionid[session["sessionid"]],str(addmoney1))
                    session["child"]=enckey.encrypt(sessionid[session["sessionid"]],child)
                    session["cm"]=enckey.encrypt(sessionid[session["sessionid"]],nowdollar)
                    session["notes"]=enckey.encrypt(sessionid[session["sessionid"]],request.form["notes"])
                    #print(session["add"])
                    #flash("Success!")
                    return redirect(url_for("add_now"))
            else:
                flash(lang["msg6"])
                return redirect(url_for("home"))
        else:
            return redirect(url_for("logout"))
    else:
        if role != "!!":
            return render_template("nano/add.html",productname=productname,year=year,action="Add")
        else:
            flash(lang["msg7"])
            return redirect(url_for("logout"))
Beispiel #3
0
 def __init__(self,
              name,
              assignable_names,
              kernel_func_rgb,
              kernel_func_of,
              kernel_func_depth,
              kernel_func_concatenate,
              C_mkl,
              C_rgb=None,
              C_of=None,
              C_depth=None,
              C_concatenate=None,
              lam_mkl=None):
     self.name = name
     self.assignable_names = [
         assignable_name.lower() for assignable_name in assignable_names
     ]
     self.kernel_rgb = Kernel(kernel_func_rgb, name, 'rgb')
     self.kernel_of = Kernel(kernel_func_of, name, 'of')
     self.kernel_depth = Kernel(kernel_func_depth, name, 'depth')
     self.kernel_concatenate = Kernel(kernel_func_concatenate, name,
                                      ['(rgb-of)', '(rgb-of-depth)'])
     self.C_mkl = C_mkl
     self.C_rgb = C_rgb if C_rgb is not None else C_mkl
     self.C_of = C_of if C_of is not None else C_mkl
     self.C_depth = C_depth if C_depth is not None else C_mkl
     self.C_concatenate = C_concatenate if C_concatenate is not None else C_mkl
     self.lam_mkl = lam_mkl
Beispiel #4
0
 def test_grid_shape(self):
     """ Make sure the grid shapes of the exec configs are correct. """
     tot_threads = lambda gs, bs: (gs[0] * bs[0], gs[1] * bs[1])
     for case in self.cases:
         space.initialize_space(case['shape'])
         z = Out(case['dtype'])
         fun = Kernel('', ('z', 'out', z.dtype))
         for cfg in fun.exec_configs:
             for ind in range(2):
                 self.assertTrue(cfg['grid_shape'][ind] * \
                                 cfg['block_shape'][ind] >= \
                                 case['shape'][ind+1])
                 self.assertTrue((cfg['grid_shape'][ind]-1) * \
                                 cfg['block_shape'][ind] < \
                                 case['shape'][ind+1])
         # One padded case.
         fun = Kernel('', ('z', 'out', z.dtype), padding=(1, 2, 3, 4))
         pad = [3, 7]
         for cfg in fun.exec_configs:
             for ind in range(2):
                 self.assertTrue(cfg['grid_shape'][ind] * \
                                 (cfg['block_shape'][ind]-pad[ind]) >= \
                                 case['shape'][ind+1])
                 self.assertTrue((cfg['grid_shape'][ind]-1) * \
                                 (cfg['block_shape'][ind]-pad[ind]) < \
                                 case['shape'][ind+1])
Beispiel #5
0
def new_kernels(teacher,
                record,
                kernel_n,
                alpha=opt.co_alpha,
                beta=opt.co_beta,
                gamma=opt.co_gamma):
    start_time = time.time()
    kernels = []
    for i in range(kernel_n):
        kernel = Kernel(teacher.rep, 0.0)
        indices = []
        for j in range(record.n):
            if random.random() < gamma:
                indices.append(j)
        if len(indices) > 0:
            x = [record.x[i] for i in indices]
            indices = torch.tensor(indices,
                                   dtype=torch.long,
                                   device=opt.device)
            y = torch.index_select(record.y, 0, indices)
            kernel.add_batch(x, y)
        ma = 0.0
        for j in range(100):
            ll = kernel.opt_step()
            opt.writer.add_scalar(
                'step_%d/kernel_%d_loglikelihood' % (opt.i, i), ll, j)
            ma = (alpha * ll + (1 - alpha) * ma) if j > 0 else ll
            if j > 5 and abs(ma - ll) < beta:
                break
        kernels.append(kernel)
    opt.writer.add_scalar('compression/kernel_time',
                          time.time() - start_time, opt.i)
    return kernels
class Fib:
    """
    A simple FIB.
    """
    def __init__(self):
        self.routes = {}
        self.kernel = Kernel()

    def put_route(self, rte):
        if self._is_route_different(rte):
            fib_route = FibRoute(rte.prefix, rte.next_hops)
            self.routes[rte.prefix] = fib_route
            self.kernel.put_route(fib_route.prefix, fib_route.next_hops)

    def delete_route(self, prefix):
        del self.routes[prefix]
        self.kernel.delete_route(prefix)

    def _is_route_different(self, rte):
        if rte.prefix not in self.routes:
            return True

        return rte.next_hops != self.routes[rte.prefix].next_hops

    def __str__(self):
        repr_str = ""
        for prefix in self.routes:
            repr_str += "%s\n" % self.routes[prefix]
        return repr_str

    def __repr__(self):
        return str(self)
Beispiel #7
0
    def train(self, dataSet, C, toler, kernel = kernel_func):
        '''用smo算法求拉格朗日乘算子'''
        Kernel.clearCache()#清空缓存
        smo = SMOAlgorithm(dataSet, C, toler, kernel)
        self.alphas = smo.run()
        self.kernel = smo.kernel
        self.dataSet = dataSet

        #计算b的值
        b_array = []
        for j in range(len(self.alphas)):
            alphaj = self.alphas[j]
            assert 0 <= alphaj and alphaj <= C
            #先则0 < alpha < c的正分量
            if alphaj == 0 or alphaj == C:
                continue
            yj = dataSet.getLabel(j)
            b = 0.0
            for i in range(len(self.alphas)):
                alphai = self.alphas[i]
                yi = dataSet.getLabel(i)
                kij = self.kernel.compute(i, j)
                b += alphai * yi * kij
            b = yj - b
            b_array.append(b)
        self.b = numpy.array(b_array).mean()
        return self.alphas, self.b
Beispiel #8
0
def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels):
	phi = calc_feature_vecs(comb.X)
	print phi.size

	# bayes classifier
	(DIMS, N) = phi.size
	w_bayes = co.matrix(1.0, (DIMS, 1))
	pred = w_bayes.trans()*phi[:,num_train:]
	(fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred.trans())
	bayes_auc = metric.auc(fpr, tpr)

	# train one-class svm
	kern = Kernel.get_kernel(phi[:,0:num_train], phi[:,0:num_train])
	ocsvm = OCSVM(kern, C=1.0/(num_train*anom_prob))
	ocsvm.train_dual()
	kern = Kernel.get_kernel(phi, phi)
	(oc_as, foo) = ocsvm.apply_dual(kern[num_train:,ocsvm.get_support_dual()])
	(fpr, tpr, thres) = metric.roc_curve(labels[num_train:], oc_as)
	base_auc = metric.auc(fpr, tpr)
	if (base_auc<0.5):
	    base_auc = 1.0-base_auc

	# train structured anomaly detection
	#sad = StructuredOCSVM(train, C=1.0/(num_train*anom_prob))
	sad = StructuredOCSVM(train, C=1.0/(num_train*0.5))
	(lsol, lats, thres) = sad.train_dc(max_iter=50)
	(pred_vals, pred_lats) = sad.apply(test)
	(fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred_vals)
	auc = metric.auc(fpr, tpr)
	if (auc<0.5):
	    auc = 1.0-auc

	return (auc, base_auc, bayes_auc)
def main():
    print('Starting main...')
    print('Instantiating world...')
    mundo = Mundo()
    print('Done instatiating world')
    k = Kernel()
    k.loop()
    print('Kernel started')
Beispiel #10
0
    def __init__(self, p, sigma=1):
        Kernel.__init__(self, p)
        Freezeable.__init__(
            self,
            'sigma',
        )

        self.sigma = sigma
Beispiel #11
0
    def __init__(self, k1, k2, *args, **kwargs):

        self.k1 = k1
        self.k2 = k2

        assert self.k1.p == self.k2.p

        Kernel.__init__(self, self.k1.p, *args, **kwargs)
        Freezeable.__init__(self, 'k1', 'k2')
Beispiel #12
0
 def __init__(self, features=[], target_value=0.0):
     self.features = copy.deepcopy(features)  # 样本的特征向量
     self.target_value = target_value  # e.g. +1, -1
     self.label = target_value  # e.g. +1, -1, 0, 2, 3, (使用多分类)
     self.alpha_value = 0.0
     self.error_value = 0.0
     self.tolerance_error = 0.001
     self.index = 0  # Outside index number within samples of parent-class
     self.kernel = Kernel(Method.Linear)  # 预设使用线性分割(Linear)
Beispiel #13
0
 def __init__(self, db, ratio_split, lbd, kernel="raw_kernel", k=None, C=1, nkp=False):
     self.n = int(len(db)*ratio_split)
     self.lbd = lbd
     self.k = Kernel(self.lbd,k)
     self.ker = getattr(self.k, kernel)
     self.X, self.y = db[:,0], db[:,1]#shuffle(db[:,0], db[:,1])
     self.X_train, self.X_test, self.y_train, self.y_test = train_test_split(db[:,0], db[:,1], train_size=ratio_split, random_state=42, stratify=db[:,1])
     self.svc = SVC(kernel='precomputed', C=1)
     self.nkp = nkp
Beispiel #14
0
def test_create_kernel():
    _kernel_1 = Kernel(simulated_interfaces=False,
                       log=None,
                       log_id="",
                       table_name="main")
    _kernel_2 = Kernel(simulated_interfaces=False,
                       log=None,
                       log_id="",
                       table_name=3)
Beispiel #15
0
 def get_kernel(self):
     if self._kernel_opt[0] == "rbf":
         return Kernel.gaussian(self._kernel_opt[1])
     elif self._kernel_opt[0] == "linear":
         return Kernel.linear()
     elif self._kernel_opt[0] == "poly":
         return Kernel._polykernel(self._kernel_opt[1], self._kernel_opt[2])
     else:
         return 0
Beispiel #16
0
def login():
    global conn
    stmt = """SELECT count(*) FROM sqlite_master WHERE type='table' AND name='users';"""
    cur=conn.execute(stmt)
    result = cur.fetchone()
    if result[0] == 0:
        flash("Your Server Haven't Been Setup Yet.")
        return redirect(url_for("oobe.start"))
    else:
        #print(chklogin("a","a"))
        #Kernel Module Implemented
        cor=False
        if request.method == "POST":
            #hasher=Hashing()
            #session.permanent = True
            user = request.form["nm"]
            passwd = request.form["pass"]
            kernel=Kernel()
            if user == "" or passwd=="":
                flash(lang["msg12"])
                return redirect(url_for("login"))
            if kernel.checkuser(user,passwd)[0]==True:
                session["user"] = user
                session["role"] = kernel.checkuser(user,passwd)[1][2]
                gensession()
                flash(lang["msg2"])
                return redirect(url_for("home"))
            #sqlstr='select * from users'
            #tmp=conn.execute(sqlstr)
            #print(tmp)
            #rows=tmp.fetchall()
            #print(rows)
            #print(rows[5][0])
            #for row in rows:
                #print('!')
                #if hasher.check(passwd,row[1]) and user == row[0]:
                    #else:
                        #session["user"] = user
                        #session["role"] = row[2]
                        #gensession()
                        #cor=True
                        #flash(lang["msg2"])
                        #return redirect(url_for("home"))
                #else:
                    #pass
            else:
                flash(lang["msg1"])
                return redirect(url_for("login"))

        else:
            if "role" in session:
                user = session["user"]
                flash(lang["msg3"])
                return redirect(url_for("home")) 
            else:
                return render_template("nano/login.html",productname=productname,year=year)
Beispiel #17
0
 def predict(self, Y):
     # build test kernel
     kernel = Kernel.get_kernel(Y, self.X[:, self.svs], self.kernel, self.kparam)
     # kernel = Kernel.get_kernel(Y, self.X, self.kernel, self.kparam)
     # for svdd we need the data norms additionally
     norms = Kernel.get_diag_kernel(Y, self.kernel)
     # number of training examples
     res = self.cTc - 2. * kernel.dot(self.get_support()).T + norms
     # res = self.cTc - 2. * kernel.dot(self.alphas).T + norms
     return res.reshape(Y.shape[1]) - self.radius2
Beispiel #18
0
    def __init__(self, k1, k2, *args, **kwargs):
        self.k1 = k1
        self.k2 = k2
        # params = []
        # for k in [self.k1,self.k2]:
        #     params.extend([v for k,v in k.parameters.items()])

        assert self.k1.p == self.k2.p

        Kernel.__init__(self, self.k1.p, *args, **kwargs)
        Freezeable.__init__(self, 'k1', 'k2')
Beispiel #19
0
    def testDiffuseKernel(self):

        correct_heats, s = parseHeats(TEST_DIFFUSED_SOLN)
        input_heats, input_signs = parseHeats(TEST_INPUT)
        diffuser = Kernel(TEST_KERNEL)
        diffused = diffuser.diffuse(input_heats, reverse=False)
        for (key, val) in diffused.iteritems():
            if val < 1:
                self.assertAlmostEqual(val, correct_heats[key], places=5)
            else:
                self.assertAlmostEqual(val, correct_heats[key], places=3)
Beispiel #20
0
	def testDiffuseKernel(self):

		correct_heats, s = parseHeats(TEST_DIFFUSED_SOLN)	
		input_heats, input_signs = parseHeats(TEST_INPUT)
		diffuser = Kernel(TEST_KERNEL)
		diffused = diffuser.diffuse(input_heats, reverse=False)
		for (key, val) in diffused.iteritems():
			if val < 1:
				self.assertAlmostEqual(val, correct_heats[key], places=5)
			else:
				self.assertAlmostEqual(val, correct_heats[key], places=3)
 def predict(self, Y):
     # build test kernel
     kernel = Kernel.get_kernel(Y, self.X[:, self.svs], self.kernel,
                                self.kparam)
     # kernel = Kernel.get_kernel(Y, self.X, self.kernel, self.kparam)
     # for svdd we need the data norms additionally
     norms = Kernel.get_diag_kernel(Y, self.kernel)
     # number of training examples
     res = self.cTc - 2. * kernel.dot(self.get_support()).T + norms
     # res = self.cTc - 2. * kernel.dot(self.alphas).T + norms
     return res.reshape(Y.shape[1]) - self.radius2
Beispiel #22
0
def test_put_del_route_errors():
    kern = Kernel(simulated_interfaces=False,
                  log=None,
                  log_id="",
                  table_name="main")
    if not kern.platform_supported:
        return
    # Attempt to add route with nonsense next-hop interface
    prefix = make_ip_prefix("99.99.99.99/32")
    nhops = [NextHop(False, "nonsense", None, None)]
    rte = FibRoute(prefix, nhops)
    assert not kern.put_route(rte)
Beispiel #23
0
def example(num_samples=100, num_features=2, grid_size=200, filename="svm.pdf"):
    samples = np.array(np.random.normal(size=num_samples * num_features)
                        .reshape(num_samples, num_features))
    labels = 2 * (samples.sum(axis=1) > 0) - 1.0

    clf = SVM(Kernel.rbf(0.1), 0.1)
    clf.fit(samples, labels)
    plot(clf, samples, labels, grid_size, "svm1.pdf")

    clf = SVM(Kernel.linear(), 0.1)
    clf.fit(samples, labels)
    plot(clf, samples, labels, grid_size, "svm2.pdf")
Beispiel #24
0
def remove_now2():
    #Remove Now!
    kernel=Kernel()
    if request.method == "POST":
        if "add" in session and "child" in session and "cm" in session and "notes" in session:
            #session["add"]
            if chkroleloggedin(None) == "!!":
                return redirect(url_for("logout"))
            enckey=EncryptPass()
            if enckey.decrypt(sessionid[session["sessionid"]],session["step"]) !="2":
                return redirect(url_for('removemoney'))
            else:
                chklogin1=kernel.checkuser(request.form['nm'],request.form['pass'])
                if chklogin1[0]:
                    if chklogin1[1][2] == "child" and request.form['nm'] == enckey.decrypt(sessionid[session["sessionid"]],session["child"]):
                        #PREP THE COMMAND
                        #enckey=EncryptPass()
                        child=enckey.decrypt(sessionid[session["sessionid"]],session["child"])
                        adder=enckey.decrypt(sessionid[session["sessionid"]],session["add"])
                        nowdollar=enckey.decrypt(sessionid[session["sessionid"]],session["cm"])
                        notes=enckey.decrypt(sessionid[session["sessionid"]],session["notes"])
                        popadds()
                        #Remake KEY
                        regen()
                        #cmd='UPDATE users SET "Money" = "'+str(int(nowdollar)-int(adder))+'" WHERE Name="'+child+'"'
                        #curs = conn.cursor()
                        #curs.execute(cmd)
                        #conn.commit()
                        #History Module
                        #Coming Soon :>
                        kernel.removemoney(child,session["user"],session["role"],adder,notes)
                        flash(lang["success"])
                        return redirect(url_for("home"))
                    else:
                        popadds()
                        flash(lang["msg1"]+"/"+lang["msg8"]+"/ Your User is not the specified user.")
                        return redirect(url_for("home"))
                else:
                    flash(lang["msg1"]+"/"+lang["msg8"]+"/ Your User is not the specified user.")
                    popadds()
                    return redirect(url_for("home"))
        else:
            flash("Unknown Error.")
            popadds()
            return redirect(url_for("removemoney"))
    else:
        if "add" in session and "child" in session and "cm" in session and "step" in session:
            flash(lang["msg11"])
            return render_template("nano/login.html",productname=productname,year=year)
        else:
            flash(lang["msg10"])
            popadds()
            return redirect(url_for("logout"))
Beispiel #25
0
    def learn(self, source_data, target_data, kernel=None):
        """Launch learning process."""
        if kernel is None: kernel = Kernel(kernel_str='linear')

        if self.verbose: print('Building kernel matrix.')
        data_matrix  = np.vstack(( source_data.X, target_data.X) )
        label_vector = np.hstack(( source_data.Y, np.zeros(target_data.get_nb_examples()) ))
        
        kernel_matrix = kernel.create_matrix(data_matrix)
        
        alpha_vector = self.learn_on_kernel_matrix(kernel_matrix, label_vector)
        
        return KernelClassifier(kernel, data_matrix, alpha_vector)
Beispiel #26
0
    def handle(self):
        self.kernel = Kernel()
        sock = self.request.makefile(mode='rw')
        self.console = Console(self.kernel, sock, sock)

        self.kernel.start()
        self.console.start()
        try:
            self.kernel.join()
        except KeyboardInterrupt:
            self.kernel.shutdown()
            self.kernel.join(1.0)
            self.console.join(1.0)
Beispiel #27
0
 def __copy_first_boot_files_to_system_image(self):
     log.info('Copy boot files to system image')
     kernel = Kernel(self.boot_image_task.boot_root_directory)
     if kernel.get_kernel():
         log.info('--> boot image kernel as first boot linux.vmx')
         kernel.copy_kernel(
             self.root_dir, '/boot/linux.vmx'
         )
     else:
         raise KiwiDiskBootImageError(
             'No kernel in boot image tree %s found' %
             self.boot_image_task.boot_root_directory
         )
     if self.machine and self.machine.get_domain() == 'dom0':
         if kernel.get_xen_hypervisor():
             log.info('--> boot image Xen hypervisor as xen.gz')
             kernel.copy_xen_hypervisor(
                 self.root_dir, '/boot/xen.gz'
             )
         else:
             raise KiwiDiskBootImageError(
                 'No hypervisor in boot image tree %s found' %
                 self.boot_image_task.boot_root_directory
             )
     log.info('--> initrd archive as first boot initrd.vmx')
     Command.run(
         [
             'mv', self.boot_image_task.initrd_filename,
             self.root_dir + '/boot/initrd.vmx'
         ]
     )
Beispiel #28
0
    def learn(self, source_data, target_data, kernel=None):
        """Launch learning process."""
        if kernel is None: kernel = Kernel(kernel_str='linear')

        if self.verbose: print('Building kernel matrix.')
        data_matrix = np.vstack((source_data.X, target_data.X))
        label_vector = np.hstack(
            (source_data.Y, np.zeros(target_data.get_nb_examples())))

        kernel_matrix = kernel.create_matrix(data_matrix)

        alpha_vector = self.learn_on_kernel_matrix(kernel_matrix, label_vector)

        return KernelClassifier(kernel, data_matrix, alpha_vector)
Beispiel #29
0
 def __create_iso_install_kernel_and_initrd(self):
     boot_path = self.media_dir + '/boot/x86_64/loader'
     Path.create(boot_path)
     kernel = Kernel(self.boot_image_task.boot_root_directory)
     if kernel.get_kernel():
         kernel.copy_kernel(boot_path, '/linux')
     else:
         raise KiwiInstallBootImageError(
             'No kernel in boot image tree %s found' %
             self.boot_image_task.boot_root_directory
         )
     if self.machine and self.machine.get_domain() == 'dom0':
         if kernel.get_xen_hypervisor():
             kernel.copy_xen_hypervisor(boot_path, '/xen.gz')
         else:
             raise KiwiInstallBootImageError(
                 'No hypervisor in boot image tree %s found' %
                 self.boot_image_task.boot_root_directory
             )
     self.boot_image_task.create_initrd(self.mbrid)
     Command.run(
         [
             'mv', self.boot_image_task.initrd_filename,
             boot_path + '/initrd'
         ]
     )
Beispiel #30
0
 def test_init(self):
     """ Just make sure we can initialize the kernel. """
     for case in self.cases:
         # Form data to work on.
         space.initialize_space(case['shape'])
         x_np = np.random.randn(*case['shape']).astype(case['dtype'])
         x = Grid(x_np)
         fun = Kernel('', ('x', 'grid', x.dtype))
         fun = Kernel('', ('x', 'grid', x.dtype), shape_filter='all')
         fun = Kernel('', ('x', 'grid', x.dtype), shape_filter='skinny')
         fun = Kernel('', ('x', 'grid', x.dtype), shape_filter='square')
         self.assertRaises(TypeError, Kernel, '', ('x', 'grid', x.dtype), \
                                         shape_filter1='all')
         self.assertRaises(TypeError, Kernel, '', ('x', 'grid', x.dtype), \
                                         shape_filter='blah')
Beispiel #31
0
class Sample:
    def __init__(self, features=[], target_value=0.0):
        self.features = copy.deepcopy(features)  # 样本的特征向量
        self.target_value = target_value  # e.g. +1, -1
        self.label = target_value  # e.g. +1, -1, 0, 2, 3, (使用多分类)
        self.alpha_value = 0.0
        self.error_value = 0.0
        self.tolerance_error = 0.001
        self.index = 0  # Outside index number within samples of parent-class
        self.kernel = Kernel(Method.Linear)  # 预设使用线性分割(Linear)

    # return BOOL, 检查目前的样本点是否符合 KKT条件
    def is_confom_kkt(self, samples=[], bias=0.0, const_value=0.0):
        sum_x = -bias
        for sample_x in samples:
            if sample_x.alpha_value != 0:
                sum_x += sample_x.alpha_value * sample_x.target_value * self.kernel.calculate(
                    sample_x.features, self.features)

        kkt_value = self.target_value * (sum_x)
        # 进行KKT 条件判断(有3个KKT满足条件)
        alpha_value = self.alpha_value
        tolerance_error = self.tolerance_error
        is_conformed = True
        if alpha_value == 0.0 and (kkt_value + tolerance_error) >= 1.0:
            pass
        elif alpha_value == const_value and (kkt_value -
                                             tolerance_error) <= 1.0:
            pass
        elif alpha_value > 0.0 and alpha_value < const_value and abs(
                kkt_value - 1.0) <= tolerance_error:
            pass
        else:
            is_conformed = False
        return is_conformed
Beispiel #32
0
class Convolute:
    def __init__(self, image_name, kernel_type):
        self.image = cv2.imread(image_name, cv2.IMREAD_GRAYSCALE)
        self.kernel = Kernel(kernel_type)
        self.kernel = self.kernel.getKernel()
        self.kernel_size = 3
        self.type = kernel_type

    def convolution(self):
        padded_image = np.zeros((self.kernel_size - 1 + len(self.image),
                                 self.kernel_size - 1 + len(self.image[0])))
        for x in range(len(self.image)):
            for y in range(len(self.image[0])):
                padded_image[int(self.kernel_size / 2) +
                             x][int(self.kernel_size / 2) +
                                y] = self.image[x][y]

        mod_image = np.zeros((len(self.image), len(self.image[0])))
        for i in range(len(self.image)):
            for j in range(len(self.image[0])):
                val = 0
                for x in range(-1 * int((self.kernel_size - 1) / 2),
                               int((self.kernel_size - 1) / 2) + 1):
                    for y in range(-int((self.kernel_size - 1) / 2),
                                   int((self.kernel_size - 1) / 2) + 1):
                        val += padded_image[x + i][y + j] * self.kernel[
                            x + int(self.kernel_size /
                                    2)][y + int(self.kernel_size / 2)]
                mod_image[i][j] = val
        return mod_image
Beispiel #33
0
 def __init__(self,
              dataset,
              labels,
              C=1.0,
              tolerance=1e-4,
              maxIter=10000,
              kernelTup=('linear', )):
     self.dataset = dataset
     self.labels = labels
     self.C = C
     self.tolerance = tolerance
     self.maxIter = maxIter
     self.kern = Kernel(kernelTup=kernelTup)
     self.m = len(dataset)
     self.alphaArr = np.zeros((self.m, 1))
     self.b = 0
Beispiel #34
0
def build_histograms(data, phi, num_train, bins=2, ord=2):
    # first num_train phis are used for estimating
    # histogram boundaries.
    N = len(data)
    (F, LEN) = data[0].size

    max_phi = np.max(phi[:, :num_train])
    min_phi = np.min(phi[:, :num_train])
    print("Build histograms with {0} bins.".format(bins))
    print(max_phi, min_phi)
    thres = np.linspace(min_phi, max_phi + 1e-8, bins + 1)
    print(max_phi, min_phi)

    hist = co.matrix(0.0, (F * bins, 1))
    phi_hist = co.matrix(0.0, (F * bins, N))
    for i in xrange(N):
        for f in xrange(F):
            phi_hist[0 + f * bins,
                     i] = np.where(np.array(data[i][f, :]) < thres[0])[0].size
            for b in range(1, bins - 1):
                cnt = np.where((np.array(data[i][f, :]) >= thres[b]) & (
                    np.array(data[i][f, :]) < thres[b + 1]))[0].size
                phi_hist[b + f * bins, i] = float(cnt)
            phi_hist[bins - 1 + f * bins, i] = np.where(
                np.array(data[i][f, :]) >= thres[bins - 1])[0].size
        phi_hist[:, i] /= np.linalg.norm(phi_hist[:, i], ord=ord)
        hist += phi_hist[:, i] / float(N)
    print('Histogram:')
    print hist.trans()
    kern = Kernel.get_kernel(phi_hist, phi_hist)
    return kern, phi_hist
Beispiel #35
0
def add_now():
    #Add Now!
    kernel=Kernel()
    if request.method == "POST":
        if "add" in session and "child" in session and "cm" in session and "notes" in session:
            #session["add"]
            if chkroleloggedin(None) == "!!":
                return redirect(url_for("logout"))
            else:
                chklogin1=chklogin(request.form['nm'],request.form['pass'])
                if chklogin1[0]:
                    if chklogin1[2] == "adult":
                        #PREP THE COMMAND
                        enckey=EncryptPass()
                        child=enckey.decrypt(sessionid[session["sessionid"]],session["child"])
                        adder=enckey.decrypt(sessionid[session["sessionid"]],session["add"])
                        nowdollar=enckey.decrypt(sessionid[session["sessionid"]],session["cm"])
                        notes=enckey.decrypt(sessionid[session["sessionid"]],session["notes"])
                        popadds()
                        #cmd='UPDATE users SET "Money" = "'+str(int(nowdollar)+int(adder))+'" WHERE Name="'+child+'"'
                        #curs = conn.cursor()
                        #curs.execute(cmd)
                        #conn.commit()
                        kernel.addmoney(child,session["user"],session["role"],adder,notes)
                        #Remake KEY

                        #History Module
                        #Coming Soon :>
                        regen()
                        flash(lang["success"])
                        return redirect(url_for("home"))
                else:
                    popadds()
                    flash(lang["msg1"]+"/"+lang["msg8"])
                    return redirect(url_for("home"))
        else:
            popadds()
            return redirect(url_for("addmoney"))
    else:
        #Get
        if "add" in session and "child" in session and "cm" in session and "notes" in session:
            flash(lang["msg9"])
            return render_template("nano/login.html",productname=productname,year=year)
        else:
            flash(lang["msg10"])
            return redirect(url_for("logout"))
Beispiel #36
0
 def __create_live_iso_kernel_and_initrd(self):
     boot_path = self.media_dir + '/boot/x86_64/loader'
     Path.create(boot_path)
     kernel = Kernel(self.boot_image_task.boot_root_directory)
     if kernel.get_kernel():
         kernel.copy_kernel(boot_path, '/linux')
     else:
         raise KiwiLiveBootImageError(
             'No kernel in boot image tree %s found' %
             self.boot_image_task.boot_root_directory
         )
     if self.machine and self.machine.get_domain() == 'dom0':
         if kernel.get_xen_hypervisor():
             kernel.copy_xen_hypervisor(boot_path, '/xen.gz')
         else:
             raise KiwiLiveBootImageError(
                 'No hypervisor in boot image tree %s found' %
                 self.boot_image_task.boot_root_directory
             )
     self.boot_image_task.create_initrd(self.mbrid)
     Command.run(
         [
             'mv', self.boot_image_task.initrd_filename,
             boot_path + '/initrd'
         ]
     )
class KernelTest(unittest.TestCase):

    def setUp(self):
        self.IO = Mock()
        self.clock = Mock()
        self.sem = Mock()
        self.kernel = Kernel(1000)
        self.kernel.disc = Mock()
        self.kernel.memory = Mock()
        self.kernel.scheduler = Mock()
        self.kernel.handler = Mock()
        self.kernel.cpu = Mock()
        self.kernel.clock = Mock()
        
    def test_startUp(self):
        self.kernel.startUp()
        verify(self.kernel.clock,times(1)).startUp()

    def test_getProgramasDelDisco(self):
        self.program = Mock()
        when(self.kernel.disc).getProgram("program").thenReturn(self.program)
        self.kernel.getProgramasDelDisco("program")
        verify(self.kernel.disc,times(1)).getProgram("program")
        assert (self.kernel.getProgramasDelDisco("program") == self.program)

    def test_run(self):
        self.kernel.run("program")
        verify(self.kernel.handler,times(1)).newIrq("program")     
Beispiel #38
0
def experiment_anomaly_detection(train, test, comb, num_train, anom_prob, labels):
	# train one-class svm
	phi = calc_feature_vecs(comb.X)
	kern = Kernel.get_kernel(phi[:,0:num_train], phi[:,0:num_train])
	ocsvm = OCSVM(kern, C=1.0/(num_train*anom_prob))
	ocsvm.train_dual()
	kern = Kernel.get_kernel(phi, phi)
	(oc_as, foo) = ocsvm.apply_dual(kern[num_train:,ocsvm.get_support_dual()])
	(fpr, tpr, thres) = metric.roc_curve(labels[num_train:], oc_as)
	base_auc = metric.auc(fpr, tpr)

	# train structured anomaly detection
	sad = StructuredOCSVM(train, C=1.0/(num_train*anom_prob))
	(lsol, lats, thres) = sad.train_dc(max_iter=40)
	(pred_vals, pred_lats) = sad.apply(test)
	(fpr, tpr, thres) = metric.roc_curve(labels[num_train:], pred_vals)
	auc = metric.auc(fpr, tpr)

	return (auc, base_auc)
Beispiel #39
0
    def __init__(self, dataSet, C, toler, kernel_func = kernel_func):
        self.C = C
        self.toler = toler
        self.dataSet = dataSet
        self.row, self.dim = self.dataSet.shape()
        self.alphas = numpy.array([0 for i in range(self.row)], dtype=float)  #格朗日乘算子
        self.b = 0
        self.kernel = Kernel(dataSet, kernel_func) #核函数

        self.EiCache = numpy.array([float('inf') for i in range(self.row)], dtype = float) #初始化Ei缓存,q正无穷
Beispiel #40
0
	def train_dc(self, max_iter=200):
		""" Solve the LatentSVDD optimization problem with a  
		    sequential convex programming/DC-programming
		    approach: 
		    Iteratively, find the most likely configuration of
		    the latent variables and then, optimize for the
		    model parameter using fixed latent states.
		"""
		N = self.sobj.get_num_samples()
		DIMS = self.sobj.get_num_dims()
		
		# intermediate solutions
		# latent variables
		latent = [0]*N

		#sol = 1.0*uniform(DIMS,1)-0.5
		sol = matrix(0.0, (DIMS,1))

		psi = matrix(0.0, (DIMS,N)) # (dim x exm)
		old_psi = matrix(0.0, (DIMS,N)) # (dim x exm)
		threshold = 0

		obj = -1
		iter = 0 

		# terminate if objective function value doesn't change much
		while iter<max_iter and (iter<3 or sum(sum(abs(np.array(psi-old_psi))))>=0.001):
			print('Starting iteration {0}.'.format(iter))
			print(sum(sum(abs(np.array(psi-old_psi)))))
			iter += 1
			old_psi = matrix(psi)
			latent_old = list(latent)


			# 1. linearize
			# for the current solution compute the 
			# most likely latent variable configuration
			for i in range(N):
				# min_z ||sol - Psi(x,z)||^2 = ||sol||^2 + min_z -2<sol,Psi(x,z)> + ||Psi(x,z)||^2
				# Hence => ||sol||^2 - max_z  2<sol,Psi(x,z)> - ||Psi(x,z)||^2
				(foo, latent[i], psi[:,i]) = self.sobj.argmax(sol, i)

			# 2. solve the intermediate convex optimization problem 
			kernel = Kernel.get_kernel(psi,psi)			
			svdd = SVDD(kernel, self.C)
			svdd.train_dual()
			threshold = svdd.get_threshold()
			inds = svdd.get_support_dual()
			alphas = svdd.get_support_dual_values()
			sol = psi[:,inds]*alphas
			#print alphas

		self.sol = sol
		self.latent = latent
		return (sol, latent, threshold)
 def setUp(self):
     self.IO = Mock()
     self.clock = Mock()
     self.sem = Mock()
     self.kernel = Kernel(1000)
     self.kernel.disc = Mock()
     self.kernel.memory = Mock()
     self.kernel.scheduler = Mock()
     self.kernel.handler = Mock()
     self.kernel.cpu = Mock()
     self.kernel.clock = Mock()
Beispiel #42
0
def main(argv):
  import os
  import config
  from interface import InteractiveTerminalInterface, DumbTerminalInterface
  from kernel import Kernel
  try:
    os.ttyname(1)
    terminal = InteractiveTerminalInterface(sys.stdout, config.TERMINAL_CHARSET)
  except OSError:
    terminal = DumbTerminalInterface(sys.stdout, config.TERMINAL_CHARSET)
  kernel = Kernel(terminal)
  try:
    shell = ShalingShell(terminal, kernel)
    if len(argv) < 2:
      shell.cmdloop()
    else:
      (cmd, args) = (argv[1], argv[2:])
      shell.execute(cmd, [ terminal.from_terminal(x) for x in args ])
  finally:
    kernel.close()
  return
Beispiel #43
0
	def __init__(self):
		self.mykernel = Kernel(self, self.debug)
		self.__loadOutput__()
		self.__loadInput__()
		self.mykernel.setInputPlugins(self.input_modules_object)
		self.mykernel.setOutputPlugins(self.output_modules_object)
		if self.debug:
			self.mykernel.sendToDebug(str(self.output_modules_object))
			self.mykernel.sendToDebug(str(self.input_modules_object))

		# Tell the kernel to get started!
		self.mykernel.start()
Beispiel #44
0
 def load(self, name):
     '''加载训练的中间结果'''
     with open('{0}.json'.format(name)) as f:
         json_dict = json.load(f)
     self.b = json_dict['b']
     alphas_list = json_dict['alphas']
     self.alphas = numpy.array(alphas_list)
     from dataset import DigitDataSet
     dataSet = DigitDataSet() #note,可以考虑把dataSet类型信息加入到json信息里,待完善
     dataSet.load('{0}.csv'.format(name))
     self.dataSet = dataSet
     self.kernel = Kernel(dataSet, kernel_func)  #note,可以考虑把kernel类型信息加入到json信息里,待完善
Beispiel #45
0
class Jarvis:

	debug = True
	mykernel = ''

	def __init__(self):
		self.mykernel = Kernel(self, self.debug)
		self.__loadOutput__()
		self.__loadInput__()
		self.mykernel.setInputPlugins(self.input_modules_object)
		self.mykernel.setOutputPlugins(self.output_modules_object)
		if self.debug:
			self.mykernel.sendToDebug(str(self.output_modules_object))
			self.mykernel.sendToDebug(str(self.input_modules_object))

		# Tell the kernel to get started!
		self.mykernel.start()
	
	def __loadInput__(self):
		self.input_modules_object = []
		files = os.listdir(os.getcwd() + "/input")
		for file_to_load in files:
			if (file_to_load.find("__init") == -1 and file_to_load.find('.pyc') < 0):
				exec "from input import " + file_to_load.replace(".py","") + " as first"
				obj = first.Plugin(self.mykernel)
				if obj.getType()['type'] == "input":
					self.input_modules_object.append(obj)

	def __loadOutput__(self):
		# Time to get stuff loading
		self.output_modules_object = []
		# Fetch the files in the output folder
		files = os.listdir(os.getcwd() + "/output")
		for file_to_load in files:
			if (file_to_load.find("__init") == -1 and file_to_load.find('.pyc')<0):
				# We have a file, load it!	
				exec "from output import " + file_to_load.replace(".py","") + " as first"
				obj = first.Plugin(self.mykernel)
				if obj.getType()['type'] == "output":
					self.output_modules_object.append(obj) # Add the object to our list
Beispiel #46
0
 def test_Kernel(self):
     return
     d = DigitDataSet()
     data_path = os.path.join('..','data', 'sample')
     d.load(data_path)
     m, n = d.shape()
     p = 0
     n = 0
     el = 0
     k = Kernel(d, kernel_func)
     for i in range(m):
         for j in range(m):
             v = k.compute(i, j)
             if v == 0:
                 n += 1
             elif v == 1:
                 p +=1
             else:
                 el +=1
     print('p', p)
     print('n', n)
     print('el',el)
Beispiel #47
0
 def __create_pxe_install_kernel_and_initrd(self):
     kernel = Kernel(self.boot_image_task.boot_root_directory)
     if kernel.get_kernel():
         kernel.copy_kernel(self.pxe_dir, "/pxeboot.kernel")
     else:
         raise KiwiInstallBootImageError(
             "No kernel in boot image tree %s found" % self.boot_image_task.boot_root_directory
         )
     if self.machine and self.machine.get_domain() == "dom0":
         if kernel.get_xen_hypervisor():
             kernel.copy_xen_hypervisor(self.pxe_dir, "/pxeboot.xen.gz")
         else:
             raise KiwiInstallBootImageError(
                 "No hypervisor in boot image tree %s found" % self.boot_image_task.boot_root_directory
             )
     self.boot_image_task.create_initrd(self.mbrid)
     Command.run(["mv", self.boot_image_task.initrd_filename, self.pxe_dir + "/pxeboot.initrd.xz"])
Beispiel #48
0
 def __create_iso_install_kernel_and_initrd(self):
     boot_path = self.media_dir + "/boot/x86_64/loader"
     Path.create(boot_path)
     kernel = Kernel(self.boot_image_task.boot_root_directory)
     if kernel.get_kernel():
         kernel.copy_kernel(boot_path, "/linux")
     else:
         raise KiwiInstallBootImageError(
             "No kernel in boot image tree %s found" % self.boot_image_task.boot_root_directory
         )
     if self.machine and self.machine.get_domain() == "dom0":
         if kernel.get_xen_hypervisor():
             kernel.copy_xen_hypervisor(boot_path, "/xen.gz")
         else:
             raise KiwiInstallBootImageError(
                 "No hypervisor in boot image tree %s found" % self.boot_image_task.boot_root_directory
             )
     self.boot_image_task.create_initrd(self.mbrid)
     Command.run(["mv", self.boot_image_task.initrd_filename, boot_path + "/initrd"])
Beispiel #49
0
	def train_dc(self, max_iter=50, hotstart=matrix([])):
		""" Solve the optimization problem with a  
		    sequential convex programming/DC-programming
		    approach: 
		    Iteratively, find the most likely configuration of
		    the latent variables and then, optimize for the
		    model parameter using fixed latent states.
		"""
		N = self.sobj.get_num_samples()
		DIMS = self.sobj.get_num_dims()
		
		# intermediate solutions
		# latent variables
		latent = [0.0]*N

		#setseed(0)
		sol = self.sobj.get_hotstart_sol()
		#sol[0:4] *= 0.01
		if hotstart.size==(DIMS,1):
			print('New hotstart position defined.')
			sol = hotstart

		psi = matrix(0.0, (DIMS,N)) # (dim x exm)
		old_psi = matrix(0.0, (DIMS,N)) # (dim x exm)
		threshold = 0

		obj = -1
		iter = 0 
		allobjs = []

		# terminate if objective function value doesn't change much
		while iter<max_iter and (iter<3 or sum(sum(abs(np.array(psi-old_psi))))>=0.001):
			print('Starting iteration {0}.'.format(iter))
			print(sum(sum(abs(np.array(psi-old_psi)))))
			iter += 1
			old_psi = matrix(psi)
			old_sol = sol
			
			# 1. linearize
			# for the current solution compute the 
			# most likely latent variable configuration
			for i in range(N):
				(foo, latent[i], psi[:,i]) = self.sobj.argmax(sol, i, add_prior=True)
				norm = np.linalg.norm(psi[:,i],2)
				psi[:,i] /= norm
				#if i>10:
				#	(foo, latent[i], psi[:,i]) = self.sobj.argmax(sol,i)
				#else:
				#	psi[:,i] = self.sobj.get_joint_feature_map(i)
				#	latent[i] = self.sobj.y[i]

			# 2. solve the intermediate convex optimization problem 
			psi_star = matrix(psi)
			#psi_star[0:7,:] *= 4.0
			#psi_star[0:3,:] *= 0.01
			#psi_star[0,:] *= 1.2
			#psi_star[2,:] *= 2.4
			
			kernel = Kernel.get_kernel(psi_star, psi_star)
			svm = OCSVM(kernel, self.C)
			svm.train_dual()
			threshold = svm.get_threshold()
			#inds = svm.get_support_dual()
			#alphas = svm.get_support_dual_values()
			#sol = phi[:,inds]*alphas

			#inds = svm.get_support_dual()
			#alphas = svm.get_support_dual_values()
			sol = psi_star*svm.get_alphas()
			print matrix([sol.trans(), old_sol.trans()]).trans()

			# calculate objective
			slacks = [max([0.0, np.single(threshold - sol.trans()*psi[:,i]) ]) for i in xrange(N)]
			obj = 0.5*np.single(sol.trans()*sol) - np.single(threshold) + self.C*sum(slacks)
			print("Iter {0}: Values (Threshold-Slacks-Objective) = {1}-{2}-{3}".format(int(iter),np.single(threshold),np.single(sum(slacks)),np.single(obj)))
			allobjs.append(float(np.single(obj)))

		print '+++++++++'
		print threshold
		print slacks
		print obj
		print '+++++++++'

		print allobjs
		print(sum(sum(abs(np.array(psi-old_psi)))))
		print '+++++++++ SAD END'		
		self.sol = sol
		self.latent = latent
		return (sol, latent, threshold)
Beispiel #50
0
import pylab as pl
from bdd import BDD
from kernel import Kernel
import cvxopt as co


if __name__ == '__main__':
	kparam = 2.1
	ktype = 'rbf'

	# generate raw training data
	Dtrain1 = co.normal(2,100)*0.2
	Dtrain2 = co.normal(2,100)*0.3 + 0.8
	Dtrain = co.matrix([Dtrain1.trans(), Dtrain2.trans()]).trans()
	# build kernel
	kernel = Kernel.get_kernel(Dtrain,Dtrain,ktype,kparam)
	# train bdd
	bdd = BDD(kernel)
	# print bdd
	bdd.train_BDD()


	# generate test data grid
	delta = 0.1
	x = np.arange(-4.0, 4.0, delta)
	y = np.arange(-4.0, 4.0, delta)
	X, Y = np.meshgrid(x, y)    
	(sx,sy) = X.shape
	Xf = np.reshape(X,(1,sx*sy))
	Yf = np.reshape(Y,(1,sx*sy))
	Dtest = np.append(Xf,Yf,axis=0)
Beispiel #51
0
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt

from svdd import SVDD
from kernel import Kernel

if __name__ == '__main__':
	# kernel parameter and type
	kparam = 0.1
	ktype = 'rbf'

	# generate raw training data
	Dtrain = co.normal(2,100)
	# build kernel
	kernel = Kernel.get_kernel(Dtrain,Dtrain,ktype,kparam)
	# train svdd
	svdd = SVDD(kernel,0.9)
	svdd.train_dual()

	# generate test data grid
	delta = 0.1
	x = np.arange(-4.0, 4.0, delta)
	y = np.arange(-4.0, 4.0, delta)
	X, Y = np.meshgrid(x, y)    
	(sx,sy) = X.shape
	Xf = np.reshape(X,(1,sx*sy))
	Yf = np.reshape(Y,(1,sx*sy))
	Dtest = np.append(Xf,Yf,axis=0)
	print(Dtest.shape)
Beispiel #52
0
	plt.figure()
	scores = []
	for i in range(train.samples):
	 	LENS = len(train.y[i])
	 	(anom_score, scores_exm) = train.get_scores(lsol, i, lats[i])
	 	scores.append(anom_score)
	 	plt.plot(range(LENS),scores_exm.trans() + i*8,'-g')

	 	plt.plot(range(LENS),train.y[i].trans() + i*8,'-b')
	 	plt.plot(range(LENS),lats[i].trans() + i*8,'-r')

	 	if i==0:
		 	plt.plot(range(LENS),train.X[i][0,:].trans() + i*8,'-k')


	(fpr, tpr, thres) = metric.roc_curve(label, co.matrix(scores))
	auc = metric.auc(fpr, tpr)
	print auc

	# train one-class svm
	kern = Kernel.get_kernel(phi, phi)
	ocsvm = OCSVM(kern, C=1.0/(EXMS_TRAIN*0.1))
	ocsvm.train_dual()
	(oc_as, foo) = ocsvm.apply_dual(kern[:,ocsvm.get_support_dual()])
	(fpr, tpr, thres) = metric.roc_curve(label[0:EXMS_TRAIN], oc_as)
	base_auc = metric.auc(fpr, tpr)
	print base_auc

	plt.show()

	print('finished')
Beispiel #53
0
	Dtrainu = co.normal(2,N_unl)*0.4
	Dtrainn = co.normal(2,N_neg)*0.3
	Dtrain21 = Dtrainn-1
	Dtrain21[0,:] = Dtrainn[0,:]+1
	Dtrain22 = -Dtrain21

	# 1.3. concatenate training data
	Dtrain = co.matrix([[Dtrainp], [Dtrainu], [Dtrainn+1.0], [Dtrainn-1.0], [Dtrain21], [Dtrain22]])

	# 1.4. build the training kernels:
	# - same training sample for each kernel = they have the same size
	# - each kernel captures a feature representation of the samples Dtrain
	#   e.g., kernel1 is a BOW kernel, kernel2 a Lexical Diversity kernel
	#   here: kernel1 und kernel2 are Gaussian kernels with different shape parameters
	# 	and kernel3 is a simple linear kernel
	kernel1 = Kernel.get_kernel(Dtrain, Dtrain, type='rbf', param=1.0)
	kernel2 = Kernel.get_kernel(Dtrain, Dtrain, type='rbf', param=1.0/50.0)
	kernel3 = Kernel.get_kernel(Dtrain, Dtrain, type='rbf', param=1.0/100.0)
	kernel4 = Kernel.get_kernel(Dtrain, Dtrain, type='linear')

	# MKL: (default) use SSAD
	ad = SSAD([],Dy,1.0,1.0,1.0/(N_unl*0.05),1.0)
	#ad = OCSVM(kernel1,C=0.02)

	# 2. STEP: TRAIN WITH A LIST OF KERNELS 
	ssad = MKLWrapper(ad,[kernel1,kernel2,kernel3,kernel4],Dy,P_NORM)
	ssad.train_dual()

	# 3. TEST THE TRAINING DATA (just because we are curious)
	# 3.1. build the test kernel
	kernel1 = Kernel.get_kernel(Dtrain, Dtrain[:,ssad.get_support_dual()], type='rbf', param=1.0)
Beispiel #54
0
    def create(self):
        log.info('Creating PXE root filesystem image')
        self.filesystem.create()
        self.image = self.filesystem.filename
        if self.compressed:
            log.info('xz compressing root filesystem image')
            compress = Compress(self.image)
            compress.xz()
            self.image = compress.compressed_filename

        log.info('Creating PXE root filesystem MD5 checksum')
        self.filesystem_checksum = self.filesystem.filename + '.md5'
        checksum = Checksum(self.image)
        checksum.md5(self.filesystem_checksum)

        # prepare boot(initrd) root system
        log.info('Creating PXE boot image')
        self.boot_image_task.prepare()

        # export modprobe configuration to boot image
        self.system_setup.export_modprobe_setup(
            self.boot_image_task.boot_root_directory
        )

        # extract kernel from boot(initrd) root system
        kernel = Kernel(self.boot_image_task.boot_root_directory)
        kernel_data = kernel.get_kernel()
        if kernel_data:
            self.kernel_filename = ''.join(
                [self.image_name, '-', kernel_data.version, '.kernel']
            )
            kernel.copy_kernel(
                self.target_dir, self.kernel_filename
            )
        else:
            raise KiwiPxeBootImageError(
                'No kernel in boot image tree %s found' %
                self.boot_image_task.boot_root_directory
            )

        # extract hypervisor from boot(initrd) root system
        if self.machine and self.machine.get_domain() == 'dom0':
            kernel_data = kernel.get_xen_hypervisor()
            if kernel_data:
                self.hypervisor_filename = ''.join(
                    [self.image_name, '-', kernel_data.name]
                )
                kernel.copy_xen_hypervisor(
                    self.target_dir, self.hypervisor_filename
                )
                self.result.add(
                    'xen_hypervisor', self.hypervisor_filename
                )
            else:
                raise KiwiPxeBootImageError(
                    'No hypervisor in boot image tree %s found' %
                    self.boot_image_task.boot_root_directory
                )

        # create initrd for pxe boot
        self.boot_image_task.create_initrd()
        self.result.add(
            'kernel', self.kernel_filename
        )
        self.result.add(
            'initrd', self.boot_image_task.initrd_filename
        )
        self.result.add(
            'filesystem_image', self.image
        )
        self.result.add(
            'filesystem_md5', self.filesystem_checksum
        )

        if self.pxedeploy:
            log.warning(
                'Creation of client config file from pxedeploy not implemented'
            )

        return self.result
Beispiel #55
0
import numpy as np
import pylab as pl
import matplotlib.pyplot as plt

from ocsvm import OCSVM
from kernel import Kernel

if __name__ == '__main__':
	# kernel parameter and type
	kparam = 0.1
	ktype = 'rbf'

	# generate raw training data
	Dtrain = co.normal(2,100)
	# build kernel
	kernel = Kernel.get_kernel(Dtrain,Dtrain,ktype,kparam)
	# train svdd

	svm = OCSVM(kernel,0.1)
	svm.train_dual()

	delta = 0.07
	x = np.arange(-4.0, 4.0, delta)
	y = np.arange(-4.0, 4.0, delta)
	X, Y = np.meshgrid(x, y)
	(sx,sy) = X.shape
	Xf = np.reshape(X,(1,sx*sy))
	Yf = np.reshape(Y,(1,sx*sy))
	Dtest = np.append(Xf,Yf,axis=0)
	print(Dtest.shape)
	print('halloooo')
    parser.add_argument('method', help='Calculation method') 
    parser.add_argument('dimension', type=int, help='Space dimension')
    parser.add_argument('nsample1', type=int, help='Size of the first sample')
    parser.add_argument('nsample2', type=int, help='Size of the second sample')
    parser.add_argument('--p1', type=float, default=1.1, help='Initialization parameter #1')
    parser.add_argument('--p2', type=float, default=1.2, help='Initialization parameter #2')
    parser.add_argument('--p3', type=float, default=1.3, help='Initialization parameter #3')
    parser.add_argument('--warming_time', type=float, default=1., help='Minimal warming time')
    parser.add_argument('--run_time', type=float, default=10., help='Minimal run time')
    parser.add_argument('--min_run', type=int, default=3, help='Minimal number of kernel execution')
    args = parser.parse_args()

    # Kernel initialization
    sys.path.append(args.problem + '/' + args.method)
    from kernel import Kernel
    kernel = Kernel(args.dimension, args.nsample1, args.nsample2)
    kernel.init(args.p1, args.p2, args.p3)

    # Runs
    nrun = 0
    is_warming = True
    total_duration = 0.
    tic = time.time()

    while True:

        kernel.run()

        total_duration = time.time() - tic

        # Managing warming and run time
Beispiel #57
0
from killroutine import KillRoutine
from logicalMemory import LogicalMemory
from mainmemory import MainMemory
from memoryManager import MemoryManager
from newroutine import NewRoutine
from pcb import Pcb
from priorityQueue import PriorityQueue
from program import Program
from routines import Routines
from setting import Setting
from status import Status
from timeoutroutine import TimeOutRoutine

memorySize = 4

kernel = Kernel(memorySize)
#kernel.getScheduler().setPriorityMode()
#########################################
#########################################
instruction1 = Instruction("{Programa 1} primera instruccion",False,False)
instruction2 = Instruction("{Programa 1} segunda instruccion",False,True)


instruction3 = Instruction("{Programa 2} primera instruccion",True,False)
instruction4 = Instruction("{Programa 2} segunda instruccion",False,True)

instruction5 = Instruction("{Programa 3} primera instruccion",False,False)
instruction6 = Instruction("{Programa 3} segunda instruccion",False,True)

instruction7 = Instruction("{Programa 4} primera instruccion",False,False)
instruction8 = Instruction("{Programa 4} segunda instruccion",False,True)
Beispiel #58
0
 def test_clear(self):
     Kernel.clearCache()
     print(Kernel.cacheMatrix)
Beispiel #59
0
	Dy = co.matrix([[yp], [yu], [yn], [yn], [yn], [yn]])
	
	# generate training data
	co.setseed(11)
	Dtrainp = co.normal(2,N_pos)*0.4
	Dtrainu = co.normal(2,N_unl)*0.5
	Dtrainn = co.normal(2,N_neg)*0.3
	Dtrain21 = Dtrainn-1
	Dtrain21[0,:] = Dtrainn[0,:]+1
	Dtrain22 = -Dtrain21

	# training data
	Dtrain = co.matrix([[Dtrainp], [Dtrainu], [Dtrainn+1.0], [Dtrainn-1.0], [Dtrain21], [Dtrain22]])

	# build the training kernel
	kernel = Kernel.get_kernel(Dtrain, Dtrain, type=k_type, param=k_param)

	# use SSAD
	ssad = SSAD(kernel,Dy,1.0,1.0,1.0/(N_unl*0.1),1.0)
	ssad.train_dual()

	# build the test kernel
	kernel = Kernel.get_kernel(Dtrain, Dtrain[:,ssad.get_support_dual()], type=k_type, param=k_param)

	thres = np.array(ssad.get_threshold())[0,0]
	(pred,MSG) = ssad.apply_dual(kernel)
	pred = np.array(pred)
	pred = pred.transpose()

	# generate test data from a grid for nicer plots
	delta = 0.1