Expr

Struct Expr 

Source
pub struct Expr {
    pub result: f64,
    pub is_learnable: bool,
    pub name: Option<String>,
    /* private fields */
}
Expand description

Expression representing a node in a calculation graph.

This struct represents a node in a calculation graph. It can be a leaf node, a unary operation or a binary operation.

A leaf node holds a value, which is the one that is used in the calculation.

A unary expression is the result of applying a unary operation to another expression. For example, the result of applying the tanh operation to a leaf node.

A binary expression is the result of applying a binary operation to two other expressions. For example, the result of adding two leaf nodes.

Fields§

§result: f64

The numeric result of the expression, as result of applying the operation to the operands.

§is_learnable: bool

Whether the expression is learnable or not. Only learnable Expr will have their values updated during backpropagation (learning).

§name: Option<String>

The name of the expression, used to identify it in the calculation graph.

Implementations§

Source§

impl Expr

Source

pub fn new_leaf(value: f64) -> Expr

Creates a new leaf expression with the given value.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
Source

pub fn new_leaf_with_name(value: f64, name: &str) -> Expr

Creates a new leaf expression with the given value and name.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf_with_name(1.0, "x");
 
assert_eq!(expr.name, Some("x".to_string()));
Source

pub fn tanh(self) -> Expr

Applies the hyperbolic tangent function to the expression and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = expr.tanh();
 
assert_eq!(expr2.result, 0.7615941559557649);
Source

pub fn relu(self) -> Expr

Applies the rectified linear unit function to the expression and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(-1.0);
let expr2 = expr.relu();
 
assert_eq!(expr2.result, 0.0);
Source

pub fn exp(self) -> Expr

Applies the exponential function (e^x) to the expression and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = expr.exp();
 
assert_eq!(expr2.result, 2.718281828459045);
Source

pub fn pow(self, exponent: Expr) -> Expr

Raises the expression to the power of the given exponent (expression) and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(2.0);
let exponent = Expr::new_leaf(3.0);
let result = expr.pow(exponent);
 
assert_eq!(result.result, 8.0);
Source

pub fn log(self) -> Expr

Applies the natural logarithm function to the expression and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(2.0);
let expr2 = expr.log();
 
assert_eq!(expr2.result, 0.6931471805599453);
Source

pub fn neg(self) -> Expr

Negates the expression and returns it as a new expression.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = expr.neg();
 
assert_eq!(expr2.result, -1.0);
Source

pub fn recalculate(&mut self)

Recalculates the value of the expression recursively, from new values of the operands.

Usually will be used after a call to Expr::learn, where the gradients have been calculated and the internal values of the expression tree have been updated.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let mut expr2 = expr.tanh();
expr2.learn(1e-09);
expr2.recalculate();
 
assert_eq!(expr2.result, 0.7615941557793864);

You can also vary the values of the operands and recalculate the expression:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf_with_name(1.0, "x");
let mut expr2 = expr.tanh();
 
let mut original = expr2.find_mut("x").expect("Could not find x");
original.result = 2.0;
expr2.recalculate();
 
assert_eq!(expr2.result, 0.9640275800758169);
Source

pub fn learn(&mut self, learning_rate: f64)

Applies backpropagation to the expression, updating the values of the gradients and the expression itself.

This method will change the gradients based on the gradient of the last expression in the calculation graph.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let mut expr2 = expr.tanh();
expr2.learn(1e-09);

After adjusting the gradients, the method will update the values of the individual expression tree nodes to minimize the loss function.

In order to get a new calculation of the expression tree, you’ll need to call Expr::recalculate after calling Expr::learn.

Source

pub fn find(&self, name: &str) -> Option<&Expr>

Finds a node in the expression tree by its name.

This method will search the expression tree for a node with the given name. If the node is not found, it will return None.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf_with_name(1.0, "x");
let expr2 = expr.tanh();
let original = expr2.find("x");
 
assert_eq!(original.expect("Could not find x").result, 1.0);
Source

pub fn find_mut(&mut self, name: &str) -> Option<&mut Expr>

Finds a node in the expression tree by its name and returns a mutable reference to it.

This method will search the expression tree for a node with the given name. If the node is not found, it will return None.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf_with_name(1.0, "x");
let mut expr2 = expr.tanh();
let mut original = expr2.find_mut("x").expect("Could not find x");
original.result = 2.0;
expr2.recalculate();
 
assert_eq!(expr2.result, 0.9640275800758169);
Source

pub fn parameter_count(&self, learnable_only: bool) -> usize

Returns the count of nodes (parameters)in the expression tree.

This method will return the total number of nodes in the expression tree, including the root node.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = expr.tanh();
 
assert_eq!(expr2.parameter_count(false), 2);
assert_eq!(expr2.parameter_count(true), 1);

Trait Implementations§

Source§

impl Add<Expr> for f64

Implements the Add trait for the f64 type, when the right operand is an Expr.

