Crates.io | tch-serde |
lib.rs | tch-serde |
version | 0.8.0 |
source | src |
created_at | 2020-07-21 21:15:08.059442 |
updated_at | 2022-04-03 07:14:21.022078 |
description | Serialize/Deserialize tch-rs types with serde |
homepage | https://github.com/jerry73204/tch-serde.git |
repository | https://github.com/jerry73204/tch-serde |
max_upload_size | |
id | 267781 |
size | 34,181 |
This crate provides {ser,de}ialization methods for tch-rs common types.
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);
}
MIT license. See the LICENSE file.