enchanter.utils¶
enchanter.utils.comet¶
- class
enchanter.utils.comet.TunerConfigGenerator(algorithm: str = 'bayes', metric: str = 'validate_avg_loss', objective: str = 'minimize', seed: Optional[int] = None, max_combo: int = 0, grid_size: int = 10, min_sample_size: int = 100, retry_limit: int = 20, retry_assign_limit: int = 0, name: Optional[str] = None, trials: int = 1)[source]¶ Bases:
objectSee https://www.comet.ml/docs/python-sdk/introduction-optimizer/ for a more detailed explanation of each argument.
- Parameters
algorithm – Specifies the algorithm used for parameter tuning. The supported algorithms are
['grid','random','bayes'].metric – Specify the value to be minimized / maximized. By default,``validate_avg_loss`` is specified.
objective – Specifies whether to maximize / minimize metrics. Specify with
['minimize','maximize'].seed – Set the seed value. Not specified by default.
max_combo – Integer. Limit on the combination of parameters to try (default is 0, meaning no limit)
grid_size – Integer. Number of bins per parameter when creating a grid (default is 10).
min_sample_size – Integer. Number of samples to help find a suitable grid range (default is 100).
retry_limit – integer. A limit that attempts to create a unique set of parameters before suspending (default is 20).
retry_assign_limit –
name – String. A personalizable name associated with this search instance. (option)
trials – Specifies the number of trials in a single experiment.
Methods
export(filename)It is a method to save the created Config as a file.
generate()A method for generating a Config for
comet_ml.Optimizer.suggest_categorical(name, values)A method for searching categorical variables.
suggest_linear(name, min_value, max_value[, …])If Integer, independent distribution.
suggest_uniform(name, min_value, max_value)This is a method for sampling and searching variables from a uniform distribution.
suggest_normal(name, min_value, max_value[, …])This is a method for sampling and searching variables from a normal distribution.
suggest_lognormal(name, min_value, max_value)A method for sampling and searching variables from a lognormal distribution.
suggest_loguniform(name, min_value, max_value)This is a method for sampling and searching variables from a uniform distribution.
suggest_discrete(name, values)This method is used to search for the specified numeric type variable.
-
generate() → Dict[str, Any][source]¶ A method for generating a Config for
comet_ml.Optimizer.Examples
>>> cfg = TunerConfigGenerator() >>> cfg.suggest_discrete("discrete", [10, 20, 30]) >>> cfg_dict = cfg.generate()
-
suggest_categorical(name: str, values: List[str])[source]¶ A method for searching categorical variables.
- Parameters
name – Specify the name of the variable.
values – I have a variable to search. Due to the specifications of comet.ml, it is necessary to give a list of strings.
Examples
>>> import comet_ml >>> from enchanter.utils.comet import TunerConfigGenerator >>> config = TunerConfigGenerator() >>> config.suggest_categorical("activation", ["torch.relu", "torch.sigmoid", "torch.softmax"]) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> activation = experiment.get_parameter(eval("activation"))
-
suggest_discrete(name: str, values: List[Union[int, float]])[source]¶ This method is used to search for the specified numeric type variable.
- Parameters
name – Variable name
values – A list of numeric elements
Examples
>>> import comet_ml >>> config = TunerConfigGenerator() >>> config.suggest_discrete("discrete", [10, 20, 30]) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> discrete = experiment.get_parameter("discrete")
-
suggest_linear(name: str, min_value: Union[float, int], max_value: Union[float, int], dtype: Optional[type] = None)[source]¶ If Integer, independent distribution. else if float the same as uniform.
- Parameters
name –
min_value –
max_value –
dtype –
Returns:
-
suggest_lognormal(name: str, min_value: Union[float, int], max_value: Union[float, int], mu: float = 0.0, sigma: float = 1.0, dtype: Optional[type] = None)[source]¶ A method for sampling and searching variables from a lognormal distribution.
- Parameters
name – Variable name
min_value – minimum value
max_value – Maximum value
mu – Normal distribution μ
sigma – Normal distribution σ
dtype – Data type. If not specified, it is automatically estimated from the values of
min_valueand` max_value`.
Examples
>>> import comet_ml >>> config = TunerConfigGenerator() >>> config.suggest_lognormal("lognormal", 0.0, 1.0) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> lognormal = experiment.get_parameter("lognormal")
-
suggest_loguniform(name: str, min_value: Union[float, int], max_value: Union[float, int], dtype: Optional[type] = None)[source]¶ This is a method for sampling and searching variables from a uniform distribution.
- Parameters
name – Variable name
min_value – minimum value
max_value – Maximum value
dtype – Data type. If not specified, it is automatically estimated from the values of
min_valueand` max_value`.
Examples
>>> import comet_ml >>> config = TunerConfigGenerator() >>> config.suggest_loguniform("loguniform", 0.0, 1.0) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> loguniform = experiment.get_parameter("loguniform")
-
suggest_normal(name: str, min_value: Union[float, int], max_value: Union[float, int], mu: float = 0.0, sigma: float = 1.0, dtype: Optional[type] = None)[source]¶ This is a method for sampling and searching variables from a normal distribution.
- Parameters
name – Variable name
min_value – minimum value
max_value – Maximum value
mu – Normal distribution μ
sigma – Normal distribution σ
dtype – Data type. If not specified, it is automatically estimated from the values of
min_valueand` max_value`.
Examples
>>> import comet_ml >>> config = TunerConfigGenerator() >>> config.suggest_normal("normal", 0.0, 1.0) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> normal = experiment.get_parameter("normal")
-
suggest_uniform(name: str, min_value: Union[float, int], max_value: Union[float, int], dtype: Optional[type] = None)[source]¶ This is a method for sampling and searching variables from a uniform distribution.
- Parameters
name – Variable name
min_value – minimum value
max_value – Maximum value
dtype – Data type. If not specified, it is automatically estimated from the values of
min_valueand` max_value`.
Examples
>>> import comet_ml >>> config = TunerConfigGenerator() >>> config.suggest_uniform("uniform", 0.0, 1.0) >>> opt = comet_ml.Optimizer(config.generate()) >>> for experiment in opt.get_experiments(): >>> uniform = experiment.get_parameter("uniform")
enchanter.utils.backend¶
-
enchanter.utils.backend.is_scalar(data: Union[numbers.Number, numpy.ndarray, torch.Tensor]) → bool[source]¶ Returns True if the type of
datais a scalar type.- Parameters
data (Union[Number, Union[np.ndarray, torch.Tensor]]) – Numerical value
- Returns
True if
datais a scalar type, False if it is not.
Examples
>>> a = torch.tensor([1.0]) >>> is_scalar(a) # True >>> a = torch.tensor(1.0) >>> is_scalar(a) # True >>> a = torch.tensor([1, 2, 3]) >>> is_scalar(a) # False >>> a = 1.0 >>> is_scalar(a) # True
-
enchanter.utils.backend.slice_axis(data: torch.Tensor, axis: int, begin: int, end: int) → torch.Tensor[source]¶ Examples
>>> import torch >>> x = torch.tensor([ >>> [ 1., 2., 3., 4.], >>> [ 5., 6., 7., 8.], >>> [ 9., 10., 11., 12.] >>> ]) >>> >>> slice_axis(x, axis=0, begin=1, end=3) >>> # [[ 5., 6., 7., 8.], >>> # [ 9., 10., 11., 12.]] >>> >>> slice_axis(x, axis=1, begin=0, end=2) >>> # [[ 1., 2.], >>> # [ 5., 6.], >>> # [ 9., 10.]] >>> >>> slice_axis(x, axis=1, begin=-3, end=-1) >>> # [[ 2., 3.], >>> # [ 6., 7.], >>> # [ 10., 11.]]
References
- Parameters
data – Source input
axis – Axis along which to be sliced
begin – The beginning index along the axis to be sliced
end – The ending index along the axis to be sliced
- Returns
output - the output of this function.
enchanter.utils.visualize¶
-
enchanter.utils.visualize.with_netron(model: torch.nn.modules.module.Module, dummy: Tuple[torch.Tensor, …], backend: str = 'onnx', open_browser: bool = True, port: int = 8080, host: str = 'localhost')[source]¶ Visualize the PyTorch graph in a browser.
Examples
>>> from enchanter.addons.layers import AutoEncoder >>> x = torch.randn(1, 32) # [N, in_features] >>> model = AutoEncoder([32, 16, 8, 2]) >>> with_netron(model, (x, ))
Warning
Models that cannot be graphed using TorchScript or ONNX cannot be visualized.
- Parameters
model – PyTorch model
dummy – dummy input for generate graph
backend – specified graph format. [torchscript or onnx]. default onnx.
open_browser – if True, open browser.
port – port number. default: 8080.
host – hostname. default: localhost.