[−][src]Crate tch_serde
Serialize/Deserialize tch types with serde.
The serializing and deserializing methods are groupped in serde_tensor
,
serde_kind
and other similar modules. You can annotate #[serde(with = "tch_serde::serde_tensor")]
attributes on fields to enable serialization.
The snipplet serializes a compound type of Tensor, Kind and Device.
use tch::{Tensor, Device, Kind}; #[derive(Debug, serde::Serialize, serde::Deserialize)] struct Example { #[serde(with = "tch_serde::serde_tensor")] tensor: Tensor, #[serde(with = "tch_serde::serde_kind")] kind: Kind, #[serde(with = "tch_serde::serde_device")] device: Device, } fn main() { let example = Example { tensor: Tensor::randn( &[2, 3], (Kind::Float, Device::cuda_if_available()), ), kind: Kind::Float, device: Device::Cpu }; let text = serde_json::to_string_pretty(&example).unwrap(); println!("{}", text); }
For example, it produces the following JSON text.
{
"tensor": {
"requires_grad": false,
"device": "cuda(0)",
"shape": [
2,
3
],
"kind": "float",
"data": [
182,
59,
207,
190,
12,
195,
95,
62,
123,
68,
200,
191,
242,
98,
231,
190,
108,
94,
225,
62,
56,
45,
3,
190
]
},
"kind": "float",
"device": "cpu"
}
Modules
serde_device | Serializing/Deserializing functions for Device. |
serde_kind | Serializing/Deserializing functions for Kind. |
serde_tensor | Serializing/Deserializing functions for Tensor. |
Structs
TensorRepr | The serialized representation of Tensor. |