深度学习3.2 线性回归的从零开始实现

3.2.1 生成数据集

python 复制代码
%matplotlib inline
import random
import torch
from d2l import torch as d2l

def synthetic_data(w, b, num_examples):
    # 生成特征矩阵X,形状为(num_examples, len(w)),符合标准正态分布
    X = torch.normal(0, 1, (num_examples, len(w)))
    # 计算标签y = Xw + b
    y = torch.matmul(X, w) + b
    # 添加均值为0、标准差为0.01的噪声
    y += torch.normal(0, 0.01, y.shape)
    # 将y转换为列向量(形状:num_examples × 1)
    return X, y.reshape((-1, 1))
python 复制代码
true_w = torch.tensor([2, -3.4])  # 定义真实权重
true_b = 4.2                      # 定义真实偏置
features, labels = synthetic_data(true_w, true_b, 1000)  # 生成1000个样本

d2l.set_figsize()
d2l.plt.scatter(features[:, 1].detach().numpy(), labels.detach().numpy(), 1)

features[:, 1]: 选取所有样本的第二个特征(索引为1的列)。

3.2.1 读取数据集

python 复制代码
def data_iter(batch_size, features, labels):
    num_examples = len(features)
    indices = list(range(num_examples))

    random.shuffle(indices)
    for i in range(0, num_examples, batch_size):
        batch_indices = torch.tensor(
            indices[i: min(i + batch_size, num_examples)])
        yield features[batch_indices], labels[batch_indices]

batch_size = 10
for X, y in data_iter(batch_size, features, labels):
    print(X, '\n', y)
    break

tensor([[ 1.6556, 0.1851],

-1.4880, 0.0684\], \[ 1.0536, 0.9818\], \[-0.7794, -1.9199\], \[-0.3383, 0.2244\], \[-0.2260, 3.1530\], \[-2.3626, 1.1877\], \[-0.3301, 0.1781\], \[-0.6136, -1.2974\], \[-0.3397, -0.2088\]\]) tensor(\[\[ 6.8888\], \[ 0.9887\], \[ 2.9757\], \[ 9.1748\], \[ 2.7541\], \[-6.9671\], \[-4.5522\], \[ 2.9436\], \[ 7.3728\], \[ 4.2270\]\])

相关推荐
trsoliu7 分钟前
🚀 AI行业震荡:高通挑战英伟达霸主地位,生成式AI引擎赋能生命科学,OpenAI重新定义「电力新石油」
人工智能
2301_764441338 分钟前
身份证校验工具
前端·python·1024程序员节
汽车仪器仪表相关领域9 分钟前
工业商业安全 “哨兵”:GT-NHVR-20-A1 点型可燃气体探测器实操解析与场景适配
大数据·人工智能·功能测试·安全·安全性测试
长桥夜波10 分钟前
【第十九周】机器学习笔记08
人工智能·笔记·机器学习
trsoliu35 分钟前
快手StreamLake重磅发布AI编程产品矩阵,自研大模型超越GPT-5,AI开发者新时代来临!
人工智能·ai编程
天涯路s1 小时前
OpenCV 高级图像处理
人工智能·opencv·计算机视觉
小宁爱Python1 小时前
从入门到实践:LangGraph 构建复杂 AI 工作流的完整指南
人工智能·python·microsoft·django
百锦再1 小时前
Python、Java与Go:AI大模型时代的语言抉择
java·前端·vue.js·人工智能·python·go·1024程序员节
IT_陈寒1 小时前
Vite 3.0终极提速指南:5个鲜为人知的配置技巧让构建效率翻倍
前端·人工智能·后端