pub struct Func { /* private fields */ }Expand description
A function of named inputs and parameters, traced into a graph. Unit of
composition for the grad/vmap/
jvp/hvp transforms.
Inputs vs parameters. Declare per-call data with
GraphScope::input and trainable weights with GraphScope::param.
Bind weight values once with with_param; they are
applied on every run and survive transforms — so f.grad(&["w"]) gives
gradients w.r.t. the weights (training), runnable with just the inputs.
Implementations§
Source§impl Func
impl Func
Sourcepub fn new(
name: impl Into<String>,
build: impl FnOnce(&mut GraphScope) -> Tensor,
) -> Self
pub fn new( name: impl Into<String>, build: impl FnOnce(&mut GraphScope) -> Tensor, ) -> Self
Trace a single-output function. The closure builds the body from inputs
declared via GraphScope::input; the returned tensor is the output.
Sourcepub fn multi(
name: impl Into<String>,
build: impl FnOnce(&mut GraphScope) -> Vec<Tensor>,
) -> Self
pub fn multi( name: impl Into<String>, build: impl FnOnce(&mut GraphScope) -> Vec<Tensor>, ) -> Self
Trace a multi-output function (the closure returns all outputs in order).
Sourcepub fn from_graph(graph: Graph) -> Self
pub fn from_graph(graph: Graph) -> Self
Wrap an already-built graph (its outputs are this function’s outputs).
Sourcepub fn with_param(
self,
name: impl Into<String>,
data: impl Into<Vec<f32>>,
) -> Self
pub fn with_param( self, name: impl Into<String>, data: impl Into<Vec<f32>>, ) -> Self
Bind a parameter (weight) value by name. Overwrites any prior binding
for that name. Builder-style: chain several, then run/grad/jit.
Sourcepub fn output_count(&self) -> usize
pub fn output_count(&self) -> usize
Number of outputs this function produces.
Sourcepub fn param_binding(&self, name: &str) -> Option<&[f32]>
pub fn param_binding(&self, name: &str) -> Option<&[f32]>
The currently bound value of a parameter, if any. After
train_step this is the updated weight.