adaptive.LearnerND¶
LearnerND
¶
Bases: BaseLearner
Learns and predicts a function 'f: ℝ^N → ℝ^M'.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
The function to learn. Must take a tuple of N real parameters and return a real number or an arraylike of length M. |
required | |
bounds
|
list of 2-tuples or scipy.spatial.ConvexHull
|
A list |
required |
loss_per_simplex
|
callable
|
A function that returns the loss for a simplex. If not provided, then a default is used, which uses the deviation from a linear estimate, as well as triangle area, to determine the loss. |
None
|
anisotropic
|
bool
|
If True, the triangulation is stretched along the local gradient when choosing new points. Only works with scalar output. |
False
|
triangulation_backend
|
str or type
|
Which triangulation implementation to use: |
'auto'
|
Attributes:
| Name | Type | Description |
|---|---|---|
data |
dict
|
Sampled points and values. |
points |
numpy array
|
Coordinates of the currently known points |
values |
numpy array
|
The values of each of the known points |
pending_points |
set
|
Points that still have to be evaluated. |
Notes
The sample points are chosen by estimating the point where the gradient is maximal. This is based on the currently known points.
In practice, this sampling protocol results to sparser sampling of flat regions, and denser sampling of regions where the function has a high gradient, which is useful if the function is expensive to compute.
This sampling procedure is not fast, so to benefit from it, your function needs to be slow enough to compute.
This class keeps track of all known points. It triangulates these points and with every simplex it associates a loss. Then if you request points that you will compute in the future, it will subtriangulate a real simplex with the pending points inside it and distribute the loss among it's children based on volume.
Source code in adaptive/learner/learnerND.py
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 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 | |
loss_per_simplex
instance-attribute
¶
loss_per_simplex = loss_per_simplex or rust_default_loss or default_loss
vdim
property
¶
Length of the output of learner.function.
If the output is unsized (when it's a scalar)
then vdim = 1.
As long as no data is known vdim = 1.
tri
property
¶
An adaptive.learner.triangulation.Triangulation instance
with all the points of the learner.
new
¶
new() -> LearnerND
Create a new learner with the same function and bounds.
Source code in adaptive/learner/learnerND.py
to_numpy
¶
Data as NumPy array of size (npoints, dim+vdim), where dim is the
size of the input dimension and vdim is the length of the return value
of learner.function.
Source code in adaptive/learner/learnerND.py
to_dataframe
¶
to_dataframe(with_default_function_args: bool = True, function_prefix: str = 'function.', point_names: tuple[str, ...] = ('x', 'y', 'z'), value_name: str = 'value') -> DataFrame
Return the data as a pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_default_function_args
|
bool
|
Include the |
True
|
function_prefix
|
str
|
Prefix to the |
'function.'
|
point_names
|
tuple[str, ...]
|
Names of the input points, should be the same length as number of input parameters by default ("x", "y", "z" ) |
('x', 'y', 'z')
|
value_name
|
str
|
Name of the output value, by default "value" |
'value'
|
Returns:
| Type | Description |
|---|---|
DataFrame
|
|
Raises:
| Type | Description |
|---|---|
ImportError
|
If |
Source code in adaptive/learner/learnerND.py
load_dataframe
¶
load_dataframe(df: DataFrame, with_default_function_args: bool = True, function_prefix: str = 'function.', point_names: tuple[str, ...] = ('x', 'y', 'z'), value_name: str = 'value')
Load data from a pandas.DataFrame.
If with_default_function_args is True, then learner.function's
default arguments are set (using functools.partial) from the values
in the pandas.DataFrame.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
df
|
DataFrame
|
The data to load. |
required |
with_default_function_args
|
bool
|
The |
True
|
function_prefix
|
str
|
The |
'function.'
|
point_names
|
str
|
The |
('x', 'y', 'z')
|
value_name
|
str
|
The |
'value'
|
Source code in adaptive/learner/learnerND.py
tell
¶
Source code in adaptive/learner/learnerND.py
get_local_transform_matrix
¶
Source code in adaptive/learner/learnerND.py
inside_bounds
¶
Check whether a point is inside the bounds.
Source code in adaptive/learner/learnerND.py
tell_pending
¶
Source code in adaptive/learner/learnerND.py
ask
¶
loss
¶
remove_unfinished
¶
plot
¶
Plot the function we want to learn, only works in 2D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
the number of boxes in the interpolation grid along each axis |
None
|
tri_alpha
|
float (0 to 1)
|
Opacity of triangulation lines |
0
|
Source code in adaptive/learner/learnerND.py
plot_slice
¶
Plot a 1D or 2D interpolated slice of a N-dimensional function.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cut_mapping
|
dict (int → float)
|
for each fixed dimension the value, the other dimensions
are interpolated. e.g. |
required |
n
|
int
|
the number of boxes in the interpolation grid along each axis |
None
|
Source code in adaptive/learner/learnerND.py
1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 | |
plot_3D
¶
Plot the learner's data in 3D using plotly.
Does not work with the
adaptive.notebook_integration.live_plot functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
with_triangulation
|
bool
|
Add the verticices to the plot. |
False
|
return_fig
|
bool
|
Return the |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
plot |
plotly.offline.iplot object
|
The 3D plot of |
Source code in adaptive/learner/learnerND.py
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 | |
plot_isoline
¶
Plot the isoline at a specific level, only works in 2D.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
float
|
The value of the function at which you would like to see the isoline. |
0
|
n
|
int
|
The number of boxes in the interpolation grid along each axis.
This is passed to |
None
|
tri_alpha
|
float
|
The opacity of the overlaying triangulation. This is passed
to |
0
|
Returns:
| Type | Description |
|---|---|
Overlay
|
The plot of the isoline(s). This overlays a |
Source code in adaptive/learner/learnerND.py
plot_isosurface
¶
Plots a linearly interpolated isosurface.
This is the 3D analog of an isoline. Does not work with the
adaptive.notebook_integration.live_plot functionality.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
float
|
the function value which you are interested in. |
0.0
|
hull_opacity
|
float
|
the opacity of the hull of the domain. |
0.0
|
Returns:
| Name | Type | Description |
|---|---|---|
plot |
plotly.offline.iplot object
|
The plot object of the isosurface. |
Source code in adaptive/learner/learnerND.py
Custom loss functions¶
default_loss
¶
Computes the average of the volumes of the simplex.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simplex
|
list of tuples
|
Each entry is one point of the simplex. |
required |
values
|
list of values
|
The scaled function values of each of the simplex points. |
required |
value_scale
|
float
|
The scale of values, where |
required |
Returns:
| Name | Type | Description |
|---|---|---|
loss |
float
|
|
Source code in adaptive/learner/learnerND.py
uniform_loss
¶
Uniform loss.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simplex
|
list of tuples
|
Each entry is one point of the simplex. |
required |
values
|
list of values
|
The scaled function values of each of the simplex points. |
required |
value_scale
|
float
|
The scale of values, where |
required |
Returns:
| Name | Type | Description |
|---|---|---|
loss |
float
|
|
Source code in adaptive/learner/learnerND.py
std_loss
¶
Computes the loss of the simplex based on the standard deviation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
simplex
|
list of tuples
|
Each entry is one point of the simplex. |
required |
values
|
list of values
|
The scaled function values of each of the simplex points. |
required |
value_scale
|
float
|
The scale of values, where |
required |
Returns:
| Name | Type | Description |
|---|---|---|
loss |
float
|
|