1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350 | # Copyright 2019-2020 The GPflow Contributors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
This is a private module that manages GPflow configuration.
The module provides functions to modify default settings of GPflow, such as:
- the standard float precision and integer type
- the type of positive transformation
- a value for a minimum shift from zero for the positive transformation
- an output format for `gpflow.utilities.print_summary`
The module holds global configuration :class:`Config` variable that stores all
setting values.
Environment variables are an alternative way for changing the default GPflow
configuration.
.. warning::
The user has to set environment variables before running python
interpreter to modify the configuration.
Full set of environment variables and available options:
* ``GPFLOW_INT``: "int16", "int32", or "int64"
* ``GPFLOW_FLOAT``: "float16", "float32", or "float64"
* ``GPFLOW_POSITIVE_BIJECTOR``: "exp" or "softplus"
* ``GPFLOW_POSITIVE_MINIMUM``: Any positive float number
* ``GPFLOW_SUMMARY_FMT``: "notebook" or any other format that :mod:`tabulate` can handle.
* ``GPFLOW_JITTER``: Any positive float number
The user can also change the GPflow configuration temporarily with a context
manager :func:`as_context`:
>>> config = Config(jitter=1e-5)
>>> with as_context(config):
>>> # ...code here sees new config
"""
import contextlib
import enum
import os
from dataclasses import dataclass, field, replace
from typing import Any, Dict, Generator, List, Mapping, Optional, Union
import numpy as np
import tabulate
import tensorflow as tf
import tensorflow_probability as tfp
__all__ = [
"Config",
"as_context",
"config",
"default_float",
"default_int",
"default_jitter",
"default_positive_bijector",
"default_positive_minimum",
"default_summary_fmt",
"positive_bijector_type_map",
"set_config",
"set_default_float",
"set_default_int",
"set_default_jitter",
"set_default_positive_bijector",
"set_default_positive_minimum",
"set_default_summary_fmt",
]
__config: Optional["Config"] = None
class _Values(enum.Enum):
"""Setting's names collection with default values. The `name` method returns name
of the environment variable. E.g. for `SUMMARY_FMT` field the environment variable
will be `GPFLOW_SUMMARY_FMT`."""
INT = np.int32
FLOAT = np.float64
POSITIVE_BIJECTOR = "softplus"
POSITIVE_MINIMUM = 0.0
SUMMARY_FMT = "fancy_grid"
JITTER = 1e-6
@property
def name(self) -> str: # type: ignore # name is generated and has weird typing.
return f"GPFLOW_{super().name}"
def _default(value: _Values) -> Any:
"""Checks if value is set in the environment."""
return os.getenv(value.name, default=value.value)
def _default_numeric_type_factory(
valid_types: Mapping[str, type], enum_key: _Values, type_name: str
) -> type:
value: Union[str, type] = _default(enum_key)
if isinstance(value, type) and (value in valid_types.values()):
return value
assert isinstance(value, str) # Hint for mypy
if value not in valid_types:
raise TypeError(f"Config cannot recognize {type_name} type.")
return valid_types[value]
def _default_int_factory() -> type:
valid_types = dict(int16=np.int16, int32=np.int32, int64=np.int64)
return _default_numeric_type_factory(valid_types, _Values.INT, "int")
def _default_float_factory() -> type:
valid_types = dict(float16=np.float16, float32=np.float32, float64=np.float64)
return _default_numeric_type_factory(valid_types, _Values.FLOAT, "float")
def _default_jitter_factory() -> float:
value = _default(_Values.JITTER)
try:
return float(value)
except ValueError:
raise TypeError("Config cannot set the jitter value with non float type.")
def _default_positive_bijector_factory() -> str:
bijector_type: str = _default(_Values.POSITIVE_BIJECTOR)
if bijector_type not in positive_bijector_type_map().keys():
raise TypeError(
"Config cannot set the passed value as a default positive bijector."
f"Available options: {set(positive_bijector_type_map().keys())}"
)
return bijector_type
def _default_positive_minimum_factory() -> float:
value = _default(_Values.POSITIVE_MINIMUM)
try:
return float(value)
except ValueError:
raise TypeError("Config cannot set the positive_minimum value with non float type.")
def _default_summary_fmt_factory() -> Optional[str]:
result: Optional[str] = _default(_Values.SUMMARY_FMT)
return result
# The following type alias is for the Config class, to help a static analyser distinguish
# between the built-in 'float' type and the 'float' type defined in the that class.
Float = Union[float]
@dataclass(frozen=True)
class Config:
"""
Immutable object for storing global GPflow settings
"""
int: type = field(default_factory=_default_int_factory)
"""Integer data type, int32 or int64."""
float: type = field(default_factory=_default_float_factory)
"""Float data type, float32 or float64"""
jitter: Float = field(default_factory=_default_jitter_factory)
"""
Jitter value. Mainly used for for making badly conditioned matrices more stable.
Default value is `1e-6`.
"""
positive_bijector: str = field(default_factory=_default_positive_bijector_factory)
"""
Method for positive bijector, either "softplus" or "exp".
Default is "softplus".
"""
positive_minimum: Float = field(default_factory=_default_positive_minimum_factory)
"""Lower bound for the positive transformation."""
summary_fmt: Optional[str] = field(default_factory=_default_summary_fmt_factory)
"""Summary format for module printing."""
def config() -> Config:
"""Returns current active config."""
assert __config is not None, "__config is None. This should never happen."
return __config
def default_int() -> type:
"""Returns default integer type"""
return config().int
def default_float() -> type:
"""Returns default float type"""
return config().float
def default_jitter() -> float:
"""
The jitter is a constant that GPflow adds to the diagonal of matrices
to achieve numerical stability of the system when the condition number
of the associated matrices is large, and therefore the matrices nearly singular.
"""
return config().jitter
def default_positive_bijector() -> str:
"""Type of bijector used for positive constraints: exp or softplus."""
return config().positive_bijector
def default_positive_minimum() -> float:
"""Shift constant that GPflow adds to all positive constraints."""
return config().positive_minimum
def default_summary_fmt() -> Optional[str]:
"""Summary printing format as understood by :mod:`tabulate` or a special case "notebook"."""
return config().summary_fmt
def set_config(new_config: Config) -> None:
"""Update GPflow config with new settings from `new_config`."""
global __config
__config = new_config
def set_default_int(value_type: type) -> None:
"""
Sets default integer type. Available options are ``np.int16``, ``np.int32``,
or ``np.int64``.
"""
try:
tf_dtype = tf.as_dtype(value_type) # Test that it's a tensorflow-valid dtype
except TypeError:
raise TypeError(f"{value_type} is not a valid tf or np dtype")
if not tf_dtype.is_integer:
raise TypeError(f"{value_type} is not an integer dtype")
set_config(replace(config(), int=tf_dtype.as_numpy_dtype))
def set_default_float(value_type: type) -> None:
"""
Sets default float type. Available options are `np.float16`, `np.float32`,
or `np.float64`.
"""
try:
tf_dtype = tf.as_dtype(value_type) # Test that it's a tensorflow-valid dtype
except TypeError:
raise TypeError(f"{value_type} is not a valid tf or np dtype")
if not tf_dtype.is_floating:
raise TypeError(f"{value_type} is not a float dtype")
set_config(replace(config(), float=tf_dtype.as_numpy_dtype))
def set_default_jitter(value: float) -> None:
"""
Sets constant jitter value.
The jitter is a constant that GPflow adds to the diagonal of matrices
to achieve numerical stability of the system when the condition number
of the associated matrices is large, and therefore the matrices nearly singular.
"""
if not (
isinstance(value, (tf.Tensor, np.ndarray)) and len(value.shape) == 0
) and not isinstance(value, float):
raise TypeError("Expected float32 or float64 scalar value")
if value < 0:
raise ValueError("Jitter must be non-negative")
set_config(replace(config(), jitter=value))
def set_default_positive_bijector(value: str) -> None:
"""
Sets positive bijector type.
There are currently two options implemented: "exp" and "softplus".
"""
type_map = positive_bijector_type_map()
if isinstance(value, str):
value = value.lower()
if value not in type_map:
raise ValueError(f"`{value}` not in set of valid bijectors: {sorted(type_map)}")
set_config(replace(config(), positive_bijector=value))
def set_default_positive_minimum(value: float) -> None:
"""Sets shift constant for positive transformation."""
if not (
isinstance(value, (tf.Tensor, np.ndarray)) and len(value.shape) == 0
) and not isinstance(value, float):
raise TypeError("Expected float32 or float64 scalar value")
if value < 0:
raise ValueError("Positive minimum must be non-negative")
set_config(replace(config(), positive_minimum=value))
def set_default_summary_fmt(value: Optional[str]) -> None:
formats: List[Optional[str]] = list(tabulate.tabulate_formats)
formats.extend(["notebook", None])
if value not in formats:
raise ValueError(f"Summary does not support '{value}' format")
set_config(replace(config(), summary_fmt=value))
def positive_bijector_type_map() -> Dict[str, type]:
return {
"exp": tfp.bijectors.Exp,
"softplus": tfp.bijectors.Softplus,
}
@contextlib.contextmanager
def as_context(temporary_config: Optional[Config] = None) -> Generator[None, None, None]:
"""Ensure that global configs defaults, with a context manager. Useful for testing."""
current_config = config()
temporary_config = replace(current_config) if temporary_config is None else temporary_config
try:
set_config(temporary_config)
yield
finally:
set_config(current_config)
# Set global config.
set_config(Config())
|