Skip to content

Instantly share code, notes, and snippets.

@bjourne
Created February 14, 2025 22:27
Show Gist options
  • Save bjourne/91f32cec8ee4ddd6ff2409ed22ac43c3 to your computer and use it in GitHub Desktop.
Save bjourne/91f32cec8ee4ddd6ff2409ed22ac43c3 to your computer and use it in GitHub Desktop.
diff
import sympy as sp
def sigmoid(x):
return 1 / (1 + sp.exp(-x))
w1, w2, w3, w4, w5, w6 = sp.symbols("w1 w2 w3 w4 w5 w6")
x1 = 0.5
x2 = 0.2
z1 = x1*w1 + x2*w3
z2 = x1*w2 + x2*w4
h1 = sigmoid(z1)
h2 = sigmoid(z2)
z3 = h1*w5 + h2*w6
o1 = sigmoid(z3)
C = (1 - o1)**2
subs = dict(w1 = -5, w2 = 2, w3 = 6, w4 = -4, w5 = 0.5, w6 = 3)
for w in [w1, w2, w3, w4, w5, w6]:
dc_dwN = sp.diff(C, w)
print("Symbolic:", dc_dwN)
print("Numeric :", dc_dwN.evalf(subs=subs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment