How to use add_kernel method in autotest

Best Python code snippet using autotest_python

models.py

Source:models.py Github

copy

Full Screen

1import gpytorch2from gpytorch import lazify3from gpytorch.models import ExactGP, AbstractVariationalGP4from gpytorch.variational import CholeskyVariationalDistribution, VariationalStrategy5import torch6from gp_models.kernels import GeneralizedProjectionKernel7from gp_models import CustomAdditiveKernel8class ExactGPModel(ExactGP):9 """Basic exact GP model with const mean and a provided kernel"""10 def __init__(self, train_x, train_y, likelihood, kernel):11 super(ExactGPModel, self).__init__(train_x, train_y, likelihood)12 self.mean_module = gpytorch.means.ConstantMean()13 self.covar_module = kernel14 def forward(self, x):15 mean_x = self.mean_module(x)16 covar_x = self.covar_module(x)17 return gpytorch.distributions.MultivariateNormal(mean_x, covar_x)18class AdditiveExactGPModel(ExactGPModel):19 def __init__(self, train_x, train_y, likelihood, kernel):20 if isinstance(kernel, gpytorch.kernels.ScaleKernel):21 if not isinstance(kernel.base_kernel, CustomAdditiveKernel):22 raise ValueError("Not an additive kernel.")23 else:24 if not isinstance(kernel, CustomAdditiveKernel):25 raise ValueError("Not an additive kernel.")26 super(AdditiveExactGPModel, self).__init__(train_x, train_y, likelihood, kernel)27 def additive_pred(self, x, group=None): # Pretty sure this should count as a prediction strategy28 # TODO: implement somehow for RP models as well.29 if isinstance(self.covar_module, gpytorch.kernels.ScaleKernel):30 scale = self.covar_module.outputscale31 add_kernel = self.covar_module.base_kernel32 elif isinstance(self.covar_module, CustomAdditiveKernel):33 scale = torch.tensor(1.0, dtype=torch.float)34 add_kernel = self.covar_module35 else:36 raise NotImplementedError("Only implemented for Additive kernels and Scale kernel wrappings only")37 train_prior_dist = self.forward(self.train_inputs[0])38 lik_covar_train_train = self.likelihood(train_prior_dist, self.train_inputs[0]).lazy_covariance_matrix.detach()39 # TODO: cache?40 K_inv_y = lik_covar_train_train.inv_matmul(self.train_targets)41 def get_pred(k):42 # is there a better way to handle the scalings?43 test_train_covar = scale * gpytorch.lazy.delazify(k(x, self.train_inputs[0]))44 test_test_covar = scale * gpytorch.lazy.delazify(k(x, x))45 train_test_covar = test_train_covar.transpose(-1, -2)46 pred_mean = test_train_covar.matmul(K_inv_y)47 covar_correction_rhs = lik_covar_train_train.inv_matmul(train_test_covar)48 pred_covar = lazify(torch.addmm(1, test_test_covar, -1, test_train_covar, covar_correction_rhs))49 return gpytorch.distributions.MultivariateNormal(pred_mean, pred_covar, validate_args=False)50 if group is None:51 outputs = []52 for k in add_kernel.kernel.kernels:53 outputs.append(get_pred(k))54 return outputs55 else:56 return get_pred(add_kernel.kernel.kernels[group])57 def get_groups(self):58 if isinstance(self.covar_module, gpytorch.kernels.ScaleKernel):59 add_kernel = self.covar_module.base_kernel60 else:61 add_kernel = self.covar_module62 return add_kernel.groups63class ProjectedAdditiveExactGPModel(ExactGPModel):64 def __init__(self, train_x, train_y, likelihood, kernel):65 if isinstance(kernel, gpytorch.kernels.ScaleKernel):66 if not isinstance(kernel.base_kernel, GeneralizedProjectionKernel):67 raise ValueError("Not an projected additive kernel.")68 else:69 if not isinstance(kernel, GeneralizedProjectionKernel):70 raise ValueError("Not an projected additive kernel.")71 super(ProjectedAdditiveExactGPModel, self).__init__(train_x, train_y, likelihood, kernel)72 def get_corresponding_additive_model(self, return_proj=True):73 return convert_rp_model_to_additive_model(self, return_proj=return_proj)74class SVGPRegressionModel(AbstractVariationalGP):75 """SVGP with provided kernel, Cholesky variational prior, and provided inducing points."""76 def __init__(self, inducing_points, kernel, likelihood):77 variational_distribution = CholeskyVariationalDistribution(78 inducing_points.size(0))79 variational_strategy = VariationalStrategy(self,80 inducing_points,81 variational_distribution,82 learn_inducing_locations=True)83 super(SVGPRegressionModel, self).__init__(variational_strategy)84 self.mean_module = gpytorch.means.ConstantMean()85 self.covar_module = kernel86 self.likelihood = likelihood87 def forward(self,x):88 mean_x = self.mean_module(x)89 covar_x = self.covar_module(x)90 latent_pred = gpytorch.distributions.MultivariateNormal(mean_x, covar_x)91 return latent_pred92def convert_rp_model_to_additive_model(model, return_proj=True):93 if isinstance(model.covar_module, gpytorch.kernels.ScaleKernel):94 rp_kernel = model.covar_module.base_kernel95 else:96 rp_kernel = model.covar_module97 add_kernel = rp_kernel.to_additive_kernel()98 proj = rp_kernel.projection_module99 if isinstance(model.covar_module, gpytorch.kernels.ScaleKernel):100 add_kernel = gpytorch.kernels.ScaleKernel(add_kernel)101 add_kernel.initialize(outputscale=model.covar_module.outputscale)102 Z = rp_kernel.projection_module(model.train_inputs[0])103 res = AdditiveExactGPModel(Z, model.train_targets, model.likelihood, add_kernel)104 res.mean_module = model.mean_module # either zero or constant, so doesn't matter if the input is projected.105 if return_proj:106 res = (res, proj)...

