Crate tch_serde[−][src]
Expand description
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, Reduction}; #[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, #[serde(with = "tch_serde::serde_reduction")] reduction: Reduction, } let example = Example { tensor: Tensor::randn( &[2, 3], (Kind::Float, Device::Cuda(0)), ), kind: Kind::Float, device: Device::Cpu, reduction: Reduction::Mean, }; 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",
"reduction": "mean",
}
Modules
Serializing/Deserializing functions for Device.
Serializing/Deserializing functions for Kind.
Serializing/Deserializing functions for Reduction.
Serializing/Deserializing functions for Tensor.
Structs
The serialized representation of Tensor.