ParametersRecord#
- class ParametersRecord(array_dict: OrderedDict[str, Array] | None = None, keep_input: bool = False)[소스]#
기반 클래스:
TypedDict
[str
,Array
]매개변수 기록.
A dataclass storing named Arrays in order. This means that it holds entries as an OrderedDict[str, Array]. ParametersRecord objects can be viewed as an equivalent to PyTorch’s state_dict, but holding serialised tensors instead. A
ParametersRecord
is one of the types of records that a flwr.common.RecordSet supports and can therefore be used to constructcommon.Message
objects.- 매개변수:
array_dict (Optional[OrderedDict[str, Array]]) – A dictionary that stores serialized array-like or tensor-like objects.
keep_input (bool (default: False)) – A boolean indicating whether parameters should be deleted from the input dictionary immediately after adding them to the record. If False, the dictionary passed to set_parameters() will be empty once exiting from that function. This is the desired behaviour when working with very large models/tensors/arrays. However, if you plan to continue working with your parameters after adding it to the record, set this flag to True. When set to True, the data is duplicated in memory.
예제
The usage of
ParametersRecord
is envisioned for storing data arrays (e.g. parameters of a machine learning model). These first need to be serialized into aflwr.common.Array
data structure.Let’s see some examples:
>>> import numpy as np >>> from flwr.common import ParametersRecord >>> from flwr.common import array_from_numpy >>> >>> # Let's create a simple NumPy array >>> arr_np = np.random.randn(3, 3) >>> >>> # If we print it >>> array([[-1.84242409, -1.01539537, -0.46528405], >>> [ 0.32991896, 0.55540414, 0.44085534], >>> [-0.10758364, 1.97619858, -0.37120501]]) >>> >>> # Let's create an Array out of it >>> arr = array_from_numpy(arr_np) >>> >>> # If we print it you'll see (note the binary data) >>> Array(dtype='float64', shape=[3,3], stype='numpy.ndarray', data=b'@\x99\x18...') >>> >>> # Adding it to a ParametersRecord: >>> p_record = ParametersRecord({"my_array": arr})
Now that the NumPy array is embedded into a
ParametersRecord
it could be sent if added as part of acommon.Message
or it could be saved as a persistent state of aClientApp
via its context. Regardless of the usecase, we will sooner or later want to recover the array in its original NumPy representation. For the example above, where the array was serialized using the built-in utility function, deserialization can be done as follows:>>> # Use the Array's built-in method >>> arr_np_d = arr.numpy() >>> >>> # If printed, it will show the exact same data as above: >>> array([[-1.84242409, -1.01539537, -0.46528405], >>> [ 0.32991896, 0.55540414, 0.44085534], >>> [-0.10758364, 1.97619858, -0.37120501]])
If you need finer control on how your arrays are serialized and deserialized, you can construct
Array
objects directly like this:>>> from flwr.common import Array >>> # Serialize your array and construct Array object >>> arr = Array( >>> data=ndarray.tobytes(), >>> dtype=str(ndarray.dtype), >>> stype="", # Could be used in a deserialization function >>> shape=list(ndarray.shape), >>> ) >>> >>> # Then you can deserialize it like this >>> arr_np_d = np.frombuffer( >>> buffer=array.data, >>> dtype=array.dtype, >>> ).reshape(array.shape)
Note that different arrays (e.g. from PyTorch, Tensorflow) might require different serialization mechanism. Howerver, they often support a conversion to NumPy, therefore allowing to use the same or similar steps as in the example above.
메소드
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
()- clear() None. Remove all items from D. #
- count_bytes() int [소스]#
이 객체에 저장된 바이트 수를 반환합니다.
역직렬화에 필요한 직렬화된 객체의 메타데이터(예: NumPy 배열)에 해당하는 소량의 바이트도 이 카운팅에 포함될 수 있습니다.
- 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 #