automl / PFNs

Our maintained PFN repository. Come here to train SOTA PFNs.
Apache License 2.0
51 stars 10 forks source link

Adapt tutorial to new naming convention #4

Closed cyrilpic closed 9 months ago

cyrilpic commented 10 months ago

While trying to run Tutorial_Training_for_BO.ipynb, I faced several errors.

  1. I realized that some arguments seem to have a new name (bptt: seq_len, and priordataloader_class: priordataloader_class_or_get_batch)
  2. Also, I believe the test in train.py should test if the class is a subclass of PriorDataLoader since priordataloader_class_or_get_batch only gets initialized on line 50.

This should allow others to run the notebook without errors.

SamuelGabriel commented 9 months ago

Thanks for fixing this up! Both fixes seem right to me! 🙏🏼

SamuelGabriel commented 9 months ago

I just tried the notebook out after the merge and it seems like I run into another error, probably because of version issues, did you run into this one too?

{
    "name": "AttributeError",
    "message": "'GaussianLikelihood' object has no attribute 'dim'",
    "stack": "---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
Cell In[3], line 2
      1 # Now let's train either with
----> 2 train.train(**add_criterion(config_heboplus,device='cpu:0'))
      3 # or
      4 #train.train(**add_criterion(config_heboplus_userpriors))
      5 # or
      6 #train.train(**add_criterion(config_bnn))

Cell In[1], line 106, in add_criterion(config, device)
    104 def add_criterion(config, device='cuda:0'):
    105     return {**config, 'criterion': bar_distribution.FullSupportBarDistribution(
--> 106         bar_distribution.get_bucket_limits(1000,ys=get_ys(config,device).cpu())
    107     )}

Cell In[1], line 98, in get_ys(config, device)
     96 all_targets = []
     97 for num_hps in [2,8,12]: # a few different samples in case the number of features makes a difference in y dist
---> 98     b = config['priordataloader_class_or_get_batch'].get_batch_method(
     99         bs,1000,  num_hps, epoch=0, device=device, hyperparameters={**config['extra_prior_kwargs_dict']['hyperparameters'],
    100                                                                     'num_hyperparameter_samples_per_batch': -1,})
    101     all_targets.append(b.target_y.flatten())
    102 return torch.cat(all_targets,0)

File /workspaces/PFNs/pfns/priors/utils.py:277, in get_batch_sequence.__call__(self, *args, **kwargs)
    275 for get_batch in self[1:]:
    276     final_get_batch = partial(get_batch, get_batch=final_get_batch)
--> 277 return final_get_batch(*args, **kwargs)

File ~/.local/lib/python3.10/site-packages/torch/utils/_contextlib.py:115, in context_decorator.<locals>.decorate_context(*args, **kwargs)
    112 @functools.wraps(func)
    113 def decorate_context(*args, **kwargs):
    114     with ctx_factory():
--> 115         return func(*args, **kwargs)

File /workspaces/PFNs/pfns/priors/utils.py:141, in sample_num_feaetures_get_batch(batch_size, seq_len, num_features, hyperparameters, get_batch, **kwargs)
    139 if hyperparameters.get('sample_num_features', True) and kwargs['epoch'] > 0: # don't sample on test batch
    140     num_features = random.randint(1, num_features)
--> 141 return get_batch(batch_size, seq_len, num_features, hyperparameters=hyperparameters, **kwargs)

File ~/.local/lib/python3.10/site-packages/torch/utils/_contextlib.py:115, in context_decorator.<locals>.decorate_context(*args, **kwargs)
    112 @functools.wraps(func)
    113 def decorate_context(*args, **kwargs):
    114     with ctx_factory():
--> 115         return func(*args, **kwargs)

File /workspaces/PFNs/pfns/priors/hebo_prior.py:438, in get_batch(batch_size, seq_len, num_features, device, hyperparameters, batch_size_per_gp_sample, single_eval_pos, fix_to_range, equidistant_x, verbose, **kwargs)
    436     used_local_x = local_x
    437 get_model_and_likelihood = lambda: get_model(used_local_x, torch.zeros(num_candidates,x.shape[1], device=device), hyperparameters)
--> 438 model, likelihood = get_model_and_likelihood()
    439 if verbose: print(list(model.named_parameters()),
    440                   (list(model.input_transform.named_parameters()), model.input_transform.concentration1, model.input_transform.concentration0)
    441                       if model.input_transform is not None else None,
    442                   )
    444 # trained_model = ExactGPModel(train_x, train_y, likelihood).cuda()
    445 # trained_model.eval()

File /workspaces/PFNs/pfns/priors/hebo_prior.py:437, in get_batch.<locals>.<lambda>()
    435 else:
    436     used_local_x = local_x
--> 437 get_model_and_likelihood = lambda: get_model(used_local_x, torch.zeros(num_candidates,x.shape[1], device=device), hyperparameters)
    438 model, likelihood = get_model_and_likelihood()
    439 if verbose: print(list(model.named_parameters()),
    440                   (list(model.input_transform.named_parameters()), model.input_transform.concentration1, model.input_transform.concentration0)
    441                       if model.input_transform is not None else None,
    442                   )

File /workspaces/PFNs/pfns/priors/hebo_prior.py:363, in get_model(x, y, hyperparameters, sample)
    361 if len(y.shape) < len(x.shape):
    362     y = y.unsqueeze(-1)
--> 363 model = botorch.models.SingleTaskGP(x, y, likelihood, covar_module=covar_module, input_transform=warp_tf)
    364 model.mean_module = ZeroMean(x.shape[:-2])
    365 model.to(device)

File /usr/local/python/3.10.13/lib/python3.10/site-packages/botorch/models/gp_regression.py:161, in SingleTaskGP.__init__(self, train_X, train_Y, train_Yvar, likelihood, covar_module, mean_module, outcome_transform, input_transform)
    159 if outcome_transform is not None:
    160     train_Y, train_Yvar = outcome_transform(train_Y, train_Yvar)
--> 161 self._validate_tensor_args(X=transformed_X, Y=train_Y, Yvar=train_Yvar)
    162 ignore_X_dims = getattr(self, \"_ignore_X_dims_scaling_check\", None)
    163 validate_input_scaling(
    164     train_X=transformed_X,
    165     train_Y=train_Y,
    166     train_Yvar=train_Yvar,
    167     ignore_X_dims=ignore_X_dims,
    168 )

File /usr/local/python/3.10.13/lib/python3.10/site-packages/botorch/models/gpytorch.py:107, in GPyTorchModel._validate_tensor_args(X, Y, Yvar, strict)
     99         warnings.warn(
    100             \"Non-strict enforcement of botorch tensor conventions. The \"
    101             \"following error would have been raised with strict enforcement: \"
    102             f\"{message}\",
    103             BotorchTensorDimensionWarning,
    104         )
    105 # Yvar may not have the same batch dimensions, but the trailing dimensions
    106 # of Yvar should be the same as the trailing dimensions of Y.
--> 107 if Yvar is not None and Y.shape[-(Yvar.dim()) :] != Yvar.shape:
    108     raise BotorchTensorDimensionError(
    109         \"An explicit output dimension is required for observation noise.\"
    110         f\" Expected Yvar with shape: {Y.shape[-Yvar.dim() :]} (got\"
    111         f\" {Yvar.shape}).\"
    112     )
    113 # Check the dtypes.

File ~/.local/lib/python3.10/site-packages/torch/nn/modules/module.py:1695, in Module.__getattr__(self, name)
   1693     if name in modules:
   1694         return modules[name]
-> 1695 raise AttributeError(f\"'{type(self).__name__}' object has no attribute '{name}'\")

AttributeError: 'GaussianLikelihood' object has no attribute 'dim'"
}
cyrilpic commented 9 months ago

I just tested again and did not get this error (I only get InputDataWarning warnings). I have gyptorch==1.11 and botorch==0.9.2