This implementation allows the addition of a f64 value and an Expr object.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = 2.0 + expr;
 
assert_eq!(result.result, 3.0);
Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, other: Expr) -> Expr

Performs the + operation. Read more
Source§

impl Add<f64> for Expr

Implements the Add trait for the Expr struct, when the right operand is a f64.

This implementation allows the addition of an Expr object and a f64 value.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = expr + 2.0;
 
assert_eq!(result.result, 3.0);
Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, other: f64) -> Expr

Performs the + operation. Read more
Source§

impl Add for Expr

Implements the Add trait for the Expr struct.

This implementation allows the addition of two Expr objects.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = Expr::new_leaf(2.0);
 
let result = expr + expr2;
 
assert_eq!(result.result, 3.0);
Source§

type Output = Expr

The resulting type after applying the + operator.
Source§

fn add(self, other: Expr) -> Expr

Performs the + operation. Read more
Source§

impl Clone for Expr

Source§

fn clone(&self) -> Expr

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Expr

Source§

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

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

impl Div<f64> for Expr

Implements the Div trait for the Expr struct, when the right operand is a f64.

This implementation allows the division of an Expr object and a f64 value.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = expr / 2.0;
 
assert_eq!(result.result, 0.5);
Source§

type Output = Expr

The resulting type after applying the / operator.
Source§

fn div(self, other: f64) -> Expr

Performs the / operation. Read more
Source§

impl Div for Expr

Implements the Div trait for the Expr struct.

This implementation allows the division of two Expr objects.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = Expr::new_leaf(2.0);
 
let result = expr / expr2;
 
assert_eq!(result.result, 0.5);
Source§

type Output = Expr

The resulting type after applying the / operator.
Source§

fn div(self, other: Expr) -> Expr

Performs the / operation. Read more
Source§

impl Mul<Expr> for f64

Implements the Mul trait for the f64 type, when the right operand is an Expr.

This implementation allows the multiplication of a f64 value and an Expr object.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = 2.0 * expr;
 
assert_eq!(result.result, 2.0);
Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, other: Expr) -> Expr

Performs the * operation. Read more
Source§

impl Mul<f64> for Expr

Implements the Mul trait for the Expr struct, when the right operand is a f64.

This implementation allows the multiplication of an Expr object and a f64 value.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = expr * 2.0;
 
assert_eq!(result.result, 2.0);
Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, other: f64) -> Expr

Performs the * operation. Read more
Source§

impl Mul for Expr

Implements the Mul trait for the Expr struct.

This implementation allows the multiplication of two Expr objects.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = Expr::new_leaf(2.0);
 
let result = expr * expr2;
 
assert_eq!(result.result, 2.0);
Source§

type Output = Expr

The resulting type after applying the * operator.
Source§

fn mul(self, other: Expr) -> Expr

Performs the * operation. Read more
Source§

impl Sub<Expr> for f64

Implements the Sub trait for the f64 type, when the right operand is an Expr.

This implementation allows the subtraction of a f64 value and an Expr object.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = 2.0 - expr;
 
assert_eq!(result.result, 1.0);
Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, other: Expr) -> Expr

Performs the - operation. Read more
Source§

impl Sub<f64> for Expr

Implements the Sub trait for the Expr struct, when the right operand is a f64.

This implementation allows the subtraction of an Expr object and a f64 value.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let result = expr - 2.0;
 
assert_eq!(result.result, -1.0);
Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, other: f64) -> Expr

Performs the - operation. Read more
Source§

impl Sub for Expr

Implements the Sub trait for the Expr struct.

This implementation allows the subtraction of two Expr objects.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = Expr::new_leaf(2.0);
 
let result = expr - expr2;
 
assert_eq!(result.result, -1.0);
Source§

type Output = Expr

The resulting type after applying the - operator.
Source§

fn sub(self, other: Expr) -> Expr

Performs the - operation. Read more
Source§

impl Sum for Expr

Implements the Sum trait for the Expr struct.

Note that this implementation will generate temporary Expr objects, which may not be the most efficient way to sum a collection of Expr objects. However, it is provided as a convenience method for users that want to use sum over an Iterator<Expr>.

Example:

use alpha_micrograd_rust::value::Expr;
 
let expr = Expr::new_leaf(1.0);
let expr2 = Expr::new_leaf(2.0);
let expr3 = Expr::new_leaf(3.0);
 
let sum = vec![expr, expr2, expr3].into_iter().sum::<Expr>();
 
assert_eq!(sum.result, 6.0);
Source§

fn sum<I>(iter: I) -> Self
where I: Iterator<Item = Self>,

Takes an iterator and generates Self from the elements by “summing up” the items.

Auto Trait Implementations§

§

impl Freeze for Expr

§

impl RefUnwindSafe for Expr

§

impl Send for Expr

§

impl Sync for Expr

§

impl Unpin for Expr

§

impl UnwindSafe for Expr

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. 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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V