Casting - Convertissez votre structure de données en octets

manta_light.utils.dict_to_bytes(d: dict) bytes

Convertir un dictionnaire en octets

Paramètres:

d (dict) – Le dictionnaire pour convertir

Renvoie:

La représentation des octets du dictionnaire

Type renvoyé:

bytes

Exemples

>>> from manta_light.utils import dict_to_bytes
>>> dictionary = {"key1": ["a", "b"], "key2", 0.2}
>>> dict_to_bytes(dictionary)

Note

Vous pouvez également trouver dict_to_bytes dans manta.utils.

manta_light.utils.bytes_to_dict(b: bytes) dict

Convertir des octets en dictionnaire

Paramètres:

b (bytes) – Les octets à convertir

Renvoie:

La représentation du dictionnaire des octets

Type renvoyé:

dict

Exemples

>>> from manta_light.utils import dict_to_bytes
>>> dictionary = {"key1": ["a", "b"], "key2", 0.2}
>>> dict_bytes = dict_to_bytes(dictionary)
>>> bytes_to_dict(dict_bytes)

Note

Vous pouvez également trouver bytes_to_dict dans manta.utils.

manta_light.utils.torchmodel_to_bytes(model: torch.nn.Module) bytes

Transformer un modèle de torche en octets

Paramètres:

model ("torch.nn.Module") – Modèle de torche

Renvoie:

Octets du modèle de torche

Type renvoyé:

bytes

Exemples

>>> from torch.nn import Linear, ReLU, Sequential
>>> from manta_light.utils import bytes_to_torchmodel, torchmodel_to_bytes
>>> torch_model = Sequential(
...     Linear(3, 2), ReLU(), Linear(2, 1), ReLU(), Linear(1, 1), ReLU()
... )
>>> torchmodel_to_bytes(torch_model)
manta_light.utils.bytes_to_torchmodel(b: bytes) torch.nn.Module

Transformer les octets en modèle de torche

Paramètres:

b (bytes) – Octets d’un modèle de torche

Renvoie:

Modèle de torche

Type renvoyé:

« torch.nn.Module »

Exemples

>>> from torch.nn import Linear, ReLU, Sequential
>>> from manta_light.utils import bytes_to_torchmodel, torchmodel_to_bytes
>>> torch_model = Sequential(
...     Linear(3, 2), ReLU(), Linear(2, 1), ReLU(), Linear(1, 1), ReLU()
... )
>>> model_bytes = torchmodel_to_bytes(torch_model)
>>> bytes_to_torchmodel(model_bytes)
manta_light.utils.numpy_to_bytes(data: ndarray | list | dict) bytes | dict

Fonction récursive qui convertit un tableau numpy ou un itérateur avec numpytableaux en octets

Paramètres:

data (np.array) – Le tableau numpy à convertir

Renvoie:

La représentation en octets du tableau numpy

Type renvoyé:

bytes

Exemples

  • De np.array

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> np_array = np.array([1, 2, 3])
>>> numpy_to_bytes(np_array)
  • De Dict[str, np.array]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> dict_np_array = {"key1": np.array([1, 2, 3]), "key2": np.array([4, 5, 6])}
>>> numpy_to_bytes(dict_np_array)
  • De Dict[str, list]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> dict_list = {"key1": [1, 2, 3], "key2": [4, 5, 6]}
>>> numpy_to_bytes(dict_list)
  • De List[np.array]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> list_np_array = [np.array([1, 2, 3]), np.array([4, 5, 6])]
>>> numpy_to_bytes(list_np_array)
manta_light.utils.bytes_to_numpy(b: bytes | dict) ndarray | dict

Convertir des octets en un tableau numpy

Paramètres:

b (bytes) – Les octets à convertir

Renvoie:

La représentation du tableau numpy des octets

Type renvoyé:

np.array

Exemples

  • De np.array

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> np_array = np.array([1, 2, 3])
>>> np_bytes = numpy_to_bytes(np_array)
>>> bytes_to_numpy(np_bytes)
  • De Dict[str, np.array]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> dict_np_array = {"key1": np.array([1, 2, 3]), "key2": np.array([4, 5, 6])}
>>> np_bytes = numpy_to_bytes(dict_np_array)
>>> bytes_to_numpy(np_bytes)
  • De Dict[str, list]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> dict_list = {"key1": [1, 2, 3], "key2": [4, 5, 6]}
>>> np_bytes = numpy_to_bytes(dict_list)
>>> bytes_to_numpy(np_bytes)
  • De List[np.array]

>>> import numpy as np
>>> from manta_light.utils import bytes_to_numpy, numpy_to_bytes
>>> list_np_array = [np.array([1, 2, 3]), np.array([4, 5, 6])]
>>> np_bytes = numpy_to_bytes(list_np_array)
>>> bytes_to_numpy(np_bytes)