tch-serde: Serialize/Deserialize tch-rs types with serde

This crate provides {ser,de}ialization methods for tch-rs common types.

Usage

Add this line to your Cargo.toml to work with this crate.

toml tch-serde = "0.3"

For example, annotate #[serde(with = "tch_serde::serde_tensor")] attributes to enable serialization on tensor field.

```rust use tch::{Device, Kind, Tensor};

[derive(Debug, serde::Serialize, serde::Deserialize)]

struct Example { #[serde(with = "tchserde::serdetensor")] tensor: Tensor, #[serde(with = "tchserde::serdekind")] kind: Kind, #[serde(with = "tchserde::serdedevice")] device: Device, }

fn main() { let example = Example { tensor: Tensor::randn(&[2, 3], (Kind::Float, Device::cudaifavailable())), kind: Kind::Float, device: Device::Cpu, }; let text = serdejson::tostring_pretty(&example).unwrap(); println!("{}", text); } ```

License

MIT license. See the LICENSE file.