secretflow.ml.nn.sl.backend.tensorflow#

secretflow.ml.nn.sl.backend.tensorflow.sl_base#

sl model base

Classes:

SLBaseModel(builder_base[, builder_fuse])

SLBaseModule(*args, **kwargs)

SLBaseTFModel(builder_base, builder_fuse, ...)

ModelPartition(model_fn, optim_fn, loss_fn, ...)

PYUSLTFModel

ActorProxy(PYUSLTFModel) 的别名

PYUModel

ActorProxy(PYUModel) 的别名

class secretflow.ml.nn.sl.backend.tensorflow.sl_base.SLBaseModel(builder_base: Callable, builder_fuse: Optional[Callable] = None)[源代码]#

基类:ABC

Methods:

__init__(builder_base[, builder_fuse])

base_forward()

base_backward()

fuse_net(hiddens)

__init__(builder_base: Callable, builder_fuse: Optional[Callable] = None)[源代码]#
abstract base_forward()[源代码]#
abstract base_backward()[源代码]#
abstract fuse_net(hiddens)[源代码]#
class secretflow.ml.nn.sl.backend.tensorflow.sl_base.SLBaseModule(*args, **kwargs)[源代码]#

基类:ABC, Module

Methods:

forward(x)

Defines the computation performed at every call.

get_weights()

set_weights(weights)

get_gradients([parameters])

set_gradients(gradients[, parameters])

Attributes:

abstract forward(x)[源代码]#

Defines the computation performed at every call.

Should be overridden by all subclasses.

备注

Although the recipe for forward pass needs to be defined within this function, one should call the Module instance afterwards instead of this since the former takes care of running the registered hooks while the latter silently ignores them.

get_weights()[源代码]#
set_weights(weights)[源代码]#
get_gradients(parameters=None)[源代码]#
set_gradients(gradients: List[Union[Tensor, ndarray]], parameters: Optional[List[Tensor]] = None)[源代码]#
training: bool#
class secretflow.ml.nn.sl.backend.tensorflow.sl_base.SLBaseTFModel(builder_base: Callable[[], Model], builder_fuse: Callable[[], Model], dp_strategy: DPStrategy, compressor: Compressor, random_seed: Optional[int] = None, **kwargs)[源代码]#

基类:SLBaseModel

Methods:

__init__(builder_base, builder_fuse, ...[, ...])

fuse_op(x, y)

init_data()

set_steps_per_epoch(steps_per_epoch)

get_basenet_output_num()

build_dataset_from_numeric(*x[, y, s_w, ...])

build tf.data.Dataset

build_dataset_from_builder(*x[, y, s_w, ...])

build tf.data.Dataset

set_dataset_stage(data_set[, stage, has_y, ...])

base_forward([stage, compress])

compute hidden embedding :param stage: Which stage of the base forward :param compress: Whether to compress cross device data.

base_backward(gradient[, compress])

backward on fusenet

get_base_losses()

get_base_weights()

get_fuse_weights()

init_training(callbacks[, epochs, steps, ...])

get_stop_training()

on_train_begin()

on_epoch_begin(epoch)

on_train_batch_begin([step])

on_train_batch_end([step])

on_validation(val_logs)

on_epoch_end(epoch)

on_train_end()

set_sample_weight(sample_weight[, stage])

fuse_net(*forward_data[, _num_returns, compress])

Fuses the hidden layer and calculates the reverse gradient only on the side with the label

reset_metrics()

evaluate(*forward_data[, compress])

Returns the loss value & metrics values for the model in test mode.

wrap_local_metrics()

metrics()

predict(*forward_data[, compress])

Generates output predictions for the input hidden layer features.

save_base_model(base_model_path, **kwargs)

save_fuse_model(fuse_model_path, **kwargs)

load_base_model(base_model_path, **kwargs)

load_fuse_model(fuse_model_path, **kwargs)

export_base_model(model_path[, save_format])

export_fuse_model(model_path[, save_format])

get_privacy_spent(step[, orders])

Get accountant of dp mechanism.

get_skip_gradient()

__init__(builder_base: Callable[[], Model], builder_fuse: Callable[[], Model], dp_strategy: DPStrategy, compressor: Compressor, random_seed: Optional[int] = None, **kwargs)[源代码]#
static fuse_op(x, y)[源代码]#
init_data()[源代码]#
set_steps_per_epoch(steps_per_epoch)[源代码]#
get_basenet_output_num()[源代码]#
build_dataset_from_numeric(*x: List[ndarray], y: Optional[ndarray] = None, s_w: Optional[ndarray] = None, batch_size=32, buffer_size=128, shuffle=False, repeat_count=1, stage='train', random_seed=1234)[源代码]#

build tf.data.Dataset

参数:
  • x – feature, FedNdArray or HDataFrame

  • y – label, FedNdArray or HDataFrame

  • s_w – sample weight, FedNdArray or HDataFrame

  • batch_size – Number of samples per gradient update

  • buffer_size – buffer size for shuffling

  • shuffle – whether shuffle the dataset or not

  • repeat_count – num of repeats

  • stage – stage of this datset

  • random_seed – Prg seed for shuffling

