-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathmachine_learning_9_sgd.py
More file actions
57 lines (43 loc) · 2.17 KB
/
machine_learning_9_sgd.py
File metadata and controls
57 lines (43 loc) · 2.17 KB
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
import numpy as np
import matplotlib.pyplot as plt
# сигмоидная функция потерь
def loss(w, x, y):
M = np.dot(w, x) * y
return 2 / (1 + np.exp(M))
# производная сигмоидной функции потерь по вектору w
def df(w, x, y):
M = np.dot(w, x) * y
return -2 * (1 + np.exp(M)) ** (-2) * np.exp(M) * x * y
# обучающая выборка с тремя признаками (третий - константа +1)
x_train = [[10, 50], [20, 30], [25, 30], [20, 60], [15, 70], [40, 40], [30, 45], [20, 45], [40, 30], [7, 35]]
x_train = [x + [1] for x in x_train]
x_train = np.array(x_train)
y_train = np.array([-1, 1, 1, -1, -1, 1, 1, -1, 1, -1])
n_train = len(x_train) # размер обучающей выборки
w = [0.0, 0.0, 0.0] # начальные весовые коэффициенты
nt = 0.0005 # шаг сходимости SGD
lm = 0.01 # скорость "забывания" для Q
N = 500 # число итераций SGD
Q = np.mean([loss(w, x, y) for x, y in zip(x_train, y_train)]) # показатель качества
Q_plot = [Q]
for i in range(N):
k = np.random.randint(0, n_train - 1) # случайный индекс
ek = loss(w, x_train[k], y_train[k]) # вычисление потерь для выбранного вектора
w = w - nt * df(w, x_train[k], y_train[k]) # корректировка весов по SGD
Q = lm * ek + (1 - lm) * Q # пересчет показателя качества
Q_plot.append(Q)
print(w)
print(Q_plot)
line_x = list(range(max(x_train[:, 0]))) # формирование графика разделяющей линии
line_y = [-x * w[0] / w[1] - w[2] / w[1] for x in line_x]
x_0 = x_train[y_train == 1] # формирование точек для 1-го
x_1 = x_train[y_train == -1] # и 2-го классов
plt.scatter(x_0[:, 0], x_0[:, 1], color='red')
plt.scatter(x_1[:, 0], x_1[:, 1], color='blue')
plt.plot(line_x, line_y, color='green')
plt.xlim([0, 45])
plt.ylim([0, 75])
plt.ylabel("длина")
plt.xlabel("ширина")
plt.grid(True)
plt.show()