老是过拟合,有没有大佬知道怎么改啊
%% 模型训练x_mu_all=[];x_sig_all=[];y_mu_all=[];y_sig_all=[];
for NUM_all=1:length(data_select1_cell)
data_process=data_select1_cell{1,NUM_all};
=timeseries_process(data_process,select_predict_num,num_feature,num_series);
[~,y_feature_label1]=timeseries_process(data_select1,select_predict_num,num_feature,num_series); %未分解之前
index_label1=1:(size(x_feature_label,1)); index_label=index_label1;
spilt_ri=G_out_data.spilt_ri;
train_num=round(spilt_ri(1)/(sum(spilt_ri))*size(x_feature_label,1)); %训练集个数
vaild_num=round((spilt_ri(1)+spilt_ri(2))/(sum(spilt_ri))*size(x_feature_label,1)); %验证集个数
%训练集,验证集,测试集
train_x_feature_label=x_feature_label(index_label(1:train_num),:);
train_y_feature_label=y_feature_label(index_label(1:train_num),:);
vaild_x_feature_label=x_feature_label(index_label(train_num+1:vaild_num),:);
vaild_y_feature_label=y_feature_label(index_label(train_num+1:vaild_num),:);
test_x_feature_label=x_feature_label(index_label(vaild_num+1:end),:);
test_y_feature_label=y_feature_label(index_label(vaild_num+1:end),:);
%Zscore 标准化
%训练集
x_mu = mean(train_x_feature_label);x_sig = std(train_x_feature_label);
train_x_feature_label_norm = (train_x_feature_label - x_mu) ./ x_sig; % 训练数据标准化
y_mu = mean(train_y_feature_label);y_sig = std(train_y_feature_label);
train_y_feature_label_norm = (train_y_feature_label - y_mu) ./ y_sig; % 训练数据标准化
x_mu_all(NUM_all,:)=x_mu;x_sig_all(NUM_all,:)=x_sig;y_mu_all(NUM_all,:)=y_mu;y_sig_all(NUM_all,:)=y_sig;
%验证集
vaild_x_feature_label_norm = (vaild_x_feature_label - x_mu) ./ x_sig; % 训练数据标准化
vaild_y_feature_label_norm = (vaild_y_feature_label - y_mu) ./ y_sig; % 训练数据标准化
%测试集
test_x_feature_label_norm = (test_x_feature_label - x_mu) ./ x_sig; % 训练数据标准化
test_y_feature_label_norm = (test_y_feature_label - y_mu) ./ y_sig; % 训练数据标准化
y_train_predict_norm=zeros(size(train_y_feature_label,1),size(train_y_feature_label,2));y_vaild_predict_norm=zeros(size(vaild_y_feature_label,1),size(vaild_y_feature_label,2));
y_test_predict_norm=zeros(size(test_y_feature_label,1),size(test_y_feature_label,2));
for N1=1:length(list_cell)
hidden_size=G_out_data.hidden_size;
p_train1=cell(size(train_x_feature_label,1),1);p_test1=cell(size(test_x_feature_label,1),1);p_vaild1=cell(size(vaild_x_feature_label,1),1);
O_train1=cell(size(train_x_feature_label,1),1);O_test1=cell(size(test_x_feature_label,1),1);O_vaild1=cell(size(vaild_x_feature_label,1),1);
for i = 1: size(train_x_feature_label,1) %修改输入变成元胞形式
p_train1{i, 1} = (train_x_feature_label_norm(i,:))';
end
for i = 1 : size(test_x_feature_label,1)
p_test1{i, 1}= (test_x_feature_label_norm(i,:))';
end
for i = 1 : size(vaild_x_feature_label,1)
p_vaild1{i, 1}= (vaild_x_feature_label_norm(i,:))';
end
for i = 1: size(train_x_feature_label,1) %修改输入变成元胞形式
O_train1{i, 1} = (train_y_feature_label_norm(i,list_cell{1,N1}))';
end
for i = 1 : size(test_x_feature_label,1)
O_test1{i, 1}= (test_y_feature_label_norm(i,list_cell{1,N1}))';
end
for i = 1 : size(vaild_x_feature_label,1)
O_vaild1{i, 1}= (vaild_y_feature_label_norm(i,list_cell{1,N1}))';
end
if(length(hidden_size)<2)
layers = [sequenceInputLayer(size(train_x_feature_label,2))
lstmLayer(hidden_size(1), 'OutputMode', 'sequence') % LSTM层
reluLayer % Relu激活层
dropoutLayer(0.5) % 防止过拟合
fullyConnectedLayer(size(train_y_feature_label(:,list_cell{1,N1}),2)) % 全连接层
regressionLayer];
elseif (length(hidden_size)>=2)
layers = [sequenceInputLayer(size(train_x_feature_label,2))
lstmLayer(hidden_size(1),'OutputMode','sequence')
dropoutLayer(0.5)
lstmLayer(hidden_size(2),'OutputMode','sequence')
dropoutLayer(0.5)
fullyConnectedLayer(size(train_y_feature_label(:,list_cell{1,N1}),2))
regressionLayer];
end
options = trainingOptions('adam', ...
'MaxEpochs',max_epoch_LC, ...
'MiniBatchSize',min_batchsize,...
'InitialLearnRate',0.001,...
'ValidationFrequency',20, ...
'LearnRateSchedule','piecewise', ...
'LearnRateDropPeriod',128, ...
'LearnRateDropFactor',0.5, ...
'Plots','training-progress');
Mdl = trainNetwork(p_train1, O_train1, layers, options);
y_train_predict_norm1 = predict(Mdl, p_train1,'MiniBatchSize',min_batchsize);
y_vaild_predict_norm1 = predict(Mdl, p_vaild1,'MiniBatchSize',min_batchsize);
y_test_predict_norm1 =predict(Mdl, p_test1,'MiniBatchSize',min_batchsize);
y_train_predict_norm_roll=[];y_vaild_predict_norm_roll=[];y_test_predict_norm_roll=[];
for i=1:length(y_train_predict_norm1)
y_train_predict_norm_roll(i,:) = (y_train_predict_norm1{i,1});
end
for i=1:length(y_vaild_predict_norm1)
y_vaild_predict_norm_roll(i,:) = (y_vaild_predict_norm1{i,1});
end
for i=1:length(y_test_predict_norm1)
y_test_predict_norm_roll(i,:) = (y_test_predict_norm1{i,1});
end
y_train_predict_norm(:,list_cell{1,N1})=y_train_predict_norm_roll;
y_vaild_predict_norm(:,list_cell{1,N1})=y_vaild_predict_norm_roll;
y_test_predict_norm(:,list_cell{1,N1})=y_test_predict_norm_roll;
Model{1,N1}=Mdl;
model_all{NUM_all,N1}=Mdl;
graph = layerGraph(Mdl.Layers); figure; plot(graph)
end
y_train_predict_cell{1,NUM_all}=y_train_predict_norm.*y_sig+y_mu;%反标准化操作
y_vaild_predict_cell{1,NUM_all}=y_vaild_predict_norm.*y_sig+y_mu;
y_test_predict_cell{1,NUM_all}=y_test_predict_norm.*y_sig+y_mu;
end
y_train_predict=0;y_vaild_predict=0;y_test_predict=0;
for i=1:length(data_select1_cell)
y_train_predict=y_train_predict+ y_train_predict_cell{1,i};
y_vaild_predict=y_vaild_predict+ y_vaild_predict_cell{1,i};
y_test_predict=y_test_predict+ y_test_predict_cell{1,i};
end
train_y_feature_label=y_feature_label1(index_label(1:train_num),:);
vaild_y_feature_label=y_feature_label1(index_label(train_num+1:vaild_num),:);
test_y_feature_label=y_feature_label1(index_label(vaild_num+1:end),:); 过拟合是指模型在训练集上表现良好,但在测试集或验证集上表现较差的问题。下面是一些可能帮助您解决过拟合问题的方法:
1. 增加数据量:如果您的训练数据较少,模型容易过拟合。您可以尝试增加训练数据量,或使用数据增强技术来生成更多样本。
2. 简化模型:过于复杂的模型更容易过拟合。可以尝试减少模型层数、节点数量等来降低模型的复杂度。
3. 正则化:正则化是一种常用的降低过拟合的方法。主要有L1正则化和L2正则化两种方式,可以通过向目标函数添加正则项来约束模型的参数。
4. 早停法:可以设置一个验证集,在每个Epoch结束后计算验证集上的准确率或损失。当验证集上的准确率或损失不再提升时,停止训练以防止过拟合。
5. Dropout:Dropout是一种常用的正则化技术,它在神经网络中随机丢弃一部分神经元,使得模型的学习变得更为鲁棒。
6. 数据预处理:对输入数据进行标准化或归一化等处理,可以帮助模型更好地学习。
以下是您提供的代码中可能出现过拟合的问题所在:
1. 模型训练中没有应用正则化技术,可以尝试添加L1正则化或L2正则化来限制模型参数的大小。
2. 训练过程中没有使用早停法,可以在每个Epoch结束后计算验证集的准确率或损失,并判断是否需要停止训练。
3. 可以尝试增加训练数据量,或使用数据增强技术来生成更多样本,以降低模型过拟合的风险。
4. 可以尝试简化模型,减少隐藏层的大小或者神经元的数量,以降低模型复杂度。
通过这些方法,您可以尝试解决过拟合问题。希望对您有所帮助!
以上回复来自 FishC AI 小助理,如未能正确解答您的问题,请继续追问。
页:
[1]