RecordDict

class RecordDict(records: dict[str, RecordType] | None = None)[소스]

기반 클래스: 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 a Context in your apps or communicated as part of a Message between your apps.

매개변수:

records (Optional[dict[str, RecordType]]) – A dictionary mapping string keys to record instances, where each value is either a ParametersRecord, MetricsRecord, or ConfigsRecord.

예제

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 a flwr.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 and ArrayRecord.

메소드

clear()

get(k[,d])

items()

keys()

pop(k[,d])

키를 찾을 수 없으면 주어진 경우 d가 반환되고, 그렇지 않으면 KeyError가 발생합니다.

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()

속성

array_records

Dictionary holding only ArrayRecord instances.

config_records

Dictionary holding only ConfigRecord instances.

metric_records

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.

키를 찾을 수 없으면 주어진 경우 d가 반환되고, 그렇지 않으면 KeyError가 발생합니다.

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.