import numpy as np
x = np.array ([[2, 3], [4, 5], [6, 7], [8, 9]])
t = np.array ([6, 8, 12, 15])
alpha = 0.1
h0 = np.random.random()
h1 = np.random.random()
h2 = np.random.random()
theta = np.array([h1, h2])
eps = 1e-4
e0 = 999
e1 = 999
e2 = 999
while e0 >= eps or e1 >= eps or e2 >= eps:
i = 0
e0 = (np.sum((x[:, i] * theta[i])) + h0 * 1 - t[i])
e1 = e0 * x[1, i]
e2 = e0 * x[2, i]
h0 = h0 - alpha * e0
h1 = h1 - alpha * e1
h2 = h2 - alpha * e2
i += 1
print (h0, h1, h2)
1.0870975252577748 0.597082087416549 1.0458490772536577
|