使用Numpy或pytorch实现简单的机器学习

目标: y = 3 ∗ x 2 + 2 y=3*x^2+2 y=3x2+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)

在这里插入图片描述

  • 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)

在这里插入图片描述

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值