gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core module

Controlled sampling of concatenated encodings via Gaussian Process.

Summary

Classes:

GaussianProcessRepresentationsSampler

Generator

Objective

Objective function for representations.

PropertyPredictor

Property prediction class.

Representation

A generic representation for a composition problem.

Scaler

Scaler class.

Functions:

point_to_tensor

Convert point to tensor.

Reference

point_to_tensor(point)[source]

Convert point to tensor.

Parameters

point (Union[ndarray, Tensor, Series]) – a point.

Return type

Tensor

Returns

tensor representing a point.

class Representation(model, z_dimension, fixed_representation=None, z_index=None)[source]

Bases: object

A generic representation for a composition problem.

model

a torch module for decoding.

Type

torch.nn.modules.module.Module

z_dimension

dimension of the latent space.

Type

int

fixed_representation

fixed representation in the latent space.

Type

Optional[torch.Tensor]

z_index

slice for indexing a point to represent the latent space.

Type

Optional[slice]

model: Module
z_dimension: int
fixed_representation: Optional[Tensor] = None
z_index: Optional[slice] = None
decode(z)[source]

Decode the representation from the latent space.

Parameters

z (Union[ndarray, Tensor, Series]) – a point in the latent space.

Return type

Any

Returns

the decoded representation.

deserialize(filepath, device=None)[source]

Deserialize a representation from file.

Parameters
  • filepath (str) – path to the serialized represantation.

  • device (Union[device, str, None]) – device where the inference is running either as a dedicated class or a string. If not provided is inferred.

Return type

None

Returns

the representation object.

