Я увеличил и уменьшил скорость обучения и, похоже, не сходится и не берет навсегда. если я устанавливаю скорость обучения равную 0,0004, она медленно пытается сходиться, но требует столько итераций, что мне пришлось установить более 1 мил + итерацию, и мне удалось только go с 93 ошибки в квадрате до 58
Я следую Andrews NG forumla
Изображение графика с градиентной линией:
![image of the graph with the gradient line](https://i.imgur.com/8HUcOjF.png)
мой код:
import numpy as np
import pandas as pd
from matplotlib import pyplot as plt
import matplotlib.patches as mpatches
import time
data = pd.read_csv('weight-height.csv')
x = np.array(data['Height'])
y = np.array(data['Weight'])
plt.scatter(x, y, c='blue')
plt.suptitle('Male')
plt.xlabel('Height')
plt.ylabel('Weight')
total = mpatches.Patch(color='blue', label='Total amount of data {}'.format(len(x)))
plt.legend(handles=[total])
theta0 = 0
theta1 = 0
learning_rate = 0.0004
epochs = 10000
# gradient = theta0 + theta1*X
def hypothesis(x):
return theta0 + theta1 * x
def cost_function(x):
return 1 / (2 * len(x)) * sum((hypothesis(x) - y) ** 2)
start = time.time()
for i in range(epochs):
print(f'{i}/ {epochs}')
theta0 = theta0 - learning_rate * 1/len(x) * sum (hypothesis(x) - y)
theta1 = theta1 - learning_rate * 1/len(x) * sum((hypothesis(x) - y) * x)
print('\ncost: {}\ntheta0: {},\ntheta1: {}'.format(cost_function(x), theta0, theta1))
end = time.time()
plt.plot(x, hypothesis(x), c= 'red')
print('\ncost: {}\ntheta0: {},\ntheta1: {}'.format(cost_function(x), theta0, theta1))
print('time finished at {} seconds'.format(end - start))
plt.show()