earlystopping实现 https://github.com/Bjarten/early-stopping-pytorch
自己实现LinearRegression
import torch
import numpy as np
import torch.nn as nn
n_sample=300
X=np.random.randn(n_sample,3)
noise=np.random.randn(n_sample)
Y=X[:,0]*0.5+X[:,1]*(-0.7)+X[:,2]*4-0.3+0.2*noise
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(X,Y)
print(model.coef_)
print(model.intercept_)
train_X=X.astype("float32").copy()
train_Y=Y.astype("float32").copy()
train_Y=train_Y.reshape((len(train_Y),1))
from torch.utils.data import TensorDataset
trainset=TensorDataset(torch.from_numpy(train_X),torch.from_numpy(train_Y))
dataloader=torch.utils.data.DataLoader(trainset,batch_size=20,num_workers=0,shuffle=True)
class LR_torch(nn.Module):
def __init__(self,input_shape=3,output_shape=1):
super(LR_torch,self).__init__()
print("初始化")
self.input_shape=input_shape
self.output_shape=output_shape
self.linear=nn.Linear(self.input_shape,self.output_shape)
torch.nn.init.xavier_uniform_(self.linear.weight)
def forward(self,x):
return self.linear(x)
model=LR_torch()
optimizer=torch.optim.SGD(params=model.parameters(),lr=0.01)
criterion=nn.MSELoss()
from torch.autograd import Variable
num_epochs=100
import matplotlib.pyplot as plt
train_loss=[]
for i in range(1,num_epochs+1):
temp_loss=[]
for ids,(input,target) in enumerate(dataloader):
input=Variable(input)
target=Variable(target)
y_pred=model(input)
loss=criterion(y_pred,target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
temp_loss.append(loss.item())
train_loss.append(np.mean(temp_loss))
plt.plot(range(len(train_loss)),train_loss)
for name, param in model.named_parameters():
if param.requires_grad:
print(name)
print(model.linear.weight)
print(model.linear.bias)
结果如下 可以看到,优化的结果没有什么问题
那么我现在的想法很简单,用earlystopping 让模型在100个epoch之内停止下来
加入earlystopping
"""
Created on Sat Apr 2 06:17:43 2022
@author: xiaokangyu
"""
import torch
import numpy as np
import torch.nn as nn
import sys
sys.path.append("/Users/xiaokangyu/Desktop/DESC_INSCT/学习/early-stopping-pytorch-master/")
from pytorchtools import EarlyStopping
n_sample=300
X=np.random.randn(n_sample,3)
noise=np.random.randn(n_sample)
Y=X[:,0]*0.5+X[:,1]*(-0.7)+X[:,2]*4-0.3+0.2*noise
from sklearn.linear_model import LinearRegression
model=LinearRegression()
model.fit(X,Y)
print(model.coef_)
print(model.intercept_)
train_X=X.astype("float32").copy()
train_Y=Y.astype("float32").copy()
train_Y=train_Y.reshape((len(train_Y),1))
from torch.utils.data import TensorDataset
trainset=TensorDataset(torch.from_numpy(train_X),torch.from_numpy(train_Y))
dataloader=torch.utils.data.DataLoader(trainset,batch_size=20,num_workers=0,shuffle=True)
class LR_torch(nn.Module):
def __init__(self,input_shape=3,output_shape=1):
super(LR_torch,self).__init__()
print("初始化")
self.input_shape=input_shape
self.output_shape=output_shape
self.linear=nn.Linear(self.input_shape,self.output_shape)
torch.nn.init.xavier_uniform_(self.linear.weight)
def forward(self,x):
return self.linear(x)
model=LR_torch()
optimizer=torch.optim.SGD(params=model.parameters(),lr=0.01)
criterion=nn.MSELoss()
from torch.autograd import Variable
num_epochs=100
import matplotlib.pyplot as plt
train_loss=[]
patience=3
early_stopping = EarlyStopping(patience=patience, delta=1e-4,verbose=True)
for i in range(1,num_epochs+1):
temp_loss=[]
for ids,(input,target) in enumerate(dataloader):
input=Variable(input)
target=Variable(target)
y_pred=model(input)
loss=criterion(y_pred,target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
temp_loss.append(loss.item())
train_loss.append(np.mean(temp_loss))
print("epoch={},train_loss={}".format(i,train_loss[-1]))
early_stopping(train_loss[-1], model)
if early_stopping.early_stop:
print("Early stopping")
break
model.load_state_dict(torch.load('checkpoint.pt'))
plt.plot(range(len(train_loss)),train_loss)
print(model.linear.weight)
print(model.linear.bias)
最终结果如下 可以开电脑,模型在20多个epoch时候集直接停止了,达到需求
|