|
1 | 1 | #!/usr/bin/env python3
|
2 | 2 |
|
| 3 | +import warnings |
| 4 | +from typing import Any, Optional |
| 5 | + |
3 | 6 | import torch
|
4 | 7 |
|
5 |
| -from ..utils.broadcasting import _mul_broadcast_shape |
| 8 | +from ..constraints import Interval |
| 9 | +from ..priors import Prior |
| 10 | +from ..utils.warnings import OldVersionWarning |
6 | 11 | from .mean import Mean
|
7 | 12 |
|
8 | 13 |
|
| 14 | +def _ensure_updated_strategy_flag_set( |
| 15 | + state_dict, prefix, local_metadata, strict, missing_keys, unexpected_keys, error_msgs |
| 16 | +): |
| 17 | + if prefix + "constant" in state_dict: |
| 18 | + constant = state_dict.pop(prefix + "constant").squeeze(-1) # Remove deprecated singleton dimension |
| 19 | + state_dict[prefix + "raw_constant"] = constant |
| 20 | + warnings.warn( |
| 21 | + "You have loaded a GP model with a ConstantMean from a previous version of " |
| 22 | + "GPyTorch. The mean module parameter `constant` has been renamed to `raw_constant`. " |
| 23 | + "Additionally, the shape of `raw_constant` is now *batch_shape, whereas the shape of " |
| 24 | + "`constant` was *batch_shape x 1. " |
| 25 | + "We have updated the name/shape of the parameter in your state dict, but we recommend that you " |
| 26 | + "re-save your model.", |
| 27 | + OldVersionWarning, |
| 28 | + ) |
| 29 | + |
| 30 | + |
9 | 31 | class ConstantMean(Mean):
|
10 |
| - def __init__(self, prior=None, batch_shape=torch.Size(), **kwargs): |
| 32 | + r""" |
| 33 | + A (non-zero) constant prior mean function, i.e.: |
| 34 | +
|
| 35 | + .. math:: |
| 36 | + \mu(\mathbf x) = C |
| 37 | +
|
| 38 | + where :math:`C` is a learned constant. |
| 39 | +
|
| 40 | + :param constant_prior: Prior for constant parameter :math:`C`. |
| 41 | + :type constant_prior: ~gpytorch.priors.Prior, optional |
| 42 | + :param constant_constraint: Constraint for constant parameter :math:`C`. |
| 43 | + :type constant_constraint: ~gpytorch.priors.Interval, optional |
| 44 | + :param batch_shape: The batch shape of the learned constant(s) (default: []). |
| 45 | + :type batch_shape: torch.Size, optional |
| 46 | +
|
| 47 | + :var torch.Tensor constant: :math:`C` parameter |
| 48 | + """ |
| 49 | + |
| 50 | + def __init__( |
| 51 | + self, |
| 52 | + constant_prior: Optional[Prior] = None, |
| 53 | + constant_constraint: Optional[Interval] = None, |
| 54 | + batch_shape: torch.Size = torch.Size(), |
| 55 | + **kwargs: Any, |
| 56 | + ): |
11 | 57 | super(ConstantMean, self).__init__()
|
| 58 | + |
| 59 | + # Deprecated kwarg |
| 60 | + constant_prior_deprecated = kwargs.get("prior") |
| 61 | + if constant_prior_deprecated is not None: |
| 62 | + if constant_prior is None: # Using the old kwarg for the constant_prior |
| 63 | + warnings.warn( |
| 64 | + "The kwarg `prior` for ConstantMean has been renamed to `constant_prior`, and will be deprecated.", |
| 65 | + DeprecationWarning, |
| 66 | + ) |
| 67 | + constant_prior = constant_prior_deprecated |
| 68 | + else: # Weird edge case where someone set both `prior` and `constant_prior` |
| 69 | + warnings.warn( |
| 70 | + "You have set both the `constant_prior` and the deprecated `prior` arguments for ConstantMean. " |
| 71 | + "`prior` is deprecated, and will be ignored.", |
| 72 | + DeprecationWarning, |
| 73 | + ) |
| 74 | + |
| 75 | + # Ensure that old versions of the model still load |
| 76 | + self._register_load_state_dict_pre_hook(_ensure_updated_strategy_flag_set) |
| 77 | + |
12 | 78 | self.batch_shape = batch_shape
|
13 |
| - self.register_parameter(name="constant", parameter=torch.nn.Parameter(torch.zeros(*batch_shape, 1))) |
14 |
| - if prior is not None: |
15 |
| - self.register_prior("mean_prior", prior, self._constant_param, self._constant_closure) |
| 79 | + self.register_parameter(name="raw_constant", parameter=torch.nn.Parameter(torch.zeros(batch_shape))) |
| 80 | + if constant_prior is not None: |
| 81 | + self.register_prior("mean_prior", constant_prior, self._constant_param, self._constant_closure) |
| 82 | + if constant_constraint is not None: |
| 83 | + self.register_constraint("raw_constant", constant_constraint) |
| 84 | + |
| 85 | + @property |
| 86 | + def constant(self): |
| 87 | + return self._constant_param(self) |
16 | 88 |
|
| 89 | + @constant.setter |
| 90 | + def constant(self, value): |
| 91 | + self._constant_closure(self, value) |
| 92 | + |
| 93 | + # We need a getter of this form so that we can pickle ConstantMean modules with a mean prior, see PR #1992 |
17 | 94 | def _constant_param(self, m):
|
18 |
| - return m.constant |
| 95 | + if hasattr(m, "raw_constant_constraint"): |
| 96 | + return m.raw_constant_constraint.transform(m.raw_constant) |
| 97 | + return m.raw_constant |
19 | 98 |
|
| 99 | + # We need a setter of this form so that we can pickle ConstantMean modules with a mean prior, see PR #1992 |
20 | 100 | def _constant_closure(self, m, value):
|
21 | 101 | if not torch.is_tensor(value):
|
22 |
| - value = torch.as_tensor(value).to(self.constant) |
23 |
| - m.initialize(constant=value.reshape(self.constant.shape)) |
| 102 | + value = torch.as_tensor(value).to(m.raw_constant) |
24 | 103 |
|
25 |
| - def forward(self, input): |
26 |
| - if input.shape[:-2] == self.batch_shape: |
27 |
| - return self.constant.expand(input.shape[:-1]) |
| 104 | + if hasattr(m, "raw_constant_constraint"): |
| 105 | + m.initialize(raw_constant=m.raw_constant_constraint.inverse_transform(value)) |
28 | 106 | else:
|
29 |
| - return self.constant.expand(_mul_broadcast_shape(input.shape[:-1], self.constant.shape)) |
| 107 | + m.initialize(raw_constant=value) |
| 108 | + |
| 109 | + def forward(self, input): |
| 110 | + constant = self.constant.unsqueeze(-1) # *batch_shape x 1 |
| 111 | + return constant.expand(torch.broadcast_shapes(constant.shape, input.shape[:-1])) |
0 commit comments