MetricsRecord¶
- class MetricsRecord(metrics_dict: dict[str, int | float | list[int] | list[float]] | None = None, keep_input: bool = True)[source]¶
Bases:
TypedDict
[str
,int
|float
|list
[int
] |list
[float
]]Metrics recod.
A
MetricsRecord
is a Python dictionary designed to ensure that each key-value pair adheres to specified data types. AMetricsRecord
is one of the types of records that a flwr.common.RecordSet supports and can therefore be used to constructcommon.Message
objects.- Parameters:
metrics_dict (Optional[Dict[str, MetricsRecordValues]]) – A dictionary that stores basic types (i.e. int, float as defined in MetricsScalar) and list of such types (see MetricsScalarList).
keep_input (bool (default: True)) – A boolean indicating whether metrics should be deleted from the input dictionary immediately after adding them to the record. When set to True, the data is duplicated in memory. If memory is a concern, set it to False.
Examples
The usage of a
MetricsRecord
is envisioned for communicating results obtained when a node performs an action. A few typical examples include: communicating the training accuracy after a model is trained locally by aClientApp
, reporting the validation loss obtained at aClientApp
, or, more generally, the output of executing a query by theClientApp
. Common to these examples is that the output can be typically represented by a single scalar (int
,float
) or list of scalars.Let’s see some examples of how to construct a
MetricsRecord
from scratch:>>> from flwr.common import MetricsRecord >>> >>> # A `MetricsRecord` is a specialized Python dictionary >>> record = MetricsRecord({"accuracy": 0.94}) >>> # You can add more content to an existing record >>> record["loss"] = 0.01 >>> # It also supports lists >>> record["loss-historic"] = [0.9, 0.5, 0.01]
Since types are enforced, the types of the objects inserted are checked. For a
MetricsRecord
, value types allowed are those in defined inflwr.common.MetricsRecordValues
. Similarly, onlystr
keys are allowed.>>> from flwr.common import MetricsRecord >>> >>> record = MetricsRecord() # an empty record >>> # Add unsupported value >>> record["something-unsupported"] = {'a': 123} # Will throw a `TypeError`
If you need a more versatily type of record try
ConfigsRecord
orParametersRecord
.Methods
clear
()Return number of Bytes stored in this object.
get
(k[,d])items
()keys
()pop
(k[,d])If key is not found, d is returned if given, otherwise KeyError is raised.
popitem
()as a 2-tuple; but raise KeyError if D is empty.
setdefault
(k[,d])update
([E, ]**F)If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
values
()- clear() None. Remove all items from D. ¶
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- items() a set-like object providing a view on D's items. ¶
- keys() a set-like object providing a view on D's keys. ¶
- pop(k[, d]) v, remove specified key and return the corresponding value. ¶
If key is not found, d is returned if given, otherwise KeyError is raised.
- popitem() (k, v), remove and return some (key, value) pair ¶
as a 2-tuple; but raise KeyError if D is empty.
- setdefault(k[, d]) D.get(k,d), also set D[k]=d if k not in D ¶
- update([E, ]**F) None. Update D from mapping/iterable E and F. ¶
If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v
- values() an object providing a view on D's values. ¶