MetricRecord

class MetricRecord(metric_dict: dict[str, int | float | list[int] | list[float]] | None = None, keep_input: bool = True)[소스]

기반 클래스: TypedDict[str, int | float | list[int] | list[float]]

Metrics recod.

A MetricRecord is a Python dictionary designed to ensure that each key-value pair adheres to specified data types. A MetricRecord is one of the types of records that a flwr.common.RecordDict supports and can therefore be used to construct common.Message objects.

매개변수:
  • metric_dict (Optional[Dict[str, MetricRecordValues]]) – A dictionary that stores basic types (i.e. int, float as defined in MetricScalar) and list of such types (see MetricScalarList).

  • 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.

예제

The usage of a MetricRecord 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 a ClientApp, reporting the validation loss obtained at a ClientApp, or, more generally, the output of executing a query by the ClientApp. 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 MetricRecord from scratch:

>>> from flwr.common import MetricRecord
>>>
>>> # A `MetricRecord` is a specialized Python dictionary
>>> record = MetricRecord({"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 MetricRecord, value types allowed are those in defined in flwr.common.MetricRecordValues. Similarly, only str keys are allowed.

>>> from flwr.common import MetricRecord
>>>
>>> record = MetricRecord() # 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 ConfigRecord or ArrayRecord.

메소드

clear()

count_bytes()

이 객체에 저장된 바이트 수를 반환합니다.

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

clear() None.  Remove all items from D.
count_bytes() int[소스]

이 객체에 저장된 바이트 수를 반환합니다.

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.

키를 찾을 수 없으면 주어진 경우 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.