Full Screen

Full Screen

4. cuda_kernels.py

Source:4. cuda_kernels.py Github

copy

Full Screen

...19# each grid has blocks on threads20# Each thread is identified by a threadidx (index for threads)21# and blockidx(index for the block)22@cuda.jit23def add_kernel(x,y,out):24 tx=cuda.threadIdx.x25 ty=cuda.blockIdx.y26 block_size=cuda.blockDim.x # num threads per block27 grid_size=cuda.gridDim.x # num blocks in the grid28 start=tx+ty*block_size29 stride=block_size*grid_size30 for i in range(start,x.shape[0],stride):31 out[i]=x[i]+y[i]32n=100000033x=np.arange(n).astype(np.float32)34y=2*x35out=np.empty_like(x)36threads_per_block=12837blocks_per_grid=3038add_kernel[blocks_per_grid,threads_per_block](x,y,out)39out[:10]40# Using Numba helper functions41@cuda.jit42def add_kernel(x,y,out):43 start=cuda.grid(1) # 1-d thread grid44 stride=cuda.gridsize(1) # 1-d thread grid45 for i in range(start,x.shape[0],stride):46 out[i]=x[i]+y[i]47x_device=cuda.to_device(x)48y_device=cuda.to_device(y)49out_device=cuda.device_array_like(x)50start=perf_counter()51add_kernel[threads_per_block,blocks_per_grid](x,y,out)52print("Time: "+repr(perf_counter()-start)+"s")53start=perf_counter()54add_kernel[threads_per_block,blocks_per_grid](x_device,y_device,out_device)55print("Time: "+repr(perf_counter()-start)+"s")56out_device.copy_to_host()...

Full Screen

Full Screen

elwise_gfuncs.py

Source:elwise_gfuncs.py Github

copy

Full Screen

...18 __all__.append(root)19 for r in kernel_roots:20 for t in types:21 name = r + '_' + t22 f.add_kernel(elwise_kernels.__dict__[name])23types = ['int32', 'int64', 'uint32', 'uint64', 'float32', 'float64']24add_basic_gfunc('add', types)25add_basic_gfunc('subtract', types)26add_basic_gfunc('multiply', types)27add_basic_gfunc('divide', types)28#add_basic_gfunc('maximum', types, ['maximum2', 'maximum3'])29#add_basic_gfunc('minimum', types, ['minimum2', 'minimum3'])30add_basic_gfunc('maximum', types, ['maximum2'])31add_basic_gfunc('minimum', types, ['minimum2'])32add_basic_gfunc('square', types)33add_basic_gfunc('abs', types)34add_basic_gfunc('floor', types)35add_basic_gfunc('ceil', types)36if sys.platform == 'win32':37 fmod = gfunc.elwise('fmod')38 fmod.add_kernel(elwise_kernels.fmod)39 pow = gfunc.elwise('pow')40 pow.add_kernel(elwise_kernels.pow)41 sqrt = gfunc.elwise('sqrt')42 sqrt.add_kernel(elwise_kernels.sqrt)43 exp = gfunc.elwise('exp')44 exp.add_kernel(elwise_kernels.exp)45 log = gfunc.elwise('log')46 log.add_kernel(elwise_kernels.log)47 log10 = gfunc.elwise('log10')48 log10.add_kernel(elwise_kernels.log10)49 sin = gfunc.elwise('sin')50 sin.add_kernel(elwise_kernels.sin)51 cos = gfunc.elwise('cos')52 cos.add_kernel(elwise_kernels.cos)53 tan = gfunc.elwise('tan')54 tan.add_kernel(elwise_kernels.tan)55 arcsin = gfunc.elwise('arcsin')56 arcsin.add_kernel(elwise_kernels.arcsin)57 arccos = gfunc.elwise('arccos')58 arccos.add_kernel(elwise_kernels.arccos)59 arctan = gfunc.elwise('arctan')60 arctan.add_kernel(elwise_kernels.arctan)61 arctan2 = gfunc.elwise('arctan2')62 arctan2.add_kernel(elwise_kernels.arctan2)63 sinh = gfunc.elwise('sinh')64 sinh.add_kernel(elwise_kernels.sinh)65 cosh = gfunc.elwise('cosh')66 cosh.add_kernel(elwise_kernels.cosh)67 tanh = gfunc.elwise('tanh')68 tanh.add_kernel(elwise_kernels.tanh)69 ldexp = gfunc.elwise('ldexp')70 ldexp.add_kernel(elwise_kernels.ldexp)71 isnan = gfunc.elwise('isnan')72 isnan.add_kernel(elwise_kernels.isnan)73 isfinite = gfunc.elwise('isfinite')74 isfinite.add_kernel(elwise_kernels.isfinite)75 nextafter = gfunc.elwise('nextafter')...

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run autotest automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful