使用torch已有的框架进行搭建
- import
import numpy as np
import torch
from torch.utils import data
- 人造数据
使用线性模型参数 w = [2, -3.4]T、b = 4.2和噪声项noise生成数据集及其表现:
y = Wx + b + noise
def synthetic_data(w, b, num_examples):
"""生成 y = wx + b + noise"""
X = torch.normal(0,1, (num_examples, len(w))) # 第三个是out的shape
y = torch.matmul(X, w) + b ## matmul 比起mm或者mv,会自己判断是m还是v
y += torch.normal(0, 0.01