1、部分代码
% 数据集 clc clear close all % addpath('./') load('Train.mat') Train.weekend = dummyvar(Train.weekend); Train.month = dummyvar(Train.month); Train = movevars(Train,{'weekend','month'},'After','demandLag'); Train.ts = []; % Train.hour = dummyvar(Train.hour); % Train(1,:) =[]; y = Train.demand; x = Train{:,2:5}; [xnorm,xopt] = mapminmax(x',0,1); [ynorm,yopt] = mapminmax(y',0,1); % % xnorm = [xnorm;Train.weekend';Train.month']; %% % x = x'; xnorm = xnorm(:,1:1000); ynorm = ynorm(1:1000); k = 24; % 滞后长度 % 转换成2-D image for i = 1:length(ynorm)-k Train_xNorm(:,i,:) = xnorm(:,i:i+k-1); Train_yNorm(i) = ynorm(i+k-1); Train_y(i) = y(i+k-1); end Train_yNorm= Train_yNorm'; ytest = Train.demand(1001:1170); xtest = Train{1001:1170,2:5}; [xtestnorm] = mapminmax('apply', xtest',xopt); [ytestnorm] = mapminmax('apply',ytest',yopt); % xtestnorm = [xtestnorm; Train.weekend(1001:1170,:)'; Train.month(1001:1170,:)']; xtest = xtest'; for i = 1:length(ytestnorm)-k Test_xNorm(:,i,:) = xtestnorm(:,i:i+k-1); Test_yNorm(i) = ytestnorm(i+k-1); Test_y(i) = ytest(i+k-1); end Test_yNorm = Test_yNorm'; clear k i x y % Train_xNorm = dlarray(Train_xNorm,'CBT'); Train_yNorm = dlarray(Train_yNorm,'BC'); Test_xNorm = dlarray(Test_xNorm,'CBT'); Test_yNorm = dlarray(Test_yNorm,'BC'); %% 训练集和验证集划分 TrainSampleLength = length(Train_yNorm); validatasize = floor(TrainSampleLength * 0.1); Validata_xNorm = Train_xNorm(:,end - validatasize:end,:); Validata_yNorm = Train_yNorm(:,TrainSampleLength-validatasize:end); Validata_y = Train_y(TrainSampleLength-validatasize:end); Train_xNorm = Train_xNorm(:,1:end-validatasize,:); Train_yNorm = Train_yNorm(:,1:end-validatasize); Train_y = Train_y(1:end-validatasize); %% %参数设置 inputSize = size(Train_xNorm,1); %数据输入x的特征维度 outputSize = 1; %数据输出y的维度 numhidden_units1=50; [params,~] = paramsInit(numhidden_units1,inputSize,outputSize); % 导入初始化参数 [~,validatastate] = paramsInit(numhidden_units1,inputSize,outputSize); % 导入初始化参数 [~,TestState] = paramsInit(numhidden_units1,inputSize,outputSize); % 导入初始化参数 % 训练相关参数 TrainOptions; numIterationsPerEpoch = floor((TrainSampleLength-validatasize)/minibatchsize); LearnRate = 0.01; %% Loop over epochs. figure start = tic; lineLossTrain = animatedline('color','r'); validationLoss = animatedline('color','g','Marker','o'); xlabel("Iteration") ylabel("Loss") % epoch 更新 iteration = 0; for epoch = 1 : numEpochs [~,state] = paramsInit(numhidden_units1,inputSize,outputSize); % 每轮epoch,state初始化 disp(['Epoch: ', int2str(epoch)]) % batch 更新 for i = 1 : numIterationsPerEpoch iteration = iteration + 1; disp(['Iteration: ', int2str(iteration)]) idx = (i-1)*minibatchsize+1:i*minibatchsize; dlX = gpuArray(Train_xNorm(:,idx,:)); dlY = gpuArray(Train_yNorm(idx)); [gradients,loss,state] = dlfeval(@TPAModel,dlX,dlY,params,state); % L2正则化 % L2regulationFactor = 0.000011; % gradients = dlupdate( @(g,parameters) L2Regulation(g,parameters,L2regulationFactor),gradients,params); % gradients = dlupdate(@(g) thresholdL2Norm(g, gradientThreshold),gradients); [params,averageGrad,averageSqGrad] = adamupdate(params,gradients,averageGrad,averageSqGrad,iteration,LearnRate); % 验证集测试 if iteration == 1 || mod(iteration,validationFrequency) == 0 output_Ynorm = TPAModelPredict(gpuArray(Validata_xNorm),params,validatastate); lossValidation = mse(output_Ynorm, gpuArray(Validata_yNorm)); end % 作图(训练过程损失图)--------------------------********———————————————————————————————————————————————— D = duration(0,0,toc(start),'Format','hh:mm:ss'); addpoints(lineLossTrain,iteration,double(gather(extractdata(loss)))) if iteration == 1 || mod(iteration,validationFrequency) == 0 addpoints(validationLoss,iteration,double(gather(extractdata(lossValidation)))) end title("Epoch: " + epoch + ", Elapsed: " + string(D)) legend('训练集','验证集') drawnow end % 每轮epoch 更新学习率 if mod(epoch,5) == 0 LearnRate = LearnRate * LearnRateDropFactor; end end %% 训练集 Predict_yNorm = TPAModelPredict(gpuArray(Train_xNorm),params,TestState); Predict_yNorm = extractdata(Predict_yNorm); Predict_y = mapminmax('reverse',Predict_yNorm,yopt); % figure plot(Predict_y,'r'); hold on plot(Train_y,'g') legend('训练集预测值','训练集实际值') %% 验证集 Predict_yNorm = TPAModelPredict(gpuArray(Validata_xNorm),params,TestState); Predict_yNorm = extractdata(Predict_yNorm); Predict_y = mapminmax('reverse',Predict_yNorm,yopt); % figure plot(Predict_y,'r'); hold on plot(Validata_y,'g') legend('验证集预测值','验证集实际值') %% predict(传统递归测试集) % clear Predict_yNorm % % Test_xNorm = extractdata(Test_xNorm); % for i = 1:size(Test_xNorm,2) % if i ==1 % [a,Teststate] = TPAModelPredict(dlarray(Test_xNorm(:,i,:),'CBT'),params,TestState); % Predict_yNorm(i) = extractdata(a); % else % % a = extractclcdata(Test_xNorm(:,i,:)); % Test_xNorm(end,i,end)= gather(Predict_yNorm(i-1)); % [a,Teststate] = TPAModelPredict(dlarray(Test_xNorm(:,i,:),'CBT'),params,TestState); % Predict_yNorm(i) = extractdata(a); % end % Predict_YNorm(i) = Predict_yNorm(i); % end %% predict(直接测试集) Predict_yNorm = TPAModelPredict(dlarray(Test_xNorm,'CBT'),params,TestState); Predict_YNorm = extractdata(Predict_yNorm); Predict_y = mapminmax('reverse',Predict_YNorm,yopt); % figure plot(Predict_y,'r'); hold on plot(Test_y,'g') legend('测试集预测值','测试集实际值')
2、结果展示