p2pfl.node moduleΒΆ

P2PFL Node.

class p2pfl.node.Node(model, data, address='127.0.0.1', learner=None, aggregator=None, protocol=<class 'p2pfl.communication.protocols.grpc.grpc_communication_protocol.GrpcCommunicationProtocol'>, simulation=False, **kwargs)[source]ΒΆ

Bases: object

Represents a learning node in the federated learning network.

The following example shows how to create a node with a MLP model and a MnistFederatedDM dataset. Then, the node is started, connected to another node, and the learning process is started.

>>> node = Node(
...     MLP(),
...     MnistFederatedDM(),
... )
>>> node.start()
>>> node.connect("127.0.0.1:666")
>>> node.set_start_learning(rounds=2, epochs=1)
Parameters:
  • model (P2PFLModel) – Model to be used in the learning process.

  • data (P2PFLDataset) – Dataset to be used in the learning process.

  • address (str) – The address of the node.

  • learner (Optional[Type[Learner]]) – The learner class to be used.

  • aggregator (Optional[Aggregator]) – The aggregator class to be used.

  • protocol (Type[CommunicationProtocol]) – The communication protocol to be used.

  • **kwargs – Additional arguments.

Todo

Instanciate the aggregator dynamically.

Todo

Connect nodes dynamically (while learning).

assert_running(running)[source]ΒΆ

Assert that the node is running or not running.

Parameters:

running (bool) – True if the node must be running, False otherwise.

Raises:
  • NodeRunningException – If the node is not running and running is True, or if the node is running and running

  • is False. –

Return type:

None

connect(addr)[source]ΒΆ

Connect a node to another.

Warning

Adding nodes while learning is running is not fully supported.

Parameters:

addr (str) – The address of the node to connect to.

Return type:

bool

Returns:

True if the node was connected, False otherwise.

disconnect(addr)[source]ΒΆ

Disconnects a node from another.

Parameters:

addr (str) – The address of the node to disconnect from.

Return type:

None

get_data()[source]ΒΆ

Get the data.

Return type:

P2PFLDataset

Returns:

The current data of the node.

get_model()[source]ΒΆ

Get the model.

Return type:

P2PFLModel

Returns:

The current model of the node.

get_neighbors(only_direct=False)[source]ΒΆ

Return the neighbors of the node.

Parameters:

only_direct (bool) – If True, only the direct neighbors will be returned.

Return type:

Dict[str, Any]

Returns:

The list of neighbors.

set_data(data)[source]ΒΆ

Set the data to be used in the learning process (by the learner).

Parameters:

data (P2PFLDataset) – Dataset to be used in the learning process.

Raises:

LearnerRunningException – If the learner is already set.

Return type:

None

set_learner(learner)[source]ΒΆ

Set the learner to be used in the learning process.

Parameters:

learner (Learner) – The learner to be used in the learning process.

Raises:

LearnerRunningException – If the learner is already set.

Return type:

None

set_model(model)[source]ΒΆ

Set the model to be used in the learning process (by the learner).

Parameters:

model (P2PFLModel) – Model to be used in the learning process.

Raises:

LearnerRunningException – If the learner is already set.

Return type:

None

set_start_learning(rounds=1, epochs=1)[source]ΒΆ

Start the learning process in the entire network.

Parameters:
  • rounds (int) – Number of rounds of the learning process.

  • epochs (int) – Number of epochs of the learning process.

Raises:

ZeroRoundsException – If rounds is less than 1.

Return type:

None

set_stop_learning()[source]ΒΆ

Stop the learning process in the entire network.

Return type:

None

start(wait=False)[source]ΒΆ

Start the node: server and neighbors(gossip and heartbeat).

Parameters:

wait (bool) – If True, the function will wait until the server is terminated.

Raises:

NodeRunningException – If the node is already running.

Return type:

None

stop()[source]ΒΆ

Stop the node: server and neighbors(gossip and heartbeat).

Raises:

NodeRunningException – If the node is not running.

Return type:

None