Federated Matrix Factorization Module References

federatedscope.mf.dataset

class federatedscope.mf.dataset.HFLMovieLens10M(root, num_client, train_portion=0.9, download=True)[source]

MovieLens10M dataset in HFL setting

class federatedscope.mf.dataset.HFLMovieLens1M(root, num_client, train_portion=0.9, download=True)[source]

MovieLens1M dataset in HFL setting

class federatedscope.mf.dataset.HMFDataset[source]

Dataset of matrix factorization task in horizontal federated learning.

class federatedscope.mf.dataset.MovieLens10M(root, num_client, train_portion=0.9, download=True)[source]

MoviesLens 10M Dataset (https://grouplens.org/datasets/movielens)

Format:

UserID::MovieID::Rating::Timestamp

Parameters
  • root (str) – Root directory of dataset where directory MoviesLen1M exists or will be saved to if download is set to True.

  • config (callable) – Parameters related to matrix factorization.

  • train_size (float, optional) – The proportion of training data.

  • test_size (float, optional) – The proportion of test data.

  • download (bool, optional) – If true, downloads the dataset from the

  • and (internet) – puts it in root directory. If dataset is already downloaded, it is not downloaded again.

class federatedscope.mf.dataset.MovieLens1M(root, num_client, train_portion=0.9, download=True)[source]

MoviesLens 1M Dataset (https://grouplens.org/datasets/movielens)

Format:

UserID::MovieID::Rating::Timestamp

Parameters
  • root (str) – Root directory of dataset where directory MoviesLen1M exists or will be saved to if download is set to True.

  • config (callable) – Parameters related to matrix factorization.

  • train_size (float, optional) – The proportion of training data.

  • test_size (float, optional) – The proportion of test data.

  • download (bool, optional) – If true, downloads the dataset from the

  • already (internet and puts it in root directory. If dataset is) –

  • downloaded

  • again. (it is not downloaded) –

class federatedscope.mf.dataset.MovieLensData(root, num_client, train_portion=0.9, download=True)[source]

Download and split MF datasets

Parameters
  • root (string) – the path of data

  • num_client (int) – the number of clients

  • train_portion (float) – the portion of training data

  • download (bool) – indicator to download dataset

class federatedscope.mf.dataset.VFLMovieLens10M(root, num_client, train_portion=0.9, download=True)[source]

MovieLens10M dataset in VFL setting

class federatedscope.mf.dataset.VFLMovieLens1M(root, num_client, train_portion=0.9, download=True)[source]

MovieLens1M dataset in VFL setting

class federatedscope.mf.dataset.VMFDataset[source]

Dataset of matrix factorization task in vertical federated learning.

federatedscope.mf.model

class federatedscope.mf.model.BasicMFNet(num_user, num_item, num_hidden)[source]

Basic model for MF task

Parameters
  • num_user (int) – the number of users

  • num_item (int) – the number of items

  • num_hidden (int) – the dimension of embedding vector

forward(indices, ratings)[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.

load_state_dict(state_dict, strict: bool = True)[source]

Copies parameters and buffers from state_dict into this module and its descendants. If strict is True, then the keys of state_dict must exactly match the keys returned by this module’s state_dict() function.

Parameters
  • state_dict (dict) – a dict containing parameters and persistent buffers.

  • strict (bool, optional) – whether to strictly enforce that the keys in state_dict match the keys returned by this module’s state_dict() function. Default: True

Returns

  • missing_keys is a list of str containing the missing keys

  • unexpected_keys is a list of str containing the unexpected keys

Return type

NamedTuple with missing_keys and unexpected_keys fields

Note

If a parameter or buffer is registered as None and its corresponding key exists in state_dict, load_state_dict() will raise a RuntimeError.

state_dict(destination=None, prefix='', keep_vars=False)[source]

Returns a dictionary containing a whole state of the module.

Both parameters and persistent buffers (e.g. running averages) are included. Keys are corresponding parameter and buffer names. Parameters and buffers set to None are not included.

Returns

a dictionary containing a whole state of the module

Return type

dict

Example:

>>> module.state_dict().keys()
['bias', 'weight']
class federatedscope.mf.model.HMFNet(num_user, num_item, num_hidden)[source]

MF model for horizontal federated learning

class federatedscope.mf.model.VMFNet(num_user, num_item, num_hidden)[source]

MF model for vertical federated learning

federatedscope.mf.model.get_mfnet(model_config, data_shape)[source]

Return the MF model according to model configs

Parameters
  • model_config – the model related parameters

  • data_shape (int) – the input shape of the model

federatedscope.mf.dataloader

class federatedscope.mf.dataloader.MFDataLoader(data: csc_matrix, batch_size: int, shuffle=True, drop_last=False, theta=None)[source]

DataLoader for MF dataset

Parameters
  • data (csc_matrix) – sparse MF dataset

  • batch_size (int) – the size of batch data

  • shuffle (bool) – shuffle the dataset

  • drop_last (bool) – drop the last batch if True

  • theta (int) – the maximal number of ratings for each user

_trim_data(data, theta=None)[source]

Trim rating data by parameter theta (per-user privacy)

Parameters
  • data (csc_matrix) – the dataset

  • theta (int) – The maximal number of ratings for each user

federatedscope.mf.dataloader.load_mf_dataset(config=None, client_cfgs=None)[source]

Return the dataset of matrix factorization

Format:
{
‘client_id’: {

‘train’: DataLoader(), ‘test’: DataLoader(), ‘val’: DataLoader()

}

}

federatedscope.mf.trainer

class federatedscope.mf.trainer.MFTrainer(model, data, device, config, only_for_eval=False, monitor=None)[source]

Trainer for MF task

Parameters
  • model (torch.nn.module) – MF model.

  • data (dict) – input data

  • device (str) – device.

_hook_on_batch_end(ctx)[source]

Note

The modified attributes and according operations are shown below:

Attribute

Operation

ctx.num_samples

Add ctx.batch_size

ctx.loss_batch_total

Add batch loss

ctx.loss_regular_total

Add batch regular loss

ctx.ys_true

Append ctx.y_true

ctx.ys_prob

Append ctx.ys_prob

_hook_on_batch_forward(ctx)[source]

Note

The modified attributes and according operations are shown below:

Attribute

Operation

ctx.y_true

Move to ctx.device

ctx.y_prob

Forward propagation get y_prob

ctx.loss_batch

Calculate the loss

ctx.batch_size

Get the batch_size

_hook_on_batch_forward_flop_count(ctx)[source]

The monitoring hook to calculate the flops during the fl course

Note

For customized cases that the forward process is not only based on ctx.model, please override this function (inheritance case) or replace this hook (plug-in case)

The modified attributes and according operations are shown below:

Attribute

Operation

ctx.monitor

Track average flops

_hook_on_fit_end(ctx)[source]

Evaluate metrics.

Note

The modified attributes and according operations are shown below:

Attribute

Operation

ctx.ys_true

Convert to numpy.array

ctx.ys_prob

Convert to numpy.array

ctx.monitor

Evaluate the results

ctx.eval_metrics

Get evaluated results from ctx.monitor

parse_data(data)[source]

Populate “{}_data”, “{}_loader” and “num_{}_data” for different modes

federatedscope.mf.trainer.embedding_clip(param, R: int)[source]

Clip embedding vector according to $R$

Parameters
  • param (tensor) – The embedding vector

  • R (int) – The upper bound of ratings

federatedscope.mf.trainer.hook_on_batch_backward(ctx)[source]

Private local updates in SGDMF

federatedscope.mf.trainer.init_sgdmf_ctx(base_trainer)[source]

Init necessary attributes used in SGDMF, some new attributes will be with prefix SGDMF optimizer to avoid namespace pollution

federatedscope.mf.trainer.wrap_MFTrainer(base_trainer: Type[MFTrainer]) Type[MFTrainer][source]

Build SGDMFTrainer with a plug-in manner, by registering new functions into specific MFTrainer