Linear regression (Pytorch version)

# # Linear Regression achieve scratch 

# the In [. 6]: 


. Get_ipython () run_line_magic ( ' matplotlib ' , ' inline ' )
 Import Torch
 from IPython Import the display
 from matplotlib Import pyplot AS PLT
 Import numpy AS NP
 Import Random 


# ## generating a data set 

# the In [. 7]: 


num_inputs = 2 
num_examples = 1000 
true_w = [2, -3.4 ] 
true_b = 4.2 
Features = torch.randn(num_examples, num_inputs,
                       dtype=torch.float32)
labels = true_w[0] * features[:, 0] + true_w[1] * features[:, 1] + true_b
labels += torch.tensor(np.random.normal(0, 0.01, size=labels.size()),
                       dtype=torch.float32)


# In[8]:


print(features[0], labels[0])


# In[9]:


def use_svg_display():
    # 用矢量图显示
    display.set_matplotlib_formats('svg')

def set_figsize(figsize=(3.5, 2.5)):
    use_svg_display ()
    # Sized FIG 
    plt.rcParams [ ' figure.figsize ' ] = figsize 

# # add the above two functions ../d2lzh_pytorch after import that inside 
# Import SYS 
# sys.path.append ( "..") 
# from d2lzh_pytorch Import * 

set_figsize () 
plt.scatter (Features [:, . 1] .numpy (), labels.numpy (),. 1 ); 


# ## reads data 

# the in [10]: 


# this function has been saved in d2lzh package to facilitate future use 
DEF data_iter (the batch_size, Features, Labels): 
    num_examples = len (Features) 
    indices =List (Range (num_examples)) 
    random.shuffle (indices)   # reading order of the random sample 
    for I in Range (0, num_examples, the batch_size): 
        J = torch.LongTensor (indices [I: min (I + the batch_size, num_examples)]) # last time may be less than a BATCH 
        the yield   features.index_select (0, J), labels.index_select (0, J) 


# the in [. 11]: 


the batch_size = 10 for X-, Y in data_iter (the batch_size, Features, Labels ):
     Print (X-, Y)
     BREAK # ## initialization parameter model # the In [12 is]: 
W








 = torch.tensor (np.random.normal (0, 0.01, (num_inputs,. 1)), DTYPE = torch.float32)
B = torch.zeros (. 1, DTYPE = torch.float32) 


# the In [13 is]: 


w.requires_grad_ (requires_grad = True) 
b.requires_grad_ (requires_grad = True) 


# ## Definition Model 

# the In [14]: 


DEF the LinReg ( X-, W, B):   # after this function has been stored in a user-friendly package d2lzh_pytorch 
    return torch.mm (X-, W) + B 


# ## defined loss function 

# the in [15]: 


DEF squared_loss (y_hat, Y):   # this function has been saved to facilitate future package used in d2lzh_pytorch 
    # Note that this return is a vector addition, pytorch not divided in the MSELoss 2 
    return(y_hat - y.view (y_hat.size ())) ** 2/2 # ## defined optimization algorithm # the In [16]: DEF SGD (the params, LR, the batch_size):   # This function has been stored in the package d2lzh_pytorch after easy to use for param in the params: 
        param.data - LR * = param.grad / the batch_size # param.data Note that changes with time param # ## to train the model # the in [. 17]: 
LR = 0.03 
num_epochs =. 3 
NET = the LinReg 
Loss = squared_loss for Epoch in the Range (num_epochs):   # training model needs a total num_epochs iterative cycle #








    









     in each iteration, the training data set will be used for all samples (assuming that the number of samples that can be divisible batch size) once. The X- 
    # and y are small quantities of sample features and labels 
    for X-, y in data_iter (the batch_size, Features, Labels): 
        L = Loss (NET (X-, W, B), y) .sum ()   # L is about loss of small quantities of X and y 
        l.backward ()   # loss of small quantities of the model parameters required gradient 
        SGD ([W, B], LR, the batch_size)   # use of small quantities of stochastic gradient descent iterative model parameter 

        # Do not forget to clean the gradient zero 
        w.grad.data.zero_ () 
        b.grad.data.zero_ () 
    train_l = Loss (NET (Features, W, B), Labels)
     Print ( ' Epoch% D, F Loss% ' % (. 1 + Epoch , train_l.mean (). item () ))


# The In [18 is]: 


Print (true_w,' \ N- ' , W)
 Print (true_b, ' \ n- ' , B) 


# # simple linear regression implemented 

# ## generates a data set 

# the In [. 19]: 


num_inputs = 2 
num_examples = 1000 
true_w = [2, -3.4 ] 
true_b = 4.2 
Features = torch.tensor (np.random.normal (0,. 1, (num_examples, num_inputs)), DTYPE = torch.float) 
Labels = true_w [0] * Features [:, 0] + true_w [. 1 ] * Features [:,. 1] + true_b 
Labels + = torch.tensor (np.random.normal (0, 0.01, labels.size size = ()), DTYPE = torch.float) 


### reads the data 

# the In [20 is]: 


Import torch.utils.data the Data AS 

the batch_size = 10
 # wherein the training data, and labels 
DataSet = Data.TensorDataset (Features, Labels)
 # random access of small quantities 
data_iter = Data .DataLoader (DataSet, the batch_size, shuffle = True) 


# the in [21 is]: 


for X-, Y in data_iter:
     Print (X-, Y)
     BREAK 


# ## Definition model 

# the in [24]: 


from Torch Import NN 


# the in [25 ]: 


class LinearNet (nn.Module):
     DEF  __init__(Self, n_feature): 
        . Super (LinearNet, Self) the __init__ () 
        self.linear = nn.Linear (n_feature,. 1 )
     # Forward propagation previously defined 
    DEF Forward (Self, X): 
        Y = self.linear (X)
         return Y 

nET = LinearNet (num_inputs)
 print (nET) # using the print configuration of a network print 


# the in [26 is]: 


# written a 
nET = nn.Sequential ( 
    nn.Linear (num_inputs, . 1 )
     # here may also pass the other layers 
    ) 

# writing two 
net = nn.Sequential()
net.add_module('linear', nn.Linear(num_inputs, 1))
# net.add_module ......

# 写法三
from collections import OrderedDict
net = nn.Sequential(OrderedDict([
          ('linear', nn.Linear(num_inputs, 1))
          # ......
        ]))

print(net)
print(net[0])


# In[27]:


for param in net.parameters():
    print(param)


# ## initialization parameter model 

# the In [28]: 


from torch.nn Import the init 

init.normal_ (NET [0] .Weight, Mean = 0, STD = 0.01 ) 
init.constant_ (NET [0] .bias, Val = 0)   # can directly modify the bias Data: NET [0] .bias.data.fill_ (0) 


# ## is defined loss function 

# the in [29]: 


loss = nn.MSELoss () 


# ## defines optimization algorithm 

# the In [30]: 


Import torch.optim AS Optim 

Optimizer = optim.SGD (net.parameters (), LR = 0.03 )
 Print (Optimizer) 


# ## training model 

# the In [31 is]: 


num_epochs =. 3
for epoch in range(1, num_epochs + 1):
    for X, y in data_iter:
        output = net(X)
        l = loss(output, y.view(-1, 1))
        optimizer.zero_grad() # 梯度清零,等价于net.zero_grad()
        l.backward()
        optimizer.step()
    print('epoch %d, loss: %f' % (epoch, l.item()))


# In[32]:


dense = net[0]
print(true_w, dense.weight)
print(true_b, dense.bias)

 

Guess you like

Origin www.cnblogs.com/deeplearning-man/p/12309439.html