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
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. Ifstrict
isTrue
, then the keys ofstate_dict
must exactly match the keys returned by this module’sstate_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’sstate_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
withmissing_keys
andunexpected_keys
fields
Note
If a parameter or buffer is registered as
None
and its corresponding key exists instate_dict
,load_state_dict()
will raise aRuntimeError
.
- 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
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
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
- 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