对回归采用10折交叉验证评估实验结果

更新时间:2023-05-12 10:23:21 阅读: 评论:0

对回归采⽤10折交叉验证评估实验结果1 导⼊实验所需要的包
import torch
as nn
import numpy as np
import torchvision
ansforms as transforms
import matplotlib.pyplot as plt
import random
from pandas import *
%matplotlib inline
2 加载数据
#加载数据集
features = torch.rand(10000, 500)
labels = (0.0056 * features).sum(dim=1) + 0.028
labels += sor(al(0,0.01,size=labels.size()), dtype=torch.float)
3 读取数据
def get_data_iter(X_train, y_train, X_valid, y_valid,batch_size):
train_datat = torch.utils.data.TensorDatat(X_train.cuda(),y_train.cuda())
test_datat = torch.utils.data.TensorDatat(X_valid.cuda(),y_valid.cuda())
train_iter = torch.utils.data.DataLoader(train_datat,batch_size=batch_size,shuffle=True)    test_iter = torch.utils.data.DataLoader(test_datat,batch_size=batch_size,shuffle=Fal)
return train_iter, test_iter
4 定义模型
#定义⽹络
class LinearNet(nn.Module):
def__init__(lf,num_inputs,num_outputs,num_hiddens):
super(LinearNet,lf).__init__()
lf.linear1 = nn.Linear(num_inputs,num_hidden s)
lf.linear2 = nn.Linear(num_hiddens,num_outputs)
def forward(lf,x):
x = lf.linear1(x)
x = lf.relu(x)
x = lf.linear2(x)
y = lf.relu(x)
return y
5 定义训练模型
#模型训练
def train(train_iter,test_iter,if_reshape,num_epochs,num_inputs,net,loss):
optimizer = torch.optim.SGD(net.parameters(),lr=0.001)
train_ls, test_ls = [], []
for epoch in range(num_epochs):
ls, count = 0, 0
if if_reshape ==Fal:
for X,y in train_iter:
l=loss(net(X),y.view(-1,1))
<_grad()
l.backward()
optimizer.step()
ls += l.item()
count += y.shape[0]
train_ls.append(ls/count)
ls, count = 0, 0
for X,y in test_iter:
l=loss(net(X),y.view(-1,1))
ls += l.item()
count += y.shape[0]
el:
for X,y in train_iter:
X = X.reshape(-1,num_inputs)
l=loss(net(X),y).sum()
<_grad()
l.backward()
optimizer.step()
ls += l.item()
count += y.shape[0]
train_ls.append(ls/count)
ls, count = 0, 0
for X,y in test_iter:
X = X.reshape(-1,num_inputs)
l=loss(net(X),y).sum()
ls += l.item()
count += y.shape[0]
test_ls.append(ls/count)
if(epoch+1)%5==0:
print('epoch: %d, train loss: %f, valid loss: %f'%(epoch+1,train_ls[-1],test_ls[-1])) return train_ls,test_ls
6 获取k折交叉验证某⼀折的训练集和验证集
def get_kfold_data(k, i, X, y):
fold_size = X.shape[0]//k
val_start = i * fold_size
if i  != k - 1:
val_end = (i + 1) * fold_size
X_valid, y_valid = X[val_start:val_end],y[val_start:val_end]
X_train = torch.cat((X[0:val_start],X[val_end:]),dim=0)
y_train = torch.cat((y[0:val_start],y[val_end:]),dim=0)
el:
X_valid,y_valid = X[val_start:], y[val_start:]
X_train = X[0:val_start]
y_train = y[0:val_start]
return X_train, y_train, X_valid, y_valid
7 K折交叉验证
def k_fold(k, X_train, y_train,if_reshape,num_epochs,num_inputs,net,loss):
my_k_train_ls, my_k_valid_ls = [], []
train_loss_sum, valid_loss_sum = 0, 0
for i in range(k):
print('第', i+1, '折验证结果')
X_train, y_train, X_valid, y_valid = get_kfold_data(k, i, X_train, y_train)
train_iter, valid_iter = get_data_iter(X_train, y_train, X_valid, y_valid,batch_size=100)
train_loss, val_loss = train(train_iter,valid_iter,if_reshape,num_epochs,num_inputs,net,loss)        my_k_train_ls.append(train_loss)
my_k_valid_ls.append(val_loss)
train_loss_sum += train_loss[-1]
valid_loss_sum += val_loss[-1]
print("最终平均k折交叉验证结果")
print(f'average train loss: {train_loss_sum/k}')
print(f'average valid loss: {valid_loss_sum/k}')
return my_k_train_ls, my_k_valid_ls
8 训练模型
k=10
mynum_epochs= 20
num_inputs,num_outputs,num_hiddens = 500,1,256
mynet = LinearNet(num_inputs, num_outputs, num_hiddens).cuda()
MSEloss= nn.MSELoss()
# record_k_train_ls, record_k_valid_ls = k_fold(k, X_train, y_train,if_reshape=Fal,num_epochs=20,num_inputs,net ,loss) my_k_train_ls, my_k_valid_ls = k_fold(k, features, labels,if_reshape=Fal,num_epochs=mynum_epochs,num_inputs = 500,net =mynet,loss= MSEloss)
9 绘制损失函数图
# 绘图
train_loss, valid_loss = [], []
for i in range(len(my_k_train_ls)):
train_loss.append(my_k_train_ls[i][-1])
valid_loss.append(my_k_valid_ls[i][-1])
x = np.linspace(0,len(my_k_train_ls),len(my_k_train_ls))
plt.plot(x,train_loss,'o-',label='train_loss',linewidth=1.5)
plt.plot(x,valid_loss,'o-',label='valid_loss',linewidth=1.5)
plt.xlabel('K value')
plt.ylabel('loss')
plt.legend()
plt.show()
10 绘制损失表格
# 绘制表格
from pylab import mpl
randn = np.random.randn
idx = []
for i in range(1,21):
idx.append(f'epoch {i}')
data_train, data_valid = np.zeros((10,20)),np.zeros((10,20))
for i in range(10):
for j in range(20):
data_train[i,j], data_valid[i,j] = my_k_train_ls[i][j], my_k_valid_ls[i][j]
df = DataFrame(data_train.T, index=idx, columns=['第1折', '第2折', '第3折', '第4折', '第5折',
'第6折', '第7折', '第8折', '第9折', '第10折'])
vals = np.around(df.values,7)
fig = plt.figure(figsize=(8,3))
ax = fig.add_subplot(111, frameon=Fal, xticks=[], yticks=[])
the_table=plt.table(cellText=vals, rowLabels=df.index, lumns,
colWidths = [0.1]*vals.shape[1], loc='center',cellLoc='center')
the_table.t_fontsize(20) the_table.scale(2.5,2.58)

本文发布于:2023-05-12 10:23:21,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/fan/90/105582.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:验证   交叉   绘制
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图