Linear Regression问题记录

如何读取数据

首先回顾需求,每次遍历数据集读取小批量数据样本,将这个过程封装在一个函数中,而这个函数每次返回batch_size个数随机样本的特征标签

  • 如何实现随机索引?

    0.获取随机-索引用2步完成
    用列表封装索引indices = list(range(num_examples))
    这个时候我们生成了一个从0开始到(num_examples-1)的有序列表

    1.使用random.shuffle(firstdim)打乱索引生成一个无序的索引列表
    值得注意的是,我们不能直接将打乱后的索引random.shuffle(indices)赋值给indices,因为其返回类型为NoneType

    num_examples = len(features)
    indices = random.shuffle(list(range(num_examples)))
    #结果会出现如下错误:
    # 'NoneType' object is not subscriptable
    #需要改成如下形式:
    num_examples = len(features)
    indices = list(range(num_examples))
    random.shuffle(indices)
    
  • 如何通过随机索引批量返回batch_size 个数的随机样本的特征和标签?
    先生成batch_size个数的索引,再通过index_select(input,dim,index)函数返回Tensor类型的行或列

    indices[0:min(0 + batch_size,num_examples)]
    index = torch.LongTensor(indices)
    features.index_select(0,index)
    

    结果我们可以得到与特征features与标签labels对应的两个Tensor

    print(labels.index_select(0,index)) 
    print(labels.index_select(0,index).size())
    ==> tensor([ 4.7743,  1.6770,  1.8844,  4.5900, 12.9030,  7.4718,  1.8628,  3.8695,
        -2.9427, -0.0766], dtype=torch.float64)
    ==>torch.Size([10])
    print(features.index_select(0,j)) #(10,2)
    print(features.index_select(0,j).size())
    ==>tensor([[-2.2352,  0.3606],
        [-1.1131, -0.4500],
        [-0.0606,  0.1857],
        [ 0.8105,  0.1576],
        [ 0.1230, -1.0621],
        [-0.1240, -0.1650],
        [ 0.1972, -0.3546],
        [ 1.9724,  0.0762],
        [ 2.6250, -1.3328],
        [ 1.2012,  0.5165]], dtype=torch.float64)
     ==>torch.Size([10, 2])
    

    再使用for循环,range(start,stop,step)限定读取范围和次数,批量返回数据集的特征和标签

    #每隔batch_size返回num_examples对tensor
    range(0,num_examples,batch_size)
    

完整代码如下:

num_examples = len(features)
indices = list(range(num_examples))
random.shuffle(indices)
for i in range(0,num_examples,batch_size):
   j = torch.LongTensor(indices[i:min(i + batch_size,num_examples)])
   print(features.index_select(0,j),labels.index_select(0,j))

结果如下:

#返回的第一个10组(batch_size)随机样本的feature和label

tensor([[ 0.5144, -0.4909],
        [ 0.5637,  0.0094],
        [ 0.6858, -1.6840],
        [-1.3828, -0.0232],
        [-2.0897, -0.6789],
        [-2.0771,  0.0943],
        [-0.7997,  1.6995],
        [-2.1806, -0.3342],
        [ 0.1950,  0.7635],
        [ 0.3187, -0.0548]], dtype=torch.float64) 
 tensor([ 6.9142,  5.3035, 11.3044,  1.5106,  2.3434, -0.2799, -3.1884,  0.9700,
         2.0032,  5.0255], dtype=torch.float64)
         ...

如何定义模型

  • 初始化模型参数
    • 需要初始化的模型参数:权重(weights,w表示)和偏差(bias,b表示)
    • 权重w初始化为均值为0、标准差为0.01的正态随机数,
      • np.random.normal(start,stop,size)
      • 在这里,我们指定其size为(num_inputs,1)
    • 偏差b则初始化为0
      • torch.zeros(1,dtype = torch.float64)
  • 同时,需要对参数求梯度来迭代参数的值
    • w.requires_grad_(requires_grad = True)
    • b.requires_grad_(requires_grad = True)
  • 定义模型中Variable
    • 实现线性回归的矢量表达式:y = X * w + b + ∈
    • torch.mm(x,w) + b
    • 模型中的损失定义:
      • y_hat - y.view(y_hat.size()) ** 2 / 2
      • 为什么需要.view(*size)来reshape?
      • 首先返回的结果肯定是要与y_hat预测值的结果同形的,而真实值y的形状与其不一样,因此在每一次训练模型通过预测值与真实值之间差值来计算误差要将二者的shape/size进行统一。

猜你喜欢

转载自blog.csdn.net/Romaga/article/details/104308648