tch-serde

Crates.iotch-serde
lib.rstch-serde
version0.8.0
sourcesrc
created_at2020-07-21 21:15:08.059442
updated_at2022-04-03 07:14:21.022078
descriptionSerialize/Deserialize tch-rs types with serde
homepagehttps://github.com/jerry73204/tch-serde.git
repositoryhttps://github.com/jerry73204/tch-serde
max_upload_size
id267781
size34,181
(jerry73204)

documentation

https://docs.rs/tch-serde/

README

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

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

docs.rs | crates.io

Usage

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,
    #[serde(with = "tch_serde::serde_reduction")]
    reduction: Reduction,
}

fn main() {
    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);
}

License

MIT license. See the LICENSE file.

Commit count: 39

cargo fmt