Git Product home page Git Product logo

Comments (2)

hcngac avatar hcngac commented on July 26, 2024

This can be viewed as a pre-requisite to issue #119, for providing a unified data exchange format, such that different data processors can be plugged into the data-exchange, not only for reformatting, but also for other features such as local differential privacy and model/feature compression.

from plato.

baochunli avatar baochunli commented on July 26, 2024

This can be quite easily achieved by converting framework-specific weights to numpy arrays in Algorithm.extract_weights() and Algorithm.load_weights():

"""MindSpore-based federated averaging algorithm, used by both the client and the server."""
def extract_weights(self):
"""Extract weights from the model."""
# In MindSpore releases later than 1.1.3, including 1.5.0, do not support `pickle.load()`
# on `Tensor` objects (https://gitee.com/mindspore/mindspore/issues/I43RPP?from=project-issue).
# Therefore, Tensor objects must be converted to numpy arrays first
numpy_weights = OrderedDict()
for name, weight in self.model.parameters_dict().items():
numpy_weights[name] = weight.asnumpy()
return numpy_weights

def load_weights(self, weights):
"""Load the model weights passed in as a parameter."""
for name, weight in weights.items():
weight_tensor = mindspore.Tensor(weight.astype(np.float32))
weights[name] = mindspore.Parameter(weight_tensor, name=name)
# One can also use `self.model.load_parameter_slice(weights)', which
# seems to be equivalent to mindspore.load_param_into_net() in its effects
mindspore.load_param_into_net(self.model, weights, strict_load=True)

The only other location that requires a little bit of work to make sure fedavg works correctly is to implement the trainer.zeros() function to initialize a numpy-based zeros array. An example is shown in trainers/mindspore/basic.py:

def zeros(self, shape):
"""Returns a zero numpy array with the given shape."""
# This should only be called from a server
assert self.client_id == 0
return np.zeros(shape)

from plato.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.