Extras
The same
Utility
The special object same
allows the application of arbitrary methods to the input -
same.something()
is the same as transform(lambda x: x.something())
:
>>> from padl import same
>>> concat_lower = add >> same.lower()
>>> concat_lower("HELLO", "you")
"hello you"
same
can also be used to get items from the input -
same[0]
is equivalent to transform(lambda x: x[0])
:
>>> from padl import same
>>> addfirst = add >> same[0]
>>> addfirst(np.array([1, 2, 3], [2, 3, 4]))
3
Applying Transforms Depending on the Mode
Often it can be useful to apply Transforms depending on what mode (infer, eval or train) is being applied. For example, you might want to apply augmentation Transforms only during training.
Use an IfInfer
Transform to apply a Transform only in the “infer” mode:
>>> from padl import IfInfer
>>> t = MinusX(100) >> IfInfer(MinusX(100))
>>> t.infer_apply(300)
100
>>> list(t.eval_apply([300]))[0]
200
Analogously, use IfEval
or IfTrain
to apply a Transform only in the “eval”- or “train” mode, respectively.
Exception Handling
Use Try
to handle exceptions:
@transform
def open_file(filename):
return open(filename)
@transform
def read(file):
return file.read()
@transform
def close(file):
file.close()
read_from_path = (
open
>> Try(read, transform(lambda x: ''), finally=close)
)