MetricRecord¶
- class MetricRecord(metric_dict: dict[str, MetricRecordValues] | None = None, keep_input: bool = True)[소스]¶
기반 클래스:
TypedDict
[str
,int
|float
|list
[int
] |list
[float
]],InflatableObject
Metric record.
A
MetricRecord
is a Python dictionary designed to ensure that each key-value pair adheres to specified data types. AMetricRecord
is one of the types of records that a flwr.common.RecordDict supports and can therefore be used to constructcommon.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 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
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 inflwr.common.MetricRecordValues
. Similarly, onlystr
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
orArrayRecord
.메소드
clear
()이 객체에 저장된 바이트 수를 반환합니다.
deflate
()Deflate object.
get
(k[,d])inflate
(object_content[, children])Inflate a MetricRecord from bytes.
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
()속성
Get all child objects as a dictionary or None if there are no children.
Check if the object is dirty after the last deflation.
Get object_id.
- property children: dict[str, InflatableObject] | None¶
Get all child objects as a dictionary or None if there are no children.
- clear() None. Remove all items from D. ¶
- get(k[, d]) D[k] if k in D, else d. d defaults to None. ¶
- classmethod inflate(object_content: bytes, children: dict[str, InflatableObject] | None = None) MetricRecord [소스]¶
Inflate a MetricRecord from bytes.
- 매개변수:
object_content (bytes) – The deflated object content of the MetricRecord.
children (Optional[dict[str, InflatableObject]] (default: None)) – Must be
None
.MetricRecord
does not support child objects. Providing any children will raise aValueError
.
- 반환:
The inflated MetricRecord.
- 반환 형식:
- property is_dirty: bool¶
Check if the object is dirty after the last deflation.
An object is considered dirty if its content has changed since the last its object ID was computed.
- 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 object_id: str¶
Get object_id.
- 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. ¶