加载中...
Pytorch-线性回归问题
发表于:2023-07-21 | 分类: 视觉处理 Pytorch

Pytorch-线性回归问题

1
2
3
4
5
6
7
8
# 计算均方差损失函数s
def computer_error_for_line_given_points(b, w, points):
totalError = 0
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
totalError += (y - (w * x + b)) ** 2
return totalError / float(len(points))

$\omega ‘=\omega-lr*\frac{\bigtriangledown loss }{\bigtriangledown \omega} $

$b ‘=b-lr*\frac{\bigtriangledown loss }{\bigtriangledown b} $

1
2
3
4
5
6
7
8
9
10
11
12
13
# 求梯度值
def step_gradient(b_current, w_current, points, learningRate):
b_gradient = 0
w_gradient = 0
N = float(len(points))
for i in range(0, len(points)):
x = points[i, 0]
y = points[i, 1]
b_gradient += -(2 / N) * (y - ((w_current * x) + b_current))
w_gradient += -(2 / N) * x * (y - ((w_current * x) + b_current))
new_b = b_current - (learningRate * b_gradient)
new_w = w_current - (learningRate * w_gradient)
return [new_b, new_w]
1
2
3
4
5
6
7
# 梯度下降执行
def gradient_descent_runner(points, starting_b, starting_w, learning_rate, num_iterations):
b = starting_b
w = starting_w
for i in range(num_iterations):
b, w = step_gradient(b, w, np.array(points), learning_rate)
return [b, w]
1
2
3
4
5
6
7
# 绘图函数
def draw_plot(points, b, w):
x = [i[0] for i in points]
y = [i[1] for i in points]
plt.scatter(x, y, c='r')
new_y = [i[0] * w + b for i in points]
plt.plot(x, new_y)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
def run():
points = np.genfromtxt("./data/data.csv", delimiter=",")
learning_rate = 0.0001
initial_b = 0
initial_w = 0
num_iterations = 1000
print("Starting gradient descent at b = {}, w = {}, error = {}".format(
initial_b, initial_w, computer_error_for_line_given_points(initial_b, initial_w, points)))
print("Running")
[b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
print("After {} iterations b = {}, w = {}, error = {}".format(
num_iterations, b, w, computer_error_for_line_given_points(b, w, points)))
draw_plot(points, b, w)
plt.show()


if __name__ == "__main__":
run()

#--------------OUTPUT---------------
Starting gradient descent at b = 0, w = 0, error = 5565.107834483211
Running
After 1000 iterations b = 0.08893651993741346, w = 1.4777440851894448, error = 112.61481011613473

image-20230721093456110

上一篇:
Pytorch-MNIST手写数据集
下一篇:
ROS-Moveit-ABORTED_CONTROL_FAILED
本文目录
本文目录