src.environment.problem.SOO.COCO_BBOB.kan.utils¶
Module Contents¶
Functions¶
create dataset |
|
fit a, b, c, d such that |
|
get sparse mask |
|
add a symbolic function to library |
|
rounding the numbers in an expression to certain floating points |
|
augment inputs |
|
jacobian |
|
hessian |
|
create dataset from data |
|
compute the jacobian/hessian of loss wrt to model parameters |
|
turn model parameters into a flattened vector |
Data¶
API¶
- src.environment.problem.SOO.COCO_BBOB.kan.utils.create_dataset(f, n_var=2, f_mode='col', ranges=[-1, 1], train_num=1000, test_num=1000, normalize_input=False, normalize_label=False, device='cpu', seed=0)[source]¶
create dataset
Args:¶
f : function the symbolic formula used to create the synthetic dataset ranges : list or np.array; shape (2,) or (n_var, 2) the range of input variables. Default: [-1,1]. train_num : int the number of training samples. Default: 1000. test_num : int the number of test samples. Default: 1000. normalize_input : bool If True, apply normalization to inputs. Default: False. normalize_label : bool If True, apply normalization to labels. Default: False. device : str device. Default: 'cpu'. seed : int random seed. Default: 0.
Returns:¶
dataset : dic Train/test inputs/labels are dataset['train_input'], dataset['train_label'], dataset['test_input'], dataset['test_label']
Example¶
f = lambda x: torch.exp(torch.sin(torch.pi*x[:,[0]]) + x[:,[1]]**2) dataset = create_dataset(f, n_var=2, train_num=100) dataset[‘train_input’].shape torch.Size([100, 2])
- src.environment.problem.SOO.COCO_BBOB.kan.utils.fit_params(x, y, fun, a_range=(-10, 10), b_range=(-10, 10), grid_number=101, iteration=3, verbose=True, device='cpu')[source]¶
fit a, b, c, d such that
.. math:: |y-(cf(ax+b)+d)|^2
is minimized. Both x and y are 1D array. Sweep a and b, find the best fitted model.
Args:¶
x : 1D array x values y : 1D array y values fun : function symbolic function a_range : tuple sweeping range of a b_range : tuple sweeping range of b grid_num : int number of steps along a and b iteration : int number of zooming in verbose : bool print extra information if True device : str device
Returns:¶
a_best : float best fitted a b_best : float best fitted b c_best : float best fitted c d_best : float best fitted d r2_best : float best r2 (coefficient of determination)
Example¶
num = 100 x = torch.linspace(-1,1,steps=num) noises = torch.normal(0,1,(num,)) * 0.02 y = 5.0torch.sin(3.0x + 2.0) + 0.7 + noises fit_params(x, y, torch.sin) r2 is 0.9999727010726929 (tensor([2.9982, 1.9996, 5.0053, 0.7011]), tensor(1.0000))
- src.environment.problem.SOO.COCO_BBOB.kan.utils.sparse_mask(in_dim, out_dim)[source]¶
get sparse mask
- src.environment.problem.SOO.COCO_BBOB.kan.utils.add_symbolic(name, fun, c=1, fun_singularity=None)[source]¶
add a symbolic function to library
Args:¶
name : str name of the function fun : fun torch function or lambda function
Returns:¶
NoneExample¶
print(SYMBOLIC_LIB[‘Bessel’]) KeyError: ‘Bessel’ add_symbolic(‘Bessel’, torch.special.bessel_j0) print(SYMBOLIC_LIB[‘Bessel’]) (
, Bessel)
- src.environment.problem.SOO.COCO_BBOB.kan.utils.ex_round(ex1, n_digit)[source]¶
rounding the numbers in an expression to certain floating points
Args:¶
ex1 : sympy expression n_digit : int
Returns:¶
ex2 : sympy expressionExample¶
from kan.utils import * from sympy import * input_vars = a, b = symbols(‘a b’) expression = 3.14534242 * exp(sin(pi*a) + b**2) - 2.32345402 ex_round(expression, 2)
- src.environment.problem.SOO.COCO_BBOB.kan.utils.augment_input(orig_vars, aux_vars, x)[source]¶
augment inputs
Args:¶
orig_vars : list of sympy symbols aux_vars : list of auxiliary symbols x : inputs
Returns:¶
augmented inputsExample¶
from kan.utils import * from sympy import * orig_vars = a, b = symbols(‘a b’) aux_vars = [a + b, a * b] x = torch.rand(100, 2) augment_input(orig_vars, aux_vars, x).shape
- src.environment.problem.SOO.COCO_BBOB.kan.utils.batch_jacobian(func, x, create_graph=False)[source]¶
jacobian
Args:¶
func : function or model x : inputs create_graph : bool
Returns:¶
jacobianExample¶
from kan.utils import batch_jacobian x = torch.normal(0,1,size=(100,2)) model = lambda x: x[:,[0]] + x[:,[1]] batch_jacobian(model, x)
- src.environment.problem.SOO.COCO_BBOB.kan.utils.batch_hessian(model, x, create_graph=False)[source]¶
hessian
Args:¶
func : function or model x : inputs create_graph : bool
Returns:¶
jacobianExample¶
from kan.utils import batch_hessian x = torch.normal(0,1,size=(100,2)) model = lambda x: x[:,[0]]**2 + x[:,[1]]**2 batch_hessian(model, x)
- src.environment.problem.SOO.COCO_BBOB.kan.utils.create_dataset_from_data(inputs, labels, train_ratio=0.8, device='cpu')[source]¶
create dataset from data
Args:¶
inputs : 2D torch.float labels : 2D torch.float train_ratio : float the ratio of training fraction device : str
Returns:¶
dataset (dictionary)Example¶
from kan.utils import create_dataset_from_data x = torch.normal(0,1,size=(100,2)) y = torch.normal(0,1,size=(100,1)) dataset = create_dataset_from_data(x, y) dataset[‘train_input’].shape
- src.environment.problem.SOO.COCO_BBOB.kan.utils.get_derivative(model, inputs, labels, derivative='hessian', loss_mode='pred', reg_metric='w', lamb=0.0, lamb_l1=1.0, lamb_entropy=0.0)[source]¶
compute the jacobian/hessian of loss wrt to model parameters
Args:¶
inputs : 2D torch.float labels : 2D torch.float derivative : str 'jacobian' or 'hessian' device : str
Returns:¶
jacobian or hessian