Variable Class Reference

#include <variable.h>

Public Member Functions

 Variable (double value=0, double gradient=0, std::string op="", std::string name="")
 
 Variable (const Variable &other)
 
Variableoperator= (const Variable &other)
 
Variableoperator= (Variable &&other) noexcept
 
 Variable (Variable &&other) noexcept
 
 ~Variable ()
 
double value () const
 
const std::string & name () const
 
void set_name (const std::string &name)
 
void set_value (double value)
 
const std::string & op () const
 
void set_op (const std::string &op)
 
void set_backward (std::function< void(Variable *)> backward)
 
double gradient () const
 
void set_gradient (double gradient)
 
Variablereference () const
 
void set_ref (Variable *reference)
 
const std::vector< Variable > & children () const
 
std::vector< Variable > & mutable_children ()
 
void set_children (const std::vector< Variable > &children)
 
void backward ()
 
void update_gradient (double grad)
 
void zero_grad ()
 
void gradient_descent (double lr)
 
Variable activate (std::string activate_function)
 
Variable operator+ (const Variable &other)
 
Variable operator- (const Variable &other)
 
Variable operator* (const Variable &other)
 
Variable operator/ (const Variable &other)
 
Variable operator- () const
 
Variable identity () const
 
Variable operator+ (const double other) const
 
Variable operator- (const double other) const
 
Variable operator* (const double other) const
 
Variable operator/ (const double other) const
 
Variable pow (const double other) const
 
Variable exp () const
 
Variable log () const
 
Variable sin () const
 
Variable cos () const
 
Variable tan () const
 
Variable sinh () const
 
Variable cosh () const
 
Variable tanh () const
 
Variable relu () const
 
Variable sigmoid () const
 

Friends

std::ostream & operator<< (std::ostream &os, const Variable &var)
 
Variable operator+ (const double other, const Variable &var)
 
Variable operator- (const double other, const Variable &var)
 
Variable operator* (const double other, const Variable &var)
 
Variable operator/ (const double other, const Variable &var)
 
Variable dot_product (const std::vector< Variable > &a, const std::vector< Variable > &b)
 
Variable dot_product (const std::vector< Variable > &a, const std::vector< double > &b)
 

Detailed Description

This class represents a variable in a mathematical expression.

Constructor & Destructor Documentation

◆ Variable() [1/3]

Variable::Variable ( double  value = 0,
double  gradient = 0,
std::string  op = "",
std::string  name = "" 
)
inlineexplicit

Constructs a new Variable object.

Parameters
valueThe initial value of the variable.
gradientThe initial gradient of the variable.
opThe operation associated with the variable.
nameThe name of the variable.

◆ Variable() [2/3]

Variable::Variable ( const Variable other)
inline

Copy constructor.

Parameters
otherThe Variable object to copy from.

◆ Variable() [3/3]

Variable::Variable ( Variable &&  other)
inlinenoexcept

Move constructor.

Parameters
otherThe Variable object to move from.
Note
The reference will be set to this.

◆ ~Variable()

Variable::~Variable ( )
inline

Destructor.

Member Function Documentation

◆ activate()

Variable Variable::activate ( std::string  activate_function)

Activate the variable with the given activate function.

Parameters
activate_functionThe name of given activate function.
Returns
the activated variable.

◆ backward()

void Variable::backward ( )
inline

Performs the backward pass for the variable and its children.

Note
The backward function is called recursively for each child.

◆ children()

const std::vector<Variable>& Variable::children ( ) const
inline

Gets the child variables of this variable.

Returns
The child variables of this variable.

◆ cos()

Variable Variable::cos ( ) const

Calculates the cosine of the variable.

Returns
The cosine of the variable.

◆ cosh()

Variable Variable::cosh ( ) const

Calculates the hyperbolic cosine of the variable.

Returns
The hyperbolic cosine of the variable.

◆ exp()

Variable Variable::exp ( ) const

Calculates the exponential of the variable.

Returns
The exponential of the variable.

◆ gradient()

double Variable::gradient ( ) const
inline

Gets the gradient of the variable.

Returns
The gradient of the variable.

