towhee.operator.stateful_operator.StatefulOperator

class towhee.operator.stateful_operator.StatefulOperator(name)[source]

Bases: Operator

Stateful operator.

Examples:

>>> from towhee import register
>>> from towhee import DataCollection, State
>>> from towhee.functional.entity import Entity
>>> import numpy as np
>>> @register
... class my_normalize(StatefulOperator):
...     def __init__(self, name):
...         super().__init__(name=name)
...     def fit(self):
...         self._state._mu = np.mean(self._data[0])
...         self._state._std = np.std(self._data[0])
...     def predict(self, x):
...         return (x-self._state._mu)/self._state._std
>>> dc = (
...     DataCollection.range(10)
...         .set_training(State())
...         .map(lambda x: Entity(a=x))
...         .my_normalize['a', 'b'](name='mynorm')
... )
>>> [int(x.b*10) for x in dc.to_list()]
[-15, -12, -8, -5, -1, 1, 5, 8, 12, 15]
>>> dc._state.mynorm._mu
4.5

Methods

feed

fit

predict

set_state

set_training

Attributes

flag

key

shared_type

__call__(*arg)[source]

The framework calls __call__ function repeatedly for every input data.

Args:

Returns:

Raises:

An exception during __init__ can terminate the graph run.

__init__(name)[source]

Init operator, before a graph starts, the framework will call Operator __init__ function.

Args:

Raises:

An exception during __init__ can terminate the graph run.