请问怎么样用Matlab来实现基于自编码器对一个特定电力负荷数据集的预测
load data.mat
[numPoints, numFeatures] = size(data);
layers = [
% 特征输入层
featureInputLayer(numFeatures);
% 全连接层-1
fullyConnectedLayer(15)
reluLayer
% 全连接层-2
fullyConnectedLayer(2)
reluLayer
% 全连接层-3
fullyConnectedLayer(15)
reluLayer
% 回归输出层
fullyConnectedLayer(numFeatures)
regressionLayer];
% 训练参数设置
options = trainingOptions('adam', ...
'ExecutionEnvironment', 'auto', ...
'MiniBatchSize', 50, ...
'MaxEpochs', 200, ...
'Shuffle', 'every-epoch', ...
'Plots', 'training-progress', ...
'Verbose', false);
net = trainNetwork(data, data, layers, options);
% 可视化网络结构
analyzeNetwork(net)
layer = 'relu_2';
featuresTrain = activations(net, data, layer, 'OutputAs', 'rows');
figure
scasize = 32;
scatter(featuresTrain(:, 1), featuresTrain(:, 2), scasize,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', [0 0.4470 0.7410])
title('特征提取结果')
% 数据重构
dataPred = predict(net, data);
figure
scatter3(dataPred(:, 1), dataPred(:, 2), dataPred(:, 3), scasize,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', [0 0.4470 0.7410])
hold on
scatter3(data(:, 1), data(:, 2), data(:, 3), scasize,...
'MarkerEdgeColor', 'k',...
'MarkerFaceColor', [0.6350 0.0780 0.1840])
legend({'重构数据', '原始数据'})
可以参考
可以参考一下
function varargout = bis(varargin)
% BIS M-file for bis.fig
% BIS, by itself, creates a new BIS or raises the existing
% singleton*.
%
% H = BIS returns the handle to a new BIS or the handle to
% the existing singleton*.
%
% BIS('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in BIS.M with the given input arguments.
%
% BIS('Property','Value',...) creates a new BIS or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before bis_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to bis_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help bis
% Last Modified by GUIDE v2.5 28-Apr-2020 19:50:59
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @bis_OpeningFcn, ...
'gui_OutputFcn', @bis_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before bis is made visible.
function bis_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to bis (see VARARGIN)
% Choose default command line output for bis
handles.output = hObject;
ha=axes('units','normalized','position',[0 0 1 1]);
uistack(ha,'down')
II=imread('background.jpg');
image(II)
colormap gray
set(ha,'handlevisibility','off','visible','off');
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes bis wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = bis_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in start.
function start_Callback(hObject, eventdata, handles)
% hObject handle to start (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
%% 数据载入
load data1;
a=data1';
%% 选取训练数据和测试数据
u0 = min(min(a));
U0 = max(max(a));
b = (a-u0)/(U0-u0);%数据归一化
%% 创建神经网络
threshold = [0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1;0 1];
net=newff(threshold,[50,24],{'tansig','logsig'},'traingd');
net.trainParam.epochs=10000; %最大训练次数
net=train(net,P_train,T_train);
Y= sim(net,P_test);
y = Y';
Y1= y*(U0-u0)+u0;
T1= T_test'*(U0-u0)+u0;
plot(handles.axes1,Y1,'r')
hold on
plot(handles.axes1,T1,'b-.')
ylabel('用电量负荷建模检验图')
xlabel('时刻/t')
% --- Executes on button press in clear.
function clear_Callback(hObject, eventdata, handles)
% hObject handle to clear (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
try
delete(allchild(handles.axes1));
end
% --- Executes during object creation, after setting all properties.
function axes1_CreateFcn(hObject, eventdata, handles)
% hObject handle to axes1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: place code in OpeningFcn to populate axes1
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
智能电网的发展使得电网获取的数据逐渐增多,为了从多维大数据中获取有用信息并对短期内电力负荷进行准确的预测,提出了一种基于改进的深度稀疏自编码器(IDSAE)降维及果蝇优化算法(FOA)优化极限学习机(ELM)的短期电力负荷预测方法。将L1正则化加入到深度稀疏自编码器(DSAE)中能够诱导出更好的稀疏性,用IDSAE对影响电力负荷预测精度的高维数据进行特征降维,消除了指标间的多重共线性,实现高维数据向低维空间的压缩编码。采用FOA优化算法优化ELM的权值和阈值,得到最优值,能够克服因极限学习机随机选择权值和阈值导致预测精度低的缺点。首先将气象因素通过IDSAE降维,得到稀疏后的综合气象因素特征指标,协同电力负荷数据作为FOA优化的ELM预测模型的输入向量进行电力负荷预测。通过与DSAE-FOAELM、DSAE-ELM和IDSAE-ELM等模型的对比实验,证明了提出的预测模型能有效提高预测精度,经计算得出预测精度提升大约8%。
【电力负荷预测】基于matlab GUI粒子群优化支持向量机短期电力负荷预测【含Matlab源码 751期】
https://qq912100926.blog.csdn.net/article/details/115574905
【电力负荷预测】基于matlab灰狼算法优化LSTM短期电力负荷预测【含Matlab源码 1518期】
https://qq912100926.blog.csdn.net/article/details/119766803
【电力负荷预测】基于matlab粒子群算法优化支持向量机预测电力负荷【含Matlab源码 1225期】
https://qq912100926.blog.csdn.net/article/details/119808666
【电力负荷预测】基于matlab模拟退火算法结合狮群算法优化Elman神经网络电力负荷预测【含Matlab源码 1454期】
https://qq912100926.blog.csdn.net/article/details/120960455
【电力负荷预测】基于matlab EEMD+IWOA+LSSVM电力负荷预测【含Matlab源码 1810期】
https://qq912100926.blog.csdn.net/article/details/123378858?spm=1001.2014.3001.5502
https://blog.csdn.net/TIQCmatlab/article/details/113781795 这个
function [y1] = anomalyDetectorFunc(x1)
%ANOMALYDETECTORFUNC neural network simulation function.
%
% Auto-generated by MATLAB, 26-Jun-2020 11:59:56.
%
% [y1] = anomalyDetectorFunc(x1) takes these arguments:
% x = 100xQ matrix, input #1
% and returns:
% y = 100xQ matrix, output #1
% where Q is the number of samples.
%#ok<*RPMT0>
% ===== NEURAL NETWORK CONSTANTS =====
% Input 1
x1_step1.xoffset =
x1_step1.gain =
x1_step1.ymin =
% Layer 1
b1 =
IW1_1 =
% Layer 2
b2 =
LW2_1 =
% Output 1
y1_step1.ymin = 0;
y1_step1.gain =
y1_step1.xoffset =
% ===== SIMULATION ========
% Dimensions
Q = size(x1,2); % samples
% Input 1
xp1 = mapminmax_apply(x1,x1_step1);
% Layer 1
a1 = logsig_apply(repmat(b1,1,Q) + IW1_1*xp1);
% Layer 2
a2 = logsig_apply(repmat(b2,1,Q) + LW2_1*a1);
% Output 1
y1 = mapminmax_reverse(a2,y1_step1);
end
% ===== MODULE FUNCTIONS ========
% Map Minimum and Maximum Input Processing Function
function y = mapminmax_apply(x,settings)
y = bsxfun(@minus,x,settings.xoffset);
y = bsxfun(@times,y,settings.gain);
y = bsxfun(@plus,y,settings.ymin);
end
% Sigmoid Positive Transfer Function
function a = logsig_apply(n,~)
a = 1 ./ (1 + exp(-n));
end
% Map Minimum and Maximum Output Reverse-Processing Function
function x = mapminmax_reverse(y,settings)
x = bsxfun(@minus,y,settings.ymin);
x = bsxfun(@rdivide,x,settings.gain);
x = bsxfun(@plus,x,settings.xoffset);
end
实现预测:
% Preallocate
losses = nan(100,1);
dataall = [];
j = 1;
% Setup axes and plots
ax1 = subplot(2,1,1);p1 = plot(ax1,magic(2));ylim(ax1,[32 45]),title('Measured and Predicted')
xlim(ax1,[0,600])
ax2 = subplot(2,1,2);p2 = plot(ax2,1); ylim(ax2,[0,25]),title('RMSE'),xlim(ax2,[0,600])
% Loop through data points (the anomaly occurs somewhere around 1350)
for i = 1000:1500
% Take a frame of data
data = faultydata(i:i+99);
% Predict with autoencoder
yhat = predict(autoenc,data);
% Calculate error
losses = [losses;sqrt(sum((yhat - data).^2))];
% After first frame, only add one data point to the plot
if j > 1
yhat = yhat(end);
data = data(end);
end
% Update data to be plotted
dataall = [dataall;[data yhat]];
% Plot
p1(1).XData = 1:size(dataall,1);p1(2).LineWidth = 1.5;
p1(2).XData = 1:size(dataall,1);
p1(1).YData = dataall(:,1);p2.LineWidth = 1.5;
p1(2).YData = dataall(:,2);
p2.XData = 1:length(losses);
p2.YData = losses;
pause(0.005)
j = j+1;
end
xlim([0 length(losses)])
% Find anomalies by thresholding the error
anomalyIdxs = find(losses > 7.5);
% Highlight the region with high anomaly scores
patch(ax1,[min(anomalyIdxs),max(anomalyIdxs),max(anomalyIdxs),min(anomalyIdxs)],...
[min(ax1.YLim),min(ax1.YLim),max(ax1.YLim),max(ax1.YLim)],'red','FaceAlpha',.3)
我有毕设模板
可以百度一下对应的模版或者源码