目标:
y
=
3
∗
x
2
+
2
y=3*x^2+2
y=3∗x2+2
如果是pycharm,图片显示出来后一直按q键,就能看到predict曲线的变化过程
- Numpy
import numpy as np
import matplotlib.pyplot as plt
np.random.seed(100)
x = np.linspace(-1, 1, 100).reshape(100, 1)
# print(x.shape)
noise = np.random.rand(x.size).reshape(100, 1)
y = 3 * np.power(x, 2) + 2 + 0.2 * noise # label
# print(x)
# print(y)
# plt.scatter(x,y)
# plt.show()
w1 = np.random.rand(1, 1)
b1 = np.random.rand(1, 1)
lr = 1e-3
for i in range(800):
y_pred = np.power(x, 2) * w1 + b1
loss = 0.5 * (y_pred - y) ** 2
# 将loss变成标量
loss = loss.sum()
# 梯度计算
grad_w = np.sum((y_pred - y)* np.power(x, 2))
grad_b = np.sum(y_pred - y)
w1 -= lr * grad_w
b1 -= lr * grad_b
# 如果是pycharm一直按q键,能看到predict曲线的变化过程
plt.plot(x, y_pred, 'r-', label='predict')
plt.scatter(x, y, color='blue', marker='o', label='true')
plt.xlim(-1, 1)
plt.ylim(2, 6)
plt.legend()
plt.show()
print(w1, b1)
![](https://i-blog.csdnimg.cn/blog_migrate/e8bc2b3be11c1124a5e9bdebc1ff6f2f.png)
![](https://i-blog.csdnimg.cn/blog_migrate/6838a100d8f539220156242b9616249b.png)
- Tensor及Autograd
import torch
import matplotlib.pyplot as plt
torch.manual_seed(100)
x = torch.unsqueeze(torch.linspace(-1, 1, 100), dim=1)
# print(x.shape)
y = 3 * torch.pow(x, 2) + 2 + 0.2 * torch.rand(x.shape).reshape(100, 1)
# print(y.shape)
# plt.scatter(x,y)
# plt.show()
w = torch.randn(1, 1, dtype=torch.float, requires_grad=True)
b = torch.randn(1, 1, dtype=torch.float, requires_grad=True)
lr = 1e-3
for i in range(800):
y_pred = torch.pow(x, 2) * w + b
loss = 0.5 * (y_pred - y)**2
loss = loss.sum()
# 计算梯度
loss.backward()
with torch.no_grad():
w -= lr * w.grad
b -= lr * b.grad
# 梯度清零
w.grad.zero_()
b.grad.zero_()
plt.plot(x.numpy(), y_pred.detach().numpy(), 'r-',label='predict')
plt.scatter(x.numpy(), y.numpy(), color='blue', marker='o', label='true') # true data
plt.xlim(-1, 1)
plt.ylim(2, 6)
plt.legend()
plt.show()
print(w, b)