charles-r-earp / autograph

A machine learning library for Rust.
Apache License 2.0
315 stars 17 forks source link

[question] how to convert BufferBase to Vec #50

Closed chriamue closed 1 year ago

chriamue commented 2 years ago

Hi, I use the network defined in https://github.com/charles-r-earp/autograph/blob/main/examples/neural-network-mnist/src/main.rs The output of the model in inference as_raw_slice() is F32(BufferBase { device: Device(0), len: 14190, elem: "f32" }). Now I try to get a Vec<32> of it to interpret the results. Can you help me to understand how to get the data?

Here is a peace of code:

autograph=v0.1.1

let prediction = self
        .net
        .clone()
        .into_device(device)
        .await
        .unwrap()
        .infer(&x)
        .unwrap();
let prediction: FloatBuffer = prediction
        .as_raw_slice()
        .into_device(Device::host())
        .await
        .unwrap();
println!("{:?}", prediction);
// F32(BufferBase { device: Host, len: 14190, elem: "f32" })

Some examples show to "read" data back: let output = y.read().await?; But FloatBuffer does not have the read() function.

charles-r-earp commented 1 year ago

Sorry for not getting to this earlier. Note that autograph has and is going through some updates so this will be greatly improved. Internally the library had some methods to convert between FloatBuffer and Buffer, but I didn't end up exposing them! Anyway, FloatBuffer is an enum so you can just use a match / if let:

let buffer = if let FloatBuffer::F32(buffer) = prediction {
    buffer 
} else {
    unreachable!()
};

Thanks for checking out my library! :)