从零开始实现线性回归:
import torch as t
from torch.autograd import Variable as V
#设置随机种子
def setup_seed(seed):
t.manual_seed(seed)
t.cuda.manual_seed_all(seed)
t.backends.cudnn.deterministic = True
setup_seed(666)
#生成模拟数据
features=t.randn(1000,2)
tw=t.Tensor([2,1])
tb=t.Tensor([1])
label=features.mm(tw.view(-1,1))+tb+t.randn(1000,1)
#生成batch数据
def data_iter(features,label,batch_size):
index=t.randperm(features.size()[0])
for i in range(0,features.size()[0],batch_size):
for j in range(i,min(i+batch_size,features.size()[0])):
yield features.index_select(0,index[j]),label.index_select(0,index[j])
#模型设定
def net(params,feature):
return t.mm(feature,params[:-1].view(-1,1))+params[-1]
#损失函数设定
def loss(pred,ty):
return 0.5*(pred-ty)**2
#优化函数
def sgd(params,batch_size,lr=0.01):
params.data-=lr*params.grad/batch_size
#模型训练
epochs=10
batch_size=50
params=V(t.randn(3,1),requires_grad=True)
for epoch in range(epochs):
for x,y in data_iter(features,label,batch_size):
l=loss(net(params,x),y).sum()
l.backward()
sgd(params,batch_size=batch_size)
params.grad.zero_()
print("epoch"+str(epoch+1),loss(net(params,features),label).mean(),"params",params.view(1,3))
基于pytorch模块实现线性回归:
#基于pytorch模块
import torch.utils.data as Data
from torch import nn
import torch.optim as optim
batch_size=10
##读取数据集
dataset=Data.TensorDataset(features,label)
data_iter=Data.DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True,num_workers=2)
##模型设定
net=nn.Sequential()
net.add_module("linear",nn.Linear(features.size()[1],1))
##初始化模型参数
nn.init.normal_(net[0].weight,mean=0,std=0.01)
nn.init.constant_(net[0].bias,val=0)
##定义损失函数
loss=nn.MSELoss()
##定义优化函数
optimizer=optim.SGD(net.parameters(),lr=0.1)
##模型训练
epochs=10
for epoch in range(epochs):
for X,y in data_iter:
l=loss(net(X),y.view(-1,1))
optimizer.zero_grad()
l.backward()
optimizer.step()
print("epoch %d,loss:%f"%(epoch+1,l.item()))