build_dataset_from_builder(*x: List[ndarray], y: Optional[ndarray] = None, s_w: Optional[ndarray] = None, batch_size=-1, shuffle=False, buffer_size=256, random_seed=1234, stage='train', dataset_builder: Optional[Callable] = None)[源代码]#

build tf.data.Dataset

参数:
  • x – feature, FedNdArray or HDataFrame

  • y – label, FedNdArray or HDataFrame

  • s_w – sample weight, FedNdArray or HDataFrame

  • stage – stage of this datset

  • dataset_builder – dataset build callable function of worker

set_dataset_stage(data_set, stage='train', has_y=None, has_s_w=None)[源代码]#
base_forward(stage='train', compress: bool = False) ForwardData[源代码]#

compute hidden embedding :param stage: Which stage of the base forward :param compress: Whether to compress cross device data.

Returns: hidden embedding

base_backward(gradient, compress: bool = False)[源代码]#

backward on fusenet

参数:
  • gradient – gradient of fusenet hidden layer

  • compress – Whether to decompress gradient.

get_base_losses()[源代码]#
get_base_weights()[源代码]#
get_fuse_weights()[源代码]#
init_training(callbacks, epochs=1, steps=0, verbose=0)[源代码]#
get_stop_training()[源代码]#
on_train_begin()[源代码]#
on_epoch_begin(epoch)[源代码]#
on_train_batch_begin(step=None)[源代码]#
on_train_batch_end(step=None)[源代码]#
on_validation(val_logs)[源代码]#
on_epoch_end(epoch)[源代码]#
on_train_end()[源代码]#
set_sample_weight(sample_weight, stage='train')[源代码]#
fuse_net(*forward_data: List[ForwardData], _num_returns: int = 2, compress: bool = False)[源代码]#

Fuses the hidden layer and calculates the reverse gradient only on the side with the label

参数:
  • forward_data – A list of ForwardData containing hidden layers, losses, etc. that are uploaded by each party for computation.

  • compress – Whether to decompress/compress data.

返回:

gradient Of hiddens

reset_metrics()[源代码]#
evaluate(*forward_data: List[ForwardData], compress: bool = False)[源代码]#

Returns the loss value & metrics values for the model in test mode.

参数:
  • forward_data – A list of data dictionaries containing hidden layers, losses, etc. that are uploaded by each party for computation.

  • compress – Whether to decompress input data.

返回:

map of model metrics.

wrap_local_metrics()[源代码]#
metrics()[源代码]#
predict(*forward_data: List[ForwardData], compress: bool = False)[源代码]#

Generates output predictions for the input hidden layer features.

参数:
  • forward_data – A list of data dictionaries containing hidden layers, that are uploaded by each party for computation.

  • compress – Whether to decompress input data.

返回:

Array(s) of predictions.

save_base_model(base_model_path: str, **kwargs)[源代码]#
save_fuse_model(fuse_model_path: str, **kwargs)[源代码]#
load_base_model(base_model_path: str, **kwargs)[源代码]#
load_fuse_model(fuse_model_path: str, **kwargs)[源代码]#
export_base_model(model_path: str, save_format: str = 'onnx', **kwargs)[源代码]#
export_fuse_model(model_path: str, save_format: str = 'onnx', **kwargs)[源代码]#
get_privacy_spent(step: int, orders=None)[源代码]#

Get accountant of dp mechanism.

参数:
  • step – The current step of model training or prediction.

  • orders – An array (or a scalar) of RDP orders.

get_skip_gradient()[源代码]#
class secretflow.ml.nn.sl.backend.tensorflow.sl_base.ModelPartition(model_fn, optim_fn, loss_fn, dataloader_fn)[源代码]#

基类:object

Methods:

__init__(model_fn, optim_fn, loss_fn, ...)

get_one_batch([name])

forward([used_name, external_input])

zero_grad()

backward([used_name, gradients, external_input])

apply_gradients([gradients])

optim_step()

get_weights()

set_weights(weights)

call_model_fn(fn_name, *args, **kwargs)

__init__(model_fn, optim_fn, loss_fn, dataloader_fn)[源代码]#
get_one_batch(name='train')[源代码]#
forward(used_name='train', external_input=None) Tuple[Tensor, Tensor][源代码]#
zero_grad()[源代码]#
backward(used_name='train', gradients=None, external_input: Optional[Dict] = None)[源代码]#
apply_gradients(gradients=None)[源代码]#
optim_step()[源代码]#
get_weights()[源代码]#
set_weights(weights)[源代码]#
call_model_fn(fn_name, *args, **kwargs)[源代码]#
secretflow.ml.nn.sl.backend.tensorflow.sl_base.PYUSLTFModel[源代码]#

ActorProxy(PYUSLTFModel) 的别名 Methods:

__init__(*args, **kwargs)

Abstraction device object base class.

base_backward(gradient[, compress])

backward on fusenet

base_forward([stage, compress])

compute hidden embedding :param stage: Which stage of the base forward :param compress: Whether to compress cross device data.

build_dataset_from_builder(*x[, y, s_w, ...])

