动手学打卡1

从零开始实现线性回归:

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)

#模型设定
def net(params,feature):
    return t.mm(feature,params[:-1])+params[-1]

#损失函数设定
def loss(pred,ty):
    return (0.5*(pred-ty)**2).mean()

#优化函数
def sgd(params,batch_size,lr=0.01):
    params.data-=lr*params.grad/batch_size

#模型训练
epochs=1000
batch_size=600
params=V(t.randn(3,1),requires_grad=True)
for epoch in range(epochs):
    for i in range(batch_size):
        j=t.randint(0,features.size()[0],(1,1)).view(-1)
        feature=features.index_select(0,j.type(t.LongTensor))
        y=label.index_select(0,j.type(t.LongTensor))
        l=loss(net(params,feature),y)
        l.backward()
        sgd(params,batch_size=batch_size)
        params.grad.zero_()
    print("epoch"+str(epoch+1),loss(net(params,features),label),"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()))

 

你可能感兴趣的:(动手学)