tch-serde 0.1.1

Serialize/Deserialize tch-rs types with serde
docs.rs failed to build tch-serde-0.1.1
Please check the build logs for more information.
See Builds for ideas on how to fix a failed build, or Metadata for how to configure docs.rs builds.
If you believe this is docs.rs' fault, open an issue.
Visit the last successful build: tch-serde-0.8.0

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.

tch-serde = "0.1"

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

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

#[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);
}

License

MIT license. See the LICENSE file.