|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
%% 模型训练
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};
[x_feature_label,y_feature_label]=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),:); |
-
|