import torch
from matplotlib import pyplot as plt
import numpy as np
# 样本数:1000
num_examples = 1000
# 设置权重
true_w = 3
# 设置偏置
true_b = 20
# 1000个特征数为2的样本
delta_x = torch.randn(num_examples, dtype=torch.float32)
meta_x = torch.arange(0, num_examples, dtype=torch.float32)
meta_x = delta_x + (0.01 * meta_x)
# 计算出对应的标签
meta_y = true_w * meta_x + true_b
# 给标签加噪音
meta_y += torch.tensor(np.random.normal(0, 15, size=meta_y.size()), dtype=torch.float32)
meta_point = np.dstack((meta_x.numpy(), meta_y.numpy()))[0]
# 打乱
np.random.shuffle(meta_point)
points = torch.from_numpy(meta_point)
plt.scatter(points[:, 0].numpy(), points[:, 1].numpy(), c='', edgecolors='b', s=15)
plt.show()
# y = wx + b
# 真实值 与 预测值 的方差函数
def compute_error_for_line_given_points(b, w, points):
"""
计算给定超参数[w,b]的误差值
"""
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))
# 求导函数
def step_gradient(b, w, points, lr):
b_gradient = 0
w_gradient = 0
N = float(len(points))
for i in range(len(points)):
x = points[i, 0]
y = points[i, 1]
b_gradient += -(2 / N) * (y - ((w * x) + b))
w_gradient += -(2 / N) * x * (y - ((w * x) + b))
b_new = b - (lr * b_gradient)
w_new = w - (lr * w_gradient)
return [b_new, w_new]
# 梯度下降
def gradient_descent_runner(points, b, w, lr, iterations):
for i in range(iterations):
b, w = step_gradient(b, w, np.array(points), lr)
return [b, w]
def run():
lr = 0.01
initial_b = 0
initial_w = 0
iterations = 2000
print(
f"Starting project descent at b = {initial_b}, w = {initial_w},error = {compute_error_for_line_given_points(initial_b, initial_w, points)}")
print('\nRunning...')
[b, w] = gradient_descent_runner(points, initial_b, initial_w, lr, iterations)
print(f"\nAfter project descent at b = {b}, w = {w},error = {compute_error_for_line_given_points(b, w, points)}")
print('\nb:{},w:{}'.format(b, w))
paint_line(b, w)
# 画出直线
def paint_line(b, w):
x = points[:, 0]
y = w * x + b
plt.scatter(points[:, 0], points[:, 1], c='r', edgecolors='b', s=15, label='orginal')
plt.plot(x, y, c='red', label='predict', linewidth=2.0, linestyle=':')
plt.legend()
plt.show()
if __name__ == '__main__':
run()