twodlearn.core.common module¶
This module defines common structures used in twodlearn
-
class
twodlearn.core.common.
BoundedVariable
(min, max, initializer, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name='variable', variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None, options=None)[source]¶ Bases:
twodlearn.core.common.TransformedVariable
Creates a variable that can only take values inside a range
-
class
twodlearn.core.common.
ConstrainedVariable
(initial_value, min=0.0, max=inf, name=None)[source]¶
-
class
twodlearn.core.common.
EagerMethod
(finit=None, feval=None)[source]¶ Bases:
object
Decorator used to specify methods that perform operations using tensorflow, but are evaluated immediatly. These methods consist of an initialization method (speficied with the function given to the decorator) and an execute
-
class
twodlearn.core.common.
EncapsulatedMethod
(finit=None, feval=None)[source]¶ Bases:
object
Decorator used to specify methods that have a set of local variables.
These methods consist of an initialization method (speficied with the function given to the decorator) and an execute method.
-
class
twodlearn.core.common.
InferenceInput
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.InputArgument
Decorator used to specify a model input required to perform inference. The decorator works similar to @property, but the specified method correponds to the initialization of the argument, ussually checking for types and setting default values
-
class
twodlearn.core.common.
InputArgument
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.TdlDescriptor
Decorator used to specify the input arguments for a model. The decorator works similar to @property, but the specified method correponds to the initialization of the argument, ussually checking for types and setting default values.
-
class
twodlearn.core.common.
InputModel
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.InputArgument
Decorator used to specify the input models for a model. The decorator works similar to @property, but the specified method correponds to the initialization of the argument, ussually checking for types and setting default values
-
class
twodlearn.core.common.
InputModelInit
(finit=None, inference_input=None, lazzy=False, doc=None)[source]¶
-
class
twodlearn.core.common.
InputParameter
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.InputArgument
Decorator used to specify the input arguments for a model. These inputs will serve as parameters. The decorator works similar to @property, but the specified method correponds to the initialization of the argument, ussually checking for types and setting default values
-
class
twodlearn.core.common.
MethodInit
(finit=None, feval=None, doc=None)[source]¶ Bases:
object
Descriptor that encapsulates a set of local variables for the method. The following is an example of how this descriptor can be used:
Examples
The descriptor can be used to define both, an initialization method, and an evaluation method
class Example(tdl.core.TdlModel): @tdl.core.MethodInit def evaluate(self, local, units=20): local.model = tdf.core.dense.DenseLayer(units=units) @evaluate.eval def evaluate(self, local, inputs, n_samples=100): return local.model(inputs)/n_samples
-
class
twodlearn.core.common.
ModelMethod
(output_vars, input_vars, OutputClass=None)[source]¶ Bases:
object
Decorator used to specify an operation inside a model.
The decorator works similar to @property, but the specified method correponds to the definition of the operation.
Examples
Model methods can be used as follows
class MyModel(tdl.TdlModel): _submodels = ['evaluate'] @tdl.ModelMethod(['y'], # list of outputs ['x' ,'y'] # list of inputs ) def evaluate(self, x, u): return x+u
-
class
twodlearn.core.common.
OptionalProperty
(finit=None, fset=None)[source]¶ Bases:
object
Decorator used to specify an optional property inside a model. The decorator works similar to @property, but the specified method correponds to the initialization of the property
-
class
twodlearn.core.common.
OptionalPropertyWrapper
(obj, finit, fset, value)[source]¶ Bases:
twodlearn.core.common.TdlOp
-
class
twodlearn.core.common.
OutputValue
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.TdlDescriptor
Decorator used to specify the output value of a model. The decorator works similar to @property, but the specified method correponds to the definition of the output value. These methods are called at the end of the auto initialization procedure.
-
class
twodlearn.core.common.
ParameterInit
(finit=None, inference_input=None, lazzy=False, doc=None)[source]¶
-
class
twodlearn.core.common.
PositiveVariable
(initializer, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name='variable', variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None, options=None)[source]¶ Bases:
twodlearn.core.common.TransformedVariable
Creates a variable that can only take positive values
-
class
twodlearn.core.common.
PositiveVariable2
(initializer, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name='variable', variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None, options=None)[source]¶ Bases:
twodlearn.core.common.TransformedVariable
Creates a variable that can only take positive values. This function uses pow(x,2) as a reparameterization of the variable
-
class
twodlearn.core.common.
PositiveVariableExp
(initializer, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name='variable', variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None, options=None)[source]¶ Bases:
twodlearn.core.common.TransformedVariable
Creates a variable that can only take positive values. This function uses exp() as a reparameterization of the variable
-
class
twodlearn.core.common.
Regularizer
(finit=None, fset=None)[source]¶ Bases:
twodlearn.core.common.OptionalProperty
Decorator used to specify a regularizer for a model. The decorator works similar to @property, but the specified method correponds to the initialization of the regularizer.
-
class
twodlearn.core.common.
ScopedMethod
(feval=None)[source]¶ Bases:
object
adds a tf.name_scope before executing the method
-
class
twodlearn.core.common.
SimpleNamespace
[source]¶ Bases:
types.SimpleNamespace
SimpleNamespace that works with tf.convert_to_tensor and is searchable
-
class
twodlearn.core.common.
SimpleParameter
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.TdlDescriptor
Decorator used to specify a parameter inside a model. The decorator works similar to @property, but the specified method correponds to the initialization of the parameter
-
class
twodlearn.core.common.
Submodel
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.TdlDescriptor
Decorator used to specify a submodel inside a model. The decorator works similar to @property, but the specified method correponds to the initialization of the submodel.
-
class
twodlearn.core.common.
SubmodelInit
(finit=None, inference_input=None, lazzy=False, doc=None)[source]¶ Bases:
object
Indicate the initialization function for a property
Examples
Some examples of how can be used
class TestObject0(tdl.common.TdlObject): @tdl.core.SubmodelInit def submodel(self, x, y): return tdl.core.SimpleNamespace(x=x, y=y) class TestObject1(tdl.common.TdlObject): submodel = tdl.core.SubmodelInit(inference_input=True) @submodel.initializer def submodel(self, x, y): return tdl.core.SimpleNamespace(x=x, y=y) class TestObject2(tdl.common.TdlObject): @tdl.core.SubmodelInit(inference_input=True) def submodel(self, x, y): return tdl.core.SimpleNamespace(x=x, y=y)
-
init
(obj, val)[source]¶ initialization method called when TdlModel is initialized
- Parameters
obj (TdlModel) – object to which the property belongs to.
val (type) – value used for initialization. If value is a dictionary, the initialization method will be called using the dictionary values, otherwise, the property will be set with the provided value.
-
-
class
twodlearn.core.common.
SubmodelWithArgs
(finit=None, doc=None, name=None)[source]¶ Bases:
twodlearn.core.common.Submodel
Decorator used to specify a submodel inside a model. The decorator works similar to @property, but the specified method correponds to the initialization of the submodel.
-
class
twodlearn.core.common.
TdlDescriptor
(finit=None, doc=None, name=None)[source]¶ Bases:
object
Decorator used to specify a parameter inside a model. The decorator works similar to @property, but the specified method correponds to the initialization of the parameter
-
class
twodlearn.core.common.
TdlModel
(**kargs)[source]¶ Bases:
twodlearn.core.common.TdlOp
-
class
twodlearn.core.common.
TdlOp
(name, options=None)[source]¶ Bases:
object
Base class for defining operations The operation is encapsulated inside a scope
-
class
twodlearn.core.common.
TdlProgram
(options=None, name=None, **kargs)[source]¶ Bases:
object
Defines a program that executes operations over TdlModel instances
-
class
twodlearn.core.common.
TransformedVariable
(initializer, initial_value=None, trainable=True, collections=None, validate_shape=True, caching_device=None, name='variable', variable_def=None, dtype=None, expected_shape=None, import_scope=None, constraint=None, options=None)[source]¶
-
twodlearn.core.common.
add_init_descriptor
(descriptor)[source]¶ Adds the descriptor to the list of initialization descriptors This list is checked by the class initializers to verify if the provided arguments have an initialization method.
-
twodlearn.core.common.
add_tdl_descriptor
(descriptor)[source]¶ Adds the descriptor to the list of existing descriptors This list is checked by the class initializers to verify if the provided arguments have an initialization method
-
twodlearn.core.common.
any_initialized
(obj, props: List[str]) → bool[source]¶ checks if any property is initialized
- Parameters
obj – object that owns the properties.
props (typing.List[str]) – list of properties to be checked.
- Returns
True if any property is already initialized.
- Return type
bool
-
twodlearn.core.common.
assert_any_available
(object, property=None, reqs=None)[source]¶ Check if requirements are available.
This function checks if any of the requirements is already set. If no requirement is set, we rise an exeption and left _init_tdl_attrs function to handle it.
The requirements will be initialized only if they were provided by the user, no auto-initialization is performed.
- Parameters
object (TdlModel) – object being initialized/defined.
reqs (str) – requirements.
-
twodlearn.core.common.
assert_initialized
(object, prop, reqs)[source]¶ Check if the requirements have been initialized.
- Parameters
object – object being initialized
prop – property being initialized (string)
reqs – list of properties that are required to initialize prop (list of strings)
- Raises
InitPreconditionsFailed – the exception is raised if any of the requirements is not initialized. During initialization, the exeption is handled by _init_tdl_attrs
-
twodlearn.core.common.
assert_initialized_if_available
(object, property=None, reqs=None)[source]¶ check that requirements are initialized if they were provided by the user, no auto-initialization is performed.
- Parameters
object (TdlModel) – object being initialized/defined.
reqs (str) – requirements.
-
twodlearn.core.common.
build
(model, recursive=False)[source]¶ initialize parameters and submodels of a given model.
- Parameters
recursive – True for building the parameters and submodels.
-
twodlearn.core.common.
check_defaults
(options, default)[source]¶ Adds the values in default to options.
- Parameters
options (dict) – base options.
default (dict) – default options.
- Returns
options with the missing values that are in default but not in options.
- Return type
dict
-
twodlearn.core.common.
convert_variable_to_tensor
(value, dtype=None, name=None, as_ref=False)[source]¶
-
twodlearn.core.common.
create_init_docstring
(cls)[source]¶ Add a docstring to the initialization method of a tdl class.
-
twodlearn.core.common.
hasattr
(object, name)[source]¶ Checks if the object has the attribute name bypassing the autoinitialization procedure.
-
twodlearn.core.common.
init_attrs
(model, attrs=None, AttrTypes='default')[source]¶ Run auto-initialization of the model attributes.
- Parameters
model (TdlModel) – model to initialize attributes.
attrs (list) – list with the names of the attributes to initialize.
AttrTypes (str) – tdl decorator or list of tdl decorators. Use ‘default’ to initialize (InputArgument, InputParameter, InputModel, SimpleParameter, Submodel).
-
twodlearn.core.common.
is_property_initialized
(obj, prop)[source]¶ Checks if a property has been initialized. This function checks initialization even for properties that are set but not initialized, like MethodInit.
- Parameters
obj (TdlModel) – model object.
prop (str) – property name.
- Returns
True if property is set, False otherwise.
- Return type
bool
-
twodlearn.core.common.
is_property_set
(obj, prop)[source]¶ Checks if a property has already been set. This function checks only if the property has been set. For attributes like MethodInit that are set using an initializer, this function returns True, even when they have not been initialized.
- Parameters
obj (TdlModel) – model object.
prop (str) – property name.
- Returns
True if property is set, False otherwise.
- Return type
bool