Skip to content

Instantly share code, notes, and snippets.

@Anindyadeep
Last active July 22, 2024 04:26
Show Gist options
  • Save Anindyadeep/b66079f3c55007580baabcb9aad91158 to your computer and use it in GitHub Desktop.
Save Anindyadeep/b66079f3c55007580baabcb9aad91158 to your computer and use it in GitHub Desktop.
Autograd basic forward operations
class Value:
def __init__(self, data: int, label: str, _children = (), _op: str = ""):
self.data = data
self.label = label
self._children = set(_children)
self._op = _op
def __repr__(self) -> str:
return f"Value({self.label}, data={self.data})"
def __add__(self, other):
# Convert other to Value if other is not a Value object
other = other if isinstance(other, Value) else Value(
data=other,
label=str(other)
)
out = Value(
data = self.data + other.data,
label="",
_children=(self, other),
_op="+"
)
return out
def __radd__(self, other):
# This will be other + self
return self + other
def __neg__(self):
return self * -1
# Substraction
# (Special case of addition and negation)
def __sub__(self, other):
return self + (-other)
def __rsub__(self, other):
return other + (-self)
# Multiplication
def __mul__(self, other):
other = other if isinstance(other, Value) else Value(
data=other,
label=str(other)
)
out = Value(
data = self.data * other.data,
label="",
_children=(self, other),
_op="*"
)
return out
def __rmul__(self, other): # other * self
return self * other
# Power
def __pow__(self, other):
out = Value(
data = self.data**other,
label="",
_children=(self,),
_op=f'**{other}'
)
return out
# Division (Special case of division and ^-1)
def __truediv__(self, other):
return self * other**-1
def __rtruediv__(self, other):
return other * self**-1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment