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_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
- 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