◆ gradient_descent()

void Variable::gradient_descent ( double  lr)
inline

Performs gradient descent optimization on the variable.

Parameters
lrThe learning rate.
Note
The gradient is synchronized with the reference variable.

◆ identity()

Variable Variable::identity ( ) const

Returns a copy of the variable.

Returns
The copy of the variable.

◆ log()

Variable Variable::log ( ) const

Calculates the natural logarithm of the variable.

Returns
The natural logarithm of the variable.

◆ mutable_children()

std::vector<Variable>& Variable::mutable_children ( )
inline

Gets the mutable child variables of this variable.

Returns
The mutable child variables of this variable.

◆ name()

const std::string& Variable::name ( ) const
inline

Gets the name of the variable.

Returns
The name of the variable.

◆ op()

const std::string& Variable::op ( ) const
inline

Gets the operation associated with the variable.

Returns
The operation associated with the variable.

◆ operator*() [1/2]

Variable Variable::operator* ( const double  other) const

Overloads the multiplication operator to perform element-wise multiplication of a variable and a scalar.

Parameters
otherThe scalar to be multiplied.
Returns
The result of the multiplication.

◆ operator*() [2/2]

Variable Variable::operator* ( const Variable other)

Overloads the multiplication operator to perform element-wise multiplication of two variables.

Parameters
otherThe variable to be multiplied.
Returns
The result of the multiplication.

◆ operator+() [1/2]

Variable Variable::operator+ ( const double  other) const

Overloads the addition operator to perform element-wise addition of a variable and a scalar.

Parameters
otherThe scalar to be added.
Returns
The result of the addition.

◆ operator+() [2/2]

Variable Variable::operator+ ( const Variable other)

Overloads the addition operator to perform element-wise addition of two variables.

Parameters
otherThe variable to be added.
Returns
The result of the addition.

◆ operator-() [1/3]

Variable Variable::operator- ( ) const

Overloads the unary negation operator to negate the value of the variable.

Returns
The negated variable.

◆ operator-() [2/3]

Variable Variable::operator- ( const double  other) const

Overloads the subtraction operator to perform element-wise subtraction of a variable and a scalar.

Parameters
otherThe scalar to be subtracted.
Returns
The result of the subtraction.

◆ operator-() [3/3]

Variable Variable::operator- ( const Variable other)

Overloads the subtraction operator to perform element-wise subtraction of two variables.

Parameters
otherThe variable to be subtracted.
Returns
The result of the subtraction.

◆ operator/() [1/2]

Variable Variable::operator/ ( const double  other) const

Overloads the division operator to perform element-wise division of a variable and a scalar.

Parameters
otherThe scalar to be divided.
Returns
The result of the division.

◆ operator/() [2/2]

Variable Variable::operator/ ( const Variable other)

Overloads the division operator to perform element-wise division of two variables.

Parameters
otherThe variable to be divided.
Returns
The result of the division.

◆ operator=() [1/2]

Variable& Variable::operator= ( const Variable other)
inline

Assignment operator.

Parameters
otherThe Variable object to assign from.
Returns
Reference to the assigned Variable object.
Note
The reference will be kept.

◆ operator=() [2/2]

Variable& Variable::operator= ( Variable &&  other)
inlinenoexcept

Move assignment operator.

Parameters
otherThe Variable object to move from.
Returns
Reference to the moved Variable object.
Note
The reference will be kept.

◆ pow()

Variable Variable::pow ( const double  other) const

Calculates the power of the variable raised to a scalar exponent.

Parameters
otherThe scalar exponent.
Returns
The result of raising the variable to the exponent.

◆ reference()

Variable* Variable::reference ( ) const
inline

Gets the reference to another variable.

Returns
The reference to another variable.

◆ relu()

Variable Variable::relu ( ) const

Calculates the rectified linear unit (ReLU) of the variable.

Returns
The ReLU of the variable.

◆ set_backward()

void Variable::set_backward ( std::function< void(Variable *)>  backward)
inline

Sets the backward function associated with the variable.

Parameters
backwardThe backward function associated with the variable.

◆ set_children()

void Variable::set_children ( const std::vector< Variable > &  children)
inline

Sets the child variables of this variable.

Parameters
childrenThe child variables of this variable.

◆ set_gradient()

void Variable::set_gradient ( double  gradient)
inline

Sets the gradient of the variable.

Parameters
gradientThe gradient of the variable.

◆ set_name()

void Variable::set_name ( const std::string &  name)
inline

Sets the name of the variable.

Parameters
nameThe name of the variable.

◆ set_op()

void Variable::set_op ( const std::string &  op)
inline

Sets the operation associated with the variable.

Parameters
opThe operation associated with the variable.

◆ set_ref()

void Variable::set_ref ( Variable reference)
inline

Sets the reference to another variable.

Parameters
referenceThe reference to another variable.

◆ set_value()

void Variable::set_value ( double  value)
inline

Sets the value of the variable.

Parameters
valueThe value of the variable.

◆ sigmoid()

Variable Variable::sigmoid ( ) const

Calculates the sigmoid activation function of the variable.

Returns
The sigmoid of the variable.

◆ sin()

Variable Variable::sin ( ) const

Calculates the sine of the variable.

Returns
The sine of the variable.

◆ sinh()

Variable Variable::sinh ( ) const

Calculates the hyperbolic sine of the variable.

Returns
The hyperbolic sine of the variable.

◆ tan()

Variable Variable::tan ( ) const

Calculates the tangent of the variable.

Returns
The tangent of the variable.

◆ tanh()

Variable Variable::tanh ( ) const

Calculates the hyperbolic tangent of the variable.

Returns
The hyperbolic tangent of the variable.

◆ update_gradient()

void Variable::update_gradient ( double  grad)
inline

Updates the gradient of the variable.

Parameters
gradThe gradient update value.
Note
The gradient is synchronized with the reference variable.

◆ value()

double Variable::value ( ) const
inline

Gets the value of the variable.

Returns
The value of the variable.

◆ zero_grad()

void Variable::zero_grad ( )
inline

Resets the gradient of the variable to zero.

Note
The gradient is synchronized with the reference variable.

Friends And Related Function Documentation

◆ dot_product [1/2]

Variable dot_product ( const std::vector< Variable > &  a,
const std::vector< double > &  b 
)
friend

Calculates the dot product of a vector of variables and a vector of scalars.

Parameters
aThe vector of variables.
bThe vector of scalars.
Returns
The dot product of the two vectors.

◆ dot_product [2/2]

Variable dot_product ( const std::vector< Variable > &  a,
const std::vector< Variable > &  b 
)
friend

Calculates the dot product of two vectors of variables.

Parameters
aThe first vector of variables.
bThe second vector of variables.
Returns
The dot product of the two vectors.

◆ operator*

Variable operator* ( const double  other,
const Variable var 
)
friend

Overloads the multiplication operator to perform element-wise multiplication of a scalar and a variable.

Parameters
otherThe scalar to be multiplied.
varThe variable to be multiplied.
Returns
The result of the multiplication.

◆ operator+

Variable operator+ ( const double  other,
const Variable var 
)
friend

Overloads the addition operator to perform element-wise addition of a scalar and a variable.

Parameters
otherThe scalar to be added.
varThe variable to be added.
Returns
The result of the addition.

◆ operator-

Variable operator- ( const double  other,
const Variable var 
)
friend

Overloads the subtraction operator to perform element-wise subtraction of a scalar and a variable.

Parameters
otherThe scalar to be subtracted.
varThe variable to be subtracted.
Returns
The result of the subtraction.

◆ operator/

Variable operator/ ( const double  other,
const Variable var 
)
friend

Overloads the division operator to perform element-wise division of a scalar and a variable.

Parameters
otherThe scalar to be divided.
varThe variable to be divided.
Returns
The result of the division.

◆ operator<<

std::ostream& operator<< ( std::ostream &  os,
const Variable var 
)
friend

Overloads the stream insertion operator to print the value of the variable.

Parameters
osThe output stream.
varThe variable to be printed.
Returns
The modified output stream.

The documentation for this class was generated from the following files: