Graph

Struct Graph 

Source
pub struct Graph {
    pub optimizer: Optimizer,
    pub named_idxs: BTreeMap<String, Idx>,
    /* private fields */
}
Expand description

A differentiable computation graph. Use this struct to hold your differentiable program which is a directed acyclic graph of Nodes, their associated values and losses (gradients). The graph computes values moving forward in insertion order (see forward method) and propagates losses backwards in reverse insertion order (see backward method). The default graph comes with an xavier initializer and a vanilla stochastic gradient descent optimizer.

Fields§

§optimizer: Optimizer§named_idxs: BTreeMap<String, Idx>

Implementations§

Source§

impl Graph

Source

pub fn new( initializer: Box<dyn Fn(&[usize]) -> ArrayD<f32>>, optimizer: Optimizer, ) -> Self

Consider using Graph::default() if you don’t want to choose your own optimizer and initializer.

Source

pub fn register(&mut self, node: Node) -> Idx

Inserts the node into the graph and returns the index

Source

pub fn param(&mut self, shape: &[usize]) -> Idx

Registers a parameter of the given shape and initializes the value using the graph’s initializer.

Source

pub fn input( &mut self, it: Option<Box<dyn Iterator<Item = ArrayD<f32>>>>, ) -> Idx

Registers an input node which advances the iterator it each forward pass.

Source

pub fn op(&mut self, op: impl Operation + 'static, inputs: &[Idx]) -> Idx

Registers an operation and its inputs

Source

pub fn constant(&mut self, c: ArrayD<f32>) -> Idx

Registers a constant, sets its value to c, then returns the idx

Source

pub fn forward(&mut self)

Computes values for each node in insertion order. Parameters are unaffected. Inputs will set their value to the next output of their iterator, Operations will compute a new value based on the values of its inputs.

Source

pub fn backward(&mut self)

Propagates gradients in reverse insertion order. Parameters will apply gradients with the graph’s optimizer. Inputs are unaffected Operations will compute gradient given values from their inputs and gradients from its outputs

Source

pub fn forward1(&mut self, i: Idx)

Updates value and resets losses for node with Idx i.

Source

pub fn backward1(&mut self, i: Idx)

Back propagates losses for node with Idx i.

Source

pub fn remove(&mut self, idx: Idx)

Remove the node at idx as well as its associated value and loss.

Source

pub fn clear_non_parameters(&mut self)

This op removes every node from the graph that is not a parameter. This is useful for dynamic graphs and recurrent neural networks when you want to rebuild everything each forward and backward pass of the network.

Source

pub fn set_value(&mut self, idx: Idx, val: ArrayD<f32>)

Source

pub fn get_value(&self, idx: Idx) -> &ArrayD<f32>

Source

pub fn set_loss(&mut self, idx: Idx, loss: ArrayD<f32>)

Source

pub fn get_loss(&self, idx: Idx) -> &ArrayD<f32>

Source

pub fn replace_input_iterator( &mut self, idx: Idx, new: Box<dyn Iterator<Item = ArrayD<f32>>>, ) -> Result<(), String>

Replace an Input node’s iterator or converts Constant nodes into Input with this iterator. Note that Input node iterators are not saved when serialized with serde.

Source

pub fn add(&mut self, inputs: &[Idx]) -> Idx

Source

pub fn mult(&mut self, inputs: &[Idx]) -> Idx

Source

pub fn conv( &mut self, kernel: Idx, img: Idx, padding: Padding, stride: usize, ) -> Idx

Registers a convolution operation node and returns the index

Source

pub fn global_pool(&mut self, x: Idx, pool: GlobalPool) -> Idx

Registers a pooling operation takes a Batch * Height * Width * Channels image and reduces it to a Batch * Channels vector.

Source

pub fn relu(&mut self, x: Idx) -> Idx

Registers a Relu operation which takes the elementwise maximum of the input array and 0.

Source

pub fn sigmoid(&mut self, x: Idx) -> Idx

Registers a new sigmoid activation operation, an elementwise application of $\frac{ 1 }{1 - e^{-x}}$.

Source

pub fn tanh(&mut self, x: Idx) -> Idx

Registers a Tanh operation.

Source

pub fn matmul(&mut self, mat: Idx, v: Idx) -> Idx

Registers a matrix multiplication of vectors v by matrix mat.

Source

pub fn embedding(&mut self, emb: Idx, code: Idx) -> Idx

Registers an embedding later that converts A0 to vector representation

Trait Implementations§

Source§

impl Debug for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Graph

Source§

fn default() -> Self

xavier initializer and normal gradient descent

Source§

impl<'de> Deserialize<'de> for Graph

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Graph

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Serialize for Graph

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

§

impl Freeze for Graph

§

impl !RefUnwindSafe for Graph

§

impl !Send for Graph

§

impl !Sync for Graph

§

impl Unpin for Graph

§

impl !UnwindSafe for Graph

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,