Pytorch self-study notes - linear regression


 
import numpy as np
import torch
from torch.utils import data #Data processing module
from d2l import torch as d2l
 
 
#Generate data set, you don't need to look here
true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = d2l.synthetic_data(true_w, true_b, 1000)
 
 
#load_array The purpose of this function is: in the small batch gradient descent method, the selection of small batches
#data_arrays=matrix, batch_size=the number of small batches, is_train=whether to randomly extract this Small batch
def load_array(data_arrays, batch_size, is_train=True):  
    """Construct a PyTorch data iterator."""
 
 
    dataset = data.TensorDataset(*data_arrays)
 
    #data.DataLoader small batch selection relies on this function
    return data.DataLoader(dataset, batch_size, shuffle=is_train)
 
batch_size = 10
data_iter = load_array((features, labels), batch_size)
 
next(iter(data_iter))
 
#build model
from torch import nn
net = nn.Sequential(nn.Linear(2, 1))
 
#initialize the parameters in the model
net[0].weight.data.normal_(0, 0.01)
net[0].bias.data.fill_(0)
 
#Construct loss function——nn.MSELoss() is mean square loss
loss = nn.MSELoss()
 
#Trainer—update model parameters
trainer = torch.optim.SGD(net.parameters(), lr=0.03)
 
#Training process
num_epochs = 3 #num_epochs = 3 means we train the model three times                       
for epoch in range(num_epochs):
    for X, y in data_iter: # Randomly take 10 samples (small batches) to train the model
        l = loss(net(X), y) #Calculate loss
        trainer.zero_grad() #Gradient return to zero
        l.backward() #Solve the latest gradient
        trainer.step() #Parameter update
    l = loss(net(features), labels)
    print(f'epoch {epoch + 1}, loss {l:f}')
 
 
 
 

Function answer:

1. data.TensorDataset can be used to package tensor.

To be more clear, it is to combine samples and lable (the number of samples must be the same as the length of lable)

input: two tensors of the same length

output: packaged tensor

example:

a = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9], [1, 2, 3], [4, 5, 6], [7, 8, 9]])
b = torch.tensor([44, 55, 66, 44, 55, 66, 44, 55, 66, 44, 55, 66])
 
# TensorDataset packs tensor
train_ids = TensorDataset(a, b) 
for x_train, y_label in train_ids:
    print(x_train, y_label)


How to display the packed tensor:

 for x_train, y_label in train_ids: Separate output for samples and lable

next(iter(data_iter)): The overall output of the packaged data

2、 data.DataLoader(dataset, batch_size, shuffle=is_train)

Make a selection on the data packaged by data.TensorDataset.

batch_size=The number of selected samples

shuffle=true random selection, false sequential selection
 

Guess you like

Origin blog.csdn.net/zhaomengsen/article/details/131335957