build tf.data.Dataset

build_dataset_from_numeric(*x[, y, s_w, ...])

build tf.data.Dataset

evaluate(*forward_data[, compress])

Returns the loss value & metrics values for the model in test mode.

export_base_model(model_path[, save_format])

export_fuse_model(model_path[, save_format])

fuse_net(*forward_data[, _num_returns, compress])

Fuses the hidden layer and calculates the reverse gradient only on the side with the label

get_base_losses()

get_base_weights()

get_basenet_output_num()

get_fuse_weights()

get_privacy_spent(step[, orders])

Get accountant of dp mechanism.

get_skip_gradient()

get_stop_training()

init_data()

init_training(callbacks[, epochs, steps, ...])

load_base_model(base_model_path, **kwargs)

load_fuse_model(fuse_model_path, **kwargs)

metrics()

on_epoch_begin(epoch)

on_epoch_end(epoch)

on_train_batch_begin([step])

on_train_batch_end([step])

on_train_begin()

on_train_end()

on_validation(val_logs)

predict(*forward_data[, compress])

Generates output predictions for the input hidden layer features.

reset_metrics()

save_base_model(base_model_path, **kwargs)

save_fuse_model(fuse_model_path, **kwargs)

set_dataset_stage(data_set[, stage, has_y, ...])

set_sample_weight(sample_weight[, stage])

set_steps_per_epoch(steps_per_epoch)

wrap_local_metrics()

secretflow.ml.nn.sl.backend.tensorflow.sl_base.PYUModel[源代码]#

ActorProxy(PYUModel) 的别名 Methods:

__init__(*args, **kwargs)

Abstraction device object base class.

apply_gradients([gradients])

backward([used_name, gradients, external_input])

call_model_fn(fn_name, *args, **kwargs)

forward([used_name, external_input])

get_one_batch([name])

get_weights()

optim_step()

set_weights(weights)

zero_grad()

secretflow.ml.nn.sl.backend.tensorflow.utils#

Classes:

custom_loss(func)

Decorator to define a function with a custom loss.

TensorInfo([name, tensor_name, dtype, shape])

ForwardData([hidden, losses])

ForwardData is a dataclass for data uploaded by each party to label party for computation.

Functions:

wrap_onnx_input_output(io_pb)

wrap_tf_input_output(io_pb)

class secretflow.ml.nn.sl.backend.tensorflow.utils.custom_loss(func: Callable)[源代码]#

基类:object

Decorator to define a function with a custom loss.

This decorator allows to define loss functions with additional keyword arguments. These keyword arguments must match the results of model’s forward pass.

示例

>>> import tensorflow as tf
>>> # define model
>>> class MyModel(tf.keras.Model):
>>>     def call(self, inputs, **kwargs):
>>>         # do forward pass
>>>         return None, y_pred, {'kwarg1': kwarg1, 'kwarg2': kwarg2}
>>> # define loss function
>>> @custom_loss
>>> def my_loss(y_true, y_pred, kwarg1 = None, kwarg2 = None):
>>>     # cumpute loss
>>>     pass
>>> # compile model with custom loss function
>>> model = MyModel(...)
>>> model.compile(
>>>     loss=my_loss,
>>>     optimizer=tf.keras.optimizers.Adam(0.01),
>>>     metrics=['acc'],
>>> )

Note: custom_loss, my_loss and MyModel need to be added to custom_objects when loading the model.

Methods:

__init__(func)

with_kwargs(kwargs)

get_config()

from_config(config)

__init__(func: Callable)[源代码]#
with_kwargs(kwargs)[源代码]#
get_config()[源代码]#
classmethod from_config(config)[源代码]#
class secretflow.ml.nn.sl.backend.tensorflow.utils.TensorInfo(name: str = None, tensor_name: str = None, dtype: str = None, shape: List[int] = None)[源代码]#

基类:object

Attributes:

name

tensor_name

dtype

shape

Methods:

__init__([name, tensor_name, dtype, shape])

name: str = None#
tensor_name: str = None#
dtype: str = None#
shape: List[int] = None#
__init__(name: Optional[str] = None, tensor_name: Optional[str] = None, dtype: Optional[str] = None, shape: Optional[List[int]] = None) None#
secretflow.ml.nn.sl.backend.tensorflow.utils.wrap_onnx_input_output(io_pb)[源代码]#
secretflow.ml.nn.sl.backend.tensorflow.utils.wrap_tf_input_output(io_pb)[源代码]#
class secretflow.ml.nn.sl.backend.tensorflow.utils.ForwardData(hidden: Optional[Union[Any, List[Any]]] = None, losses: Optional[Any] = None)[源代码]#

基类:object

ForwardData is a dataclass for data uploaded by each party to label party for computation.

hidden: base model hidden layers outputs losses: the sum of base model losses should added up to fuse model loss

Attributes:

hidden

losses

Methods:

__init__([hidden, losses])

hidden: Union[Any, List[Any]] = None#
losses: Any = None#
__init__(hidden: Optional[Union[Any, List[Any]]] = None, losses: Optional[Any] = None) None#