gossipy.model.sampling module#

Module contents#

class gossipy.model.sampling.TorchModelSampling#

Bases: object

Class for sampling parameters from a torch model.

This class only contains static methods because it does not need to know beforehand the specific type of model. It is therefore not possible to instantiate it. The sampling over a model is performed by randomly selecting a subset of its parameters.

classmethod merge(sample, net1, net2, reduce='mean')#

Merge a sample of the parameters of two models.

Parameters
  • sample (Dict[int, Optional[Tuple[LongTensor, ...]]]) – A dictionary containing the indices of the sampled parameters.

  • net1 (TorchModel) – The first model.

  • net2 (TorchModel) – The second model.

  • reduce ({'mean', 'sum'}) – The reduction method to be used.

Return type

None

classmethod sample(size, net)#

Sample a subset of the parameters of a given model.

Parameters
  • size (float) – The size (in percentage) of the subset to be sampled.

  • net (TorchModel) – The model to be sampled.

Returns

A dictionary containing the indices of the parameters to be sampled. The keys are the indices of the layers, and the values are the indices of the parameters to be sampled in that layer.

Return type

Dict[int, Optional[Tuple[LongTensor, …]]]