Named expressions¶
Contains named expression classes
- eval_expr(expr, no_eval=None, **kwargs)[source]¶
Evaluate a named expression recursively
- Parameters
expr – an expression to evaluate
no_eval (sequence of str) – a list of arguments not to evalute. Applicable only if expr is a dict which keys are checked against no_eval list.
- add_ops(cls)[source]¶
Add arithmetic operations to a class. Allows to create and parse syntax trees using operations like ‘+’, ‘-‘, ‘*’, ‘/’.
- Parameters
op_cls (class) – The class which represents an arithmetics expression.
- class MetaNamedExpression[source]¶
Meta class to allow for easy instantiation through attribution
Examples
B.images is equal to B(‘images’), but requires fewer letters to type
- class NamedExpression(name=None, mode='w', **kwargs)[source]¶
Base class for a named expression
- mode¶
a default assignment method: write, append, extend, update. Can be shrotened to jiust the first letter: w, a, e, u.
‘w’ - overwrite with a new value. This is a default mode.
- ‘a’ - append a new value
(see list.append https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
- ‘e’ - extend with a new value
(see list.extend https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
- ‘u’ - update with a new value
(see dict.update https://docs.python.org/3/library/stdtypes.html#dict.update or set.update https://docs.python.org/3/library/stdtypes.html#frozenset.update)
- Type
- format(string)[source]¶
Convert a value to a formatted representation, controlled by format spec.
Examples
Unlike Python built-in function, the usage is value.format(format_spec), for example:
V('variable').format('Value of the variable is {:7.7}')
- get(**kwargs)[source]¶
Return a value of a named expression
Notes
This method should be overriden in child classes. In the first line it should usually call _get_params method:
name, kwargs = self._get_params(**kwargs)
- append(value, *args, **kwargs)[source]¶
Append a value to a named expression
if a named expression is a dict or set, update is called, or append otherwise.
See also
list.append
,dict.update
,set.update
- extend(value, *args, **kwargs)[source]¶
Extend a named expression with a new value (see list.extend https://docs.python.org/3/tutorial/datastructures.html#more-on-lists)
- update(value, *args, **kwargs)[source]¶
Update a named expression with a new value (see dict.update https://docs.python.org/3/library/stdtypes.html#dict.update or set.update https://docs.python.org/3/library/stdtypes.html#frozenset.update)
- class AlgebraicNamedExpression(op=None, a=None, b=None, c=None)[source]¶
Algebraic expression over named expressions
- class B(name=None, mode='w', copy=False)[source]¶
Batch component or attribute name
Notes
B()
return the batch itself.To avoid unexpected data changes the copy of the batch may be returned, if
copy=True
.Examples
B('size') B('images_shape') B(copy=True)
- class L(name=None, mode='w', **kwargs)[source]¶
List of objects or a batch component with a list of objects.
Note
L('comp').attr
is equivalent to the list comprehension[val.attr for val in batch.comp]
.L('comp').func(*args, **kwargs)
is equivalent to the list comprehension ``[val.func(*args, **kwargs)for val in batch.comp]``.
L('comp')[item]
is equivalent to the list comprehension[val[item] for val in batch.comp]
. Any chains of consecutive calls of items or attribures like ``L(‘comp’).attr[item].attr2 … `` are also allowed.
- class PipelineNamedExpression(name=None, mode='w', **kwargs)[source]¶
Base class for pipeline expressions
- class C(name=None, mode='w', **kwargs)[source]¶
A pipeline config option
Notes
C()
return config itself.Examples
Get a value from the current pipeline config:
C('model_class', default=ResNet) C('GPU')
Get the whole config from the current pipeline:
C()
Get a value from another pipeline config:
C('model_class', pipeline=train_pipeline)
- class V(name=None, mode='w', **kwargs)[source]¶
Pipeline variable name
Examples
Get a variable value from the current pipeline:
V('model_name')
Get a variable value from another pipeline:
V('loss_history', pipeline=train_pipeline)
- class M(name=None, mode='w', **kwargs)[source]¶
Model name
Examples
Get a model from the current pipeline:
M('model_name')
Get a model from a given pipeline:
M('model_name', pipeline=train_pipeline)
Get a model from a pipeline specified in the current pipeline config:
M('model_name', pipeline=C('train_pipeline'))
Get a model from a pipeline specified in another pipeline config:
M('model_name', pipeline=C('train_pipeline', pipeline=test_template))
- class I(name='c', mode='w', **kwargs)[source]¶
Iteration counter
- Parameters
name (str) –
- Determines returned value. One of:
’current’ or its substring - current iteration number, default.
’maximum’ or ‘total’ or their substring - total number of iterations to be performed. If total number is not defined, raises an error.
’ratio’ or its substring - current iteration divided by a total number of iterations.
- Raises
If name is not valid. –
If name is 'm' or 'r' and total number of iterations is not defined. –
Examples
I('current') I('max') I('max') R('normal', loc=0, scale=I('ratio')*100)
- class R(name, *args, seed=None, size=None, **kwargs)[source]¶
A random value
- Parameters
Notes
If size is needed, it should be specified as a named, not a positional argument.
Examples
R('normal', 0, 1) R('poisson', lam=5.5, seed=42, size=3) R(['metro', 'taxi', 'bike'], p=[.6, .1, .3], size=10)
- get(size=None, **kwargs)[source]¶
Return a value of a random variable
- Parameters
size (int, tuple of int) – Output shape. If the given shape is (m, n, k), then m * n * k samples are drawn and returned as m x n x k array. If size was also specified at instance creation, then output shape is extended from the beginning. So size is treated like a batch size, while size specified at instantiation is an item size.
Examples
ne = R('normal', 0, 1, size=(10, 20))) value = ne.get(batch=batch) # value.shape will be (10, 20) value = ne.get(size=30, batch=batch) # value.shape will be (30, 10, 20) # so size is treated like a batch size
- class F(name=None, mode='w', **kwargs)[source]¶
A function, method or any other callable that might take arguments
Examples
F(MyBatch.rotate)(B(), angle=30) F(make_data) F(prepare_data)(batch=B(), item=10)
- get(_call=True, **kwargs)[source]¶
Return a value from a callable
- Parameters
_call (bool) – Whether to call name-function while evaluating the expression. Sometimes we might not want calling the func, e.g. when evaluating an F-expr within a call-expression F(func)(1, arg2=10), since we want to evaluate the whole expression.
- class D(name=None, mode='w', **kwargs)[source]¶
Dataset attribute or dataset itself
Examples
D() D('classes') D('organization')
- class W(name=None, mode='w', **kwargs)[source]¶
A wrapper which returns the wrapped named expression without evaluating it
Examples
W(V('variable')) W(B(copy=True)) W(R('normal', 0, 1, size=B('size')))
- class P(name=None, mode='w', **kwargs)[source]¶
A wrapper for values passed to actions parallelized with @inbatch_parallel
Examples
Each image in the batch will be rotated at its own angle:
pipeline .rotate(angle=P(R('normal', 0, 1)))
Without
P
all images in the batch will be rotated at the same angle, as an angle is randomized across batches only:pipeline .rotate(angle=R('normal', 0, 1))
To put it simply,
R(...)
is evaluated asR(..., size=batch.size)
.Generate 3 categorical random samples for each batch item:
pipeline .calc_route(P(R(['metro', 'taxi', 'bike'], p=[.6, 0.1, 0.3], size=3))
Generate a random number of random samples for each batch item:
pipeline .some_action(P(R('normal', 0, 1, size=R('randint', 3, 8))))
P
works with arbitrary iterables too:pipeline .do_something(n=P([1, 2, 3, 4, 5]))
The first batch item will get
n=1
, the secondn=2
and so on.See also
inbatch_parallel()
- class PP(name=None, mode='w', **kwargs)[source]¶
A wrapper for single-value expressions passed to actions parallelized with @inbatch_parallel PP(expr) is essentialy P([expr for _ in batch.indices])
Examples
Each image in the batch will be rotated at its own angle:
pipeline .rotate(angle=PP(F(get_single_angle)))
as
get_single_angle
will be calledbatch.size
times.R(...)
will be evaluated only once withinP(...)
, but many times withinPP(...)
:pipeline .rotate(angle=PP(R('normal', 0, 1)))
That is why
P(R(...))
is much more efficient thanPP(R(...))
.However,
PP
is indispensable for shape-specific operations like@
or broadcasting:pipeline .rotate(angle=PP(R('normal', R('normal', 50, 15, size=3), 15)))
Internal
R
specifies a 3D angle mean and thus defines the shape. ExternalR
knows nothing about that shape and will throw an exception withinP
, but it’ll work fine withinPP
.See also
inbatch_parallel()
,P