Struct microtensor::Graph
source · pub struct Graph<T: Real + 'static> {
pub inputs: Vec<Variable<T>>,
pub outputs: Vec<Variable<T>>,
}Expand description
Snapshot of a computation graph with defined inputs and outputs.
Can be used for recomputing the entire graph or saving it to disc.
Fields§
§inputs: Vec<Variable<T>>§outputs: Vec<Variable<T>>Implementations§
source§impl<T: Real + Serialize + DeserializeOwned + 'static> Graph<T>
impl<T: Real + Serialize + DeserializeOwned + 'static> Graph<T>
sourcepub fn new(inputs: &[Variable<T>], outputs: &[Variable<T>]) -> Self
pub fn new(inputs: &[Variable<T>], outputs: &[Variable<T>]) -> Self
Examples found in repository?
examples/graph.rs (line 31)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn build_model(filename: &str) {
// Have some inputs
let x1 = Tensor::vec(&[1.0, 2.0]).tracked();
let x2 = Tensor::ones(&[16]).tracked();
let w = Tensor::randn(&[2, 16]).trained();
// Do some computations
let y = x1.mm(&w);
let z = (&y * &x2).sum(0);
// Pack the resulting graph into a Graph structure to make its inputs
// and outputs explicit and arrange them in an order of your liking.
let graph = Graph::new(&[x1, x2], &[y, z]);
// Save entire computation graph to disc
graph.save(filename).unwrap();
}sourcepub fn run(&self, inputs: &[&Variable<T>])
pub fn run(&self, inputs: &[&Variable<T>])
Examples found in repository?
examples/graph.rs (lines 43-46)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
fn load_model(filename: &str) {
let graph = Graph::load(filename).unwrap();
// Feed new data using #run.
// Updating the entire graph in this way is more efficient
// than calling #forward on each individual output.
graph.run(&[
&Tensor::vec(&[5.0, 6.0]).tracked(),
&Tensor::randn(&[16]).tracked(),
]);
// Get new output..
let z = &graph.outputs[1];
println!("z is now {}", z.item());
// ..or train the model further
let z = &graph.outputs[1];
z.backward();
for mut param in z.parameters() {
param -= param.grad().unwrap() * 0.01
}
}pub fn parameters(&self) -> Vec<Variable<T>>
sourcepub fn load(filename: &str) -> Result<Self>
pub fn load(filename: &str) -> Result<Self>
Examples found in repository?
examples/graph.rs (line 38)
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
fn load_model(filename: &str) {
let graph = Graph::load(filename).unwrap();
// Feed new data using #run.
// Updating the entire graph in this way is more efficient
// than calling #forward on each individual output.
graph.run(&[
&Tensor::vec(&[5.0, 6.0]).tracked(),
&Tensor::randn(&[16]).tracked(),
]);
// Get new output..
let z = &graph.outputs[1];
println!("z is now {}", z.item());
// ..or train the model further
let z = &graph.outputs[1];
z.backward();
for mut param in z.parameters() {
param -= param.grad().unwrap() * 0.01
}
}sourcepub fn save(&self, filename: &str) -> Result<()>
pub fn save(&self, filename: &str) -> Result<()>
Examples found in repository?
examples/graph.rs (line 34)
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
fn build_model(filename: &str) {
// Have some inputs
let x1 = Tensor::vec(&[1.0, 2.0]).tracked();
let x2 = Tensor::ones(&[16]).tracked();
let w = Tensor::randn(&[2, 16]).trained();
// Do some computations
let y = x1.mm(&w);
let z = (&y * &x2).sum(0);
// Pack the resulting graph into a Graph structure to make its inputs
// and outputs explicit and arrange them in an order of your liking.
let graph = Graph::new(&[x1, x2], &[y, z]);
// Save entire computation graph to disc
graph.save(filename).unwrap();
}Trait Implementations§
Auto Trait Implementations§
impl<T> !RefUnwindSafe for Graph<T>
impl<T> Send for Graph<T>
impl<T> Sync for Graph<T>
impl<T> Unpin for Graph<T>
impl<T> !UnwindSafe for Graph<T>
Blanket Implementations§
source§impl<T> Type for T
impl<T> Type for T
source§const METATYPE: MetaType = MetaType::Concrete
const METATYPE: MetaType = MetaType::Concrete
Enum describing whether a type is
TraitObject, Slice or Concrete.source§fn dangling(_t: <T as Type>::Meta) -> NonNull<T>
fn dangling(_t: <T as Type>::Meta) -> NonNull<T>
Create a dangling non-null
*const Self with the provided Self::Meta.source§fn fatten(thin: *mut (), _t: <T as Type>::Meta) -> *mut T
fn fatten(thin: *mut (), _t: <T as Type>::Meta) -> *mut T
Create a
*mut Self with the provided Self::Meta.source§fn meta_type(self: *const Self) -> MetaType
fn meta_type(self: *const Self) -> MetaType
Helper method describing whether a type is
TraitObject, Slice or Concrete.source§impl<T> Type for Twhere
T: ?Sized,
impl<T> Type for Twhere T: ?Sized,
source§default fn dangling(t: <T as Type>::Meta) -> NonNull<T>
default fn dangling(t: <T as Type>::Meta) -> NonNull<T>
Create a dangling non-null
*const Self with the provided Self::Meta.source§default fn fatten(thin: *mut (), t: <T as Type>::Meta) -> *mut T
default fn fatten(thin: *mut (), t: <T as Type>::Meta) -> *mut T
Create a
*mut Self with the provided Self::Meta.source§fn meta_type(self: *const Self) -> MetaType
fn meta_type(self: *const Self) -> MetaType
Helper method describing whether a type is
TraitObject, Slice or Concrete.