代码进度
线性回归代码:
import torch
import random
from d2l import torch as d2l
def dataCreat(w, b, numSample):
X = torch.normal(0, 1, (numSample, len(w)))
y = torch.matmul(X, w) + b
y += torch.normal(0, 0.01, y.shape)
return X, y.reshape(-1, 1)
def dataInit(wsq):
w=torch.normal(0, wsq, size=(2,1), requires_grad=True)
b=torch.zeros(1, requires_grad=True)
return w, b
def dataBatch(batchSize, features, labels):
numSamples = len(features)
indices = list(range(numSamples))
random.shuffle(indices)
for i in range(0, numSamples, batchSize):
batchIndices = torch.tensor(indices[i:min(i+batchSize, numSamples)])
yield features[batchIndices], labels[batchIndices]
def linReg(X, w, b):
return torch.matmul(X,w)+b
def squLoss(yHat, y):
return (yHat-y.reshape(yHat.shape))**2/2
def sgdOptim(params, lr, batchSize):
with torch.no_grad():
for param in params:
param -= lr * param.grad/batchSize
param.grad.zero_()
初始化函数:
def varInit(lr=0.03, numEpochs=10, wsq=0.01):
trueW = torch.tensor([2, -3.4])
trueB = 4.2
numSample = 1000
batchSize = 10
lr = 0.03
numEpochs = 10
wsq = 0.01
return [trueW, trueB, numSample, wsq], [batchSize, lr, numEpochs]
回归测试函数:
def testReg(*kwargs):
kwarg=kwargs[0]
for epoch in range(kwarg[2]):
for X, y in dataBatch(kwarg[0], features, labels):
los = squLoss(linReg(X, w, b), y)
los.sum().backward()
sgdOptim([w, b], kwarg[1], kwarg[0])
with torch.no_grad():
train1 = squLoss(linReg(features, w, b), labels)
print(f'epoch{epoch + 1}, loss: {float(train1.mean()):f}')
测试线性回归:
varG, varG1 = varInit()
features, labels = dataCreat(varG[0], varG[1], varG[2])
w, b = dataInit(varG[3])
testReg(varG1)
结果:
epoch1, loss: 0.047491
epoch2, loss: 0.000198
epoch3, loss: 0.000051
epoch4, loss: 0.000050
epoch5, loss: 0.000050
epoch6, loss: 0.000050
epoch7, loss: 0.000050
epoch8, loss: 0.000050
epoch9, loss: 0.000050
epoch10, loss: 0.000050
人就像是被蒙着眼推磨的驴子,生活就像一条鞭子;当鞭子抽到你背上时,你就只能一直往前走,虽然连你也不知道要走到什么时候为止,便一直这么坚持着。
浙公网安备 33010602011771号