RecordDict¶
- class RecordDict(records: dict[str, RecordType] | None = None)[source]¶
Bases:
TypedDict
[str
,ArrayRecord
|MetricRecord
|ConfigRecord
]RecordDict stores groups of arrays, metrics and configs.
A
RecordDict
is the unified mechanism by which arrays, metrics and configs can be either stored as part of aContext
in your apps or communicated as part of aMessage
between your apps.- Parameters:
records (Optional[dict[str, RecordType]]) – A dictionary mapping string keys to record instances, where each value is either a
ParametersRecord
,MetricsRecord
, orConfigsRecord
.
Examples
A
RecordDict
can hold three types of records, each designed with an specific purpose. What is common to all of them is that they are Python dictionaries designed to ensure that each key-value pair adheres to specified data types.Let’s see an example.
>>> from flwr.common import RecordDict >>> from flwr.common import ArrayRecord, ConfigRecord, MetricRecord >>> >>> # Let's begin with an empty record >>> my_records = RecordDict() >>> >>> # We can create a ConfigRecord >>> c_record = ConfigRecord({"lr": 0.1, "batch-size": 128}) >>> # Adding it to the RecordDict would look like this >>> my_records["my_config"] = c_record >>> >>> # We can create a MetricRecord following a similar process >>> m_record = MetricRecord({"accuracy": 0.93, "losses": [0.23, 0.1]}) >>> # Adding it to the RecordDict would look like this >>> my_records["my_metrics"] = m_record
Adding an
ArrayRecord
follows the same steps as above but first, the array needs to be serialized and represented as aflwr.common.Array
. For example:>>> from flwr.common import Array >>> # Creating an ArrayRecord would look like this >>> arr_np = np.random.randn(3, 3) >>> >>> # You can use the built-in tool to serialize the array >>> arr = Array(arr_np) >>> >>> # Finally, create the record >>> arr_record = ArrayRecord({"my_array": arr}) >>> >>> # Adding it to the RecordDict would look like this >>> my_records["my_parameters"] = arr_record
For additional examples on how to construct each of the records types shown above, please refer to the documentation for
ConfigRecord
,MetricRecord
andArrayRecord
.Methods
clear
()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
()Attributes
Dictionary holding only ArrayRecord instances.
Dictionary holding only ConfigRecord instances.
Dictionary holding only MetricRecord instances.
- property array_records: TypedDict[str, ArrayRecord]¶
Dictionary holding only ArrayRecord instances.
- clear() None. Remove all items from D. ¶
- property config_records: TypedDict[str, ConfigRecord]¶
Dictionary holding only ConfigRecord instances.
- 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. ¶
- property metric_records: TypedDict[str, MetricRecord]¶
Dictionary holding only MetricRecord instances.
- 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. ¶