def _run_auc (): """Run AUC kernel.""" # handle subkernel params={ 'name': 'Gaussian', 'data': dataop.get_rand(), 'feature_class': 'simple', 'feature_type': 'Real', 'args': {'key': ('size', 'width'), 'val': (10, 1.7)} } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) subk=kernel.GaussianKernel(*params['args']['val']) subk.init(feats['train'], feats['test']) output=fileop.get_output(category.KERNEL, params, 'subkernel0_') # handle AUC params={ 'name': 'AUC', 'data': dataop.get_rand(numpy.ushort, num_feats=2, max_train=dataop.NUM_VEC_TRAIN, max_test=dataop.NUM_VEC_TEST), 'feature_class': 'simple', 'feature_type': 'Word', 'accuracy': 1e-8, 'args': {'key': ('size', 'subkernel'), 'val': (10, subk)} } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) _compute(feats, params, output)
def _run_auc(): """Run AUC kernel.""" # handle subkernel params = { "name": "Gaussian", "data": dataop.get_rand(), "feature_class": "simple", "feature_type": "Real", "args": {"key": ("size", "width"), "val": (10, 1.7)}, } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) subk = kernel.GaussianKernel(*params["args"]["val"]) subk.init(feats["train"], feats["test"]) output = fileop.get_output(category.KERNEL, params, "subkernel0_") # handle AUC params = { "name": "AUC", "data": dataop.get_rand( numpy.ushort, num_feats=2, max_train=dataop.NUM_VEC_TRAIN, max_test=dataop.NUM_VEC_TEST ), "feature_class": "simple", "feature_type": "Word", "accuracy": 1e-8, "args": {"key": ("size", "subkernel"), "val": (10, subk)}, } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) _compute(feats, params, output)
def _run_real (name, args=None): """Run preprocessor applied on RealFeatures. @param name name of the preprocessor @param args argument list (in a dict) for the preprocessor """ params={ 'name': 'Gaussian', 'accuracy': 1e-8, 'data': dataop.get_rand(), 'feature_class': 'simple', 'feature_type': 'Real', 'args': {'key': ('width',), 'val': (1.2,)} } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) if args: feats=featop.add_preproc(name, feats, *args['val']) else: feats=featop.add_preproc(name, feats) output=_compute(feats, params) params={ 'name': name } if args: params['args']=args output.update(fileop.get_output(category.PREPROC, params)) fileop.write(category.PREPROC, output)
def _run_real(name, args=None): """Run preprocessor applied on RealFeatures. @param name name of the preprocessor @param args argument list (in a dict) for the preprocessor """ params = { "name": "Gaussian", "accuracy": 1e-8, "data": dataop.get_rand(), "feature_class": "simple", "feature_type": "Real", "args": {"key": ("width",), "val": (1.2,)}, } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) if args: feats = featop.add_preproc(name, feats, *args["val"]) else: feats = featop.add_preproc(name, feats) output = _compute(feats, params) params = {"name": name} if args: params["args"] = args output.update(fileop.get_output(category.PREPROC, params)) fileop.write(category.PREPROC, output)
def _run_custom(): """Run Custom kernel.""" params = {"name": "Custom", "accuracy": 1e-7, "feature_class": "simple", "feature_type": "Real"} dim_square = 7 data = dataop.get_rand(dim_square=dim_square) feats = featop.get_features(params["feature_class"], params["feature_type"], data) data = data["train"] symdata = data + data.T lowertriangle = numpy.array( [symdata[(x, y)] for x in xrange(symdata.shape[1]) for y in xrange(symdata.shape[0]) if y <= x] ) kern = kernel.CustomKernel() # kern.init(feats['train'], feats['train'] kern.set_triangle_kernel_matrix_from_triangle(lowertriangle) km_triangletriangle = kern.get_kernel_matrix() kern.set_triangle_kernel_matrix_from_full(symdata) km_fulltriangle = kern.get_kernel_matrix() kern.set_full_kernel_matrix_from_full(data) km_fullfull = kern.get_kernel_matrix() output = { "kernel_matrix_triangletriangle": km_triangletriangle, "kernel_matrix_fulltriangle": km_fulltriangle, "kernel_matrix_fullfull": km_fullfull, "kernel_symdata": numpy.matrix(symdata), "kernel_data": numpy.matrix(data), "kernel_dim_square": dim_square, } output.update(fileop.get_output(category.KERNEL, params)) fileop.write(category.KERNEL, output)
def _run_feats_real(): """Run kernel with RealFeatures.""" params = {"data": dataop.get_rand(), "accuracy": 1e-8, "feature_class": "simple", "feature_type": "Real"} feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) sparsefeats = featop.get_features(params["feature_class"], params["feature_type"], params["data"], sparse=True) params["name"] = "Gaussian" params["args"] = {"key": ("size", "width"), "val": (10, 1.3)} _compute(feats, params) params["name"] = "GaussianShift" params["args"] = {"key": ("size", "width", "max_shift", "shift_step"), "val": (10, 1.3, 2, 1)} _compute(feats, params) params["name"] = "Gaussian" params["args"] = {"key": ("size", "width"), "val": (10, 1.7)} _compute(sparsefeats, params) params["accuracy"] = 0 params["name"] = "Const" params["args"] = {"key": ("c",), "val": (23.0,)} _compute(feats, params) params["name"] = "Diag" params["args"] = {"key": ("size", "diag"), "val": (10, 23.0)} _compute(feats, params) params["accuracy"] = 1e-9 params["name"] = "Sigmoid" params["args"] = {"key": ("size", "gamma", "coef0"), "val": (10, 1.1, 1.3)} _compute(feats, params) params["args"]["val"] = (10, 0.5, 0.7) _compute(feats, params) params["name"] = "Chi2" params["args"] = {"key": ("size", "width"), "val": (10, 1.2)} _compute(feats, params) params["accuracy"] = 1e-8 params["name"] = "Poly" params["args"] = {"key": ("size", "degree", "inhomogene"), "val": (10, 3, True)} _compute(sparsefeats, params) params["args"]["val"] = (10, 3, False) _compute(sparsefeats, params) params["name"] = "Poly" params["normalizer"] = kernel.SqrtDiagKernelNormalizer() params["args"] = {"key": ("size", "degree", "inhomogene"), "val": (10, 3, True)} _compute(feats, params) params["args"]["val"] = (10, 3, False) _compute(feats, params) params["normalizer"] = kernel.AvgDiagKernelNormalizer() del params["args"] params["name"] = "Linear" _compute(feats, params) params["name"] = "Linear" _compute(sparsefeats, params)
def _run_distance(): """Run distance kernel.""" params = { "name": "Distance", "accuracy": 1e-9, "feature_class": "simple", "feature_type": "Real", "data": dataop.get_rand(), "args": {"key": ("size", "width", "distance"), "val": (10, 1.7, CanberraMetric())}, } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) _compute(feats, params)
def _run_feats_byte(): """Run kernel with ByteFeatures.""" params = { "name": "Linear", "accuracy": 1e-8, "feature_class": "simple", "feature_type": "Byte", "data": dataop.get_rand(dattype=numpy.ubyte), "normalizer": kernel.AvgDiagKernelNormalizer(), } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"], RAWBYTE) _compute(feats, params)
def _run_feats_byte (): """Run kernel with ByteFeatures.""" params={ 'name': 'LinearByte', 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Byte', 'data': dataop.get_rand(dattype=numpy.ubyte), 'normalizer': kernel.AvgDiagKernelNormalizer() } feats=featop.get_features(params['feature_class'], params['feature_type'], params['data'], RAWBYTE) _compute(feats, params)
def _run_feats_byte (): """Run kernel with ByteFeatures.""" params={ 'name': 'Linear', 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Byte', 'data': dataop.get_rand(dattype=numpy.ubyte), 'normalizer': kernel.AvgDiagKernelNormalizer() } feats=featop.get_features(params['feature_class'], params['feature_type'], params['data'], RAWBYTE) _compute(feats, params)
def _run_feats_word(): """Run kernel with WordFeatures.""" maxval = 42 params = { "name": "Linear", "accuracy": 1e-8, "feature_class": "simple", "feature_type": "Word", "data": dataop.get_rand(dattype=numpy.ushort, max_train=maxval, max_test=maxval), "normalizer": kernel.AvgDiagKernelNormalizer(), } feats = featop.get_features(params["feature_class"], params["feature_type"], params["data"]) _compute(feats, params)
def _run_feats_word (): """Run kernel with WordFeatures.""" maxval=42 params={ 'name': 'LinearWord', 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Word', 'data': dataop.get_rand( dattype=numpy.ushort, max_train=maxval, max_test=maxval), 'normalizer': kernel.AvgDiagKernelNormalizer() } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) _compute(feats, params)
def _run_feats_real(): """Run distances with RealFeatures.""" params = { 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand() } feats = featop.get_features(params['feature_class'], params['feature_type'], params['data']) params['name'] = 'EuclidianDistance' _compute(feats, params) params['name'] = 'CanberraMetric' _compute(feats, params) params['name'] = 'ChebyshewMetric' _compute(feats, params) params['name'] = 'GeodesicMetric' _compute(feats, params) params['name'] = 'JensenMetric' _compute(feats, params) params['name'] = 'ManhattanMetric' _compute(feats, params) params['name'] = 'BrayCurtisDistance' _compute(feats, params) params['name'] = 'ChiSquareDistance' _compute(feats, params) params['name'] = 'CosineDistance' _compute(feats, params) params['name'] = 'TanimotoDistance' _compute(feats, params) params['name'] = 'ManhattanMetric' _compute(feats, params) params['name'] = 'MinkowskiMetric' params['args'] = {'key': ('k', ), 'val': (1.3, )} _compute(feats, params) params['name'] = 'SparseEuclidianDistance' params['accuracy'] = 1e-7 del params['args'] feats = featop.get_features(params['feature_class'], params['feature_type'], params['data'], sparse=True) _compute(feats, params)
def _run_feats_word (): """Run kernel with WordFeatures.""" maxval=42 params={ 'name': 'Linear', 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Word', 'data': dataop.get_rand( dattype=numpy.ushort, max_train=maxval, max_test=maxval), 'normalizer': kernel.AvgDiagKernelNormalizer() } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) _compute(feats, params)
def _run_feats_real (): """Run distances with RealFeatures.""" params={ 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand() } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) params['name']='EuclidianDistance' _compute(feats, params) params['name']='CanberraMetric' _compute(feats, params) params['name']='ChebyshewMetric' _compute(feats, params) params['name']='GeodesicMetric' _compute(feats, params) params['name']='JensenMetric' _compute(feats, params) params['name']='ManhattanMetric' _compute(feats, params) params['name']='BrayCurtisDistance' _compute(feats, params) params['name']='ChiSquareDistance' _compute(feats, params) params['name']='CosineDistance' _compute(feats, params) params['name']='TanimotoDistance' _compute(feats, params) params['name']='ManhattanMetric' _compute(feats, params) params['name']='MinkowskiMetric' params['args']={'key': ('k',), 'val': (1.3,)} _compute(feats, params) params['name']='SparseEuclidianDistance' params['accuracy']=1e-7 del params['args'] feats=featop.get_features( params['feature_class'], params['feature_type'], params['data'], sparse=True) _compute(feats, params)
def _run_distance (): """Run distance kernel.""" params={ 'name': 'Distance', 'accuracy': 1e-9, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand(), 'args': { 'key': ('size', 'width', 'distance'), 'val': (10, 1.7, CanberraMetric()) } } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) _compute(feats, params)
def _run_distance(): """Run distance kernel.""" params = { 'name': 'Distance', 'accuracy': 1e-9, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand(), 'args': { 'key': ('size', 'width', 'distance'), 'val': (10, 1.7, CanberraMetric()) } } feats = featop.get_features(params['feature_class'], params['feature_type'], params['data']) _compute(feats, params)
def run (): """Run generator for all regression methods.""" regressions=( {'name': 'SVRLight', 'type': 'svm', 'accuracy': 1e-6}, {'name': 'LibSVR', 'type': 'svm', 'accuracy': 1e-6}, {'name': 'KRR', 'type': 'kernelmachine', 'accuracy': 1e-8}, ) params={ 'name': 'Gaussian', 'args': {'key': ('width',), 'val': (1.5,)}, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand() } output=fileop.get_output(category.KERNEL, params) feats=featop.get_simple('Real', params['data']) kernel=GaussianKernel(10, *params['args']['val']) _loop(regressions, feats, kernel, output)
def _run_custom(): """Run Custom kernel.""" params = { 'name': 'Custom', 'accuracy': 1e-7, 'feature_class': 'simple', 'feature_type': 'Real' } dim_square = 7 data = dataop.get_rand(dim_square=dim_square) feats = featop.get_features(params['feature_class'], params['feature_type'], data) data = data['train'] symdata = data + data.T lowertriangle = numpy.array([ symdata[(x, y)] for x in xrange(symdata.shape[1]) for y in xrange(symdata.shape[0]) if y <= x ]) kern = kernel.CustomKernel() #kern.init(feats['train'], feats['train'] kern.set_triangle_kernel_matrix_from_triangle(lowertriangle) km_triangletriangle = kern.get_kernel_matrix() kern.set_triangle_kernel_matrix_from_full(symdata) km_fulltriangle = kern.get_kernel_matrix() kern.set_full_kernel_matrix_from_full(data) km_fullfull = kern.get_kernel_matrix() output = { 'kernel_matrix_triangletriangle': km_triangletriangle, 'kernel_matrix_fulltriangle': km_fulltriangle, 'kernel_matrix_fullfull': km_fullfull, 'kernel_symdata': numpy.matrix(symdata), 'kernel_data': numpy.matrix(data), 'kernel_dim_square': dim_square } output.update(fileop.get_output(category.KERNEL, params)) fileop.write(category.KERNEL, output)
def _run_custom (): """Run Custom kernel.""" params={ 'name': 'Custom', 'accuracy': 1e-7, 'feature_class': 'simple', 'feature_type': 'Real' } dim_square=7 data=dataop.get_rand(dim_square=dim_square) feats=featop.get_features( params['feature_class'], params['feature_type'], data) data=data['train'] symdata=data+data.T lowertriangle=numpy.array([symdata[(x,y)] for x in xrange(symdata.shape[1]) for y in xrange(symdata.shape[0]) if y<=x]) kern=kernel.CustomKernel() #kern.init(feats['train'], feats['train'] kern.set_triangle_kernel_matrix_from_triangle(lowertriangle) km_triangletriangle=kern.get_kernel_matrix() kern.set_triangle_kernel_matrix_from_full(symdata) km_fulltriangle=kern.get_kernel_matrix() kern.set_full_kernel_matrix_from_full(data) km_fullfull=kern.get_kernel_matrix() output={ 'kernel_matrix_triangletriangle': km_triangletriangle, 'kernel_matrix_fulltriangle': km_fulltriangle, 'kernel_matrix_fullfull': km_fullfull, 'kernel_symdata': numpy.matrix(symdata), 'kernel_data': numpy.matrix(data), 'kernel_dim_square': dim_square } output.update(fileop.get_output(category.KERNEL, params)) fileop.write(category.KERNEL, output)
def run(): """Run generator for all regression methods.""" regressions = ( { 'name': 'SVRLight', 'type': 'svm', 'accuracy': 1e-6 }, { 'name': 'LibSVR', 'type': 'svm', 'accuracy': 1e-6 }, { 'name': 'KRR', 'type': 'kernelmachine', 'accuracy': 1e-8 }, ) params = { 'name': 'Gaussian', 'args': { 'key': ('width', ), 'val': (1.5, ) }, 'feature_class': 'simple', 'feature_type': 'Real', 'data': dataop.get_rand() } output = fileop.get_output(category.KERNEL, params) feats = featop.get_simple('Real', params['data']) kernel = GaussianKernel(10, *params['args']['val']) _loop(regressions, feats, kernel, output)
def _run_feats_real(): """Run kernel with RealFeatures.""" params = { 'data': dataop.get_rand(), 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Real' } feats = featop.get_features(params['feature_class'], params['feature_type'], params['data']) sparsefeats = featop.get_features(params['feature_class'], params['feature_type'], params['data'], sparse=True) params['name'] = 'Gaussian' params['args'] = { 'key': ( 'size', 'width', ), 'val': (10, 1.3) } _compute(feats, params) params['name'] = 'GaussianShift' params['args'] = { 'key': ('size', 'width', 'max_shift', 'shift_step'), 'val': (10, 1.3, 2, 1) } _compute(feats, params) params['name'] = 'SparseGaussian' params['args'] = {'key': ('size', 'width'), 'val': (10, 1.7)} _compute(sparsefeats, params) params['accuracy'] = 0 params['name'] = 'Const' params['args'] = {'key': ('c', ), 'val': (23., )} _compute(feats, params) params['name'] = 'Diag' params['args'] = {'key': ('size', 'diag'), 'val': (10, 23.)} _compute(feats, params) params['accuracy'] = 1e-9 params['name'] = 'Sigmoid' params['args'] = {'key': ('size', 'gamma', 'coef0'), 'val': (10, 1.1, 1.3)} _compute(feats, params) params['args']['val'] = (10, 0.5, 0.7) _compute(feats, params) params['name'] = 'Chi2' params['args'] = {'key': ('size', 'width'), 'val': (10, 1.2)} _compute(feats, params) params['accuracy'] = 1e-8 params['name'] = 'SparsePoly' params['args'] = { 'key': ('size', 'degree', 'inhomogene'), 'val': (10, 3, True) } _compute(sparsefeats, params) params['args']['val'] = (10, 3, False) _compute(sparsefeats, params) params['name'] = 'Poly' params['normalizer'] = kernel.SqrtDiagKernelNormalizer() params['args'] = { 'key': ('size', 'degree', 'inhomogene'), 'val': (10, 3, True) } _compute(feats, params) params['args']['val'] = (10, 3, False) _compute(feats, params) params['normalizer'] = kernel.AvgDiagKernelNormalizer() del params['args'] params['name'] = 'Linear' _compute(feats, params) params['name'] = 'SparseLinear' _compute(sparsefeats, params)
def _run_feats_real (): """Run kernel with RealFeatures.""" params={ 'data': dataop.get_rand(), 'accuracy': 1e-8, 'feature_class': 'simple', 'feature_type': 'Real' } feats=featop.get_features( params['feature_class'], params['feature_type'], params['data']) sparsefeats=featop.get_features( params['feature_class'], params['feature_type'], params['data'], sparse=True) params['name']='Gaussian' params['args']={'key': ('size', 'width',), 'val': (10, 1.3)} _compute(feats, params) params['name']='GaussianShift' params['args']={ 'key': ('size', 'width', 'max_shift', 'shift_step'), 'val': (10, 1.3, 2, 1) } _compute(feats, params) params['name']='SparseGaussian' params['args']={'key': ('size', 'width'), 'val': (10, 1.7)} _compute(sparsefeats, params) params['accuracy']=0 params['name']='Const' params['args']={'key': ('c',), 'val': (23.,)} _compute(feats, params) params['name']='Diag' params['args']={'key': ('size', 'diag'), 'val': (10, 23.)} _compute(feats, params) params['accuracy']=1e-9 params['name']='Sigmoid' params['args']={ 'key': ('size', 'gamma', 'coef0'), 'val': (10, 1.1, 1.3) } _compute(feats, params) params['args']['val']=(10, 0.5, 0.7) _compute(feats, params) params['name']='Chi2' params['args']={'key': ('size', 'width'), 'val': (10, 1.2)} _compute(feats, params) params['accuracy']=1e-8 params['name']='SparsePoly' params['args']={ 'key': ('size', 'degree', 'inhomogene'), 'val': (10, 3, True) } _compute(sparsefeats, params) params['args']['val']=(10, 3, False) _compute(sparsefeats, params) params['name']='Poly' params['normalizer']=kernel.SqrtDiagKernelNormalizer() params['args']={ 'key': ('size', 'degree', 'inhomogene'), 'val': (10, 3, True) } _compute(feats, params) params['args']['val']=(10, 3, False) _compute(feats, params) params['normalizer']=kernel.AvgDiagKernelNormalizer() del params['args'] params['name']='Linear' _compute(feats, params) params['name']='SparseLinear' _compute(sparsefeats, params)