-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (93 loc) · 3.61 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
import numpy as np
import matplotlib.pyplot as plt
if __name__ == '__main__':
n = 1000
theta = np.array([0, 0])
x = np.random.uniform(0, 1, (n, 2))
x[:, 1] = 1
y = x.dot(theta) + np.random.normal(0, 0.2, n)
def f(x, theta):
return x.dot(theta)
def least_square_error(theta):
return sum((y - x.dot(theta))**2)
def least_square_error_derivative(theta):
return -2 * sum((y - f(x, theta))[:, np.newaxis] * x)
def gradient_descent(theta0, n_iterations=100, step_size=0.01,
precision=0.1, verbose=False):
current_theta = theta0
for i in range(n_iterations):
derivative = least_square_error_derivative(current_theta)
next_theta = current_theta - step_size * derivative
if verbose:
print(f'iter #{i} - error = {least_square_error(next_theta)}')
step = next_theta - current_theta
if np.sqrt(step.dot(step)) < precision:
break
current_theta = next_theta
return current_theta
theta_estimate = gradient_descent(np.array([0, 1]), verbose=True)
print(f'{theta} VS {theta_estimate}')
# n = 1000
# alpha = 3
# mu = 0
# sigma = 0.01
# for _ in range(max_iters):
# current_alpha = next_alpha
# next_alpha = current_alpha - gamma * depsilon(current_alpha, beta, data_base, n)
# step = next_alpha - current_alpha
# if abs(step) <= precision:
# break
# def depsilon(alpha,beta,data_base,data_size):
# sum = 0
# for j in range(data_size) :
# iteration = alpha*(data_base[j][0])**2 + data_base[j][0]*(beta - data_base[j][1])
# sum += iteration
# sum = 2 * sum
# return sum / data_size
# def err_fun(alpha, beta, data_base, data_size):
# sum = 0
# for i in range(data_size):
# iteration = (data_base[i][1]-alpha*data_base[i][0]-beta)**2
# sum += iteration
# return sum
# error = np.random.normal(mu,sigma)
# y = alpha * x + beta + error
# # y_float = alpha * x_values + beta + error
# # for idx in range(len(y_float)) :
# # y_float[idx] = np.sign(y_float[idx])
# # y = y_float
# data_base = []
# for idx in range(n):
# data_base.append((x[idx], y[idx]))
# #print(data_base)
# next_alpha = 0 # We start the search at x=0
# gamma = 1 # Step size multiplier
# precision = 0.000001 # Desired precision of result
# max_iters = 10000 # Maximum number of iterations
# def err_fun(alpha, beta, data_base, data_size):
# sum = 0
# for i in range(data_size):
# iteration = (data_base[i][1]-alpha*data_base[i][0]-beta)**2
# sum += iteration
# return sum
# # Derivative function
# def depsilon(alpha,beta,data_base,data_size):
# sum = 0
# for j in range(data_size) :
# iteration = alpha*(data_base[j][0])**2 + data_base[j][0]*(beta - data_base[j][1])
# sum += iteration
# sum = 2 * sum
# return sum / data_size
# for _ in range(max_iters):
# current_alpha = next_alpha
# next_alpha = current_alpha - gamma * depsilon(current_alpha, beta, data_base, n)
# step = next_alpha - current_alpha
# if abs(step) <= precision:
# break
# alpha_list = np.arange(0,6,0.01)
# error_list = []
# for k in alpha_list:
# error_list.append(err_fun(k, beta, data_base, n))
# print("Minimum at ", next_alpha)
# plt.plot(alpha_list,error_list)
# plt.show()