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

Submodules#

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

sl model base

Classes:

SLBaseModel(builder_base[, builder_fuse])

SLBaseModule()

SLBaseTFModel(builder_base, builder_fuse, ...)

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

PYUSLTFModel

alias of ActorProxy(PYUSLTFModel)

PYUModel

alias of ActorProxy(PYUModel)

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

Bases: ABC

Methods:

__init__(builder_base[, builder_fuse])

build_dataset(x[, y, s_w, batch_size, ...])

base_forward()

base_backward()

fuse_net(hiddens)

__init__(builder_base: Callable, builder_fuse: Optional[Callable] = None)[source]#
abstract build_dataset(x: ndarray, y: Optional[ndarray] = None, s_w: Optional[ndarray] = None, batch_size=32, buffer_size=128, repeat_count=1)[source]#
abstract base_forward()[source]#
abstract base_backward()[source]#
abstract fuse_net(hiddens)[source]#
class secretflow.ml.nn.sl.backend.tensorflow.sl_base.SLBaseModule[source]#

Bases: 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)[source]#

Defines the computation performed at every call.

Should be overridden by all subclasses.

Note

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()[source]#
set_weights(weights)[source]#
get_gradients(parameters=None)[source]#
set_gradients(gradients: List[Union[Tensor, ndarray]], parameters: Optional[List[Tensor]] = None)[source]#
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, *, _ray_trace_ctx=None, **kwargs)[source]#

Bases: 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(*x[, y, s_w, batch_size, ...])

build tf.data.Dataset

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_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(*hidden_features[, _num_returns, ...])

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

reset_metrics()

evaluate(*hidden_features[, compress])

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

metrics()

predict(*hidden_features[, compress])

Generates output predictions for the input hidden layer features.

save_base_model(base_model_path[, save_traces])

save_fuse_model(fuse_model_path[, save_traces])

load_base_model(base_model_path[, ...])

load_fuse_model(fuse_model_path[, ...])

get_privacy_spent(step[, orders])

Get accountant of dp mechanism.

__init__(builder_base: Callable[[], Model], builder_fuse: Callable[[], Model], dp_strategy: DPStrategy, compressor: Compressor, *, _ray_trace_ctx=None, **kwargs)[source]#
static fuse_op(x, y)[source]#
init_data()[source]#
set_steps_per_epoch(steps_per_epoch)[source]#
get_basenet_output_num()[source]#
build_dataset(*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, dataset_builder: Optional[Callable] = None)[source]#

build tf.data.Dataset

Parameters
  • 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

base_forward(stage='train', compress: bool = False)[source]#

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)[source]#

backward on fusenet

Parameters
  • gradient – gradient of fusenet hidden layer

  • compress – Whether to decompress gradient.

get_base_weights()[source]#
get_fuse_weights()[source]#
init_training(callbacks, epochs=1, steps=0, verbose=0)[source]#
get_stop_training()[source]#
on_train_begin()[source]#
on_epoch_begin(epoch)[source]#
on_train_batch_begin(step=None)[source]#
on_train_batch_end(step=None)[source]#
on_validation(val_logs)[source]#
on_epoch_end(epoch)[source]#
on_train_end()[source]#
set_sample_weight(sample_weight, stage='train')[source]#
fuse_net(*hidden_features, _num_returns=2, compress=False)[source]#

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

Parameters
  • hidden_features – A list of hidden layers for each party to compute

  • compress – Whether to decompress/compress data.

Returns

gradient Of hiddens

reset_metrics()[source]#
evaluate(*hidden_features, compress: bool = False)[source]#

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

Parameters
  • hidden_features – A list of hidden layers for each party to compute

  • compress – Whether to decompress input data.

Returns

map of model metrics.

metrics()[source]#
predict(*hidden_features, compress: bool = False)[source]#

Generates output predictions for the input hidden layer features.

Parameters
  • hidden_features – A list of hidden layers for each party to compute

  • compress – Whether to decompress input data.

Returns

Array(s) of predictions.

