跳转至

trainer

Trainer

Bases: ABC

The base class of trainer.

ParameterType

Bases: IntEnum

Define the type of parameter.

config()

get training configuration.

Returns:

Type Description
Dict[str, float]

return a dict, at least including the following keys:

Dict[str, float]

learning_rate

Dict[str, float]

batch_num

Dict[str, float]

sample_num

Source code in iflearner/business/homo/trainer.py
def config(self) -> Dict[str, float]:
    """get training configuration.

    Returns:
        return a dict, at least including the following keys:
        learning_rate
        batch_num
        sample_num
    """
    return dict()

evaluate(epoch) abstractmethod

evaluate model and return metrics.

Parameters:

Name Type Description Default
epoch int

the current index of epoch

required

Returns:

Type Description
Dict[str, float]

dict, k: str (metric name), v: float (metric value)

Source code in iflearner/business/homo/trainer.py
@abstractmethod
def evaluate(self, epoch: int) -> Dict[str, float]:
    """evaluate model and return metrics.

    Args:
        epoch: the current index of epoch

    Returns:
        dict, k: str (metric name), v: float (metric value)
    """
    pass

fit(epoch) abstractmethod

fit model on one epoch.

Parameters:

Name Type Description Default
epoch int

the current index of epoch

required

Returns:

Type Description
None

None

Source code in iflearner/business/homo/trainer.py
@abstractmethod
def fit(self, epoch: int) -> None:
    """fit model on one epoch.

    Args:
        epoch:  the current index of epoch

    Returns:
        None
    """
    pass

get(param_type=ParameterType.ParameterModel) abstractmethod

get parameters form the client, maybe the model parameter or gradient.

Parameters:

Name Type Description Default
param_type ParameterType

Param_type is ParameterModel or ParameterGradient, default is ParameterModel.

ParameterType.ParameterModel

Returns:

Type Description
Dict[str, npt.NDArray[np.float32]]

dict, k: str (the parameter name), v: np.ndarray (the parameter value)

Source code in iflearner/business/homo/trainer.py
@abstractmethod
def get(
    self, param_type: ParameterType = ParameterType.ParameterModel
) -> Dict[str, npt.NDArray[np.float32]]:  # type: ignore
    """get parameters form the client, maybe the model parameter or
    gradient.

    Args:
        param_type: Param_type is ParameterModel or ParameterGradient, default is ParameterModel.

    Returns:
        dict, k: str (the parameter name), v: np.ndarray (the parameter value)
    """
    pass

set(parameters, param_type=ParameterType.ParameterModel) abstractmethod

set parameters to the client, maybe the model parameter or gradient.

Parameters:

Name Type Description Default
parameters Dict[str, npt.NDArray[np.float32]]

Parameters is the same as the return of 'get' function.

required
param_type ParameterType

Param_type is ParameterModel or ParameterGradient, default is ParameterModel.

ParameterType.ParameterModel
Source code in iflearner/business/homo/trainer.py
@abstractmethod
def set(
    self,
    parameters: Dict[str, npt.NDArray[np.float32]],  # type: ignore
    param_type: ParameterType = ParameterType.ParameterModel,
) -> None:
    """set parameters to the client, maybe the model parameter or gradient.

    Args:
        parameters: Parameters is the same as the return of 'get' function.
        param_type: Param_type is ParameterModel or ParameterGradient, default is ParameterModel.

    Returns: None
    """
    pass