routine
This commit is contained in:
@@ -2,6 +2,7 @@ import matplotlib.pyplot as plt
|
||||
import numpy as np
|
||||
import torch
|
||||
|
||||
|
||||
# 线性回归训练代码
|
||||
def compute_error_for_line_given_points(b, w, points):
|
||||
totalError = 0
|
||||
@@ -12,6 +13,7 @@ def compute_error_for_line_given_points(b, w, points):
|
||||
totalError += (y - (w * x + b)) ** 2
|
||||
return totalError / N
|
||||
|
||||
|
||||
def step_gradient(b_current, w_current, points, learningRate):
|
||||
b_gradient = torch.tensor(0.0, device=points.device)
|
||||
w_gradient = torch.tensor(0.0, device=points.device)
|
||||
@@ -25,25 +27,29 @@ def step_gradient(b_current, w_current, points, learningRate):
|
||||
new_w = w_current - (learningRate * w_gradient)
|
||||
return [new_b, new_w]
|
||||
|
||||
|
||||
def gradient_descent_runner(points, starting_b, starting_w, learningRate, num_iterations):
|
||||
b = torch.tensor(starting_b, device=points.device)
|
||||
w = torch.tensor(starting_w, device=points.device)
|
||||
for i in range(num_iterations):
|
||||
b, w = step_gradient(b, w, points, learningRate)
|
||||
print("round:", i)
|
||||
return [b, w]
|
||||
|
||||
|
||||
def run():
|
||||
points_np = np.genfromtxt("data1.csv", delimiter=',').astype(np.float32)
|
||||
points = torch.tensor(points_np, device='cuda')
|
||||
points = torch.tensor(points_np, device='cuda:5')
|
||||
learning_rate = 0.0001
|
||||
initial_b = 0.0
|
||||
initial_w = 0.0
|
||||
num_iterations = 100000
|
||||
[b, w] = gradient_descent_runner(points, initial_b, initial_w, learning_rate, num_iterations)
|
||||
print("After gradient descent at b={0}, w={1}, error={2}".format(b.item(), w.item(),
|
||||
compute_error_for_line_given_points(b, w, points)))
|
||||
compute_error_for_line_given_points(b, w, points)))
|
||||
return b.item(), w.item()
|
||||
|
||||
|
||||
# 运行线性回归
|
||||
final_b, final_w = run()
|
||||
|
||||
|
||||
Reference in New Issue
Block a user