save_base_model(base_model_path: str, save_traces=True)[source]#
save_fuse_model(fuse_model_path: str, save_traces=True)[source]#
load_base_model(base_model_path: str, custom_objects=None)[source]#
load_fuse_model(fuse_model_path: str, custom_objects=None)[source]#
get_privacy_spent(step: int, orders=None)[source]#

Get accountant of dp mechanism.

Parameters
  • step – The current step of model training or prediction.

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

class secretflow.ml.nn.sl.backend.tensorflow.sl_base.ModelPartition(model_fn, optim_fn, loss_fn, dataloader_fn, *, _ray_trace_ctx=None)[source]#

Bases: 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, *, _ray_trace_ctx=None)[source]#
get_one_batch(name='train')[source]#
forward(used_name='train', external_input=None) Tuple[Tensor, Tensor][source]#
zero_grad()[source]#
backward(used_name='train', gradients=None, external_input: Optional[Dict] = None)[source]#
apply_gradients(gradients=None)[source]#
optim_step()[source]#
get_weights()[source]#
set_weights(weights)[source]#
call_model_fn(fn_name, *args, **kwargs)[source]#
secretflow.ml.nn.sl.backend.tensorflow.sl_base.PYUSLTFModel[source]#

alias of ActorProxy(PYUSLTFModel) Methods:

__init__(*args, **kwargs)

Abstraction device object base class.

base_backward(gradient[, compress, ...])

backward on fusenet

base_forward([stage, compress, _ray_trace_ctx])

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

build_dataset(*x[, y, s_w, batch_size, ...])

build tf.data.Dataset

evaluate(*hidden_features[, compress, ...])

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

fuse_net(*hidden_features[, _num_returns, ...])

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

get_base_weights(*[, _ray_trace_ctx])

get_basenet_output_num(*[, _ray_trace_ctx])

get_fuse_weights(*[, _ray_trace_ctx])

get_privacy_spent(step[, orders, _ray_trace_ctx])

Get accountant of dp mechanism.

get_stop_training(*[, _ray_trace_ctx])

init_data(*[, _ray_trace_ctx])

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

load_base_model(base_model_path[, ...])

load_fuse_model(fuse_model_path[, ...])

metrics(*[, _ray_trace_ctx])

on_epoch_begin(epoch, *[, _ray_trace_ctx])

on_epoch_end(epoch, *[, _ray_trace_ctx])

on_train_batch_begin([step, _ray_trace_ctx])

on_train_batch_end([step, _ray_trace_ctx])

on_train_begin(*[, _ray_trace_ctx])

on_train_end(*[, _ray_trace_ctx])

on_validation(val_logs, *[, _ray_trace_ctx])

predict(*hidden_features[, compress, ...])

Generates output predictions for the input hidden layer features.

reset_metrics(*[, _ray_trace_ctx])

save_base_model(base_model_path[, ...])

save_fuse_model(fuse_model_path[, ...])

set_sample_weight(sample_weight[, stage, ...])

set_steps_per_epoch(steps_per_epoch, *[, ...])

secretflow.ml.nn.sl.backend.tensorflow.sl_base.PYUModel[source]#

alias of ActorProxy(PYUModel) Methods:

__init__(*args, **kwargs)

Abstraction device object base class.

apply_gradients([gradients, _ray_trace_ctx])

backward([used_name, gradients, ...])

call_model_fn(fn_name, *args[, _ray_trace_ctx])

forward([used_name, external_input, ...])

get_one_batch([name, _ray_trace_ctx])

get_weights(*[, _ray_trace_ctx])

optim_step(*[, _ray_trace_ctx])

set_weights(weights, *[, _ray_trace_ctx])

zero_grad(*[, _ray_trace_ctx])

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

Classes:

custom_loss(func)

Decorator to define a function with a custom loss.

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

Bases: 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.

Examples

>>> 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)[source]#
with_kwargs(kwargs)[source]#
get_config()[source]#
classmethod from_config(config)[source]#

Module contents#