__annotations__ = {'fixed_representation': typing.Optional[torch.Tensor], 'model': <class 'torch.nn.modules.module.Module'>, 'z_dimension': <class 'int'>, 'z_index': typing.Optional[slice]}
__dataclass_fields__ = {'fixed_representation': Field(name='fixed_representation',type=typing.Optional[torch.Tensor],default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'model': Field(name='model',type=<class 'torch.nn.modules.module.Module'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'z_dimension': Field(name='z_dimension',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'z_index': Field(name='z_index',type=typing.Optional[slice],default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}
__dataclass_params__ = _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False)
__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', '__annotations__': {'model': <class 'torch.nn.modules.module.Module'>, 'z_dimension': <class 'int'>, 'fixed_representation': typing.Optional[torch.Tensor], 'z_index': typing.Optional[slice]}, '__doc__': '\n    A generic representation for a composition problem.\n\n    Attributes:\n        model: a torch module for decoding.\n        z_dimension: dimension of the latent space.\n        fixed_representation: fixed representation in the latent space.\n        z_index: slice for indexing a point to represent the latent space.\n    ', 'fixed_representation': None, 'z_index': None, 'decode': <function Representation.decode>, 'deserialize': <function Representation.deserialize>, '__dict__': <attribute '__dict__' of 'Representation' objects>, '__weakref__': <attribute '__weakref__' of 'Representation' objects>, '__dataclass_params__': _DataclassParams(init=True,repr=True,eq=True,order=False,unsafe_hash=False,frozen=False), '__dataclass_fields__': {'model': Field(name='model',type=<class 'torch.nn.modules.module.Module'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'z_dimension': Field(name='z_dimension',type=<class 'int'>,default=<dataclasses._MISSING_TYPE object>,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'fixed_representation': Field(name='fixed_representation',type=typing.Optional[torch.Tensor],default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD), 'z_index': Field(name='z_index',type=typing.Optional[slice],default=None,default_factory=<dataclasses._MISSING_TYPE object>,init=True,repr=True,hash=None,compare=True,metadata=mappingproxy({}),kw_only=False,_field_type=_FIELD)}, '__init__': <function Representation.__init__>, '__repr__': <function Representation.__repr__>, '__eq__': <function Representation.__eq__>, '__hash__': None, '__match_args__': ('model', 'z_dimension', 'fixed_representation', 'z_index')})
__doc__ = '\n    A generic representation for a composition problem.\n\n    Attributes:\n        model: a torch module for decoding.\n        z_dimension: dimension of the latent space.\n        fixed_representation: fixed representation in the latent space.\n        z_index: slice for indexing a point to represent the latent space.\n    '
__eq__(other)

Return self==value.

__hash__ = None
__init__(model, z_dimension, fixed_representation=None, z_index=None)
__match_args__ = ('model', 'z_dimension', 'fixed_representation', 'z_index')
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__repr__()

Return repr(self).

__weakref__

list of weak references to the object (if defined)

class PropertyPredictor[source]

Bases: object

Property prediction class.

input_representations

order of the input representations.

Type

Optional[List[str]]

input_representations: Optional[List[str]] = None
__call__(z)[source]

Call the property predictor on the point.

Parameters

z (Union[ndarray, Tensor, Series]) – the point.

Return type

float

Returns

the predicted property.

__annotations__ = {'input_representations': typing.Optional[typing.List[str]]}
__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', '__annotations__': {'input_representations': typing.Optional[typing.List[str]]}, '__doc__': 'Property prediction class.\n\n    Attributes:\n        input_representations: order of the input representations.\n    ', 'input_representations': None, '__call__': <function PropertyPredictor.__call__>, '__dict__': <attribute '__dict__' of 'PropertyPredictor' objects>, '__weakref__': <attribute '__weakref__' of 'PropertyPredictor' objects>})
__doc__ = 'Property prediction class.\n\n    Attributes:\n        input_representations: order of the input representations.\n    '
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__weakref__

list of weak references to the object (if defined)

class Scaler[source]

Bases: object

Scaler class.

__call__(example)[source]

Scale the example appropriately.

Parameters

example (Any) – an example prior to scaling.

Return type

Any

Returns

the scaled example.

__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', '__doc__': 'Scaler class.', '__call__': <function Scaler.__call__>, '__dict__': <attribute '__dict__' of 'Scaler' objects>, '__weakref__': <attribute '__weakref__' of 'Scaler' objects>, '__annotations__': {}})
__doc__ = 'Scaler class.'
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__weakref__

list of weak references to the object (if defined)

class Objective(targets, property_predictors, representations, representation_order=None, scalers=None, weights=None, custom_score_function=None, custom_score_weight=1.0, minimize=True)[source]

Bases: object

Objective function for representations.

__init__(targets, property_predictors, representations, representation_order=None, scalers=None, weights=None, custom_score_function=None, custom_score_weight=1.0, minimize=True)[source]

Constructs an objective function.

Parameters
  • targets (Dict[str, float]) – a dictionary of target values.

  • property_predictors (Dict[str, PropertyPredictor]) – a dictionary of target property predictors.

  • representations (Dict[str, Representation]) – a dictionary of decodeable representations.

  • representation_order (Optional[List[str], None]) – order of the representations. Defaults to None, a.k.a., lexicographic order.

  • scalers (Optional[Dict[str, Scaler], None]) – scalers for represantation features. Defaults to None, a.k.a., no scaling.

  • weights (Optional[Dict[str, float], None]) – weights for each the target. Defaults to None, a.k.a., targets evenly weigthed.

  • custom_score_function (Optional[Callable[[Union[ndarray, Tensor, Series], Dict[str, Representation], Optional[Dict[str, Scaler], None]], float], None]) – a custom score function to apply on decoded representations. Defaults to None, a.k.a., no custom score.

  • custom_score_weight (float) – weight for the custom score. Defaults to 1.0.

  • minimize (bool) – whether the objective needs to be minimized. Defaults to True.

construct_property_representation(z, property_name)[source]

Construct a representation for a specific property.

The encoded point and fixed encodings (or slices thereof) if available are concatenated in the right order.

Todo

Check explanation and improve it.

Parameters
  • z (Tensor) – the point.

  • property_name (str) – name of the property for which to construct the representation.

Return type

Tensor

Returns

representation for which a specific property can be predicted.

evaluate(z)[source]

Evaluate objective function for a point in latent space.

Parameters

z (Union[ndarray, Tensor, Series]) – the point.

Return type

float

Returns

the score of the point.

__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', '__doc__': '\n    Objective function for representations.\n    ', '__init__': <function Objective.__init__>, 'construct_property_representation': <function Objective.construct_property_representation>, 'evaluate': <function Objective.evaluate>, '__dict__': <attribute '__dict__' of 'Objective' objects>, '__weakref__': <attribute '__weakref__' of 'Objective' objects>, '__annotations__': {}})
__doc__ = '\n    Objective function for representations.\n    '
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__weakref__

list of weak references to the object (if defined)

class GaussianProcessRepresentationsSampler(targets, property_predictors, representations, representation_order=None, bounds=None, scalers=None, weights=None, custom_score_function=None, custom_score_weight=1.0, minimize=True, random_state=42, random_starts=10)[source]

Bases: object

__init__(targets, property_predictors, representations, representation_order=None, bounds=None, scalers=None, weights=None, custom_score_function=None, custom_score_weight=1.0, minimize=True, random_state=42, random_starts=10)[source]

Constucts a GaussianProcessRepresentationsSampler.

Parameters
  • targets (Dict[str, float]) – a dictionary of target values.

  • property_predictors (Dict[str, PropertyPredictor]) – a dictionary of target property predictors.

  • representations (Dict[str, Representation]) – a dictionary of decodeable representations.

  • representation_order (Optional[List[str], None]) – order of the representations. Defaults to None, a.k.a., lexicographic order.

  • bounds (Optional[Dict[str, Union[List[Tuple[float, float]], Tuple[float, float]]], None]) – bounds for the optmization. Defaults to None, a.k.a., unbounded.

  • scalers (Optional[Dict[str, Scaler], None]) – scalers for represantation features. Defaults to None, a.k.a., no scaling.

  • weights (Optional[Dict[str, float], None]) – weights for each the target. Defaults to None, a.k.a., targets evenly weigthed.

  • custom_score_function (Optional[Callable[[Union[ndarray, Tensor, Series], Dict[str, Representation], Optional[Dict[str, Scaler], None]], float], None]) – a custom score function to apply on decoded representations. Defaults to None, a.k.a., no custom score.

  • custom_score_weight (float) – weight for the custom score. Defaults to 1.0.

  • minimize (bool) – whether the objective needs to be minimized. Defaults to True.

  • random_state (int) – random state. Defaults to 42.

  • random_starts (int) – number of random restarts. Defaults to 10.

_get_bounds(minimum_value, maximum_value, z_dimension)[source]

Define a list of bounds for an hypercube.

Parameters
  • minimum_value (Union[float, float32]) – minimum value.

  • maximum_value (Union[float, float32]) – maximum value.

  • z_dimension (int) – dimension of the hypercube.

Return type

List[Tuple[Union[float, float32], Union[float, float32]]]

Returns

the list of bounds.

set_bounds(bounds=None)[source]

Set the bounds for the optimization.

Parameters

bounds (Optional[Dict[str, Union[List[Tuple[float, float]], Tuple[float, float]]], None]) – bounds for the optmization. Defaults to None, a.k.a., unbounded.

Return type

None

define_dimensions(representation_order)[source]

Define the dimensions of the optimization space.

Parameters

representation_order (List[str]) – order of the representations.

Return type

List[Real]

Returns

a list of dimensions.

__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', '__init__': <function GaussianProcessRepresentationsSampler.__init__>, '_get_bounds': <function GaussianProcessRepresentationsSampler._get_bounds>, 'set_bounds': <function GaussianProcessRepresentationsSampler.set_bounds>, 'define_dimensions': <function GaussianProcessRepresentationsSampler.define_dimensions>, 'optimize': <function GaussianProcessRepresentationsSampler.optimize>, '__dict__': <attribute '__dict__' of 'GaussianProcessRepresentationsSampler' objects>, '__weakref__': <attribute '__weakref__' of 'GaussianProcessRepresentationsSampler' objects>, '__doc__': None, '__annotations__': {}})
__doc__ = None
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__weakref__

list of weak references to the object (if defined)

optimize(targets=None, relevant_representations=None, representation_order=None, z0=None, number_of_points=1, number_of_steps=50, random_state=42, verbose=False, weights=None, acquisition_method='PI')[source]

Run the optimization.

Parameters
  • targets (Optional[Dict[str, float], None]) – a dictionary of target values. Defaults to None, a.k.a., use the ones defined at construction time.

  • relevant_representations (Optional[List[str], None]) – list of relevant representations to be optimized. Defaults to None, a.k.a., inferred from non fixed representations.

  • representation_order (Optional[List[str], None]) – order of the representations. Defaults to None, a.k.a., use the one defined at construction time.

  • z0 (Union[ndarray, Tensor, Series, None]) – the starting point for the optimization. Defaults to None, a.k.a., perform random starts.

  • number_of_points (int) – number of optimal points to return. Defaults to 1.

  • number_of_steps (int) – number of optimization steps. Defaults to 50.

  • random_state (int) – random state. Defaults to 42.

  • verbose (bool) – control verbosity. Defaults to False.

  • weights (Optional[Dict[str, float], None]) – weights for each the target. Defaults to None, a.k.a., use the ones defined at construction time.

  • acquisition_method (str) – acquisition method to use in the Gaussian Process optimization. Defaults to “PI”. More details at: https://scikit-optimize.github.io/stable/modules/generated/skopt.gp_minimize.html.

Raises

NotImplementedError – invalid acquisition function.

Return type

List[Dict[str, Any]]

Returns

list of orderd optimal points with decoded relevant representations.

class Generator[source]

Bases: object

__dict__ = mappingproxy({'__module__': 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core', 'generate_samples': <function Generator.generate_samples>, '__dict__': <attribute '__dict__' of 'Generator' objects>, '__weakref__': <attribute '__weakref__' of 'Generator' objects>, '__doc__': None, '__annotations__': {}})
__doc__ = None
__module__ = 'gt4sd.algorithms.controlled_sampling.advanced_manufacturing.implementation.core'
__weakref__

list of weak references to the object (if defined)

generate_samples(target)[source]

Generate samples.

Parameters

target (Any) – target for generation.

Return type

List[Any]

Returns

samples generated.