程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Chapter 3 of hands-on deep learning - (2) Python implementation of linear regression concern

編輯:Python

author github

Source code address

Simple implementation of linear regression model

import torch
from d2l import torch as d2l
from torch import nn
from torch.utils import data
true_w = torch.tensor([2, -3.4])
true_b = 4.2
features, labels = d2l.synthetic_data(true_w, true_b, 1000)
def load_array(data_arrays, batch_size, is_train=True): # @save
""" Construct a PyTorch Data iterators """
dataset = data.TensorDataset(*data_arrays)
return data.DataLoader(dataset, batch_size, shuffle=is_train)
batch_size = 10
data_iter = load_array((features, labels), batch_size)
# Use the predefined layers of the framework 
net = nn.Sequential(nn.Linear(2, 1)) # The input is 2D , The output is one-dimensional 
# Initialize model parameters 
net[0].weight.data.normal_(0, 0.01)
net[0].bias.data.fill_(0)
# The mean square error is calculated using MELoss class , Also known as L_2 norm 
loss = nn.MSELoss()
# Instantiation SGD( Stochastic gradient descent ) example 
trainer = torch.optim.SGD(net.parameters(), lr=0.03)
# Training 
num_epochs = 3
for epoch in range(num_epochs):
for X, y in data_iter:
l = loss(net(X), y)
trainer.zero_grad()
l.backward()
trainer.step()
l = loss(net(features), labels)
print(f'epoch {
epoch + 1},loss {
l:f}')

Output :

epoch 1,loss 0.000229
epoch 2,loss 0.000091
epoch 3,loss 0.000091

The reason why the output will be slightly different each time :

Because our weight parameters are initialized randomly , So each run will be different loss.


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved