models
pytorch_lattice.models.CalibratedLattice
Bases: ConstrainedModule
PyTorch Calibrated Lattice Model.
Creates a torch.nn.Module
representing a calibrated lattice model, which will be
constructed using the provided model configuration. Note that the model inputs
should match the order in which they are defined in the feature_configs
.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
calibrators |
A dictionary that maps feature names to their calibrators. |
|
lattice |
The |
|
output_calibrator |
The output |
Example:
feature_configs = [...]
calibrated_model = CalibratedLattice(feature_configs, ...)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(calibrated_model.parameters(recurse=True), lr=1e-1)
dataset = pyl.utils.data.Dataset(...)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True)
for epoch in range(100):
for inputs, labels in dataloader:
optimizer.zero_grad()
outputs = calibrated_model(inputs)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
calibrated_model.apply_constraints()
Source code in pytorch_lattice/models/calibrated_lattice.py
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 |
|
__init__(features, clip_inputs=True, output_min=None, output_max=None, kernel_init=LatticeInit.LINEAR, interpolation=Interpolation.HYPERCUBE, output_calibration_num_keypoints=None)
Initializes an instance of CalibratedLattice
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
list[Union[NumericalFeature, CategoricalFeature]]
|
A list of numerical and/or categorical feature configs. |
required |
clip_inputs |
bool
|
Whether to restrict inputs to the bounds of lattice. |
True
|
output_min |
Optional[float]
|
The minimum output value for the model. If |
None
|
output_max |
Optional[float]
|
The maximum output value for the model. If |
None
|
kernel_init |
LatticeInit
|
the method of initializing kernel weights. If otherwise
unspecified, will default to |
LINEAR
|
interpolation |
Interpolation
|
the method of interpolation in the lattice's forward pass.
If otherwise unspecified, will default to |
HYPERCUBE
|
output_calibration_num_keypoints |
Optional[int]
|
The number of keypoints to use for the
output calibrator. If |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If any feature configs are not |
Source code in pytorch_lattice/models/calibrated_lattice.py
apply_constraints()
Constrains the model into desired constraints specified by the config.
Source code in pytorch_lattice/models/calibrated_lattice.py
assert_constraints(eps=1e-06)
Asserts all layers within model satisfied specified constraints.
Asserts monotonicity pairs and output bounds for categorical calibrators, monotonicity and output bounds for numerical calibrators, and monotonicity and weights summing to 1 if weighted_average for linear layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
dict[str, list[str]]
|
A dict where key is feature_name for calibrators and 'linear' for the linear |
dict[str, list[str]]
|
layer, and value is the error messages for each layer. Layers with no error |
dict[str, list[str]]
|
messages are not present in the dictionary. |
Source code in pytorch_lattice/models/calibrated_lattice.py
forward(x)
Runs an input through the network to produce a calibrated lattice output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor of feature values of shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Source code in pytorch_lattice/models/calibrated_lattice.py
pytorch_lattice.models.CalibratedLinear
Bases: ConstrainedModule
PyTorch Calibrated Linear Model.
Creates a torch.nn.Module
representing a calibrated linear model, which will be
constructed using the provided model configuration. Note that the model inputs
should match the order in which they are defined in the feature_configs
.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
calibrators |
A dictionary that maps feature names to their calibrators. |
|
linear |
The |
|
output_calibrator |
The output |
Example:
feature_configs = [...]
calibrated_model = pyl.models.CalibratedLinear(feature_configs, ...)
loss_fn = torch.nn.MSELoss()
optimizer = torch.optim.Adam(calibrated_model.parameters(recurse=True), lr=1e-1)
dataset = pyl.utils.data.Dataset(...)
dataloader = torch.utils.data.DataLoader(dataset, batch_size=64, shuffle=True)
for epoch in range(100):
for inputs, labels in dataloader:
optimizer.zero_grad()
outputs = calibrated_model(inputs)
loss = loss_fn(outputs, labels)
loss.backward()
optimizer.step()
calibrated_model.apply_constraints()
Source code in pytorch_lattice/models/calibrated_linear.py
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 |
|
__init__(features, output_min=None, output_max=None, use_bias=True, output_calibration_num_keypoints=None)
Initializes an instance of CalibratedLinear
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
features |
list[Union[NumericalFeature, CategoricalFeature]]
|
A list of numerical and/or categorical feature configs. |
required |
output_min |
Optional[float]
|
The minimum output value for the model. If |
None
|
output_max |
Optional[float]
|
The maximum output value for the model. If |
None
|
use_bias |
bool
|
Whether to use a bias term for the linear combination. If any of
|
True
|
output_calibration_num_keypoints |
Optional[int]
|
The number of keypoints to use for the
output calibrator. If |
None
|
Raises:
Type | Description |
---|---|
ValueError
|
If any feature configs are not |
Source code in pytorch_lattice/models/calibrated_linear.py
apply_constraints()
Constrains the model into desired constraints specified by the config.
Source code in pytorch_lattice/models/calibrated_linear.py
assert_constraints(eps=1e-06)
Asserts all layers within model satisfied specified constraints.
Asserts monotonicity pairs and output bounds for categorical calibrators, monotonicity and output bounds for numerical calibrators, and monotonicity and weights summing to 1 if weighted_average for linear layer.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
Union[list[str], dict[str, list[str]]]
|
A dict where key is feature_name for calibrators and 'linear' for the linear |
Union[list[str], dict[str, list[str]]]
|
layer, and value is the error messages for each layer. Layers with no error |
Union[list[str], dict[str, list[str]]]
|
messages are not present in the dictionary. |
Source code in pytorch_lattice/models/calibrated_linear.py
forward(x)
Runs an input through the network to produce a calibrated linear output.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor of feature values of shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Source code in pytorch_lattice/models/calibrated_linear.py
pytorch_lattice.models.features.CategoricalFeature
Feature configuration for categorical features.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
category_indices |
A dictionary mapping string categories to their index. |
|
monotonicity_index_pairs |
A conversion of |
Source code in pytorch_lattice/models/features.py
__init__(feature_name, categories, missing_input_value=None, monotonicity_pairs=None, lattice_size=2)
Initializes a CategoricalFeatureConfig
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_name |
str
|
The name of the feature. This should match the header for the column in the dataset representing this feature. |
required |
categories |
Union[list[int], list[str]]
|
The categories that should be used for this feature. Any categories not contained will be considered missing or unknown. If you expect to have such missing categories, make sure to |
required |
missing_input_value |
Optional[float]
|
If provided, this feature's calibrator will learn to map all instances of this missing input value to a learned output value. |
None
|
monotonicity_pairs |
Optional[list[tuple[str, str]]]
|
List of pairs of categories |
None
|
lattice_size |
int
|
The default number of keypoints outputted by the calibrator.
Only used within |
2
|
Source code in pytorch_lattice/models/features.py
pytorch_lattice.models.features.NumericalFeature
Feature configuration for numerical features.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
input_keypoints |
The input keypoints used for this feature's calibrator. These
keypoints will be initialized using the given |
Source code in pytorch_lattice/models/features.py
__init__(feature_name, data, num_keypoints=5, input_keypoints_init=InputKeypointsInit.QUANTILES, missing_input_value=None, monotonicity=None, projection_iterations=8, lattice_size=2)
Initializes a NumericalFeatureConfig
instance.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
feature_name |
str
|
The name of the feature. This should match the header for the column in the dataset representing this feature. |
required |
data |
ndarray
|
Numpy array of float-valued data used for calculating keypoint inputs and initializing keypoint outputs. |
required |
num_keypoints |
int
|
The number of keypoints used by the underlying piece-wise
linear function of a NumericalCalibrator. There will be
|
5
|
input_keypoints_init |
InputKeypointsInit
|
The scheme to use for initializing the input
keypoints. See |
QUANTILES
|
missing_input_value |
Optional[float]
|
If provided, this feature's calibrator will learn to map all instances of this missing input value to a learned output value. |
None
|
monotonicity |
Optional[Monotonicity]
|
Monotonicity constraint for this feature, if any. |
None
|
projection_iterations |
int
|
Number of times to run Dykstra's projection algorithm when applying constraints. |
8
|
lattice_size |
int
|
The default number of keypoints outputted by the
calibrator. Only used within |
2
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |