I hereby claim:
- I am tswedish on github.
- I am tswedish (https://keybase.io/tswedish) on keybase.
- I have a public key ASBmF9szOq6XhfbyhBf4NY1dTFdefEIf91vI5vstfqE7Ugo
To claim this, I am signing this object:
I hereby claim:
To claim this, I am signing this object:
def super_complicated_function(x): | |
return x * sin(x+6.)**2. / 2. - 2. / 2.**x |
class Dual: | |
def __init__(self, value=0., derivative=0.): | |
self.value = value | |
self.derivative = derivative | |
def __add__(self, other): | |
return Dual(self.value + other.value, self.derivative + other.derivative) | |
def __mul__(self, other): | |
return Dual(self.value * other.value, self.value*other.derivative + self.derivative * other.value) |
def __add__(self, other): | |
if isinstance(other, self.__class__): | |
return Dual(self.value + other.value, self.derivative + other.derivative) | |
else: | |
return self + Dual(other) |
import math | |
def sin(dual): | |
return Dual(math.sin(dual.value), dual.derivative * math.cos(dual.value)) |
def super_complicated_function(x): | |
return x * sin(x+6.)**2. / 2. - 2. / 2.**x | |
# symbolic derivative by running the above in mathematica (wolfram alpha) | |
def d_super_complicated_function(x): | |
return 2**(1 - x) * math.log(2) + 0.5 * sin(x + 6)**2 + x * math.sin(x + 6) * math.cos(x + 6) | |
for x in [-1., 0.5, 3., 2.]: | |
print('Function Input: {}'.format(x)) | |
print('Function Value: {}'.format(super_complicated_function(x))) |
def create_diff_fn(fn): | |
def diff_fn(*argv): | |
jacobian = [] | |
Dual_arguments = [Dual(x, 0.) for x in argv] | |
for input_arg in Dual_arguments: | |
input_arg.derivative = 1. | |
result = fn(*Dual_arguments) | |
jacobian.append(result.derivative) | |
input_arg.derivative = 0. |
# compose a graph of nodes | |
x = Variable(10.01) | |
y = Variable(0.05) | |
z = x * y | |
m = 1.3 | |
q = m+y | |
L = (q - (z**(m/2.) + z**2. - 1./z))**2 |
class Variable: | |
def __init__(self, operation, input_variables=[]): | |
# note the setter for @property value below | |
self.value = operation | |
self.input_variables = input_variables | |
self.gradient = 0. |
def calc_input_values(self): | |
# calculate the real-valued input to operation | |
return [v.value for v in self.input_variables] | |
def forward(self): | |
# calculate the real-valued output of operation | |
return self.forward_op(*self.calc_input_values()) | |
@property | |
def value(self): |