使用pytorch实现一个线性回归训练函数

使用sklearn.dataset 的make_regression创建用于线性回归的数据集

python 复制代码
def create_dataset():
    x, y, coef = make_regression(n_samples=100, noise=10, coef=True, bias=14.5, n_features=1, random_state=0)
    return torch.tensor(x), torch.tensor(y), coef

加载数据集,并拆分batchs训练集

python 复制代码
def load_dataset(x, y, batch_size):
    data_len = len(y)
    batch_num = data_len // batch_size
    for idx in range(batch_num):
        start = idx * batch_num
        end = idx * batch_num + batch_num
        train_x = x[start : end]
        train_y = y[start : end]
        yield train_x, train_y

定义初始权重和定义计算函数

python 复制代码
w = torch.tensor(0.1, requires_grad=True, dtype=torch.float64)
b = torch.tensor(0, requires_grad=True, dtype=torch.float64)
def linear_regression(x):
    return x * w + b

损失函数使用平方差

python 复制代码
def linear_loss(y_pred, y_true):
    return (y_pred - y_true) ** 2

优化参数使用梯度下降方法

python 复制代码
def sgd(linear_rate, batch_size):
    w.data = w.data - linear_rate * w.grad / batch_size
    b.data = b.data - linear_rate * b.grad / batch_size

训练代码

python 复制代码
def train():
    # 加载数据
    x, y, coef = create_dataset()
    data_len = len(y)

    # 定义参数
    batch_size = 10
    epochs = 100
    linear_rate = 0.01

    # 记录损失值
    epochs_loss = []

    # 迭代
    for eid in range(epochs):
        total_loss = 0.0
        for train_x, train_y in load_dataset(x, y, batch_size):
            # 输入模型
            y_pred = linear_regression(train_x)

            # 计算损失
            loss_num = linear_loss(y_pred, train_y.reshape(-1,1)).sum()

            # 梯度清理
            if w.grad is not None:
                w.grad.zero_()
            if b.grad is not None:
                b.grad.zero_()

            # 反向传播
            loss_num.backward()

            # 更新权重
            sgd(linear_rate, batch_size)

            # 统计损失数值
            total_loss = total_loss + loss_num.item()

        # 记录本次迭代的平均损失
        b_loss = total_loss / data_len
        epochs_loss.append(b_loss)
        print("epoch={},b_loss={}".format(eid, b_loss))

    # 显示预测线核真实线的拟合关系
    print(w, b)
    print(coef, 14.5)

    plt.scatter(x, y)

    test_x = torch.linspace(x.min(), x.max(), 1000)
    y1 = torch.tensor([v * w + b for v in test_x])
    y2 = torch.tensor([v * coef + 14.5 for v in test_x])
    plt.plot(test_x, y1, label='train')
    plt.plot(test_x, y2, label='true')
    plt.grid()
    plt.show()

    # 显示损失值变化曲线
    plt.plot(range(epochs), epochs_loss)
    plt.show()

拟合显示还不错

损失值在低5次迭代后基本就很小了

相关推荐
qfljg13 小时前
如何学习opencode和openclaw源码
深度学习
zhenaibo52114 小时前
摘要、研究背景、文献综述AI风险高,怎么优化?
人工智能·深度学习·自然语言处理
hhzz16 小时前
Tiger AI 平台「手势识别」功能全解析:从数字手势 0–9,到中国手语字母,再到本地视频批量识别——一条链路,三种输入,双模型可同开;双手比划,机器秒懂
人工智能·python·深度学习·aigc·音视频
自学机械人的小白ing16 小时前
深度学习系统学习
人工智能·深度学习·学习
HyperAI超神经16 小时前
128K长上下文+智能体强化训练!LFM2.5-2.6B解锁端侧大模型高效部署;DETR用Transformer斩断NMS与Anchor,重塑目标检测
人工智能·深度学习·目标检测·计算机视觉·数据集·transformer
Uncommon.18 小时前
使用pandas处理csv并转为张量
pytorch·python·深度学习·pandas
Uncommon.18 小时前
使用Pytorch操作张量(多维数组)
人工智能·pytorch·python
柳叶方舟18 小时前
Nature Medicine IF=50.0 | 公众医疗助手LLM可靠性存隐忧:真人交互表现未优于对照组,现有评估基准失效
论文阅读·人工智能·深度学习·交互·健康医疗
HZZD_HZZD18 小时前
非侵入式负荷监测选`Seq2Point`还是`LSTM`?合众致达实测:洗衣机分解F1达0.87、`NDE`误差降27%,附PyTorch完整实现
人工智能·pytorch·lstm