Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
1.1 kB
3
Indexable
Never
import numpy as np
alpha = np.array([[1, 2, 1, -1, -1, 0, -2], [1, 0, 1, 0, -1, 1, 3], [1, -1, 2, 1, 3, 1, -1], [1, 1, 3, 4, 2, -1, 2]])
beta = np.array([[1, 2, -2, 2, 1], [1, 3, -1, 1, 2], [1, 0, -1, 0, 1]])
beta_star = np.array([[2, -2, 2, 1], [3, -1, 1, 2], [0, -1, 0, 1]])
X = np.array([1, 1, 0, 1, 0, 1, 0]).T
a = alpha @ X
print("a\n", a)
z = 1 / (1 + np.exp(-a))
print("z\n", z)

z_prime = np.array([1, *z])
print("z_prime\n", z_prime)
b = beta @ z_prime
print("b\n", b)
exp_b = np.exp(b)
# print(exp_b)
y_k = exp_b / np.sum(exp_b)
print("y_k\n", y_k)
y = np.array([0, 1, 0])
loss = -1 * (np.sum(y * np.log(y_k)))
print("loss\n", loss)
dldb = -y + y_k
print("dldb\n", dldb)
dldbeta = np.outer(dldb, np.transpose(z_prime))
print("dldbeta\n", dldbeta)

eta = 1
beta_new = beta - eta * dldbeta
print("beta_new\n", beta_new)
dldz = np.dot(dldb,  beta_star)
print(dldz)
print(dldz.shape)
print(z.shape)
dlda = np.dot(np.dot(dldz, z), (1 - z))
print("dlda\n", dlda)
dldalpha = np.outer(dlda, X.T)
print("dldalpha\n", dldalpha)
alpha_new = alpha - eta * dldalpha
print("alpha_new\n", alpha_new)