#include <variable.h>
Public Member Functions | |
Variable (double value=0, double gradient=0, std::string op="", std::string name="") | |
Variable (const Variable &other) | |
Variable & | operator= (const Variable &other) |
Variable & | operator= (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) |
Variable * | reference () 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) |
This class represents a variable in a mathematical expression.
|
inlineexplicit |
Constructs a new Variable object.
value | The initial value of the variable. |
gradient | The initial gradient of the variable. |
op | The operation associated with the variable. |
name | The name of the variable. |
|
inline |
Copy constructor.
other | The Variable object to copy from. |
|
inlinenoexcept |
Move constructor.
other | The Variable object to move from. |
|
inline |
Destructor.
Variable Variable::activate | ( | std::string | activate_function | ) |
Activate the variable with the given activate function.
activate_function | The name of given activate function. |
|
inline |
Performs the backward pass for the variable and its children.
|
inline |
Gets the child variables of this variable.
Variable Variable::cos | ( | ) | const |
Calculates the cosine of the variable.
Variable Variable::cosh | ( | ) | const |
Calculates the hyperbolic cosine of the variable.
Variable Variable::exp | ( | ) | const |
Calculates the exponential of the variable.
|
inline |
Gets the gradient of the variable.
|
inline |
Performs gradient descent optimization on the variable.
lr | The learning rate. |
Variable Variable::identity | ( | ) | const |
Returns a copy of the variable.
Variable Variable::log | ( | ) | const |
Calculates the natural logarithm of the variable.
|
inline |
Gets the mutable child variables of this variable.
|
inline |
Gets the name of the variable.
|
inline |
Gets the operation associated with the variable.
Variable Variable::operator* | ( | const double | other | ) | const |
Overloads the multiplication operator to perform element-wise multiplication of a variable and a scalar.
other | The scalar to be multiplied. |
Overloads the multiplication operator to perform element-wise multiplication of two variables.
other | The variable to be multiplied. |
Variable Variable::operator+ | ( | const double | other | ) | const |
Overloads the addition operator to perform element-wise addition of a variable and a scalar.
other | The scalar to be added. |
Overloads the addition operator to perform element-wise addition of two variables.
other | The variable to be added. |
Variable Variable::operator- | ( | ) | const |
Overloads the unary negation operator to negate the value of the variable.
Variable Variable::operator- | ( | const double | other | ) | const |
Overloads the subtraction operator to perform element-wise subtraction of a variable and a scalar.
other | The scalar to be subtracted. |
Overloads the subtraction operator to perform element-wise subtraction of two variables.
other | The variable to be subtracted. |
Variable Variable::operator/ | ( | const double | other | ) | const |
Overloads the division operator to perform element-wise division of a variable and a scalar.
other | The scalar to be divided. |
Overloads the division operator to perform element-wise division of two variables.
other | The variable to be divided. |
Variable Variable::pow | ( | const double | other | ) | const |
Calculates the power of the variable raised to a scalar exponent.
other | The scalar exponent. |
|
inline |
Gets the reference to another variable.
Variable Variable::relu | ( | ) | const |
Calculates the rectified linear unit (ReLU) of the variable.
|
inline |
Sets the backward function associated with the variable.
backward | The backward function associated with the variable. |
|
inline |
Sets the child variables of this variable.
children | The child variables of this variable. |
|
inline |
Sets the gradient of the variable.
gradient | The gradient of the variable. |
|
inline |
Sets the name of the variable.
name | The name of the variable. |
|
inline |
Sets the operation associated with the variable.
op | The operation associated with the variable. |
|
inline |
Sets the reference to another variable.
reference | The reference to another variable. |
|
inline |
Sets the value of the variable.
value | The value of the variable. |
Variable Variable::sigmoid | ( | ) | const |
Calculates the sigmoid activation function of the variable.
Variable Variable::sin | ( | ) | const |
Calculates the sine of the variable.
Variable Variable::sinh | ( | ) | const |
Calculates the hyperbolic sine of the variable.
Variable Variable::tan | ( | ) | const |
Calculates the tangent of the variable.
Variable Variable::tanh | ( | ) | const |
Calculates the hyperbolic tangent of the variable.
|
inline |
Updates the gradient of the variable.
grad | The gradient update value. |
|
inline |
Gets the value of the variable.
|
inline |
Resets the gradient of the variable to zero.
Calculates the dot product of a vector of variables and a vector of scalars.
a | The vector of variables. |
b | The vector of scalars. |
|
friend |
Calculates the dot product of two vectors of variables.
a | The first vector of variables. |
b | The second vector of variables. |
Overloads the multiplication operator to perform element-wise multiplication of a scalar and a variable.
other | The scalar to be multiplied. |
var | The variable to be multiplied. |
Overloads the addition operator to perform element-wise addition of a scalar and a variable.
other | The scalar to be added. |
var | The variable to be added. |
Overloads the subtraction operator to perform element-wise subtraction of a scalar and a variable.
other | The scalar to be subtracted. |
var | The variable to be subtracted. |
Overloads the division operator to perform element-wise division of a scalar and a variable.
other | The scalar to be divided. |
var | The variable to be divided. |
|
friend |
Overloads the stream insertion operator to print the value of the variable.
os | The output stream. |
var | The variable to be printed. |