赞
踩
- data = kw;
- %导入数据,数据必须是一维向量
-
- figure
- plot(data)
- xlabel("15min Period")
- ylabel("Kw")
- title("15-minute load")
- numTimeStepsTrain = floor(0.9*numel(data));
- %划分训练集与测试集,这里选了0.9
- %确定训练次数
- dataTrain = data(1:numTimeStepsTrain+1);
- dataTest = data(numTimeStepsTrain+1:end);
- %创建两个数据集 dataTrain和dataTest
- mu = mean(dataTrain);
- sig = std(dataTrain);
- dataTrainStandardized = (dataTrain - mu) / sig;
- XTrain = dataTrainStandardized(1:end-1);
- YTrain = dataTrainStandardized(2:end);
- numFeatures = 1;
- numResponses = 1;
- numHiddenUnits = 200;
- layers = [ ...
- sequenceInputLayer(numFeatures)
- lstmLayer(numHiddenUnits)
- fullyConnectedLayer(numResponses)
- regressionLayer];
- %接下来设置求解的各项参数,指定训练选项。将求解器设置为 'adam' 并进行 250 轮训练。要防止梯度爆炸,请将梯度阈值设置为 1。指定初始学习率 0.005,在 125 轮训练后通过乘以因子 0.2 来降低学习率。
- options = trainingOptions('adam', ...
- 'MaxEpochs',800, ...
- 'GradientThreshold',1, ...
- 'InitialLearnRate',0.005, ...
- 'LearnRateSchedule','piecewise', ...
- 'LearnRateDropPeriod',400, ...
- 'LearnRateDropFactor',0.2, ...
- 'Verbose',0, ...
- 'Plots','training-progress');
- net = trainNetwork(XTrain,YTrain,layers,options);
- dataTestStandardized = (dataTest - mu) / sig;
- XTest = dataTestStandardized(1:end-1);
- net = predictAndUpdateState(net,XTrain);
- [net,YPred] = predictAndUpdateState(net,YTrain(end));
- numTimeStepsTest = numel(XTest);
- for i = 2:numTimeStepsTest
- [net,YPred(:,i)] = predictAndUpdateState(net,YPred(:,i-1),'ExecutionEnvironment','cpu');
- end
- YPred = sig*YPred + mu;
- YTest = dataTest(2:end);
- rmse = sqrt(mean((YPred-YTest).^2))
- figure
- plot(dataTrain(1:end-1))
- hold on
- idx = numTimeStepsTrain:(numTimeStepsTrain+numTimeStepsTest);
- plot(idx,[data(numTimeStepsTrain) YPred],'.-')
- hold off
- xlabel("Periods of 15-mins")
- ylabel("Kw")
- title("Forecast")
- legend(["Observed" "Forecast"])
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。